main/setup.c

Mon, 21 Jun 2021 19:04:10 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 21 Jun 2021 19:04:10 +0200
changeset 102
96e30a3a3980
parent 101
1bc6e9263ada
child 103
1885d0c75c48
permissions
-rw-r--r--

Finished experimental code to drive the German HendiControl board. Added BoilPower and RampPower buttons during the while boil process. RampPower (going to boil power) is now adjustable. Added PWM driver code to the driver task.

/**
 * @file setup.c
 * @brief Setup functions.
 */

#include "config.h"


extern sButton          Buttons[MAXBUTTONS];
extern int              Main_Screen;

bool			UpdateRec = false;		///< Update record flag			
int			CurrentRec = 1;			///< Current record
int			Records = 1;			///< Total records.


static const char               *TAG = "setup";



/*
 * Setup init function that only runs once a new screen
 * is entered.
 */
void Setup_Init(void)
{
    FILE	*f;
    size_t	bytes;
    uint8_t	*dst;

    switch (Main_Screen) {

	case MAIN_TOOLS_SETUP:
			TopMessage((char *)"Setup menu");
			Buttons_Add( 20, 40,120, 40, (char *)"Config", 0);
			Buttons_Add(180, 40,120, 40, (char *)"Apparatuur", 1);
			Buttons_Add( 20,120,120, 40, (char *)"WiFi", 2);
			Buttons_Add(180,120,120, 40, (char *)"Calibratie", 3);
			Buttons_Add(130, 200, 60, 40, (char *)"Ok", 4);
			Buttons[4].dark = true;
			Buttons_Show();
			break;

	case MAIN_TOOLS_SETUP_CONFIG:
			_bg = TFT_BLACK;
			TopMessage((char *)"Configuratie");
			TFT_setFont(DEFAULT_FONT, NULL);
			ShowFloat(2, 28, (char *)"Kook temp.", (char *)"C", config.BoilTemperature, 2);
			ShowBool(2, 44, (char *)"Vraag mout storten", config.AskAdd);
			ShowBool(2, 60, (char *)"Vraag mout verwijderen", config.AskRemove);
			ShowBool(2, 76, (char *)"Vraag Jodium proef", config.AskIodine);
			ShowInteger(2, 92, (char *)"Jodiumtest wachttijd", (char *)" min.", config.IodineTime);
			ShowText(2, 108, (char *)"Hostnaam", config.hostname);
			ShowText(2, 124, (char *)"NTP server", config.ntp_server);
			Buttons_Clear();
			Buttons_Add(  0, 210, 45, 30, (char *)"Ok"  , 0);
			Buttons_Add(276, 210, 45, 30, (char *)"Ed"  , 1);
			Buttons[0].dark = true;
			Buttons_Show();
			break;

	case MAIN_TOOLS_SETUP_CO_EDIT:
			break;

	case MAIN_TOOLS_SETUP_EQUIPMENT:
			TopMessage((char *)"Apparatuur");
			f = fopen("/spiffs/etc/equipments.conf", "r");
			fseek(f, equipment_hdr.hdrsize, SEEK_SET);
			dst = (uint8_t*)&equipment;
			Records = 0;
			while ((bytes = fread(dst, 1, equipment_hdr.recsize, f))) {
			    Records++;
			}
			fclose(f);
			// Load the default record.
			CurrentRec = config.EquipmentRec;
			UpdateRec = true;
			break;

	case MAIN_TOOLS_SETUP_EQ_EDIT:
			break;

	case MAIN_TOOLS_SETUP_CALIBRATION:
			Calibration_Init();
			break;

	default:
			break;
    }
}



/*
 * Setup loop screens, non-blocking.
 */
void Setup_Loop(void)
{
    uint32_t	crc1, crc2;
    uint8_t	*dst;

    switch (Main_Screen) {

	case MAIN_TOOLS_SETUP:
			switch (Buttons_Scan()) {
			    case 0:     Main_Screen = MAIN_TOOLS_SETUP_CONFIG;
					break;

			    case 1:	Main_Screen = MAIN_TOOLS_SETUP_EQUIPMENT;
					break;

			    case 2:	Main_Screen = MAIN_TOOLS_SETUP_WIFI;
					break;

			    case 3:     Main_Screen = MAIN_TOOLS_SETUP_CALIBRATION;
					break;

			    case 4:     Main_Screen = MAIN_TOOLS;
					break;

			    default:    break;
			}
			break;

	case MAIN_TOOLS_SETUP_CONFIG:
			switch (Buttons_Scan()) {
			    case 0:     Main_Screen = MAIN_TOOLS_SETUP;
					break;

			    case 1:	Main_Screen = MAIN_TOOLS_SETUP_CO_EDIT;
					break;

			    default:	break;
			}
			break;

	case MAIN_TOOLS_SETUP_CO_EDIT:
			dst = (uint8_t*)&config;
			crc1 = crc32_le(0, dst, sizeof(config));
			EditFloat((char *)"Kook temperatuur", &config.BoilTemperature, 80, 105, 2);
			// Round to 0.25 degrees
			config.BoilTemperature = ((int)(config.BoilTemperature * 4)) / 4.0;
			EditBool((char *)"Vraag mout storten", &config.AskAdd);
			EditBool((char *)"Vraag mout verwijderen", &config.AskRemove);
			EditBool((char *)"Vraag Jodium proef", &config.AskIodine);
			EditUint8((char *)"Jodium test wachttijd", &config.IodineTime, 0, 120);
			EditText((char *)"DHCP hostnaam", config.hostname, 31);
			EditText((char *)"Voorkeur NTP server", config.ntp_server, 31);

			crc2 = crc32_le(0, dst, sizeof(config));
			if ((crc1 != crc2) && Confirm((char *)"Gewijzigd, opslaan?", (char *)"Ja", (char *)"Nee")) {
			    write_config();
			} else {
			    read_config();
			}
			Main_Screen = MAIN_TOOLS_SETUP_CONFIG;
			break;

	case MAIN_TOOLS_SETUP_EQUIPMENT:
			if (UpdateRec) {
			    _bg = TFT_BLACK;
			    TFT_fillScreen(_bg);
			    TFT_resetclipwin();
			    TopMessage((char *)"Apparatuur");
			    UpdateRec = false;
			    read_equipment(CurrentRec);
			    TFT_setFont(DEFAULT_FONT, NULL);
			    ShowText(2, 28, (char *)"Naam", equipment.Name);
			    ShowInteger(  2, 44, (char *)"Kook vermogen", (char *)"%", equipment.BoilPower);
			    ShowInteger(161, 44, (char *)"Maisch verm.", (char *)"%", equipment.MashPower);
			    ShowInteger(  2, 60, (char *)"Ramp vermogen", (char *)"%", equipment.RampPower);
			    ShowFloat(161, 60, (char *)"Spoelwater", NULL, equipment.TempHLT, 2);
			    ShowInteger(  2, 76, (char *)"Sample tijd", (char *)"mS", equipment.SampleTime);
			    Show2Integer(161, 76, (char *)"Pomp cyclus", (char *)"m", equipment.PumpCycle, equipment.PumpRest);
			    ShowBool(  2,  92, (char *)"Pomp opwarmen", equipment.PumpPreMash);
			    ShowBool(161,  92, (char *)"Pomp maischen", equipment.PumpOnMash);
			    ShowBool(  2,  108, (char *)"Pomp uitmaischen", equipment.PumpMashOut);
			    ShowBool(161,  108, (char *)"Pomp bij koken", equipment.PumpOnBoil);
			    ShowInteger(2, 124, (char *)"Pomp maxtemp.", (char *)"C", equipment.PumpMaxTemp);
			    ShowBool(161, 124, (char *)"PID bij mout weg", equipment.PIDPipe);
			    ShowSSR2(2, 140, equipment.SSR2);
			    ShowBool(161, 140, (char *)"Hendi PWM", equipment.Hendi);
			    ShowDouble(2, 156, (char *)"PID P", NULL, equipment.PID_kP, 3);
			    ShowInteger(161, 156, (char *)"MLT watt", NULL, equipment.MLT_watt);
			    ShowDouble(2, 172, (char *)"PID I", NULL, equipment.PID_kI, 3);
			    ShowInteger(161, 172, (char *)"HLT watt", NULL, equipment.HLT_watt);
			    ShowDouble(2, 188, (char *)"PID D", NULL, equipment.PID_kD, 3);
			    ShowInteger(161, 188, (char *)"Max watt", NULL, equipment.Max_watt);
			    Buttons_Clear();
			    Buttons_Add(  0, 210, 45, 30, (char *)"Ok"  , 0);
			    Buttons_Add( 46, 210, 45, 30, (char *)"+"   , 1);
			    if (CurrentRec != config.EquipmentRec)
			        Buttons_Add( 92, 210, 45, 30, (char *)"-", 2);
			    else
				Buttons_Add( 92, 210, 45, 30, (char *)""    , 2);
			    if (CurrentRec > 1)
			    	Buttons_Add(138, 210, 45, 30, (char *)"<", 3);
			    else
				Buttons_Add(138, 210, 45, 30, (char *)"", 3);
			    if (CurrentRec < Records)
			    	Buttons_Add(184, 210, 45, 30, (char *)">", 4);
			    else
				Buttons_Add(184, 210, 45, 30, (char *)"", 4);
			    if (CurrentRec != config.EquipmentRec)
			    	Buttons_Add(230, 210, 45, 30, (char *)"Std", 5);
			    else
				Buttons_Add(230, 210, 45, 30, (char *)"", 5);
			    Buttons_Add(276, 210, 45, 30, (char *)"Ed"  , 6);
			    Buttons[0].dark = true;
			    Buttons_Show();
			}
			switch (Buttons_Scan()) {
			    case 0:	Main_Screen = MAIN_TOOLS_SETUP;
					break;

			    case 1:	memset(&equipment, 0, sizeof(equipment));
					equipment.MLT_watt = 2000;
					equipment.HLT_watt = 2000;
					sprintf(equipment.Name, "new eq %d", Records + 1);
					equipment.BoilPower = 80;
					equipment.MashPower = 100;
					equipment.PumpCycle = 8;
					equipment.PumpRest = 2;
					equipment.PumpPreMash = true;
					equipment.PumpOnMash = true;
					equipment.PumpMashOut = true;
					equipment.PumpOnBoil = false;
					equipment.PumpMaxTemp = 80;
					equipment.PIDPipe = true;
					equipment.SSR2 = 0;
					equipment.TempHLT = 85.0;
					equipment.PID_kP = 200.0;
					equipment.PID_kI = 2.0;
					equipment.PID_kD = 1.5;
					equipment.SampleTime = 3000;
					append_equipment();
					Records++;
					CurrentRec = Records;
					UpdateRec = true;
					ESP_LOGI(TAG, "New equipment record %d", CurrentRec);
					break;

			    case 2:	if ((CurrentRec != config.EquipmentRec) && (Records > 1)) {
					    _bg = TFT_BLACK;
                                             TFT_fillScreen(_bg);
                                            TFT_resetclipwin();
                                            TopMessage((char *)"Apparatuur verwijderen");
					    delete_equipment(CurrentRec);
					    Records--;
					    if (CurrentRec >= Records)
						CurrentRec = Records;
					    UpdateRec = true;
					}
					break;

			    case 3:	if (CurrentRec > 1) {
					    CurrentRec--;
					    UpdateRec = true;
					}
					break;

			    case 4:	if (CurrentRec < Records) {
					    CurrentRec++;
					    UpdateRec = true;
					}
					break;

			    case 5:	if (CurrentRec != config.EquipmentRec) {
					    config.EquipmentRec = CurrentRec;
					    write_config();
					    UpdateRec = true;
					    ESP_LOGI(TAG, "Equipment %d `%s' set as default", CurrentRec, equipment.Name);
					}
					break;

			    case 6:	Main_Screen = MAIN_TOOLS_SETUP_EQ_EDIT;
					break;

			    default:	break;
			}		
			break;

	case MAIN_TOOLS_SETUP_EQ_EDIT:
			dst = (uint8_t*)&equipment;
			crc1 = crc32_le(0, dst, sizeof(equipment));
			EditText((char *)"Naam", equipment.Name, 31);
			EditInt((char *)"Kook vermogen in %", &equipment.BoilPower, 0, 100);
			EditInt((char *)"Maisch vermogen in %", &equipment.MashPower, 0, 100);
			EditInt((char *)"Ramp vermogen in %", &equipment.RampPower, 0, 100);
			EditFloat((char *)"Spoelwater temp", &equipment.TempHLT, 75, 98, 2);
                        // Round to 0.25 values.
                        equipment.TempHLT = ((int)(equipment.TempHLT * 4)) / 4.0;
			EditInt((char *)"Sample tijd in mS", &equipment.SampleTime, 1000, 20000);
			// Round to 250 mSec units.
                        equipment.SampleTime = ((int)(equipment.SampleTime / 250)) * 250;
			EditInt((char *)"Pomp cyclus minuten", &equipment.PumpCycle, 5, 15);
			EditInt((char *)"Pomp rust minuten", &equipment.PumpRest, 0, 5);
			EditBool((char *)"Pomp bij opwarmen", &equipment.PumpPreMash);
			EditBool((char *)"Pomp bij maischen", &equipment.PumpOnMash);
			EditBool((char *)"Pomp bij uitmaischen", &equipment.PumpMashOut);
			EditBool((char *)"Pomp tijdens koken", &equipment.PumpOnBoil);
			EditInt((char *)"Pomp max. temperatuur", &equipment.PumpMaxTemp, 60, 105);
			EditBool((char *)"PID by mout verwijderen", &equipment.PIDPipe);
			EditSSR2(&equipment.SSR2);
			EditBool((char *)"Hendi PWM", &equipment.Hendi);
			EditDouble((char *)"PID P",  &equipment.PID_kP, 20, 2000, 3);
			EditDouble((char *)"PID I",  &equipment.PID_kI, 0, 100, 3);
			EditDouble((char *)"PID D",  &equipment.PID_kD, 0, 50000, 3);
			EditInt((char *)"MLT watt", &equipment.MLT_watt, 100, 45000);
			EditInt((char *)"HLT watt", &equipment.HLT_watt, 100, 45000);
			EditInt((char *)"Max watt", &equipment.Max_watt, 100, 45000);

			crc2 = crc32_le(0, dst, sizeof(equipment));
			if ((crc1 != crc2) && Confirm((char *)"Gewijzigd, opslaan?", (char *)"Ja", (char *)"Nee")) {
			    write_equipment(CurrentRec);
			}
			Main_Screen = MAIN_TOOLS_SETUP_EQUIPMENT;
			break;

	case MAIN_TOOLS_SETUP_WIFI:
			break;

	case MAIN_TOOLS_SETUP_CALIBRATION:
			Calibration_Loop();
			Main_Screen = MAIN_TOOLS_SETUP;
			break;

	default:
			break;
    }
}

mercurial