856 if (lockprog((char *)"thermferm")) { |
856 if (lockprog((char *)"thermferm")) { |
857 syslog(LOG_NOTICE, "Can't lock"); |
857 syslog(LOG_NOTICE, "Can't lock"); |
858 return 1; |
858 return 1; |
859 } |
859 } |
860 |
860 |
861 rc = devices_detect(); |
861 if ((rc = devices_detect())) { |
862 if (rc) { |
|
863 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
862 syslog(LOG_NOTICE, "Detected %d new devices", rc); |
864 wrconfig(); |
863 wrconfig(); |
865 } |
864 } |
866 |
865 |
867 #ifdef HAVE_WIRINGPI_H |
866 #ifdef HAVE_WIRINGPI_H |
891 t++; |
890 t++; |
892 #endif |
891 #endif |
893 } |
892 } |
894 |
893 |
895 #ifdef HAVE_WIRINGPI_H |
894 #ifdef HAVE_WIRINGPI_H |
896 rc = piThreadCreate(my_panel_loop); |
895 if ((rc = piThreadCreate(my_panel_loop))) { |
897 if (rc) { |
|
898 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
896 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); |
899 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
897 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); |
900 } |
898 } |
901 #endif |
899 #endif |
902 |
900 |
922 */ |
920 */ |
923 for (unit = Config.units; unit; unit = unit->next) { |
921 for (unit = Config.units; unit; unit = unit->next) { |
924 /* |
922 /* |
925 * Safety, turn everything off |
923 * Safety, turn everything off |
926 */ |
924 */ |
927 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; |
925 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; |
928 unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0; |
926 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; |
929 if (unit->mode == UNITMODE_PROFILE) { |
927 if (unit->mode == UNITMODE_PROFILE) { |
930 if (!unit->profile) |
928 if (!unit->profile) |
931 syslog(LOG_NOTICE, "Starting unit %s in profile mode, no profile defined.", unit->name); |
929 syslog(LOG_NOTICE, "Starting unit %s in profile mode, no profile defined.", unit->name); |
932 else |
930 else |
933 syslog(LOG_NOTICE, "Starting unit %s in profile state %s.", unit->name, PROFSTATE[unit->prof_state]); |
931 syslog(LOG_NOTICE, "Starting unit %s in profile state %s.", unit->name, PROFSTATE[unit->prof_state]); |
940 } else { |
938 } else { |
941 syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); |
939 syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); |
942 } |
940 } |
943 } |
941 } |
944 |
942 |
945 |
|
946 #ifdef HAVE_WIRINGPI_H |
943 #ifdef HAVE_WIRINGPI_H |
947 piLock(LOCK_LCD); |
944 piLock(LOCK_LCD); |
948 lcd_buf_write(1, (char *)" ThermFerm "); |
945 lcd_buf_write(1, (char *)" ThermFerm "); |
949 lcd_buf_write(2, (char *)" Version %s ", VERSION); |
946 lcd_buf_write(2, (char *)" Version %s ", VERSION); |
950 piUnlock(LOCK_LCD); |
947 piUnlock(LOCK_LCD); |
951 #endif |
948 #endif |
952 |
949 |
|
950 /* |
|
951 * Initialize logfiles for each unit |
|
952 */ |
953 for (unit = Config.units; unit; unit = unit->next) { |
953 for (unit = Config.units; unit; unit = unit->next) { |
954 if (unit->mode != UNITMODE_OFF) { |
954 if (unit->mode != UNITMODE_OFF) { |
955 initlog(unit->name); |
955 initlog(unit->name); |
956 } |
956 } |
957 } |
957 } |
1100 } else if (rc == DEVPRESENT_ERROR) { |
1100 } else if (rc == DEVPRESENT_ERROR) { |
1101 unit->beer_state = 1; |
1101 unit->beer_state = 1; |
1102 } else { |
1102 } else { |
1103 unit->beer_state = 2; |
1103 unit->beer_state = 2; |
1104 } |
1104 } |
|
1105 } |
|
1106 |
|
1107 if (unit->door_address) { |
|
1108 rc = read_sensor(unit->door_address, &temp); |
|
1109 if (rc == DEVPRESENT_YES) { |
|
1110 if (temp) { |
|
1111 if (unit->door_state == 0) { |
|
1112 syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name); |
|
1113 unit->door_state = 1; |
|
1114 } |
|
1115 } else { |
|
1116 if (unit->door_state) { |
|
1117 syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name); |
|
1118 unit->door_state = 0; |
|
1119 } |
|
1120 } |
|
1121 } else { |
|
1122 unit->door_state = 1; |
|
1123 } |
|
1124 } else { |
|
1125 /* |
|
1126 * No door switch, say door is closed. |
|
1127 */ |
|
1128 unit->door_state = 1; |
|
1129 } |
|
1130 |
|
1131 if (unit->psu_address) { |
|
1132 rc = read_sensor(unit->psu_address, &temp); |
|
1133 if (rc == DEVPRESENT_YES) { |
|
1134 if (temp) { |
|
1135 if (unit->psu_state == 0) { |
|
1136 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name); |
|
1137 unit->psu_state = 1; |
|
1138 } |
|
1139 } else { |
|
1140 if (unit->psu_state) { |
|
1141 syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name); |
|
1142 unit->psu_state = 0; |
|
1143 } |
|
1144 } |
|
1145 } else { |
|
1146 unit->psu_state = 1; |
|
1147 } |
|
1148 } else { |
|
1149 unit->psu_state = 1; |
1105 } |
1150 } |
1106 |
1151 |
1107 /* |
1152 /* |
1108 * Handle profile |
1153 * Handle profile |
1109 */ |
1154 */ |
1268 unit->heater_usage++; |
1313 unit->heater_usage++; |
1269 if (unit->cooler_address && unit->cooler_state) |
1314 if (unit->cooler_address && unit->cooler_state) |
1270 unit->cooler_usage++; |
1315 unit->cooler_usage++; |
1271 if (unit->fan_address && unit->fan_state) |
1316 if (unit->fan_address && unit->fan_state) |
1272 unit->fan_usage++; |
1317 unit->fan_usage++; |
|
1318 if (unit->light_address && unit->light_state) |
|
1319 unit->light_usage++; |
1273 |
1320 |
1274 #ifdef HAVE_WIRINGPI_H |
1321 #ifdef HAVE_WIRINGPI_H |
1275 if (unit->heater_address && unit->cooler_address) { |
1322 if (unit->heater_address && unit->cooler_address) { |
1276 piLock(LOCK_LCD); |
1323 piLock(LOCK_LCD); |
1277 if (unit->heater_state) { |
1324 if (unit->heater_state) { |
1305 lcd_buf_write(row++, "Cool %s ", unit->cooler_state ? "On ":"Off"); |
1352 lcd_buf_write(row++, "Cool %s ", unit->cooler_state ? "On ":"Off"); |
1306 } |
1353 } |
1307 piUnlock(LOCK_LCD); |
1354 piUnlock(LOCK_LCD); |
1308 } |
1355 } |
1309 #endif |
1356 #endif |
1310 } |
1357 |
1311 |
1358 /* |
1312 /* |
1359 * Interior lights |
1313 * Temperature control per unit, each second |
1360 */ |
1314 */ |
1361 if (unit->light_address) { |
1315 for (unit = Config.units; unit; unit = unit->next) { |
1362 if (unit->door_state && unit->light_state) { |
|
1363 if (unit->light_wait > 0) { |
|
1364 unit->light_wait--; |
|
1365 } else { |
|
1366 unit->light_state = 0; |
|
1367 syslog(LOG_NOTICE, "Lights On => Off"); |
|
1368 } |
|
1369 } |
|
1370 if (!unit->door_state && !unit->light_state) { |
|
1371 unit->light_wait = unit->light_delay; /* No delay to turn lights on */ |
|
1372 unit->light_state = 1; |
|
1373 syslog(LOG_NOTICE, "Lights Off => On"); |
|
1374 } |
|
1375 device_out(unit->light_address, unit->light_state); |
|
1376 } |
|
1377 |
|
1378 /* |
|
1379 * Temperature control in this unit |
|
1380 */ |
1316 if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) { |
1381 if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) { |
1317 int usePid = TRUE; |
1382 int usePid = TRUE; |
1318 |
1383 |
1319 sp = unit->beer_set; |
1384 sp = unit->beer_set; |
1320 pv = unit->beer_temperature / 1000.0; |
1385 pv = unit->beer_temperature / 1000.0; |
1403 syslog(LOG_NOTICE, "Heater On => Off"); |
1468 syslog(LOG_NOTICE, "Heater On => Off"); |
1404 unit->heater_state = 0; |
1469 unit->heater_state = 0; |
1405 } |
1470 } |
1406 } |
1471 } |
1407 } |
1472 } |
1408 device_out(unit->heater_address, unit->heater_state); |
1473 if (unit->door_state) |
1409 } |
1474 device_out(unit->heater_address, unit->heater_state); |
|
1475 else |
|
1476 device_out(unit->heater_address, 0); |
|
1477 } |
|
1478 |
1410 if (unit->cooler_address && ! unit->heater_state) { |
1479 if (unit->cooler_address && ! unit->heater_state) { |
1411 if (Out <= -1) { |
1480 if (Out <= -1) { |
1412 if (unit->cooler_wait < unit->cooler_delay) { |
1481 if (unit->cooler_wait < unit->cooler_delay) { |
1413 unit->cooler_wait++; |
1482 unit->cooler_wait++; |
1414 syslog(LOG_NOTICE, "cooler_wait + %d/%d", unit->cooler_wait, unit->cooler_delay); |
1483 syslog(LOG_NOTICE, "cooler_wait + %d/%d", unit->cooler_wait, unit->cooler_delay); |
1428 syslog(LOG_NOTICE, "Cooler On => Off"); |
1497 syslog(LOG_NOTICE, "Cooler On => Off"); |
1429 unit->cooler_state = 0; |
1498 unit->cooler_state = 0; |
1430 } |
1499 } |
1431 } |
1500 } |
1432 } |
1501 } |
1433 device_out(unit->cooler_address, unit->cooler_state); |
1502 if (unit->door_state) |
|
1503 device_out(unit->cooler_address, unit->cooler_state); |
|
1504 else |
|
1505 device_out(unit->cooler_address, 0); |
1434 } |
1506 } |
1435 |
1507 |
1436 if ((unit->heater_address || unit->cooler_address) && unit->fan_address) { |
1508 if ((unit->heater_address || unit->cooler_address) && unit->fan_address) { |
1437 /* |
1509 /* |
1438 * If there is a heater or cooler and we have a fan, turn fan on if |
1510 * If there is a heater or cooler and we have a fan, turn fan on if |
1457 syslog(LOG_NOTICE, "Fan On => Off"); |
1529 syslog(LOG_NOTICE, "Fan On => Off"); |
1458 unit->fan_state = 0; |
1530 unit->fan_state = 0; |
1459 } |
1531 } |
1460 } |
1532 } |
1461 } |
1533 } |
1462 device_out(unit->fan_address, unit->fan_state); |
1534 if (unit->door_state) |
|
1535 device_out(unit->fan_address, unit->fan_state); |
|
1536 else |
|
1537 device_out(unit->fan_address, 0); |
1463 } |
1538 } |
1464 |
1539 |
1465 } else { |
1540 } else { |
1466 P_err = 0.0; |
1541 P_err = 0.0; |
1467 unit->PID_iState = 0.0; |
1542 unit->PID_iState = 0.0; |
1566 */ |
1641 */ |
1567 for (unit = Config.units; unit; unit = unit->next) { |
1642 for (unit = Config.units; unit; unit = unit->next) { |
1568 /* |
1643 /* |
1569 * Turn everything off |
1644 * Turn everything off |
1570 */ |
1645 */ |
1571 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; |
1646 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; |
1572 unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0; |
1647 unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; |
1573 device_out(unit->heater_address, unit->heater_state); |
1648 device_out(unit->heater_address, unit->heater_state); |
1574 device_out(unit->cooler_address, unit->cooler_state); |
1649 device_out(unit->cooler_address, unit->cooler_state); |
1575 device_out(unit->fan_address, unit->fan_state); |
1650 device_out(unit->fan_address, unit->fan_state); |
|
1651 device_out(unit->light_address, unit->light_state); |
1576 syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); |
1652 syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); |
1577 } |
1653 } |
1578 |
1654 |
1579 if (debug) |
1655 if (debug) |
1580 fprintf(stdout, (char *)"Out of loop\n"); |
1656 fprintf(stdout, (char *)"Out of loop\n"); |