From 9905033ca97b3966ad9e7f9af0e1cdadb26d4a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 20 Jun 2023 21:08:07 +0200 Subject: [PATCH] cyklus --- src/app_io.h | 1 + src/screens/app_gui.c | 2 +- src/screens/app_gui.h | 3 +- src/screens/screen_cyklus.c | 66 ++++++++++++++++++--- src/screens/screen_delka_zalivky.c | 10 ++-- src/screens/screen_home.c | 4 +- src/screens/screen_kompenzace_tlaku.c | 10 ++-- src/screens/screen_moisture_calib.c | 6 +- src/screens/screen_program_edit.c | 10 ++-- src/screens/screen_program_prehled.c | 22 ++++--- src/screens/screen_set_moisture_threshold.c | 10 ++-- src/screens/screen_set_time.c | 6 +- src/screens/screen_settings.c | 17 +++--- 13 files changed, 101 insertions(+), 66 deletions(-) diff --git a/src/app_io.h b/src/app_io.h index 6f42ddf..90bb13e 100644 --- a/src/app_io.h +++ b/src/app_io.h @@ -10,6 +10,7 @@ void set_relays(bool one, bool two, bool three, bool four); +/** Open valve, 1,2,3,4; 0=close all */ void open_valve(uint8_t num); uint16_t moisture_read(); diff --git a/src/screens/app_gui.c b/src/screens/app_gui.c index 6554bd6..56662b7 100644 --- a/src/screens/app_gui.c +++ b/src/screens/app_gui.c @@ -20,7 +20,7 @@ void request_paint() { /** Draw the common overlay / HUD (with temperatures and heater status) */ static void draw_common_overlay(); -char stmp[100]; +char sbuf[100]; /** Main loop */ void gui_init() diff --git a/src/screens/app_gui.h b/src/screens/app_gui.h index ad3e53c..d7916c5 100644 --- a/src/screens/app_gui.h +++ b/src/screens/app_gui.h @@ -14,7 +14,8 @@ // 🌢🅰🅱🅲🅳❶❷❸❹⊛¤▌↑↓✔ /// Temporary scratch buffer -extern char stmp[100]; +extern char sbuf[100]; +#define sbuf_len 100 extern struct LcdBuffer lcd; diff --git a/src/screens/screen_cyklus.c b/src/screens/screen_cyklus.c index 8b0cb57..c83b431 100644 --- a/src/screens/screen_cyklus.c +++ b/src/screens/screen_cyklus.c @@ -6,33 +6,81 @@ // #include -#include #include "app_gui.h" #include "gui_event.h" -#include "ds_rtc.h" #include "app_io.h" +#include "app_config.h" +#include "ds_rtc.h" -// TODO +static int phase = 0; +static uint32_t phase_seconds = 0; +static uint32_t phase_seconds_max = 0; +static uint8_t last_secs = 0; +static struct rtc_time time; + +static void end_cycle() { + open_valve(0); +} + +static void start_branch() { + phase_seconds_max = ((uint32_t) app_config.circuit_base_time_s * (uint32_t) app_config.circuit_time_percent[phase]) / 100; + open_valve(phase + 1); +} void screen_cyklus(GuiEvent event) { - uint32_t now = timestamp(); - switch (event) { case GUI_EVENT_SCREEN_INIT: + phase = 0; + phase_seconds = 0; + start_branch(); + break; case GUI_EVENT_SCREEN_TICK_10MS: - // + last_secs = time.second; + rtc_get_time(&time); + if (time.second != last_secs) { + phase_seconds += 1; + + if (phase_seconds >= phase_seconds_max) { + phase += 1; + if (phase >= CIRCUIT_COUNT) { + end_cycle(); + switch_screen(screen_home, true); + return; + } + + start_branch(); + phase_seconds = 0; + } + request_paint(); + } break; case GUI_EVENT_PAINT: LcdBuffer_Write(&lcd, 0, 0, "==ZÁVLAHOVÝ CYKLUS=="); - LcdBuffer_Write(&lcd, 1, 0, "Okruh 2/4 30/200s"); - LcdBuffer_Write(&lcd, 2, 0, "█████████▌"); - LcdBuffer_Write(&lcd, 3, 0, "🅳 Přerušit"); + + snprintf(sbuf, sbuf_len, "Okruh %d/%d %3d/%d s ", phase + 1, 4, phase_seconds, phase_seconds_max); + LcdBuffer_Write(&lcd, 1, 0, sbuf); + + uint32_t pieces = (phase_seconds * 40) / phase_seconds_max; + + char *buf = sbuf; + for (int i = 0; i < 20; i++) { + if ((i*2 + 1) == pieces) { + buf += sprintf(buf, "▌"); + } else if (i * 2 < pieces) { + buf += sprintf(buf, "█"); + } else { + buf += sprintf(buf, " "); + } + } + LcdBuffer_Write(&lcd, 2, 0, sbuf); + LcdBuffer_Write(&lcd, 3, 0, "🅳 Ukončit"); break; case GUI_EVENT_KEY_D: + end_cycle(); switch_screen(screen_home, true); return; } diff --git a/src/screens/screen_delka_zalivky.c b/src/screens/screen_delka_zalivky.c index b3e892b..f2026b0 100644 --- a/src/screens/screen_delka_zalivky.c +++ b/src/screens/screen_delka_zalivky.c @@ -11,8 +11,6 @@ static int cursor; void screen_delka_zalivky(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: value = 0; @@ -26,12 +24,12 @@ void screen_delka_zalivky(GuiEvent event) LcdBuffer_SetCursor(&lcd, 2, cursor, CURSOR_BOTH); if (cursor > 0) { - snprintf(buf, 100, "%d s", value); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "%d s", value); + LcdBuffer_Write(&lcd, 2, 0, sbuf); } else { // show orig value - snprintf(buf, 100, "%d s", settings_scratch.circuit_base_time_s); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "%d s", settings_scratch.circuit_base_time_s); + LcdBuffer_Write(&lcd, 2, 0, sbuf); } LcdBuffer_Write(&lcd, 3, 0, cursor == 0 ? "🅳Zrušit" : "🅳Zrušit 🅰OK"); diff --git a/src/screens/screen_home.c b/src/screens/screen_home.c index 536fe93..4bf17b3 100644 --- a/src/screens/screen_home.c +++ b/src/screens/screen_home.c @@ -39,8 +39,8 @@ void screen_home(GuiEvent event) case GUI_EVENT_PAINT:; // TODO - sprintf(stmp, " %2d:%02d:%02d %3d%% 🌢 ", rtc_time.hour, rtc_time.minute, rtc_time.second, moisture_pt); - LcdBuffer_Write(&lcd, 0, 0, stmp); + sprintf(sbuf, " %2d:%02d:%02d %3d%% 🌢 ", rtc_time.hour, rtc_time.minute, rtc_time.second, moisture_pt); + LcdBuffer_Write(&lcd, 0, 0, sbuf); LcdBuffer_Write(&lcd, 1, 0, "Plán. závlaha 7:15"); LcdBuffer_Write(&lcd, 2, 0, "❶Spustit ❷Přeskočit"); diff --git a/src/screens/screen_kompenzace_tlaku.c b/src/screens/screen_kompenzace_tlaku.c index 2aa1aa2..08775a6 100644 --- a/src/screens/screen_kompenzace_tlaku.c +++ b/src/screens/screen_kompenzace_tlaku.c @@ -12,8 +12,6 @@ static int cursor; void screen_kompenzace_tlaku(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: cursor = 0; @@ -23,10 +21,10 @@ void screen_kompenzace_tlaku(GuiEvent event) case GUI_EVENT_PAINT: LcdBuffer_Write(&lcd, 0, 0, "Relativní čas okruhů"); - snprintf(buf, 100, "1. %3d%% 3. %3d%%", ratios[0], ratios[2]); - LcdBuffer_Write(&lcd, 1, 0, buf); - snprintf(buf, 100, "2. %3d%% 4. %3d%%", ratios[1], ratios[3]); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "1. %3d%% 3. %3d%%", ratios[0], ratios[2]); + LcdBuffer_Write(&lcd, 1, 0, sbuf); + snprintf(sbuf, sbuf_len, "2. %3d%% 4. %3d%%", ratios[1], ratios[3]); + LcdBuffer_Write(&lcd, 2, 0, sbuf); switch (cursor) { case 0: diff --git a/src/screens/screen_moisture_calib.c b/src/screens/screen_moisture_calib.c index 54d9f74..ed05be4 100644 --- a/src/screens/screen_moisture_calib.c +++ b/src/screens/screen_moisture_calib.c @@ -12,8 +12,6 @@ static uint16_t sample_dry = 0; void screen_moisture_calib(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: phase = 0; @@ -37,8 +35,8 @@ void screen_moisture_calib(GuiEvent event) LcdBuffer_Write(&lcd, 1, 0, "půdy (vlhkost 100%)"); } - snprintf(buf, 100, "Vlhkost %4d/4095", last_moisture); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "Vlhkost %4d/4095", last_moisture); + LcdBuffer_Write(&lcd, 2, 0, sbuf); LcdBuffer_Write(&lcd, 3, 0, "🅳Zrušit 🅰Potvrdit"); break; diff --git a/src/screens/screen_program_edit.c b/src/screens/screen_program_edit.c index c407491..0ef6c5b 100644 --- a/src/screens/screen_program_edit.c +++ b/src/screens/screen_program_edit.c @@ -13,8 +13,6 @@ int pgm_edit_slot; // set from the program overview screen void screen_program_edit(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: memcpy(&time, &settings_scratch.schedules[pgm_edit_slot], sizeof(time)); @@ -22,13 +20,13 @@ void screen_program_edit(GuiEvent event) break; case GUI_EVENT_PAINT: - snprintf(buf, 100, "Denní slot %d", 1 + pgm_edit_slot); - LcdBuffer_Write(&lcd, 0, 0, buf); + snprintf(sbuf, sbuf_len, "Denní slot %d", 1 + pgm_edit_slot); + LcdBuffer_Write(&lcd, 0, 0, sbuf); if (time.enable) { LcdBuffer_SetCursor(&lcd, 1, cursor + (cursor >= 2), (cursor < 4) ? CURSOR_BOTH : CURSOR_NONE); - snprintf(buf, 100, "%02d:%02d", time.h, time.m); - LcdBuffer_Write(&lcd, 1, 0, buf); + snprintf(sbuf, sbuf_len, "%02d:%02d", time.h, time.m); + LcdBuffer_Write(&lcd, 1, 0, sbuf); } else { LcdBuffer_SetCursor(&lcd, 0, 0, CURSOR_NONE); LcdBuffer_Write(&lcd, 1, 0, "--:-- (slot vypnutý)"); diff --git a/src/screens/screen_program_prehled.c b/src/screens/screen_program_prehled.c index f86225a..99d3836 100644 --- a/src/screens/screen_program_prehled.c +++ b/src/screens/screen_program_prehled.c @@ -9,8 +9,6 @@ void screen_program_prehled(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: break; @@ -19,28 +17,28 @@ void screen_program_prehled(GuiEvent event) LcdBuffer_Write(&lcd, 0, 0, "Zvolte slot ke změně"); if (settings_scratch.schedules[0].enable) { - snprintf(buf, 100, "1. %02d:%02d ", settings_scratch.schedules[0].h, settings_scratch.schedules[0].m); + snprintf(sbuf, sbuf_len, "1. %02d:%02d ", settings_scratch.schedules[0].h, settings_scratch.schedules[0].m); } else { - snprintf(buf, 100, "1. --:-- "); + snprintf(sbuf, sbuf_len, "1. --:-- "); } if (settings_scratch.schedules[2].enable) { - snprintf(buf+10, 90, "3. %02d:%02d ", settings_scratch.schedules[2].h, settings_scratch.schedules[2].m); + snprintf(sbuf+10, sbuf_len-10, "3. %02d:%02d ", settings_scratch.schedules[2].h, settings_scratch.schedules[2].m); } else { - snprintf(buf+10, 90, "3. --:-- "); + snprintf(sbuf+10, sbuf_len-10, "3. --:-- "); } - LcdBuffer_Write(&lcd, 1, 0, buf); + LcdBuffer_Write(&lcd, 1, 0, sbuf); if (settings_scratch.schedules[1].enable) { - snprintf(buf, 100, "2. %02d:%02d ", settings_scratch.schedules[1].h, settings_scratch.schedules[1].m); + snprintf(sbuf, sbuf_len, "2. %02d:%02d ", settings_scratch.schedules[1].h, settings_scratch.schedules[1].m); } else { - snprintf(buf, 100, "2. --:-- "); + snprintf(sbuf, sbuf_len, "2. --:-- "); } if (settings_scratch.schedules[3].enable) { - snprintf(buf+10, 90, "4. %02d:%02d ", settings_scratch.schedules[3].h, settings_scratch.schedules[3].m); + snprintf(sbuf+10, sbuf_len-10, "4. %02d:%02d ", settings_scratch.schedules[3].h, settings_scratch.schedules[3].m); } else { - snprintf(buf+10, 90, "4. --:-- "); + snprintf(sbuf+10, sbuf_len-10, "4. --:-- "); } - LcdBuffer_Write(&lcd, 2, 0, buf); + LcdBuffer_Write(&lcd, 2, 0, sbuf); LcdBuffer_Write(&lcd, 3, 0, "🅳Zpět"); break; diff --git a/src/screens/screen_set_moisture_threshold.c b/src/screens/screen_set_moisture_threshold.c index 0fbcbcb..5ba02ba 100644 --- a/src/screens/screen_set_moisture_threshold.c +++ b/src/screens/screen_set_moisture_threshold.c @@ -11,8 +11,6 @@ static int cursor; void screen_set_moisture_threshold(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: threshold = 0; @@ -26,11 +24,11 @@ void screen_set_moisture_threshold(GuiEvent event) LcdBuffer_SetCursor(&lcd, 2, cursor, (cursor < 3) ? CURSOR_BLINK : CURSOR_NONE); if (cursor > 0) { - snprintf(buf, 100, "%d %%", threshold); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "%d %%", threshold); + LcdBuffer_Write(&lcd, 2, 0, sbuf); } else { - snprintf(buf, 100, "%d %%", settings_scratch.moisture_threshold_percent); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "%d %%", settings_scratch.moisture_threshold_percent); + LcdBuffer_Write(&lcd, 2, 0, sbuf); } LcdBuffer_Write(&lcd, 3, 0, cursor == 0 ? "🅳Zrušit" : "🅳Zrušit 🅰OK"); diff --git a/src/screens/screen_set_time.c b/src/screens/screen_set_time.c index d7f5109..8148b1f 100644 --- a/src/screens/screen_set_time.c +++ b/src/screens/screen_set_time.c @@ -10,8 +10,6 @@ static int cursor; void screen_set_time(GuiEvent event) { - char buf[100]; - switch (event) { case GUI_EVENT_SCREEN_INIT: rtc_get_time(&time); // so it's shown in the preview @@ -26,8 +24,8 @@ void screen_set_time(GuiEvent event) LcdBuffer_SetCursor(&lcd, 1, cursor + (cursor >= 2), (cursor < 4) ? CURSOR_BOTH : CURSOR_NONE); - snprintf(buf, 100, "%02d:%02d", time.hour, time.minute); - LcdBuffer_Write(&lcd, 1, 0, buf); + snprintf(sbuf, sbuf_len, "%02d:%02d", time.hour, time.minute); + LcdBuffer_Write(&lcd, 1, 0, sbuf); LcdBuffer_Write(&lcd, 3, 0, cursor < 4 ? "🅳Zrušit" : "🅳Zrušit 🅰Nastavit"); break; diff --git a/src/screens/screen_settings.c b/src/screens/screen_settings.c index 3638e74..13615e1 100644 --- a/src/screens/screen_settings.c +++ b/src/screens/screen_settings.c @@ -25,7 +25,6 @@ static void clear_menu_lines(); void screen_settings(GuiEvent event) { - char buf[100]; switch (event) { case GUI_EVENT_SCREEN_INIT: page = 1; @@ -35,8 +34,8 @@ void screen_settings(GuiEvent event) case GUI_EVENT_PAINT: switch (page) { case 1: - snprintf(buf, 100, "1. Program=%s", settings_scratch.scheduler_enable ? "ZAP" : "VYP"); - LcdBuffer_Write(&lcd, 0, 0, buf); + snprintf(sbuf, sbuf_len, "1. Program=%s", settings_scratch.scheduler_enable ? "ZAP" : "VYP"); + LcdBuffer_Write(&lcd, 0, 0, sbuf); LcdBuffer_Write(&lcd, 1, 0, "2. Upravit program"); LcdBuffer_Write(&lcd, 2, 0, "3. Nastavit čas"); break; @@ -47,16 +46,16 @@ void screen_settings(GuiEvent event) break; case 3: - snprintf(buf, 100, "1. Blok.vlhkostí=%s", settings_scratch.moisture_enable ? "ZAP" : "VYP"); - LcdBuffer_Write(&lcd, 0, 0, buf); + snprintf(sbuf, sbuf_len, "1. Blok.vlhkostí=%s", settings_scratch.moisture_enable ? "ZAP" : "VYP"); + LcdBuffer_Write(&lcd, 0, 0, sbuf); LcdBuffer_Write(&lcd, 1, 0, "2. Kalibrace vlhkom."); - snprintf(buf, 100, "3. Práh vlh.=%d%%", settings_scratch.moisture_threshold_percent); - LcdBuffer_Write(&lcd, 2, 0, buf); + snprintf(sbuf, sbuf_len, "3. Práh vlh.=%d%%", settings_scratch.moisture_threshold_percent); + LcdBuffer_Write(&lcd, 2, 0, sbuf); break; } - snprintf(buf, 100, "⊛←%d/%d→¤ 🅳Zruš 🅰Ulož", page, PAGECOUNT); - LcdBuffer_Write(&lcd, 3, 0, buf); + snprintf(sbuf, sbuf_len, "⊛←%d/%d→¤ 🅳Zruš 🅰Ulož", page, PAGECOUNT); + LcdBuffer_Write(&lcd, 3, 0, sbuf); break; case GUI_EVENT_KEY_B: