Tue, 22 Dec 2015 21:07:14 +0100
Added initial part of the brew automation.
/***************************************************************************** * Copyright (C) 2015 * * Michiel Broek <mbroek at mbse dot eu> * * This file is part of the mbsePi-apps * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * mbsePi-apps is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ThermFerm; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ #include "brewco.h" #include "rdconfig.h" #include "rdsession.h" #include "rdrecipes.h" #include "util.h" #include "xutil.h" #include "lcd-pcf8574.h" #include "slcd.h" #include "lock.h" #include "devices.h" #include "keyboard.h" #include "simulator.h" #include "prompt.h" #include "setup.h" int my_shutdown = FALSE; int man_mlt_pump = 0; double hltInput; /* HLT PID variables */ double hltOutput; double hltSetpoint; double mltInput; /* MLT PID variables */ double mltOutput; double mltSetpoint; extern int debug; extern sys_config Config; extern a_recipe *recipes; extern int lcdHandle; extern int slcdHandle; extern int sock; #ifdef USE_SIMULATOR extern int SIM_cooler; #endif char *etcpath = NULL; char *varpath = NULL; #ifndef HAVE_WIRINGPI_H pthread_t threads[5]; #endif #define MANUAL_NONE 0 #define MANUAL_SELHLT 1 #define MANUAL_SELMLT 2 #define MANUAL_HLT 11 #define MANUAL_MLT 12 #define PERC_INIT 0 #define PERC_MLT 1 #define PERC_HLT 2 #define PERC_REST 3 int manual = MANUAL_NONE; /* * CGRAM characters */ unsigned char degC[8] = { 0b01000, 0b10100, 0b01000, 0b00111, 0b01000, 0b01000, 0b01000, 0b00111 }; unsigned char degF[8] = { 0b01000, 0b10100, 0b01000, 0b00111, 0b00100, 0b00110, 0b00100, 0b00100 }; unsigned char SP_Symbol[8] = { 0b11100, 0b10000, 0b11100, 0b00111, 0b11101, 0b00111, 0b00100, 0b00100 }; unsigned char PumpONOFF[8] = { 0b00000, 0b01110, 0b01010, 0b01110, 0b01000, 0b01000, 0b01000, 0b00000 }; unsigned char RevPumpONOFF[8] = { 0b11111, 0b10001, 0b10101, 0b10001, 0b10111, 0b10111, 0b10111, 0b11111 }; unsigned char HeatONOFF[8] = { 0b00000, 0b01010, 0b01010, 0b01110, 0b01110, 0b01010, 0b01010, 0b00000 }; unsigned char RevHeatONOFF[8] = { 0b11111, 0b10101, 0b10101, 0b10001, 0b10001, 0b10101, 0b10101, 0b11111 }; unsigned char Language[8] = { 0b11111, 0b00010, 0b01000, 0b11111, 0b00000, 0b10001, 0b10101, 0b11111 }; void help(void); void die(int); void help(void) { fprintf(stdout, "mbsePi-apps brewco v%s starting\n\n", VERSION); fprintf(stdout, "Usage: brewco [-d] [-h]\n"); fprintf(stdout, " -d --debug Debug and run in foreground\n"); fprintf(stdout, " -h --help Display this help\n"); } void die(int onsig) { switch (onsig) { #ifdef USE_SIMULATOR case SIGUSR1: syslog(LOG_NOTICE, "Got SIGUSR1, start cooler"); SIM_cooler = TRUE; return; case SIGUSR2: syslog(LOG_NOTICE, "Got SIGUSR2, stop cooler"); SIM_cooler = FALSE; return; #endif case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); break; case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); break; case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); break; case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); my_shutdown = TRUE; exit(SIGSEGV); break; default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } my_shutdown = TRUE; } void tempstatus(double hlttemp, double mlttemp) { char text[81]; snprintf(text, 8, "%6.2f\001", hltInput); #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdPosition(lcdHandle, 1, 1); lcdPuts(lcdHandle, text); #endif slcdPosition(slcdHandle, 1, 1); slcdPuts(slcdHandle, text); snprintf(text, 8, "%6.2f\001", mltInput); #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdPosition(lcdHandle, 10, 1); lcdPuts(lcdHandle, text); #endif slcdPosition(slcdHandle, 10, 1); slcdPuts(slcdHandle, text); } /* * Third line, show setPoints or heater percentage. */ void percstatus(int which) { char text[21]; if (which) snprintf(text, 8, "%6.2f\002", hltSetpoint); else snprintf(text, 8, "HLT%3d%%", (int)hltOutput); #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdPosition(lcdHandle, 1, 2); lcdPuts(lcdHandle, text); #endif slcdPosition(slcdHandle, 1, 2); slcdPuts(slcdHandle, text); if (which) snprintf(text, 8, "%6.2f\002", mltSetpoint); else snprintf(text, 8, "MLT%3d%%", (int)mltOutput); #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcdPosition(lcdHandle, 10, 2); lcdPuts(lcdHandle, text); #endif slcdPosition(slcdHandle, 10, 2); slcdPuts(slcdHandle, text); } void automatic_brew(units_list *, brew_session *, a_recipe *, int); void automatic_brew(units_list *unit, brew_session *brew, a_recipe *recipe, int dosave) { int key, save = dosave; // if (debug) // fprintf(stdout, "auto: step %d\n", brew->brewstep); switch (brew->brewstep) { case STEP_NA: if (debug) fprintf(stdout, "auto: init recipe: %s-%s unit: %s\n", recipe->code, recipe->name, unit->name); syslog(LOG_NOTICE, "AUTO: starting new brew, recipe: %s-%s unit: %s", recipe->code, recipe->name, unit->name); brew->brewstep = STEP_BREWDONE; break; case STEP_BREWINIT: break; case STEP_WATERCHECK: prompt(111, NULL); /* "AUTO --> Mash In " */ prompt(209, NULL); /* " Water Added? " */ break; case STEP_PUMPPRIME: prompt(111, NULL); /* "AUTO --> Mash In " */ prompt(210, NULL); /* " Pump Prime " */ break; case STEP_WAITSTART: prompt(111, NULL); /* "AUTO --> Mash In " */ prompt(212, NULL); /* " To be started in " */ break; case STEP_PREMASH: prompt(111, NULL); /* "AUTO --> Mash In " */ /* Heatup until strike temp reached */ break; case STEP_MASHING: switch (brew->mashstep) { case 0: prompt(111, NULL); break; case 1: prompt(112, NULL); break; case 2: prompt(113, NULL); break; case 3: prompt(114, NULL); break; case 4: prompt(115, NULL); break; case 5: prompt(116, NULL); break; case 6: prompt(117, NULL); break; case 7: prompt(118, NULL); break; } break; case STEP_IODINE: prompt(118, NULL); /* "AUTO --> Mash Out " */ prompt(213, NULL); /* " Iodine test " */ break; case STEP_MASHREMOVE: prompt(118, NULL); /* "AUTO --> Mash Out " */ break; case STEP_PREBOIL: break; case STEP_BOILING: prompt(119, NULL); /* "AUTO --> Boil " */ break; case STEP_BOILDONE: break; case STEP_HOPSTAND1: break; case STEP_COOLING1: prompt(120, NULL); /* "AUTO --> Cooling " */ prompt(214, NULL); /* " START COOLING " */ break; case STEP_WHIRLPOOL1: prompt(121, NULL); /* "AUTO --> Whirlpool " */ break; case STEP_COOLING2: prompt(120, NULL); /* "AUTO --> Cooling " */ break; case STEP_HOPSTAND2: break; case STEP_COOLING3: prompt(120, NULL); /* "AUTO --> Cooling " */ break; case STEP_HOPSTAND3: break; case STEP_COOLING: prompt(120, NULL); /* "AUTO --> Cooling " */ break; case STEP_WHIRLPOOL: prompt(121, NULL); /* "AUTO --> Whirlpool " */ break; case STEP_CLEANUP: break; case STEP_BREWDONE: syslog(LOG_NOTICE, "AUTO: brew done"); brew->brewstep = -1; brew->endtime = time(NULL); prompt(101, NULL); /* " Brewco x.x.x " */ prompt(200, NULL); /* " " */ prompt(301, NULL); /* " Finished " */ prompt(408, NULL); /* "--- --- Ok --- " */ do { key = keywait(); } while (key != KEY_RETURN); /* * Rewrite the display */ prompt(101, NULL); /* " Brewco x.x.x " */ prompt(300, NULL); /* " " */ prompt(401, NULL); /* "--- MAN AUTO SETUP" */ break; } if (save) wrsession(brew); } void manual_prompt(void) { switch (manual) { case MANUAL_SELHLT: prompt(104, NULL); /* " MANUAL MODE " */ prompt(303, NULL); /* " Manual HLT " */ prompt(402, NULL); /* "--- dwn quit ok " */ break; case MANUAL_SELMLT: prompt(104, NULL); /* " MANUAL MODE " */ prompt(304, NULL); /* " Manual MLT " */ prompt(404, NULL); /* " up --- quit ok " */ break; case MANUAL_HLT: prompt(104, NULL); /* " MANUAL MODE " */ prompt(300, NULL); /* " " */ prompt(413, NULL); /* "UP* *DWN heat --- " */ break; case MANUAL_MLT: prompt(104, NULL); /* " MANUAL MODE " */ prompt(300, NULL); /* " " */ prompt(406, NULL); /* "UP* *DWN heat pmp " */ break; } } /* * Manual menu for testing your equipment. */ int manual_menu(units_list *, int); int manual_menu(units_list *unit, int seconds) { int key; switch (manual) { case MANUAL_SELHLT: slcdDummy(slcdHandle); key = keycheck(); if (key == KEY_DOWN) { manual = MANUAL_SELMLT; manual_prompt(); } if (key == KEY_RETURN) { manual = MANUAL_NONE; man_mlt_pump = 0; PID_setMode(unit->PID_mlt, P_MANUAL); PID_setMode(unit->PID_hlt, P_MANUAL); hlt_status(0); mlt_status(0); device_out(unit->mlt_pump.uuid, man_mlt_pump); } if (key == KEY_ENTER) { // TODO: prompt for water manual = MANUAL_HLT; manual_prompt(); } break; case MANUAL_SELMLT: slcdDummy(slcdHandle); key = keycheck(); if (key == KEY_UP) { manual = MANUAL_SELHLT; manual_prompt(); } if (key == KEY_RETURN) { manual = MANUAL_NONE; man_mlt_pump = 0; PID_setMode(unit->PID_mlt, P_MANUAL); PID_setMode(unit->PID_hlt, P_MANUAL); hlt_status(0); mlt_status(0); device_out(unit->mlt_pump.uuid, man_mlt_pump); } if (key == KEY_ENTER) { // TODO: prompt for water manual = MANUAL_MLT; manual_prompt(); } break; case MANUAL_HLT: tempstatus(*unit->PID_hlt->myInput, *unit->PID_mlt->myInput); percstatus((seconds / 2) % 4); slcdDummy(slcdHandle); key = keycheck(); if (key == KEY_RETURN) { if (PID_getMode(unit->PID_hlt) == P_MANUAL) { PID_setMode(unit->PID_hlt, P_AUTOMATIC); hlt_status(1); } else { PID_setMode(unit->PID_hlt, P_MANUAL); hlt_status(0); } } if ((key == KEY_DOWN) && (hltSetpoint > 10)) hltSetpoint -= 1.0; if ((key == KEY_UP) && (hltSetpoint < 100)) hltSetpoint += 1.0; if (key == KEY_ESCAPE) { manual = MANUAL_SELHLT; manual_prompt(); } break; case MANUAL_MLT: tempstatus(*unit->PID_hlt->myInput, *unit->PID_mlt->myInput); percstatus((seconds / 2) % 4); slcdDummy(slcdHandle); key = keycheck(); if (key == KEY_RETURN) { if (PID_getMode(unit->PID_mlt) == P_MANUAL) { PID_setMode(unit->PID_mlt, P_AUTOMATIC); mlt_status(1); } else { PID_setMode(unit->PID_mlt, P_MANUAL); mlt_status(0); } } if ((key == KEY_DOWN) && (mltSetpoint > 10)) mltSetpoint -= 1.0; if ((key == KEY_UP) && (mltSetpoint < 100)) mltSetpoint += 1.0; if (key == KEY_ENTER) { if (man_mlt_pump) man_mlt_pump = 0; else man_mlt_pump = 1; } if (key == KEY_ESCAPE) { manual = MANUAL_SELMLT; manual_prompt(); } device_out(unit->mlt_pump.uuid, man_mlt_pump); break; } return 0; } char *choose_recipe(void); char *choose_recipe(void) { int total, i, key, choice = 1; static char uuid[37]; a_recipe *recipe; char pmpt[81]; strcpy(uuid, (char *)"00000000-0000-0000-0000-000000000000"); for (;;) { total = 0; for (recipe = recipes; recipe; recipe = recipe->next) total++; if (total == 0) return uuid; i = 0; 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 %s ", recipe->code, recipe->name); prompt(300, pmpt); } if (total == 1) prompt(405, NULL); /* "--- --- quit ok " */ else if (choice == 1) prompt(402, NULL); /* "--- 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 uuid; if (key == KEY_ENTER) { strcpy(uuid, recipe->uuid); return uuid; } if ((key == KEY_UP) && (total > 1) && (choice > 1)) { choice--; } if ((key == KEY_DOWN) && (total > 1) && (choice < total)) choice++; } } int server(void); int server(void) { int rc = 0, run = 1, dosave, key, temp, MLTp, HLTp, percslot, percfase = PERC_INIT; int do_init = TRUE, seconds = 0, minutes = 0; units_list *unit; a_recipe *recipe = NULL; brew_session *brew = NULL; #ifndef HAVE_WIRINGPI_H long t = 0; #endif time_t now, last = (time_t)0; long nowmillis, perctimer; struct tm *tm; prompt(101, NULL); /* * Define special characters in the display CGRAM */ if (Config.tempFormat == 'C') slcdCharDef(slcdHandle, 1, degC); else slcdCharDef(slcdHandle, 1, degF); slcdCharDef(slcdHandle, 2, SP_Symbol); slcdCharDef(slcdHandle, 3, PumpONOFF); slcdCharDef(slcdHandle, 4, RevPumpONOFF); slcdCharDef(slcdHandle, 5, HeatONOFF); slcdCharDef(slcdHandle, 6, RevHeatONOFF); slcdCharDef(slcdHandle, 7, Language); if (lockprog((char *)"brewco")) { syslog(LOG_NOTICE, "Can't lock"); return 1; } if (debug) fprintf(stdout, "Begin server()\n"); if ((rc = devices_detect())) { syslog(LOG_NOTICE, "Detected %d new devices", rc); if (debug) fprintf(stdout, "Detected %d new devices\n", rc); wrconfig(); } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_devices_loop); #else rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_keyboard_loop); #else rc = pthread_create(&threads[t], NULL, my_keyboard_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_keyboard_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_keyboard_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #ifdef USE_SIMULATOR #ifdef HAVE_WIRINGPI_H rc = piThreadCreate(my_simulator_loop); #else rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); #endif if (rc) { fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); #ifndef HAVE_WIRINGPI_H } else { t++; #endif } #endif if (! Config.units) { /* * No brewsystems defined, add the first */ prompt(218, NULL); /* Add Brewsystem? */ prompt(407, NULL); /* --- --- Ok --- */ do { key = keywait(); } while (key != KEY_RETURN); if (key == KEY_RETURN) { addUnit(1); } } /* * Initialize units for processing */ for (unit = Config.units; unit; unit = unit->next) { if (unit->active) break; } if (! unit->active) { fprintf(stdout, "No active units found\n"); } /* * Safety, turn everything off */ if (unit->active) { if (debug) fprintf(stdout, "Starting brewsystem %d `%s'\n", unit->number, unit->name); syslog(LOG_NOTICE, "Starting brewsystem %d `%s'", unit->number, unit->name); } /* * During automation there will be a state file: * ~/.brewco/var/brewing.xml * If this file is present, there has been a crash. */ brew = (brew_session *)malloc(sizeof(brew_session)); if (rdsession(brew) == 0) { } else { /* * No active brew session, make that permanent. */ free(brew); brew = NULL; } do { if (my_shutdown) { run = 0; unit->hlt_heater.value = 0; unit->mlt_heater.value = 0; unit->mlt_pump.value = 0; device_out(unit->hlt_heater.uuid, 0); device_out(unit->mlt_heater.uuid, 0); device_out(unit->mlt_pump.uuid, 0); hlt_status(0); mlt_status(0); break; } /* * Do we need to initialize this unit? */ if (do_init) { if (debug) fprintf(stdout, "Initialize brewsystem %d `%s'\n", unit->number, unit->name); syslog(LOG_NOTICE, "Initialize brewsystem %d `%s'", unit->number, unit->name); prompt(0, NULL); prompt(101, NULL); /* " Brewco x.x.x " */ prompt(401, NULL); /* "--- MAN AUTO SETUP" */ /* * Turn everything off */ unit->hlt_heater.value = 0; unit->mlt_heater.value = 0; unit->mlt_pump.value = 0; device_out(unit->hlt_heater.uuid, 0); device_out(unit->mlt_heater.uuid, 0); device_out(unit->mlt_pump.uuid, 0); /* * Initialize PID's */ hltInput = hltSetpoint = mltInput = mltSetpoint = 20.0; hltOutput = mltOutput = 0; PID_init(unit->PID_hlt, &hltInput, &hltOutput, &hltSetpoint, unit->PID_hlt->dispKd, unit->PID_hlt->dispKi, unit->PID_hlt->dispKd, unit->PID_hlt->Direction); PID_setOutputLimits(unit->PID_hlt, 0, 100); PID_setSampleTime(unit->PID_hlt, unit->PID_hlt->SampleTime); PID_init(unit->PID_mlt, &mltInput, &mltOutput, &mltSetpoint, unit->PID_mlt->dispKd, unit->PID_mlt->dispKi, unit->PID_mlt->dispKd, unit->PID_mlt->Direction); PID_setOutputLimits(unit->PID_mlt, 0, 100); PID_setSampleTime(unit->PID_mlt, unit->PID_mlt->SampleTime); hlt_status(0); mlt_status(0); manual = MANUAL_NONE; do_init = FALSE; nowmillis = perctimer = millis(); percslot = 0; percfase = PERC_INIT; dosave = 0; } /* run_pause code here */ /* * Update PID's, even if they are off. Both PID's will do * the scheduling by themselves. */ rc = PID_compute(unit->PID_hlt); rc = PID_compute(unit->PID_mlt); /* * This is the serial heaters schedule loop. The total * loop time is 5 seconds, heating is 0..100% so we * have 5 mSeconds timeslots. The MLT has priority over * the HLT heater, so the HLT heater can get too little * power. TODO: simultaneous use must be implemented. * In sequentiel mode, the total drawn power is the same * as the power used by the largest heater element. */ nowmillis = millis(); if (nowmillis > (perctimer + 50)) { percslot++; if (percfase == PERC_INIT) { HLTp = (int)hltOutput; MLTp = (int)mltOutput; if ((MLTp + HLTp) > 100) HLTp = 100 - MLTp; /* The HLT has lower priority */ if (MLTp) { percfase = PERC_MLT; device_out(unit->hlt_heater.uuid, 0); device_out(unit->mlt_heater.uuid, 1); } else if (HLTp) { percfase = PERC_HLT; device_out(unit->hlt_heater.uuid, 1); device_out(unit->mlt_heater.uuid, 0); } else { percfase = PERC_REST; device_out(unit->hlt_heater.uuid, 0); device_out(unit->mlt_heater.uuid, 0); } if (debug) fprintf(stdout, " perslot=%d MLT=%d%% HLT=%d%% fase=%d\n", percslot, MLTp, HLTp, percfase); } else if (percfase == PERC_MLT) { if (percslot > MLTp) { device_out(unit->mlt_heater.uuid, 0); if (HLTp) { device_out(unit->hlt_heater.uuid, 1); percfase = PERC_HLT; } else { percfase = PERC_REST; } } } else if (percfase == PERC_HLT) { if (percslot > (MLTp + HLTp)) { device_out(unit->hlt_heater.uuid, 0); percfase = PERC_REST; } } if (percslot == 100) { /* End of the loop, start over */ percslot = 0; percfase = PERC_INIT; } perctimer = nowmillis; } now = time(NULL); if (now != last) { /* * Each second */ last = now; seconds++; if (seconds > 59) { seconds = 0; minutes++; dosave = 1; } //fprintf(stdout, "%d seconds %d minutes %ld millis\n", seconds, minutes, millis()); rc = device_in(unit->hlt_sensor.uuid, &temp); if (rc == DEVPRESENT_YES) { hltInput = temp / 1000.0; unit->hlt_sensor.state = 0; } else if (rc == DEVPRESENT_ERROR) { unit->hlt_sensor.state = 1; } else { unit->hlt_sensor.state = 2; } rc = device_in(unit->mlt_sensor.uuid, &temp); if (rc == DEVPRESENT_YES) { mltInput = temp / 1000.0; unit->mlt_sensor.state = 0; } else if (rc == DEVPRESENT_ERROR) { unit->mlt_sensor.state = 1; } else { unit->mlt_sensor.state = 2; } if (debug && ((seconds % 10) == 1)) { fprintf(stdout, "MLT: In=%.2lf Out=%.2lf Set=%.2lf Stat=%d HLT: In=%.2lf Out=%.2lf Set=%.2lf Stat=%d\n", mltInput, mltOutput, mltSetpoint, PID_getMode(unit->PID_mlt), hltInput, hltOutput, hltSetpoint, PID_getMode(unit->PID_hlt)); } } if (brew) { /* * Automate mode */ automatic_brew(unit, brew, recipe, dosave); dosave = 0; if (brew->brewstep == -1) { /* * Save session and move it */ wrsession(brew); char *fpath, *tpath; fpath = xstrcpy(etcpath); fpath = xstrcat(fpath, (char *)"brewing.xml"); tpath = xstrcpy(varpath); tpath = xstrcat(tpath, (char *)"old/brewing.xml."); mkdirs(tpath, 0755); tpath = xstrcat(tpath, brew->name); if (debug) fprintf(stdout, "auto: saving as %s\n", tpath); file_cp(fpath, tpath); unlink(fpath); free(fpath); free(tpath); /* * Free memory, session is over. */ if (brew->name) free(brew->name); if (brew->uuid_recipe) free(brew->uuid_recipe); if (brew->uuid_unit) free(brew->uuid_unit); free(brew); brew = NULL; } } else if (manual != MANUAL_NONE) { /* * Manual mode */ manual_menu(unit, seconds); if (manual == MANUAL_NONE) { /* * Rewrite the display */ prompt(101, NULL); /* " Brewco x.x.x " */ prompt(300, NULL); /* " " */ prompt(401, NULL); /* "--- MAN AUTO SETUP" */ } } else { /* * Not running. */ tempstatus(hltInput, mltInput); key = keycheck(); if (key == KEY_ENTER) { setup(); prompt(101, NULL); /* " Brewco x.x.x " */ prompt(401, NULL); /* "--- MAN AUTO SETUP" */ } else if (key == KEY_RETURN && ! brew) { int i, isOk = TRUE; char message[41]; tm = localtime(&now); snprintf(message, 40, "%04d%02d%02d-%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min); brew = (brew_session *)malloc(sizeof(brew_session)); brew->uuid_recipe = xstrcpy(choose_recipe()); brew->uuid_unit = xstrcpy(unit->uuid); brew->name = xstrcpy(message); brew->brewstep = STEP_NA; brew->mashstep = MASH_NA; brew->timeout = brew->boiltimer = 0; brew->starttime = brew->endtime = (time_t)0; /* * Now check if everything is sane */ if (strcmp(brew->uuid_recipe, (char *)"00000000-0000-0000-0000-000000000000") == 0) { isOk = FALSE; snprintf(message, Config.lcd_cols + 1, " No recipe selected "); if (debug) fprintf(stdout, "brew init: No recipe selected\n"); } if (isOk) { isOk = FALSE; for (recipe = recipes; recipe; recipe = recipe->next) { if (strcmp(recipe->uuid, brew->uuid_recipe) == 0) { isOk = TRUE; if (! recipe->boiltime) isOk = FALSE; for (i = 0; i < 8; i++) { if (! recipe->mash[i].skip && ! recipe->mash[i].setpoint) isOk = FALSE; } for (i = 0; i < 3; i++) { if (! recipe->hopstand[i].skip && recipe->hopstand[i].hold && (recipe->hopstand[i].setpoint == 0)) isOk = FALSE; } break; } } if (isOk == FALSE) { snprintf(message, Config.lcd_cols + 1, " Recipe error "); if (debug) fprintf(stdout, "brew init: recipe error\n"); } } if (debug) fprintf(stdout, "init brew: %s\n", isOk ? (char *)"Ok":(char *)"Error"); if (isOk) { wrsession(brew); } else { prompt(300, message); prompt(408, NULL); /* "--- --- Ok --- " */ key = keywait(); if (brew->name) free(brew->name); if (brew->uuid_recipe) free(brew->uuid_recipe); if (brew->uuid_unit) free(brew->uuid_unit); free(brew); brew = NULL; /* * Rewrite the display */ prompt(101, NULL); /* " Brewco x.x.x " */ prompt(300, NULL); /* " " */ prompt(401, NULL); /* "--- MAN AUTO SETUP" */ } } else if (key == KEY_DOWN) { manual = MANUAL_SELHLT; manual_prompt(); } } usleep(5000); /* 5 mSec */ } while (run); syslog(LOG_NOTICE, "Out of loop"); if (debug) fprintf(stdout, (char *)"Out of loop\n"); prompt(0, NULL); prompt(101, NULL); /* " Brewco x.x.x " */ prompt(302, NULL); /* " Shutting down " */ /* * Give threads time to cleanup */ usleep(1500000); prompt(0, NULL); setBacklight(0); if (sock != -1) { if (shutdown(sock, SHUT_RDWR)) { syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno)); } sock = -1; } wrrecipes(); wrconfig(); ulockprog((char *)"brewco"); return 0; } int main(int argc, char *argv[]) { int rc = 0, c, i; char *homepath = NULL; while (1) { int option_index = 0; static struct option long_options[] = { {"debug", 0, 0, 'c'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "dh", long_options, &option_index); if (c == -1) break; switch (c) { case 'd': debug = TRUE; break; case 'h': help(); return 1; } } openlog("brewco", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); syslog(LOG_NOTICE, "mbsePi-apps brewco v%s starting", VERSION); if (debug) fprintf(stdout, "mbsePi-apps brewco v%s starting\n", VERSION); /* * Set home directory path */ if (getenv((char *)"USER") == NULL) { homepath = xstrcpy((char *)"/root"); } else { homepath = xstrcpy(getenv((char *)"HOME")); } varpath = xstrcpy(homepath); varpath = xstrcat(varpath, (char *)"/.brewco/var/"); etcpath = xstrcpy(homepath); etcpath = xstrcat(etcpath, (char *)"/.brewco/etc/"); mkdirs(varpath, 0755); mkdirs(etcpath, 0755); if (rdconfig()) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "Error reading configuration: halted"); return 1; } if (debug) fprintf(stdout, "configuration loaded\n"); if (rdrecipes()) { fprintf(stderr, "Error reading recipes\n"); syslog(LOG_NOTICE, "Error reading recipes: halted"); return 1; } if (debug) fprintf(stdout, "recipes loaded\n"); /* * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored * but that's live. This daemon should only be stopped by SIGTERM. * Don't catch SIGCHLD. */ for (i = 0; i < NSIG; i++) { if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) signal(i, (void (*))die); } #ifdef HAVE_WIRINGPI_H if (wiringPiSetup () ) return 1; #endif if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) { fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); return 1; } rc = server(); syslog(LOG_NOTICE, "Finished, rc=%d", rc); if (debug) fprintf(stdout, "Finished, rc=%d\n", rc); return rc; }