diff -r ab75ff8e26e1 -r 2f868eaefec2 thermferm/thermferm.c --- a/thermferm/thermferm.c Sat Aug 16 15:38:45 2014 +0200 +++ b/thermferm/thermferm.c Sat Aug 16 17:11:09 2014 +0200 @@ -96,10 +96,8 @@ char buf[21]; snprintf(buf, 20, "Old mode %s", UNITMODE[current_unit->mode]); - piLock(LOCK_LCD); lcdPuts(lcdHandle, buf); lcdPosition(lcdHandle, 0, 1); - piUnlock(LOCK_LCD); } @@ -109,9 +107,10 @@ char buf[21]; piLock(LOCK_LCD); + piLock(LOCK_MENU); lcdClear(lcdHandle); lcdPosition(lcdHandle, 0, 0); - piUnlock(LOCK_LCD); + syslog(LOG_NOTICE, "from menu %d to menu %d", setupmenu, menu); setupmenu = menu; switch (menu) { @@ -127,14 +126,8 @@ 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); - } + lcdPosition(lcdHandle, 0, 1); + lcdPuts(lcdHandle, current_unit->name); break; case MENU_MODE_OFF: show_mode(); @@ -208,6 +201,9 @@ case MENU_SYS_THERMS: lcdPuts(lcdHandle, "Start Thermometers"); break; } + + piUnlock(LOCK_MENU); + piUnlock(LOCK_LCD); } @@ -865,11 +861,13 @@ } #ifdef HAVE_WIRINGPI_H + piLock(LOCK_MENU); if (setupmenu == MENU_NONE) { piLock(LOCK_LCD); lcd_buf_show(); piUnlock(LOCK_LCD); } + piUnlock(LOCK_MENU); #endif if (seconds == 60) { @@ -960,34 +958,48 @@ go_menu(MENU_TOP_SYS); if (key == KEY_UP) go_menu(MENU_TOP_DEFAULT); - if (key == KEY_ENTER) + if ((key == KEY_ENTER) && Config.units) { + /* + * Start with the first unit + */ + current_unit = Config.units; go_menu(MENU_UNITS); + } break; case MENU_UNITS: if (key == KEY_ESCAPE) go_menu(MENU_TOP_UNITS); if (key == KEY_DOWN) { + if (current_unit->next) { + current_unit = current_unit->next; + go_menu(MENU_UNITS); + } + } + if (key == KEY_UP) { 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 */ - piLock(LOCK_LCD); - lcdPosition(lcdHandle, 0, 1); - lcdPuts(lcdHandle, Config.units->name); - piUnlock(LOCK_LCD); - } + if (unit->next && (unit->next == current_unit)) { + current_unit = unit; + go_menu(MENU_UNITS); + break; } } } 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); - } + /* + * Drop into the current mode + */ + switch (current_unit->mode) { + case UNITMODE_OFF: go_menu(MENU_MODE_OFF); + break; + case UNITMODE_NONE: go_menu(MENU_MODE_NONE); + break; + case UNITMODE_FRIDGE: go_menu(MENU_MODE_FRIDGE); + break; + case UNITMODE_BEER: go_menu(MENU_MODE_BEER); + break; + case UNITMODE_PROFILE: go_menu(MENU_MODE_PROFILE); + break; } } break;