thermferm/thermferm.c

changeset 418
0bfe08c7ba6e
parent 406
44566f986f76
child 420
644a6106d712
--- a/thermferm/thermferm.c	Sun Nov 08 21:24:52 2015 +0100
+++ b/thermferm/thermferm.c	Sun Nov 08 22:13:02 2015 +0100
@@ -42,13 +42,11 @@
 
 extern int		debug;
 extern sys_config	Config;
-#ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
 int			setupmenu = MENU_NONE;
 units_list		*current_unit = NULL;		/* In panel editor this points to the current unit. */
 profiles_list		*current_profile = NULL;
 float			temp_temp = 20.0;
-#endif
 
 #ifndef HAVE_WIRINGPI_H
 pthread_t		threads[5];
@@ -60,9 +58,9 @@
 int  server(void);
 void help(void);
 void die(int);
+void stopLCD(void);
 #ifdef HAVE_WIRINGPI_H
 void sendRCswitch(char *, int);
-void stopLCD(void);
 void stopRCswitch(void);
 #endif
 
@@ -99,14 +97,15 @@
 
 
 
-#ifdef HAVE_WIRINGPI_H
 void show_mode(void)
 {
     char	buf[21];
 
     snprintf(buf, 20, "Old mode %s", UNITMODE[current_unit->mode]);
+#ifdef HAVE_WIRINGPI_H
     lcdPuts(lcdHandle, buf);
     lcdPosition(lcdHandle, 0, 1);
+#endif
 }
 
 
@@ -115,132 +114,197 @@
 {
     char		buf[21];
 
+#ifdef HAVE_WIRINGPI_H
     piLock(LOCK_LCD);
     piLock(LOCK_MENU);
     lcdClear(lcdHandle);
     lcdPosition(lcdHandle, 0, 0);
+#endif
     syslog(LOG_NOTICE, "from menu %d to menu %d", setupmenu, menu);
     setupmenu = menu;
 
     switch (menu) {
-	case MENU_NONE:		lcd_buf_show();
+	case MENU_NONE:		
+#ifdef HAVE_WIRINGPI_H
+				lcd_buf_show();
+#endif
 				break;
 
-	case MENU_TOP_DEFAULT:	lcdPuts(lcdHandle, "Setup mode.");
+	case MENU_TOP_DEFAULT:	
+#ifdef HAVE_WIRINGPI_H
+				lcdPuts(lcdHandle, "Setup mode.");
 				lcdPosition(lcdHandle, 0, 1);
 				lcdPuts(lcdHandle, "Up&Down = Escape");
+#endif
 				break;
 
-	case MENU_TOP_UNITS:	lcdPuts(lcdHandle, "Select units");
+	case MENU_TOP_UNITS:	
+#ifdef HAVE_WIRINGPI_H
+				lcdPuts(lcdHandle, "Select units");
+#endif
 				break;
 
-	case MENU_UNITS:	lcdPuts(lcdHandle, "Choose unit:");
+	case MENU_UNITS:	
+#ifdef HAVE_WIRINGPI_H
+				lcdPuts(lcdHandle, "Choose unit:");
 				lcdPosition(lcdHandle, 0, 1);
 				lcdPuts(lcdHandle, current_unit->name);
+#endif
 				break;
 
 	case MENU_MODE_OFF:	show_mode();
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, "New mode OFF");
+#endif
 				break;
 
 	case MENU_MODE_NONE:	show_mode();
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, "New mode NONE");
+#endif
 				break;
 
 	case MENU_NONE_HEAT:	snprintf(buf, Config.lcd_cols, "Set heater %s", current_unit->heater_state ? "OFF":"ON");
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, buf);
+#endif
 				break;
 
 	case MENU_NONE_COOL:	snprintf(buf, Config.lcd_cols, "Set cooler %s", current_unit->cooler_state ? "OFF":"ON");
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, buf);
+#endif
 				break;
 
 	case MENU_NONE_FAN:	snprintf(buf, Config.lcd_cols, "Set fan %s", current_unit->fan_state ? "OFF":"ON");
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, buf);
+#endif
 				break;
 
 	case MENU_MODE_BEER:	show_mode();
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, "New mode BEER");
+#endif
 				break;
 
-	case MENU_BEER_TEMP:	lcdPuts(lcdHandle, "Set beer temp");
+	case MENU_BEER_TEMP:	
+#ifdef HAVE_WIRINGPI_H				
+				lcdPuts(lcdHandle, "Set beer temp");
 				lcdPosition(lcdHandle, 0, 1);
+#endif
 				snprintf(buf, Config.lcd_cols, "Set %.1f", temp_temp);
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, buf);
+#endif
 				break;
 
 	case MENU_MODE_FRIDGE:	show_mode();
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, "New mode FRIDGE");
+#endif
 				break;
 
-	case MENU_FRIDGE_TEMP:	lcdPuts(lcdHandle, "Set fridge temp");
+	case MENU_FRIDGE_TEMP:	
+#ifdef HAVE_WIRINGPI_H				
+				lcdPuts(lcdHandle, "Set fridge temp");
 				lcdPosition(lcdHandle, 0, 1);
+#endif
 				snprintf(buf, Config.lcd_cols, "Set %.1f", temp_temp);
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, buf);
+#endif
 				break;
 
 	case MENU_MODE_PROFILE:	show_mode();
+#ifdef HAVE_WIRINGPI_H
 				lcdPuts(lcdHandle, "New mode PROFILE");
+#endif
 				break;
 
 	case MENU_PROFILE_SELECT:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Select profile");
+#endif
 					break;
 	
 	case MENU_PROFILE_START:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Start profile");
+#endif
 					break;
 	
 	case MENU_PROFILE_PAUSE:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Pause profile");
+#endif
 					break;
 
 	case MENU_PROFILE_ABORT:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Abort profile");
+#endif
 					break;
 
 	case MENU_PROFILE_RESUME:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Resume profile");
+#endif
 					break;
 
 	case MENU_PROFILE_GOOFF:	snprintf(buf, Config.lcd_cols, "%s", current_profile->name);
+#ifdef HAVE_WIRINGPI_H
 					lcdPuts(lcdHandle, buf);
 					lcdPosition(lcdHandle, 0, 1);
 					lcdPuts(lcdHandle, "Set profile OFF");
+#endif
 					break;
 
-	case MENU_TOP_SYS:	lcdPuts(lcdHandle, "System menu");
+	case MENU_TOP_SYS:	
+#ifdef HAVE_WIRINGPI_H
+				lcdPuts(lcdHandle, "System menu");
+#endif
 				break;
 
-	case MENU_SYS_HALT:	lcdPuts(lcdHandle, "Halt system");
+	case MENU_SYS_HALT:	
+#ifdef HAVE_WIRINGPI_H				
+				lcdPuts(lcdHandle, "Halt system");
+#endif
 				break;
 
-	case MENU_SYS_REBOOT:	lcdPuts(lcdHandle, "Reboot system");
+	case MENU_SYS_REBOOT:	
+#ifdef HAVE_WIRINGPI_H				
+				lcdPuts(lcdHandle, "Reboot system");
+#endif
 				break;
     }
 
+#ifdef HAVE_WIRINGPI_H
     piUnlock(LOCK_MENU);
     piUnlock(LOCK_LCD);
+#endif
 }
 
 
 
 void stopLCD(void)
 {
+#ifdef HAVE_WIRINGPI_H
     piLock(LOCK_LCD);
     lcdClear(lcdHandle);
     setBacklight(0);
     piUnlock(LOCK_LCD);
+#endif
 }
 
 
@@ -275,9 +339,6 @@
 }
 
 
-#endif
-
-
 
 /*
  * Handle panel key events
@@ -769,9 +830,7 @@
 	switch (frk) {
 	    case -1:	
 		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
-#ifdef HAVE_WIRINGPI_H
 			stopLCD();
-#endif
 			exit(1);
 	    case 0:	/*
 			 * Run the daemon
@@ -819,11 +878,11 @@
     units_list		*unit;
     profiles_list	*profile;
     prof_step		*step;
-    int			rc, run = 1, seconds = 0, minutes = 0, temp, deviation;
+    int			row, rc, run = 1, seconds = 0, minutes = 0, temp, deviation;
     int			run_seconds, run_minutes, run_hours, tot_minutes;
+    struct tm		*tm;
 #ifdef HAVE_WIRINGPI_H
-    struct tm		*tm;
-    int			row, key;
+    int			key;
 #else
     long		t = 0;
 #endif
@@ -920,8 +979,10 @@
 
 #ifdef HAVE_WIRINGPI_H
     piLock(LOCK_LCD);
+#endif
     lcd_buf_write(1, (char *)"   ThermFerm    ");
     lcd_buf_write(2, (char *)" Version %s     ", VERSION);
+#ifdef HAVE_WIRINGPI_H
     piUnlock(LOCK_LCD);
 #endif
 
@@ -957,8 +1018,10 @@
 	     * In case the LCD buffers were cleared, setup the first page.
 	     */
 	    piLock(LOCK_LCD);
+#endif
 	    lcd_buf_write(1, (char *)"   ThermFerm    ");
 	    lcd_buf_write(2, (char *)" Version %s     ", VERSION);
+#ifdef HAVE_WIRINGPI_H
 	    piUnlock(LOCK_LCD);
 #endif
 	}
@@ -971,12 +1034,14 @@
 	    last = now;
 	    seconds++;
 
-#ifdef HAVE_WIRINGPI_H
 	    row = 3;
 	    tm = localtime(&now);
+#ifdef HAVE_WIRINGPI_H
 	    piLock(LOCK_LCD);
+#endif
 	    lcd_buf_write(row++, "   %02d-%02d-%04d   ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
 	    lcd_buf_write(row++, "    %02d:%02d:%02d    ", tm->tm_hour, tm->tm_min, tm->tm_sec);
+#ifdef HAVE_WIRINGPI_H
 	    piUnlock(LOCK_LCD);
 #endif
 
@@ -987,7 +1052,9 @@
 		    Config.temp_state = 0;
 #ifdef HAVE_WIRINGPI_H
 		    piLock(LOCK_LCD);
+#endif
 		    lcd_buf_write(row++, "Room temp %.1f %cC    ", Config.temp_value / 1000.0, 0xdf);
+#ifdef HAVE_WIRINGPI_H
 		    piUnlock(LOCK_LCD);
 #endif
 		} else if (rc == DEVPRESENT_ERROR) {
@@ -1004,7 +1071,9 @@
 		    Config.hum_state = 0;
 #ifdef HAVE_WIRINGPI_H
 		    piLock(LOCK_LCD);
+#endif
 		    lcd_buf_write(row++, " Humidity %.1f %%    ", Config.hum_value / 1000.0, 0xdf);
+#ifdef HAVE_WIRINGPI_H
 		    piUnlock(LOCK_LCD);
 #endif
 		} else if (rc == DEVPRESENT_ERROR) {
@@ -1017,8 +1086,10 @@
 	    for (unit = Config.units; unit; unit = unit->next) {
 #ifdef HAVE_WIRINGPI_H
 		piLock(LOCK_LCD);
+#endif
 		lcd_buf_write(row++, "Unit %s              ", unit->name);
 		lcd_buf_write(row++, "Mode %s              ", UNITMODE[unit->mode]);
+#ifdef HAVE_WIRINGPI_H
 		piUnlock(LOCK_LCD);
 #endif
 
@@ -1040,7 +1111,9 @@
 			    unit->air_state = 0;
 #ifdef HAVE_WIRINGPI_H
 			    piLock(LOCK_LCD);
+#endif
 			    lcd_buf_write(row++, " Air %.3f %cC         ", unit->air_temperature / 1000.0, 0xdf);
+#ifdef HAVE_WIRINGPI_H
 			    piUnlock(LOCK_LCD);
 #endif
 			} else {
@@ -1066,7 +1139,9 @@
 			    unit->beer_state = 0;
 #ifdef HAVE_WIRINGPI_H
 			    piLock(LOCK_LCD);
+#endif
 			    lcd_buf_write(row++, "Beer %.3f %cC         ", unit->beer_temperature / 1000.0, 0xdf);
+#ifdef HAVE_WIRINGPI_H
 			    piUnlock(LOCK_LCD);
 #endif
 			} else {
@@ -1335,18 +1410,21 @@
 		if (unit->light_address && unit->light_state)
 		    unit->light_usage++;
 
+		if (unit->heater_address && unit->cooler_address) {
 #ifdef HAVE_WIRINGPI_H
-		if (unit->heater_address && unit->cooler_address) {
-		    piLock(LOCK_LCD);
-		    if (unit->heater_state) {
+    		    piLock(LOCK_LCD);
+#endif
+    		    if (unit->heater_state) {
 			lcd_buf_write(row++, "Heater On          ");
 		    } else if (unit->cooler_state) {
 			lcd_buf_write(row++, "Cooler On          ");
 		    } else {
 			lcd_buf_write(row++, "Standby            ");
 		    }
+#ifdef HAVE_WIRINGPI_H
 		    piUnlock(LOCK_LCD);
 		    piLock(LOCK_LCD);
+#endif
 		    switch (unit->mode) {
 			case UNITMODE_BEER:	lcd_buf_write(row++, "Target %.1f %cC     ", unit->beer_set, 0xdf);
 						break;
@@ -1359,18 +1437,23 @@
 						break;
 			default:		lcd_buf_write(row++, "Target not set     ");
 		    }
+#ifdef HAVE_WIRINGPI_H
 		    piUnlock(LOCK_LCD);
+#endif
 		} else {
-		    piLock(LOCK_LCD);
+#ifdef HAVE_WIRINGPI_H
+    		    piLock(LOCK_LCD);
+#endif
 		    if (unit->heater_address) {
 		    	lcd_buf_write(row++, "Heat %s            ", unit->heater_state ? "On ":"Off");
 		    }
 		    if (unit->cooler_address) {
 		    	lcd_buf_write(row++, "Cool %s            ", unit->cooler_state ? "On ":"Off");
 		    }
+#ifdef HAVE_WIRINGPI_H
 		    piUnlock(LOCK_LCD);
+#endif
 		}
-#endif
 
 		/*
 		 * Interior lights
@@ -1553,11 +1636,17 @@
 
 #ifdef HAVE_WIRINGPI_H
 	    piLock(LOCK_MENU);
+#endif
 	    if (setupmenu == MENU_NONE) {
-		piLock(LOCK_LCD);
+#ifdef HAVE_WIRINGPI_H
+    		piLock(LOCK_LCD);
+#endif
 	    	lcd_buf_show();
+#ifdef HAVE_WIRINGPI_H
 		piUnlock(LOCK_LCD);
+#endif
 	    }
+#ifdef HAVE_WIRINGPI_H
 	    piUnlock(LOCK_MENU);
 #endif
 
@@ -1676,10 +1765,7 @@
      */
     usleep(1500000);
 
-#ifdef HAVE_WIRINGPI_H
     stopLCD();
-#endif
-
     wrconfig();
     ulockprog((char *)"thermferm");
     return 0;

mercurial