main/setup.c

Sat, 06 Jun 2020 13:28:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 06 Jun 2020 13:28:46 +0200
changeset 77
66c77497d86d
parent 54
7b134c27fadb
child 82
7d17e2cb31a8
permissions
-rw-r--r--

Changed the recipe database so that it is expandable, version 2. More mash fields and allow 16 steps. Allow 20 Additions. Removed separate mash steps from the state machine, the steps are moved to the runtime data. There is no fixed step number for mashout anymore. There is no fixed step for mash-in anymore, just use the first step and heat to the infusion temperature. After malt add, switch to the normal step temperature. Implemented decoction steps.

/**
 * @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 *)"AP SSID", config.ap_ssid);
			ShowText(2, 140, (char *)"AP pwd", config.ap_pwd);
			ShowInteger(2, 156, (char *)"AP kanaal", NULL, config.ap_channel);
			ShowBool(2, 172, (char *)"AP SSID verborgen", config.ap_ssid_hidden);
			ShowText(2, 188, (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");
			dst = (uint8_t*)&equipment;
			Records = 0;
			while ((bytes = fread(dst, 1, sizeof(equipment), 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 *)"AP SSID", config.ap_ssid, 31);
			EditTextMin((char *)"AP password", config.ap_pwd, 40, 8);
			EditUint8((char *)"AP kanaal", &config.ap_channel, 1, 14);
			bool hidden = config.ap_ssid_hidden;
			EditBool((char *)"AP SSID verborgen", &hidden);
			config.ap_ssid_hidden = (uint8_t)hidden;
			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 *)"Pomp cyclus", (char *)"m", equipment.PumpCycle);
			    ShowInteger(161, 60, (char *)"Pomp rust", (char *)"m", equipment.PumpRest);
			    ShowBool(  2,  76, (char *)"Pomp opwarmen", equipment.PumpPreMash);
			    ShowBool(161,  76, (char *)"Pomp maischen", equipment.PumpOnMash);
			    ShowBool(  2,  92, (char *)"Pomp uitmaischen", equipment.PumpMashOut);
			    ShowBool(161,  92, (char *)"Pomp bij koken", equipment.PumpOnBoil);
			    ShowInteger(2, 108, (char *)"Pomp maxtemp.", (char *)"C", equipment.PumpMaxTemp);
			    ShowBool(161, 108, (char *)"PID bij mout weg", equipment.PIDPipe);
			    ShowSSR2(2, 124, equipment.SSR2);
			    ShowFloat(161, 124, (char *)"Spoelwater", NULL, equipment.TempHLT, 2);
			    ShowDouble(2, 140, (char *)"PID P", NULL, equipment.PID_kP, 3);
			    ShowBool(161, 140, (char *)"PID klassiek", equipment.PID_POn);
			    ShowDouble(2, 156, (char *)"PID I", NULL, equipment.PID_kI, 3);
			    ShowInteger(161, 156, (char *)"Sample tijd", (char *)"mS", equipment.SampleTime);
			    ShowDouble(2, 172, (char *)"PID D", NULL, equipment.PID_kD, 3);
			    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.Version = 1;
					equipment.Record = Records + 1;
					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 = 150.0;
					equipment.PID_kI = 1.5;
					equipment.PID_kD = 15000.0;
					equipment.PID_POn = PID_P_ON_E;
					equipment.SampleTime = 5000;
					append_equipment();
					Records++;
					CurrentRec = Records;
					UpdateRec = true;
					ESP_LOGI(TAG, "New equipment record %d", equipment.Record);
					break;

			    case 2:	if ((CurrentRec != config.EquipmentRec) && (Records > 1)) {
					    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 *)"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);
			EditFloat((char *)"Spoelwater temp", &equipment.TempHLT, 75, 98, 2);
			// Round to 0.25 values.
			equipment.TempHLT = ((int)(equipment.TempHLT * 4)) / 4.0;
			EditBool((char *)"PID klassiek", &equipment.PID_POn);
			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 *)"Sample tijd in mS", &equipment.SampleTime, 1000, 20000);
			// Round to 250 mSec units.
			equipment.SampleTime = ((int)(equipment.SampleTime / 250)) * 250;

			crc2 = crc32_le(0, dst, sizeof(equipment));
			if ((crc1 != crc2) && Confirm((char *)"Gewijzigd, opslaan?", (char *)"Ja", (char *)"Nee")) {
			    write_equipment(equipment.Record);
			}
			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