thermferm/thermferm.c

changeset 645
49eb753a958b
parent 644
07cc86900473
child 646
e3edc783006b
equal deleted inserted replaced
644:07cc86900473 645:49eb753a958b
48 extern int slcdHandle; 48 extern int slcdHandle;
49 int setupmenu = MENU_NONE; 49 int setupmenu = MENU_NONE;
50 units_list *current_unit = NULL; /* In panel editor this points to the current unit. */ 50 units_list *current_unit = NULL; /* In panel editor this points to the current unit. */
51 float temp_temp = 20.0; 51 float temp_temp = 20.0;
52 52
53 #ifndef HAVE_WIRINGPI_H
54 pthread_t threads[5]; 53 pthread_t threads[5];
55 #endif 54 pthread_mutex_t mutexes[5];
56 extern const char UNITMODE[5][8]; 55 extern const char UNITMODE[5][8];
57 extern const char PROFSTATE[4][6]; 56 extern const char PROFSTATE[4][6];
58 57
59 extern int sock; 58 extern int sock;
60 59
139 138
140 void go_menu(int menu) 139 void go_menu(int menu)
141 { 140 {
142 char buf[21]; 141 char buf[21];
143 142
144 #ifdef HAVE_WIRINGPI_H 143 pthread_mutex_lock(&mutexes[LOCK_LCD]);
145 piLock(LOCK_LCD); 144 pthread_mutex_lock(&mutexes[LOCK_MENU]);
146 piLock(LOCK_MENU); 145 #ifdef HAVE_WIRINGPI_H
147 lcdClear(lcdHandle); 146 lcdClear(lcdHandle);
148 lcdPosition(lcdHandle, 0, 0); 147 lcdPosition(lcdHandle, 0, 0);
149 #endif 148 #endif
150 slcdClear(slcdHandle); 149 slcdClear(slcdHandle);
151 slcdPosition(slcdHandle, 0, 0); 150 slcdPosition(slcdHandle, 0, 0);
372 #endif 371 #endif
373 slcdPuts(slcdHandle, "Reboot system"); 372 slcdPuts(slcdHandle, "Reboot system");
374 break; 373 break;
375 } 374 }
376 375
377 #ifdef HAVE_WIRINGPI_H 376 pthread_mutex_unlock(&mutexes[LOCK_MENU]);
378 piUnlock(LOCK_MENU); 377 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
379 piUnlock(LOCK_LCD);
380 #endif
381 } 378 }
382 379
383 380
384 381
385 void stopLCD(void) 382 void stopLCD(void)
386 { 383 {
387 #ifdef HAVE_WIRINGPI_H 384 pthread_mutex_lock(&mutexes[LOCK_LCD]);
388 piLock(LOCK_LCD); 385 #ifdef HAVE_WIRINGPI_H
389 lcdClear(lcdHandle); 386 lcdClear(lcdHandle);
390 #endif 387 #endif
391 slcdClear(slcdHandle); 388 slcdClear(slcdHandle);
392 setBacklight(0); 389 setBacklight(0);
393 #ifdef HAVE_WIRINGPI_H 390 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
394 piUnlock(LOCK_LCD);
395 #endif
396 } 391 }
397 392
398 393
399 394
400 /* 395 /*
1012 units_list *unit; 1007 units_list *unit;
1013 prof_step *step; 1008 prof_step *step;
1014 int row, rc, run = 1, seconds = 0, minutes = 0, temp; 1009 int row, rc, run = 1, seconds = 0, minutes = 0, temp;
1015 int run_seconds, run_minutes, run_hours, tot_minutes, key; 1010 int run_seconds, run_minutes, run_hours, tot_minutes, key;
1016 struct tm *tm; 1011 struct tm *tm;
1017 #ifndef HAVE_WIRINGPI_H
1018 long t = 0; 1012 long t = 0;
1019 #endif
1020 int current_step, valid_step, time_until_now, previous_fridge_mode; 1013 int current_step, valid_step, time_until_now, previous_fridge_mode;
1021 float previous_target_lo, previous_target_hi; 1014 float previous_target_lo, previous_target_hi;
1022 float LCDair, LCDbeer, LCDspL, LCDspH; 1015 float LCDair, LCDbeer, LCDspL, LCDspH;
1023 unsigned char LCDstatC, LCDstatH; 1016 unsigned char LCDstatC, LCDstatH;
1024 int LCDunit; 1017 int LCDunit;
1034 if ((rc = devices_detect())) { 1027 if ((rc = devices_detect())) {
1035 syslog(LOG_NOTICE, "Detected %d new devices", rc); 1028 syslog(LOG_NOTICE, "Detected %d new devices", rc);
1036 wrconfig(); 1029 wrconfig();
1037 } 1030 }
1038 1031
1039 #ifdef HAVE_WIRINGPI_H
1040 rc = piThreadCreate(my_devices_loop);
1041 #else
1042 rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); 1032 rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t );
1043 #endif
1044 if (rc) { 1033 if (rc) {
1045 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); 1034 fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc);
1046 syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); 1035 syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc);
1047 #ifndef HAVE_WIRINGPI_H
1048 } else { 1036 } else {
1049 t++; 1037 t++;
1050 #endif 1038 }
1051 } 1039
1052
1053 #ifdef HAVE_WIRINGPI_H
1054 rc = piThreadCreate(my_server_loop);
1055 #else
1056 rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); 1040 rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t );
1057 #endif
1058 if (rc) { 1041 if (rc) {
1059 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); 1042 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
1060 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); 1043 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
1061 #ifndef HAVE_WIRINGPI_H
1062 } else { 1044 } else {
1063 t++; 1045 t++;
1064 #endif 1046 }
1065 } 1047
1066
1067 #ifdef HAVE_WIRINGPI_H
1068 rc = piThreadCreate(my_panel_loop);
1069 #else
1070 rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t ); 1048 rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t );
1071 #endif
1072 if (rc) { 1049 if (rc) {
1073 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); 1050 fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc);
1074 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); 1051 syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc);
1075 #ifndef HAVE_WIRINGPI_H
1076 } else { 1052 } else {
1077 t++; 1053 t++;
1078 #endif
1079 } 1054 }
1080 1055
1081 #ifdef USE_SIMULATOR 1056 #ifdef USE_SIMULATOR
1082 #ifdef HAVE_WIRINGPI_H
1083 rc = piThreadCreate(my_simulator_loop);
1084 #else
1085 rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); 1057 rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t );
1086 #endif
1087 if (rc) { 1058 if (rc) {
1088 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); 1059 fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc);
1089 syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); 1060 syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc);
1090 #ifndef HAVE_WIRINGPI_H
1091 } else { 1061 } else {
1092 t++; 1062 t++;
1093 #endif
1094 } 1063 }
1095 #endif 1064 #endif
1096 1065
1097 1066
1098 /* 1067 /*
1130 free(unit->event_msg); 1099 free(unit->event_msg);
1131 unit->event_msg = NULL; 1100 unit->event_msg = NULL;
1132 } 1101 }
1133 } 1102 }
1134 1103
1135 #ifdef HAVE_WIRINGPI_H 1104 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1136 piLock(LOCK_LCD);
1137 #endif
1138 lcd_buf_write(1, (char *)" ThermFerm "); 1105 lcd_buf_write(1, (char *)" ThermFerm ");
1139 lcd_buf_write(2, (char *)" Version %s ", VERSION); 1106 lcd_buf_write(2, (char *)" Version %s ", VERSION);
1140 #ifdef HAVE_WIRINGPI_H 1107 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1141 piUnlock(LOCK_LCD);
1142 #endif
1143
1144 1108
1145 do { 1109 do {
1146 if (my_shutdown) 1110 if (my_shutdown)
1147 run = 0; 1111 run = 0;
1148 1112
1158 if (! run_pause) 1122 if (! run_pause)
1159 break; 1123 break;
1160 } 1124 }
1161 syslog(LOG_NOTICE, "run_pause: leaving hold state"); 1125 syslog(LOG_NOTICE, "run_pause: leaving hold state");
1162 run_hold = FALSE; 1126 run_hold = FALSE;
1163 #ifdef HAVE_WIRINGPI_H
1164 /* 1127 /*
1165 * In case the LCD buffers were cleared, setup the first page. 1128 * In case the LCD buffers were cleared, setup the first page.
1166 */ 1129 */
1167 piLock(LOCK_LCD); 1130 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1168 #endif
1169 lcd_buf_write(1, (char *)" ThermFerm "); 1131 lcd_buf_write(1, (char *)" ThermFerm ");
1170 lcd_buf_write(2, (char *)" Version %s ", VERSION); 1132 lcd_buf_write(2, (char *)" Version %s ", VERSION);
1171 #ifdef HAVE_WIRINGPI_H 1133 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1172 piUnlock(LOCK_LCD);
1173 #endif
1174 } 1134 }
1175 1135
1176 now = time(NULL); 1136 now = time(NULL);
1177 if (now != last) { 1137 if (now != last) {
1178 /* 1138 /*
1181 last = now; 1141 last = now;
1182 seconds++; 1142 seconds++;
1183 1143
1184 row = 3; 1144 row = 3;
1185 tm = localtime(&now); 1145 tm = localtime(&now);
1186 #ifdef HAVE_WIRINGPI_H 1146 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1187 piLock(LOCK_LCD);
1188 #endif
1189 lcd_buf_write(row++, " %02d-%02d-%04d ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900); 1147 lcd_buf_write(row++, " %02d-%02d-%04d ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
1190 lcd_buf_write(row++, " %02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec); 1148 lcd_buf_write(row++, " %02d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
1191 #ifdef HAVE_WIRINGPI_H 1149 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1192 piUnlock(LOCK_LCD); 1150
1193 #endif 1151 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1194
1195 #ifdef HAVE_WIRINGPI_H
1196 piLock(LOCK_LCD);
1197 #endif
1198 lcd_buf_write(row, "Room temp N/A "); 1152 lcd_buf_write(row, "Room temp N/A ");
1199 #ifdef HAVE_WIRINGPI_H 1153 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1200 piUnlock(LOCK_LCD);
1201 #endif
1202 int updateHT = 0; 1154 int updateHT = 0;
1203 if (Config.temp_address) { 1155 if (Config.temp_address) {
1204 rc = device_in(Config.temp_address, &temp); 1156 rc = device_in(Config.temp_address, &temp);
1205 if (rc == DEVPRESENT_YES) { 1157 if (rc == DEVPRESENT_YES) {
1206 if (Config.temp_value != temp) 1158 if (Config.temp_value != temp)
1207 updateHT = 1; 1159 updateHT = 1;
1208 Config.temp_value = temp; 1160 Config.temp_value = temp;
1209 Config.temp_state = 0; 1161 Config.temp_state = 0;
1210 #ifdef HAVE_WIRINGPI_H 1162 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1211 piLock(LOCK_LCD);
1212 #endif
1213 lcd_buf_write(row, "Room temp %.1f%c ", Config.temp_value / 1000.0, 0x01); 1163 lcd_buf_write(row, "Room temp %.1f%c ", Config.temp_value / 1000.0, 0x01);
1214 #ifdef HAVE_WIRINGPI_H 1164 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1215 piUnlock(LOCK_LCD);
1216 #endif
1217 } else if (rc == DEVPRESENT_ERROR) { 1165 } else if (rc == DEVPRESENT_ERROR) {
1218 Config.temp_state = 1; 1166 Config.temp_state = 1;
1219 } else { 1167 } else {
1220 Config.temp_state = 2; 1168 Config.temp_state = 2;
1221 } 1169 }
1222 usleep(10000); 1170 usleep(10000);
1223 } 1171 }
1224 row++; 1172 row++;
1225 1173
1226 #ifdef HAVE_WIRINGPI_H 1174 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1227 piLock(LOCK_LCD);
1228 #endif
1229 lcd_buf_write(row, " Humidity N/A "); 1175 lcd_buf_write(row, " Humidity N/A ");
1230 #ifdef HAVE_WIRINGPI_H 1176 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1231 piUnlock(LOCK_LCD);
1232 #endif
1233 1177
1234 if (Config.hum_address) { 1178 if (Config.hum_address) {
1235 rc = device_in(Config.hum_address, &temp); 1179 rc = device_in(Config.hum_address, &temp);
1236 if (rc == DEVPRESENT_YES) { 1180 if (rc == DEVPRESENT_YES) {
1237 if (Config.hum_value != temp) 1181 if (Config.hum_value != temp)
1238 updateHT = 1; 1182 updateHT = 1;
1239 Config.hum_value = temp; 1183 Config.hum_value = temp;
1240 Config.hum_state = 0; 1184 Config.hum_state = 0;
1241 #ifdef HAVE_WIRINGPI_H 1185 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1242 piLock(LOCK_LCD);
1243 #endif
1244 lcd_buf_write(row, " Humidity %.1f%% ", Config.hum_value / 1000.0); 1186 lcd_buf_write(row, " Humidity %.1f%% ", Config.hum_value / 1000.0);
1245 #ifdef HAVE_WIRINGPI_H 1187 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1246 piUnlock(LOCK_LCD);
1247 #endif
1248 } else if (rc == DEVPRESENT_ERROR) { 1188 } else if (rc == DEVPRESENT_ERROR) {
1249 Config.hum_state = 1; 1189 Config.hum_state = 1;
1250 } else { 1190 } else {
1251 Config.hum_state = 2; 1191 Config.hum_state = 2;
1252 } 1192 }
1841 // unit->mqtt_flag |= MQTT_FLAG_DATA; 1781 // unit->mqtt_flag |= MQTT_FLAG_DATA;
1842 // } 1782 // }
1843 if (seconds == 60) { 1783 if (seconds == 60) {
1844 unit->mqtt_flag |= MQTT_FLAG_DATA; 1784 unit->mqtt_flag |= MQTT_FLAG_DATA;
1845 } 1785 }
1846 #ifdef HAVE_WIRINGPI_H 1786 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1847 piLock(LOCK_LCD);
1848 #endif
1849 /* 1787 /*
1850 * Write 4 rows to the LCD to display the unit state 1788 * Write 4 rows to the LCD to display the unit state
1851 */ 1789 */
1852 lcd_buf_write(row++, "Unit %d: %s ", LCDunit, UNITMODE[unit->mode]); 1790 lcd_buf_write(row++, "Unit %d: %s ", LCDunit, UNITMODE[unit->mode]);
1853 lcd_buf_write(row++, "%s ", unit->product_name); 1791 lcd_buf_write(row++, "%s ", unit->product_name);
1854 lcd_buf_write(row++, "%c%5.1f\2 A%6.2f\1 ", LCDstatC, LCDspH, LCDair); 1792 lcd_buf_write(row++, "%c%5.1f\2 A%6.2f\1 ", LCDstatC, LCDspH, LCDair);
1855 lcd_buf_write(row++, "%c%5.1f\2 B%6.2f\1 ", LCDstatH, LCDspL, LCDbeer); 1793 lcd_buf_write(row++, "%c%5.1f\2 B%6.2f\1 ", LCDstatH, LCDspL, LCDbeer);
1856 #ifdef HAVE_WIRINGPI_H 1794 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1857 piUnlock(LOCK_LCD);
1858 #endif
1859 1795
1860 /* 1796 /*
1861 * Publish MQTT messages set in flag 1797 * Publish MQTT messages set in flag
1862 */ 1798 */
1863 if (unit->mqtt_flag) { 1799 if (unit->mqtt_flag) {
1881 syslog(LOG_NOTICE, "Unit `%s' Alarm %d => %d", unit->alias, unit->alarm_last, unit->alarm_flag); 1817 syslog(LOG_NOTICE, "Unit `%s' Alarm %d => %d", unit->alias, unit->alarm_last, unit->alarm_flag);
1882 unit->alarm_last = unit->alarm_flag; 1818 unit->alarm_last = unit->alarm_flag;
1883 } 1819 }
1884 } /* for units */ 1820 } /* for units */
1885 1821
1886 #ifdef HAVE_WIRINGPI_H 1822 pthread_mutex_lock(&mutexes[LOCK_MENU]);
1887 piLock(LOCK_MENU);
1888 #endif
1889 if (setupmenu == MENU_NONE) { 1823 if (setupmenu == MENU_NONE) {
1890 #ifdef HAVE_WIRINGPI_H 1824 pthread_mutex_lock(&mutexes[LOCK_LCD]);
1891 piLock(LOCK_LCD);
1892 #endif
1893 lcd_buf_show(); 1825 lcd_buf_show();
1894 #ifdef HAVE_WIRINGPI_H 1826 pthread_mutex_unlock(&mutexes[LOCK_LCD]);
1895 piUnlock(LOCK_LCD);
1896 #endif
1897 } 1827 }
1898 #ifdef HAVE_WIRINGPI_H 1828 pthread_mutex_unlock(&mutexes[LOCK_MENU]);
1899 piUnlock(LOCK_MENU);
1900 #endif
1901 1829
1902 if (seconds == 60) { 1830 if (seconds == 60) {
1903 seconds = 0; 1831 seconds = 0;
1904 1832
1905 /* 1833 /*

mercurial