# HG changeset patch # User Michiel Broek # Date 1427918195 -7200 # Node ID 7283561977b1d02d272cc4dabfd303fec072b05d # Parent 1f89da6511a93f3f006b52fa923252509001d946 Added door and PSU sensors. Added interior light logic and switching. diff -r 1f89da6511a9 -r 7283561977b1 thermferm/thermferm.c --- a/thermferm/thermferm.c Wed Apr 01 17:28:26 2015 +0200 +++ b/thermferm/thermferm.c Wed Apr 01 21:56:35 2015 +0200 @@ -858,8 +858,7 @@ return 1; } - rc = devices_detect(); - if (rc) { + if ((rc = devices_detect())) { syslog(LOG_NOTICE, "Detected %d new devices", rc); wrconfig(); } @@ -893,8 +892,7 @@ } #ifdef HAVE_WIRINGPI_H - rc = piThreadCreate(my_panel_loop); - if (rc) { + if ((rc = piThreadCreate(my_panel_loop))) { fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); } @@ -924,8 +922,8 @@ /* * Safety, turn everything off */ - unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; - unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0; + unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; + unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; if (unit->mode == UNITMODE_PROFILE) { if (!unit->profile) syslog(LOG_NOTICE, "Starting unit %s in profile mode, no profile defined.", unit->name); @@ -942,7 +940,6 @@ } } - #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); lcd_buf_write(1, (char *)" ThermFerm "); @@ -950,6 +947,9 @@ piUnlock(LOCK_LCD); #endif + /* + * Initialize logfiles for each unit + */ for (unit = Config.units; unit; unit = unit->next) { if (unit->mode != UNITMODE_OFF) { initlog(unit->name); @@ -1104,6 +1104,51 @@ } } + if (unit->door_address) { + rc = read_sensor(unit->door_address, &temp); + if (rc == DEVPRESENT_YES) { + if (temp) { + if (unit->door_state == 0) { + syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name); + unit->door_state = 1; + } + } else { + if (unit->door_state) { + syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name); + unit->door_state = 0; + } + } + } else { + unit->door_state = 1; + } + } else { + /* + * No door switch, say door is closed. + */ + unit->door_state = 1; + } + + if (unit->psu_address) { + rc = read_sensor(unit->psu_address, &temp); + if (rc == DEVPRESENT_YES) { + if (temp) { + if (unit->psu_state == 0) { + syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name); + unit->psu_state = 1; + } + } else { + if (unit->psu_state) { + syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name); + unit->psu_state = 0; + } + } + } else { + unit->psu_state = 1; + } + } else { + unit->psu_state = 1; + } + /* * Handle profile */ @@ -1270,6 +1315,8 @@ unit->cooler_usage++; if (unit->fan_address && unit->fan_state) unit->fan_usage++; + if (unit->light_address && unit->light_state) + unit->light_usage++; #ifdef HAVE_WIRINGPI_H if (unit->heater_address && unit->cooler_address) { @@ -1307,12 +1354,30 @@ piUnlock(LOCK_LCD); } #endif - } - /* - * Temperature control per unit, each second - */ - for (unit = Config.units; unit; unit = unit->next) { + /* + * Interior lights + */ + if (unit->light_address) { + if (unit->door_state && unit->light_state) { + if (unit->light_wait > 0) { + unit->light_wait--; + } else { + unit->light_state = 0; + syslog(LOG_NOTICE, "Lights On => Off"); + } + } + if (!unit->door_state && !unit->light_state) { + unit->light_wait = unit->light_delay; /* No delay to turn lights on */ + unit->light_state = 1; + syslog(LOG_NOTICE, "Lights Off => On"); + } + device_out(unit->light_address, unit->light_state); + } + + /* + * Temperature control in this unit + */ if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) { int usePid = TRUE; @@ -1405,8 +1470,12 @@ } } } - device_out(unit->heater_address, unit->heater_state); + if (unit->door_state) + device_out(unit->heater_address, unit->heater_state); + else + device_out(unit->heater_address, 0); } + if (unit->cooler_address && ! unit->heater_state) { if (Out <= -1) { if (unit->cooler_wait < unit->cooler_delay) { @@ -1430,7 +1499,10 @@ } } } - device_out(unit->cooler_address, unit->cooler_state); + if (unit->door_state) + device_out(unit->cooler_address, unit->cooler_state); + else + device_out(unit->cooler_address, 0); } if ((unit->heater_address || unit->cooler_address) && unit->fan_address) { @@ -1459,7 +1531,10 @@ } } } - device_out(unit->fan_address, unit->fan_state); + if (unit->door_state) + device_out(unit->fan_address, unit->fan_state); + else + device_out(unit->fan_address, 0); } } else { @@ -1568,11 +1643,12 @@ /* * Turn everything off */ - unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; - unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0; + unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; + unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; device_out(unit->heater_address, unit->heater_state); device_out(unit->cooler_address, unit->cooler_state); device_out(unit->fan_address, unit->fan_state); + device_out(unit->light_address, unit->light_state); syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); }