diff --git a/CMakeLists.txt b/CMakeLists.txt index 99d5c09..cd11b9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(zavlaha src/screens/screen_settings.c src/screens/screen_cyklus.c src/screens/screen_set_time.c + src/screens/screen_set_moisture_threshold.c src/app_io.c src/app_config.c) diff --git a/src/screens/app_gui.h b/src/screens/app_gui.h index 3eb8c0d..f7baf6d 100644 --- a/src/screens/app_gui.h +++ b/src/screens/app_gui.h @@ -49,8 +49,10 @@ void request_paint(); void screen_home(GuiEvent event); void screen_cyklus(GuiEvent event); +extern struct AppConfig settings_scratch; void screen_settings(GuiEvent event); void screen_set_time(GuiEvent event); +void screen_set_moisture_threshold(GuiEvent event); // XXX other prototypes struct State { diff --git a/src/screens/screen_set_moisture_threshold.c b/src/screens/screen_set_moisture_threshold.c new file mode 100644 index 0000000..1bad130 --- /dev/null +++ b/src/screens/screen_set_moisture_threshold.c @@ -0,0 +1,60 @@ +#include +#include "app_gui.h" +#include "gui_event.h" +#include "ds_rtc.h" +#include "app_io.h" +#include "lcd.h" +#include "app_config.h" + +static int threshold; +static int cursor; + +void screen_set_moisture_threshold(GuiEvent event) +{ + char buf[100]; + + switch (event) { + case GUI_EVENT_SCREEN_INIT: + threshold = 0; + cursor = 0; + break; + + case GUI_EVENT_PAINT: + LcdBuffer_Write(&lcd, 0, 0, "Blokuj zálivku pokud"); + LcdBuffer_Write(&lcd, 1, 0, "je vlhkost vyšší než"); + + LcdBuffer_SetCursor(&lcd, 2, cursor, (cursor < 3) ? CURSOR_BOTH : CURSOR_NONE); + + if (cursor > 0) { + snprintf(buf, 100, "%d %%", threshold); + LcdBuffer_Write(&lcd, 2, 0, buf); + } else { + LcdBuffer_Write(&lcd, 2, 0, " %"); + } + + LcdBuffer_Write(&lcd, 3, 0, cursor == 0 ? "🅳Zrušit" : "🅳Zrušit 🅰Uložit"); + break; + + case GUI_EVENT_KEY_A: // Confirm + if (cursor > 0) { + settings_scratch.moisture_threshold_percent = threshold; + switch_screen(screen_settings, false); + } + break; + + case GUI_EVENT_KEY_D: // CANCEL + switch_screen(screen_settings, false); + break; + + default: + if (event >= '0' && event <= '9') { + int digit = event - '0'; + int th2 = threshold * 10 + digit; + if (th2 <= 100) { + threshold = th2; + cursor++; + request_paint(); + } + } + } +} diff --git a/src/screens/screen_set_time.c b/src/screens/screen_set_time.c index 399bb5a..803802b 100644 --- a/src/screens/screen_set_time.c +++ b/src/screens/screen_set_time.c @@ -28,12 +28,14 @@ void screen_set_time(GuiEvent event) snprintf(buf, 100, "%02d:%02d", time.hour, time.minute); LcdBuffer_Write(&lcd, 1, 0, buf); - LcdBuffer_Write(&lcd, 3, 0, "🅰Uložit 🅳Zrušit"); + LcdBuffer_Write(&lcd, 3, 0, cursor < 4 ? "🅳Zrušit" : "🅳Zrušit 🅰Uložit"); break; case GUI_EVENT_KEY_A: // Confirm - rtc_set_time(&time); - switch_screen(screen_settings, false); + if (cursor == 4) { + rtc_set_time(&time); + switch_screen(screen_settings, false); + } break; case GUI_EVENT_KEY_D: // CANCEL diff --git a/src/screens/screen_settings.c b/src/screens/screen_settings.c index 5d0703f..16a15ff 100644 --- a/src/screens/screen_settings.c +++ b/src/screens/screen_settings.c @@ -17,7 +17,7 @@ static int page = 1; #define PAGECOUNT 3 -static struct AppConfig scratch; +struct AppConfig settings_scratch; /** Clear the top 3 lines in the LCD - this fixes some problems with utf8 allocations * (char still held by a line later overwritten) */ @@ -29,13 +29,13 @@ void screen_settings(GuiEvent event) switch (event) { case GUI_EVENT_SCREEN_INIT: page = 1; - memcpy(&scratch, &app_config, APPCONFIG_SIZE); + memcpy(&settings_scratch, &app_config, APPCONFIG_SIZE); break; case GUI_EVENT_PAINT: switch (page) { case 1: - snprintf(buf, 100, "1. Program=%s", scratch.scheduler_enable ? "ZAP" : "VYP"); + snprintf(buf, 100, "1. Program=%s", settings_scratch.scheduler_enable ? "ZAP" : "VYP"); LcdBuffer_Write(&lcd, 0, 0, buf); LcdBuffer_Write(&lcd, 1, 0, "2. Upravit program"); LcdBuffer_Write(&lcd, 2, 0, "3. Nastavit čas"); @@ -47,14 +47,15 @@ void screen_settings(GuiEvent event) break; case 3: - snprintf(buf, 100, "1. Blok.vlhkostí=%s", scratch.moisture_enable ? "ZAP" : "VYP"); + snprintf(buf, 100, "1. Blok.vlhkostí=%s", settings_scratch.moisture_enable ? "ZAP" : "VYP"); LcdBuffer_Write(&lcd, 0, 0, buf); LcdBuffer_Write(&lcd, 1, 0, "2. Kalibrace vlhkom."); - LcdBuffer_Write(&lcd, 2, 0, "3. Nastavit práh"); + snprintf(buf, 100, "3. Práh vlh.=%d%%", settings_scratch.moisture_threshold_percent); + LcdBuffer_Write(&lcd, 2, 0, buf); break; } - snprintf(buf, 100, "⊛←%d/%d→¤ 🅰Ulož 🅳Zruš", page, PAGECOUNT); + snprintf(buf, 100, "⊛←%d/%d→¤ 🅳Zruš 🅰Ulož", page, PAGECOUNT); LcdBuffer_Write(&lcd, 3, 0, buf); break; @@ -77,7 +78,7 @@ void screen_settings(GuiEvent event) break; case GUI_EVENT_KEY_A: // SAVE - memcpy(&app_config, &scratch, APPCONFIG_SIZE); + memcpy(&app_config, &settings_scratch, APPCONFIG_SIZE); appconfig_save(); switch_screen(screen_home, true); break; @@ -91,7 +92,7 @@ void screen_settings(GuiEvent event) case 1: switch (event) { case GUI_EVENT_KEY_1: // Program Zap/VYP - scratch.scheduler_enable ^= 1; + settings_scratch.scheduler_enable ^= 1; request_paint(); break; @@ -119,7 +120,7 @@ void screen_settings(GuiEvent event) case 3: switch (event) { case GUI_EVENT_KEY_1: // Blokace vlhkosti Zap/VYP - scratch.moisture_enable ^= 1; + settings_scratch.moisture_enable ^= 1; request_paint(); break; @@ -128,7 +129,7 @@ void screen_settings(GuiEvent event) break; case GUI_EVENT_KEY_3: // Nastavit prah - // TODO + switch_screen(screen_set_moisture_threshold, true); break; } break;