diff -r 43a8ecb53637 -r 1f88be70f253 brewco/setup.c --- a/brewco/setup.c Sat Dec 12 22:17:25 2015 +0100 +++ b/brewco/setup.c Tue Dec 15 23:11:42 2015 +0100 @@ -27,6 +27,7 @@ #include "xutil.h" #include "keyboard.h" #include "rdconfig.h" +#include "rdrecipes.h" extern int my_shutdown; @@ -34,6 +35,7 @@ extern sys_config Config; extern int lcdHandle; extern int slcdHandle; +extern a_recipe *recipes; @@ -755,6 +757,341 @@ +/* + * Edit a single recipe + */ +void editRecipe(a_recipe *recipe) +{ + int idx = 1, key; + char pmpt[81]; + uLong ocrc, ncrc; + + if (debug) + fprintf(stdout, "Start edit recipe `%s' %s\n", recipe->name, recipe->uuid); + + prompt(0, NULL); + ncrc = ocrc = crc32(0L, Z_NULL, 0); + ocrc = crc32(ocrc, (const Bytef *) recipe, sizeof(a_recipe)); + + for (;;) { + + prompt(0, NULL); + prompt(139, NULL); + + if (idx == 1) + prompt(402, NULL); + else if (idx == 21) + prompt(404, NULL); + else + prompt(403, NULL); + + switch (idx) { // 12345678901234567890 + case 1: snprintf(pmpt, Config.lcd_cols + 1, "Recipe name:"); + prompt(200, pmpt); + snprintf(pmpt, Config.lcd_cols + 1, "%s", recipe->name); + prompt(300, pmpt); + break; + case 2: snprintf(pmpt, Config.lcd_cols + 1, "Recipe code:"); + prompt(200, pmpt); + snprintf(pmpt, Config.lcd_cols + 1, "%s", recipe->code); + prompt(300, pmpt); + break; + case 3: snprintf(pmpt, Config.lcd_cols + 1, "Boil time: %3d mins", recipe->boiltime); + prompt(200, pmpt); + break; + } + + key = keywait(); + if ((key == KEY_RETURN) || my_shutdown) { + ncrc = crc32(ncrc, (const Bytef *)recipe, sizeof(a_recipe)); + if (ocrc != ncrc) + wrrecipes(); + if (debug) + fprintf(stdout, "End edit recipe `%s' %s\n", recipe->name, recipe->uuid); + return; + } + + if ((key == KEY_UP) && (idx > 1)) + idx--; + if ((key == KEY_DOWN) && (idx < 6)) + idx++; + + if (key == KEY_ENTER) { + switch(idx) { + case 1: // name + break; + case 2: // code + break; + case 3: editInteger(&recipe->boiltime, 60, 240, 5, (char *)"Boil time:", (char *)"mins"); + break; + } + } + } +} + + + +void addRecipe(int number) +{ + a_recipe *tmpu, *recipe = (a_recipe *)malloc(sizeof(a_recipe)); + char name[81]; + uuid_t uu; + int i; + + if (debug) + fprintf(stdout, "Adding new recipe %d\n", number); + recipe->next = NULL; + recipe->uuid = malloc(37); + uuid_generate(uu); + uuid_unparse(uu, recipe->uuid); + snprintf(name, 21, "New recipe %d", number); + recipe->name = xstrcpy(name); + snprintf(name, 21, "%04d", number); + recipe->code = xstrcpy(name); + recipe->boiltime = 90; + recipe->starttime = recipe->endtime = (time_t)0; + /* + * Initial mash schedule, set a single-step 67 degr. mash + */ + recipe->mash[0].name = xstrcpy((char *)"Mash-in"); + recipe->mash[0].min = 20; + recipe->mash[0].max = 80; + recipe->mash[0].canskip = 0; + recipe->mash[0].setpoint = 68.0; + recipe->mash[0].skip = 0; + recipe->mash[0].duration = 1; + recipe->mash[1].name = xstrcpy((char *)"Phytase"); + recipe->mash[1].min = 25; + recipe->mash[1].max = 55; + recipe->mash[1].canskip = 1; + recipe->mash[1].setpoint = 40.0; + recipe->mash[1].skip = 1; + recipe->mash[1].duration = 10; + recipe->mash[2].name = xstrcpy((char *)"Glucanase"); + recipe->mash[2].min = 35; + recipe->mash[2].max = 50; + recipe->mash[2].canskip = 1; + recipe->mash[2].setpoint = 45.0; + recipe->mash[2].skip = 1; + recipe->mash[2].duration = 10; + recipe->mash[3].name = xstrcpy((char *)"Protease"); + recipe->mash[3].min = 45; + recipe->mash[3].max = 60; + recipe->mash[3].canskip = 1; + recipe->mash[3].setpoint = 55.0; + recipe->mash[3].skip = 1; + recipe->mash[3].duration = 10; + recipe->mash[4].name = xstrcpy((char *)"B-Amylase"); + recipe->mash[4].min = 50; + recipe->mash[4].max = 70; + recipe->mash[4].canskip = 1; + recipe->mash[4].setpoint = 62.0; + recipe->mash[4].skip = 1; + recipe->mash[4].duration = 30; + recipe->mash[5].name = xstrcpy((char *)"A-Amylase 1"); + recipe->mash[5].min = 60; + recipe->mash[5].max = 76; + recipe->mash[5].canskip = 1; + recipe->mash[5].setpoint = 67.0; + recipe->mash[5].skip = 1; + recipe->mash[5].duration = 30; + recipe->mash[6].name = xstrcpy((char *)"A-Amylase 2"); + recipe->mash[6].min = 60; + recipe->mash[6].max = 76; + recipe->mash[6].canskip = 0; + recipe->mash[6].setpoint = 67.0; + recipe->mash[6].skip = 0; + recipe->mash[6].duration = 60; + recipe->mash[7].name = xstrcpy((char *)"Mash-out"); + recipe->mash[7].min = 75; + recipe->mash[7].max = 80; + recipe->mash[7].canskip = 0; + recipe->mash[7].setpoint = 78.0; + recipe->mash[7].skip = 0; + recipe->mash[7].duration = 10; + + /* + * Add 2 hop additions, maximum 10 + */ + recipe->hops[0].name = xstrcpy((char *)"Hops 1"); + recipe->hops[0].boiltime = 90; + recipe->hops[1].name = xstrcpy((char *)"Hops 2"); + recipe->hops[1].boiltime = 5; + for (i = 2; i < 10; i++) { + recipe->hops[i].name = NULL; + recipe->hops[i].boiltime = 0; + } + + + /* + * Add 3 hopstands, disabled by default. + */ + recipe->hopstand[0].name = xstrcpy((char *)"Hopstand hot"); + recipe->hopstand[0].min = 88; + recipe->hopstand[0].max = 100; + recipe->hopstand[0].hold = 0; + recipe->hopstand[0].setpoint = 93.0; + recipe->hopstand[0].skip = 1; + recipe->hopstand[0].duration = 30; + recipe->hopstand[1].name = xstrcpy((char *)"Hopstand default"); + recipe->hopstand[1].min = 72; + recipe->hopstand[1].max = 77; + recipe->hopstand[1].hold = 0; + recipe->hopstand[1].setpoint = 75.0; + recipe->hopstand[1].skip = 1; + recipe->hopstand[1].duration = 60; + recipe->hopstand[2].name = xstrcpy((char *)"Hopstand cool"); + recipe->hopstand[2].min = 60; + recipe->hopstand[2].max = 66; + recipe->hopstand[2].hold = 0; + recipe->hopstand[2].setpoint = 63.0; + recipe->hopstand[2].skip = 1; + recipe->hopstand[2].duration = 60; + + editRecipe(recipe); + + if (recipes == NULL) { + recipes = recipe; + } else { + for (tmpu = recipes; tmpu; tmpu = tmpu->next) { + if (tmpu->next == NULL) { + tmpu->next = recipe; + break; + } + } + } + syslog(LOG_NOTICE, "Recipe %d added", number); + if (debug) + fprintf(stdout, "Recipe %d added\n", number); +} + + + +void editRecipes(void) +{ + int total, i, key, choice = 1;; + a_recipe *recipe; + char pmpt[81]; + + prompt(0, NULL); + for (;;) { + total = 0; + for (recipe = recipes; recipe; recipe = recipe->next) { + total++; + } + + if (debug) + fprintf(stdout, "editRecipes total=%d choice=%d\n", total, choice); + + i = 0; + if (total) { + for (recipe = recipes; recipe; recipe = recipe->next) { + i++; + if (i == choice) + break; + } + } + + prompt(102, NULL); /* " SETUP MENU " */ + prompt(221, NULL); /* " Select Recipe " */ + if (total) { + snprintf(pmpt, Config.lcd_cols + 1, "%s", recipe->name); + prompt(300, pmpt); + } + if (total == 0) + prompt(416, NULL); /* "add --- quit --- " */ + else if (total == 1) + prompt(415, NULL); /* "add --- quit ok " */ + else if (total && (choice == 1)) + prompt(414, NULL); /* "add dwn quit ok " */ + else if (total && (choice == total)) + prompt(404, NULL); /* " up --- quit ok " */ + else + prompt(403, NULL); /* " up dwn quit ok " */ + + key = keywait(); + if ((key == KEY_RETURN) || my_shutdown) + return; + if (total && (key == KEY_ENTER)) { + editRecipe(recipe); + prompt(0, NULL); + } + if (key == KEY_UP) { + if ((total == 1) || (choice == 1)) + addRecipe(total + 1); + else if (choice > 1) + choice--; + } + if ((key == KEY_DOWN) && (total > 1) && (choice < total)) + choice++; + } +} + + + +void editUnits(void) +{ + int total, i, key, choice = 1;; + units_list *unit; + char pmpt[81]; + + prompt(0, NULL); + for (;;) { + total = 0; + for (unit = Config.units; unit; unit = unit->next) { + total++; + } + + if (debug) + fprintf(stdout, "editUnits total=%d choice=%d\n", total, choice); + + if (total == 0) { + /* + * Impossible unless first setup was skipped + */ + addUnit(1); + total = 1; + } else { + i = 0; + for (unit = Config.units; unit; unit = unit->next) { + i++; + if (i == choice) + break; + } + prompt(102, NULL); /* " SETUP MENU " */ + prompt(222, NULL); /* " Select Brewsystem " */ + snprintf(pmpt, Config.lcd_cols + 1, "%s", unit->name); + prompt(300, pmpt); + if (total == 1) + prompt(415, NULL); /* "add --- quit ok " */ + else if (choice == 1) + prompt(414, NULL); /* "add dwn quit ok " */ + else if (choice == total) + prompt(404, NULL); /* " up --- quit ok " */ + else + prompt(403, NULL); /* " up dwn quit ok " */ + + key = keywait(); + if ((key == KEY_RETURN) || my_shutdown) + return; + if (key == KEY_ENTER) { + editUnit(unit); + prompt(0, NULL); + } + if (key == KEY_UP) { + if ((total == 1) || (choice == 1)) + addUnit(total + 1); + else if (choice > 1) + choice--; + } + if ((key == KEY_DOWN) && (total > 1) && (choice < total)) + choice++; + } + } +} + + + void setup(void) { int key, option = 202; @@ -763,18 +1100,18 @@ if (debug) fprintf(stdout, "setup() option=%d\n", option); prompt(0, NULL); - prompt(102, NULL); + prompt(102, NULL); /* " SETUP MENU " */ prompt(option, NULL); if (option == 202) - prompt(402, NULL); + prompt(402, NULL); /* "--- dwn quit ok " */ #ifdef USE_SIMULATOR else if (option == 205) #else else if (option == 204) #endif - prompt(404, NULL); + prompt(404, NULL); /* " up --- quit ok " */ else - prompt(403, NULL); + prompt(403, NULL); /* " up dwn quit ok " */ key = keywait(); @@ -793,9 +1130,9 @@ if (key == KEY_ENTER) { switch(option) { - case 202: // recipes + case 202: editRecipes(); break; - case 203: editUnit(Config.units); /* If more units, via a selector */ + case 203: editUnits(); break; case 204: // devices break;