thermferm/thermferm.c

changeset 238
a0f1deb65889
parent 235
885ad8d52126
child 240
6bdda35b4a13
--- a/thermferm/thermferm.c	Wed Aug 13 22:02:14 2014 +0200
+++ b/thermferm/thermferm.c	Thu Aug 14 21:50:38 2014 +0200
@@ -41,6 +41,7 @@
 #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. */
 
 #endif
 int			lcdupdate;
@@ -88,9 +89,23 @@
 }
 
 
+
+void show_mode(void)
+{
+    char	buf[21];
+
+    snprintf(buf, 20, "Old mode %s", UNITMODE[current_unit->mode]);
+    lcdPuts(lcdHandle, buf);
+    lcdPosition(lcdHandle, 0, 1);
+}
+
+
+
 #ifdef HAVE_WIRINGPI_H
 void go_menu(int menu)
 {
+    char	buf[21];
+
     lcdClear(lcdHandle);
     lcdPosition(lcdHandle, 0, 0);
     setupmenu = menu;
@@ -108,36 +123,52 @@
 				break;
 
 	case MENU_UNITS:	lcdPuts(lcdHandle, "Choose unit:");
+				if ((current_unit == NULL) && Config.units) {
+				    /*
+				     * First time in this menu, select the first unit.
+				     */
+				    current_unit = Config.units;
+				    lcdPosition(lcdHandle, 0, 1);
+				    lcdPuts(lcdHandle, Config.units->name);
+				}
 				break;
 
-	case MENU_MODE_OFF:	lcdPuts(lcdHandle, "Set unit OFF");
-				break;
-
-	case MENU_MODE_NONE:	lcdPuts(lcdHandle, "Set unit NONE");
+	case MENU_MODE_OFF:	show_mode();
+				lcdPuts(lcdHandle, "New mode OFF");
 				break;
 
-	case MENU_NONE_HEAT:	lcdPuts(lcdHandle, "Switch heater");
+	case MENU_MODE_NONE:	show_mode();
+				lcdPuts(lcdHandle, "New mode NONE");
+				break;
+
+	case MENU_NONE_HEAT:	snprintf(buf, 20, "Set heater %s", current_unit->heater_state ? "OFF":"ON");
+				lcdPuts(lcdHandle, buf);
 				break;
 
-	case MENU_NONE_COOL:	lcdPuts(lcdHandle, "Switch cooler");
+	case MENU_NONE_COOL:	snprintf(buf, 20, "Set cooler %s", current_unit->cooler_state ? "OFF":"ON");
+				lcdPuts(lcdHandle, buf);
 				break;
 
-	case MENU_NONE_FAN:	lcdPuts(lcdHandle, "Switch Fan");
+	case MENU_NONE_FAN:	snprintf(buf, 20, "Set fan %s", current_unit->fan_state ? "OFF":"ON");
+				lcdPuts(lcdHandle, buf);
 				break;
 
-	case MENU_MODE_BEER:	lcdPuts(lcdHandle, "Set unit BEER");
+	case MENU_MODE_BEER:	show_mode();
+				lcdPuts(lcdHandle, "New mode BEER");
 				break;
 
 	case MENU_BEER_TEMP:	lcdPuts(lcdHandle, "Set beer temp");
 				break;
 
-	case MENU_MODE_FRIDGE:	lcdPuts(lcdHandle, "Set unit FRIDGE");
+	case MENU_MODE_FRIDGE:	show_mode();
+				lcdPuts(lcdHandle, "New mode FRIDGE");
 				break;
 
 	case MENU_FRIDGE_TEMP:	lcdPuts(lcdHandle, "Set fridge temp");
 				break;
 
-	case MENU_MODE_PROFILE:	lcdPuts(lcdHandle, "Set unit PROFILE");
+	case MENU_MODE_PROFILE:	show_mode();
+				lcdPuts(lcdHandle, "New mode PROFILE");
 				break;
 
 	case MENU_PROFILE_SELECT:	lcdPuts(lcdHandle, "Select profile");
@@ -182,6 +213,34 @@
     lcdClear(lcdHandle);
     setBacklight(0);
 }
+
+
+
+/*
+ * Change mode of current_unit
+ */
+void change_mode(int mode)
+{
+    if ((current_unit->mode == UNITMODE_OFF) && (mode != UNITMODE_OFF))
+	initlog(current_unit->name);
+    syslog(LOG_NOTICE, "Mode from %s to %s via panel interface", UNITMODE[current_unit->mode], UNITMODE[mode]);
+    current_unit->mode = mode;
+    /* Allways turn everything off after a mode change */
+    current_unit->PID_I_err = current_unit->PID_err_old = 0.0;
+    current_unit->heater_state = current_unit->cooler_state = current_unit->fan_state = 0;
+    device_out(current_unit->heater_address, current_unit->heater_state);
+    device_out(current_unit->cooler_address, current_unit->cooler_state);
+    device_out(current_unit->fan_address, current_unit->fan_state);
+    if (current_unit->mode == UNITMODE_PROFILE) {
+	/*
+	 * Set a sane default until it will be overruled by the
+	 * main processing loop.
+	 */
+	current_unit->prof_target = 20.0;
+    }
+}
+
+
 #endif
 
 
@@ -417,7 +476,7 @@
 
 
 #ifdef HAVE_WIRINGPI_H
-    lcd_buf_write(1, (char *)"    ThermFerm   ");
+    lcd_buf_write(1, (char *)"   ThermFerm    ");
     lcd_buf_write(2, (char *)" Version %s     ", VERSION);
 #endif
 
@@ -865,6 +924,175 @@
 			    go_menu(MENU_TOP_SYS);
 			if (key == KEY_UP)
 			    go_menu(MENU_TOP_DEFAULT);
+			if (key == KEY_ENTER)
+			    go_menu(MENU_UNITS);
+			break;
+
+	    case MENU_UNITS:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_TOP_UNITS);
+			if (key == KEY_DOWN) {
+			    for (unit = Config.units; unit; unit = unit->next) {
+				if (strcmp(unit->uuid, current_unit->uuid) == 0) {	/* Current unit		*/
+				    if (unit->next) {				/* Is there a next	*/
+					unit = unit->next;			/* Select that		*/
+					lcdPosition(lcdHandle, 0, 1);
+					lcdPuts(lcdHandle, Config.units->name);
+				    }
+				}
+			    }
+			}
+			if (key == KEY_ENTER) {
+			    for (unit = Config.units; unit; unit = unit->next) {
+				if (strcmp(unit->uuid, current_unit->uuid) == 0) {
+				    if (unit->mode == UNITMODE_OFF)
+					go_menu(MENU_MODE_NONE);
+				    else
+			    		go_menu(MENU_MODE_OFF);
+				}
+			    }
+			}
+			break;
+
+	    case MENU_MODE_OFF:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_UNITS);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_MODE_NONE);
+			if (key == KEY_UP)
+			    go_menu(MENU_MODE_PROFILE);
+			if (key == KEY_ENTER) {
+			    change_mode(UNITMODE_OFF);
+			    go_menu(MENU_MODE_OFF);
+			}
+			break;
+
+	    case MENU_MODE_NONE:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_UNITS);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_MODE_FRIDGE);
+			if (key == KEY_UP)
+			    go_menu(MENU_MODE_OFF);
+			if (key == KEY_ENTER) {
+			    if (current_unit->mode == UNITMODE_NONE)
+				go_menu(MENU_NONE_HEAT);
+			    else {
+			    	change_mode(UNITMODE_NONE);
+				go_menu(MENU_MODE_NONE);
+			    }
+			}
+			break;
+
+	    case MENU_NONE_HEAT:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_MODE_NONE);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_NONE_COOL);
+			if (key == KEY_UP)
+			    go_menu(MENU_NONE_FAN);
+			if (key == KEY_ENTER) {
+			    if (current_unit->heater_state)
+				current_unit->heater_state = 0;
+			    else
+				current_unit->heater_state = 100;
+			    go_menu(MENU_NONE_HEAT);
+			}
+			break;
+
+	    case MENU_NONE_COOL:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_MODE_NONE);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_NONE_FAN);
+			if (key == KEY_UP)      
+			    go_menu(MENU_NONE_HEAT);
+			if (key == KEY_ENTER) {
+			    if (current_unit->cooler_state)
+				current_unit->cooler_state = 0;
+			    else
+				current_unit->cooler_state = 100;
+			    go_menu(MENU_NONE_COOL);
+			}
+			break;
+
+	    case MENU_NONE_FAN:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_MODE_NONE);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_NONE_HEAT);
+			if (key == KEY_UP)      
+			    go_menu(MENU_NONE_COOL);
+			if (key == KEY_ENTER) {
+			    if (current_unit->fan_state)
+				current_unit->fan_state = 0;
+			    else
+				current_unit->fan_state = 100;
+			    go_menu(MENU_NONE_FAN);
+			}
+			break;
+
+	    case MENU_MODE_FRIDGE:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_UNITS);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_MODE_BEER);
+			if (key == KEY_UP)
+			    go_menu(MENU_MODE_NONE);
+			if (key == KEY_ENTER) {
+			    change_mode(UNITMODE_FRIDGE);
+			    go_menu(MENU_MODE_FRIDGE);
+			}
+			break;
+
+	    case MENU_FRIDGE_TEMP:
+			break;
+
+	    case MENU_MODE_BEER:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_UNITS);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_MODE_PROFILE);
+			if (key == KEY_UP)
+			    go_menu(MENU_MODE_FRIDGE);
+			if (key == KEY_ENTER) {
+			    change_mode(UNITMODE_BEER);
+			    go_menu(MENU_MODE_BEER);
+			}
+			break;
+
+	    case MENU_BEER_TEMP:
+			break;
+
+	    case MENU_MODE_PROFILE:
+			if (key == KEY_ESCAPE)
+			    go_menu(MENU_UNITS);
+			if (key == KEY_DOWN)
+			    go_menu(MENU_MODE_OFF);     
+			if (key == KEY_UP)
+			    go_menu(MENU_MODE_BEER);                                
+			if (key == KEY_ENTER) {
+			    change_mode(UNITMODE_PROFILE);
+			    go_menu(MENU_MODE_PROFILE);
+			}
+			break;
+
+	    case MENU_PROFILE_SELECT:
+			break;
+
+	    case MENU_PROFILE_START:
+			break;
+
+	    case MENU_PROFILE_PAUSE:
+			break;
+
+	    case MENU_PROFILE_ABORT:
+			break;
+
+	    case MENU_PROFILE_RESUME:
+			break;
+
+	    case MENU_PROFILE_GOOFF:
 			break;
 
 	    case MENU_TOP_SYS:

mercurial