From f9ffe8b3660016f9a88825318777f1482afe4e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 19 Jun 2023 22:10:55 +0200 Subject: [PATCH] implement moisture calib --- CMakeLists.txt | 1 + src/app_io.c | 23 +++++++++-- src/app_io.h | 2 + src/screens/app_gui.c | 2 +- src/screens/app_gui.h | 1 + src/screens/gui_event.h | 2 +- src/screens/screen_cyklus.c | 2 +- src/screens/screen_home.c | 8 ++-- src/screens/screen_moisture_calib.c | 62 +++++++++++++++++++++++++++++ src/screens/screen_settings.c | 2 +- 10 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 src/screens/screen_moisture_calib.c diff --git a/CMakeLists.txt b/CMakeLists.txt index cd11b9c..29ea741 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable(zavlaha src/screens/screen_cyklus.c src/screens/screen_set_time.c src/screens/screen_set_moisture_threshold.c + src/screens/screen_moisture_calib.c src/app_io.c src/app_config.c) diff --git a/src/app_io.c b/src/app_io.c index bfb7f63..19d0ee6 100644 --- a/src/app_io.c +++ b/src/app_io.c @@ -7,24 +7,41 @@ #include #include "app_io.h" #include "pinout.h" +#include "app_config.h" -void set_relays(bool one, bool two, bool three, bool four) { +void set_relays(bool one, bool two, bool three, bool four) +{ gpio_put(PIN_RE1, one); gpio_put(PIN_RE2, two); gpio_put(PIN_RE3, three); gpio_put(PIN_RE1, four); } -void open_valve(uint8_t num) { +void open_valve(uint8_t num) +{ gpio_put(PIN_RE1, num == 1); gpio_put(PIN_RE2, num == 2); gpio_put(PIN_RE3, num == 3); gpio_put(PIN_RE4, num == 4); } -uint16_t moisture_read() { +uint16_t moisture_read() +{ adc_select_input(ADC_NUM_MOISTURE); return adc_read(); } +uint16_t moisture_convert(uint16_t moisture) +{ + uint32_t moisture_pt; + if (moisture >= app_config.moisture_dry) { + moisture_pt = 0; + } else if (moisture <= app_config.moisture_wet) { + moisture_pt = 100; + } else { + moisture_pt = ((uint32_t) (app_config.moisture_dry - moisture) * 100) + / (app_config.moisture_dry - app_config.moisture_wet); + } + return (uint16_t) moisture_pt; +} diff --git a/src/app_io.h b/src/app_io.h index ee5281e..6f42ddf 100644 --- a/src/app_io.h +++ b/src/app_io.h @@ -14,4 +14,6 @@ void open_valve(uint8_t num); uint16_t moisture_read(); +uint16_t moisture_convert(uint16_t moisture); + #endif //ZAVLAHA_APP_IO_H diff --git a/src/screens/app_gui.c b/src/screens/app_gui.c index 221cf43..6554bd6 100644 --- a/src/screens/app_gui.c +++ b/src/screens/app_gui.c @@ -36,7 +36,7 @@ void gui_loop_iter(GuiEvent message) { // 10ms tick event if (tickNow - s_app.last_tick_time > 10) { - s_app.screen(GUI_EVENT_SCREEN_TICK); + s_app.screen(GUI_EVENT_SCREEN_TICK_10MS); s_app.last_tick_time = tickNow; } diff --git a/src/screens/app_gui.h b/src/screens/app_gui.h index f7baf6d..58a019f 100644 --- a/src/screens/app_gui.h +++ b/src/screens/app_gui.h @@ -53,6 +53,7 @@ extern struct AppConfig settings_scratch; void screen_settings(GuiEvent event); void screen_set_time(GuiEvent event); void screen_set_moisture_threshold(GuiEvent event); +void screen_moisture_calib(GuiEvent event); // XXX other prototypes struct State { diff --git a/src/screens/gui_event.h b/src/screens/gui_event.h index 51c1b4a..57485c2 100644 --- a/src/screens/gui_event.h +++ b/src/screens/gui_event.h @@ -15,7 +15,7 @@ typedef enum GuiEvent { GUI_EVENT_SCREEN_INIT = 1, /// Time tick, used to carry timing to the screen functions. /// This tick has 10ms interval - GUI_EVENT_SCREEN_TICK = 2, + GUI_EVENT_SCREEN_TICK_10MS = 2, /// Keypad GUI_EVENT_KEY_0 = '0', GUI_EVENT_KEY_1, diff --git a/src/screens/screen_cyklus.c b/src/screens/screen_cyklus.c index 8403a50..8b0cb57 100644 --- a/src/screens/screen_cyklus.c +++ b/src/screens/screen_cyklus.c @@ -21,7 +21,7 @@ void screen_cyklus(GuiEvent event) switch (event) { case GUI_EVENT_SCREEN_INIT: - case GUI_EVENT_SCREEN_TICK: + case GUI_EVENT_SCREEN_TICK_10MS: // break; diff --git a/src/screens/screen_home.c b/src/screens/screen_home.c index 6f340ef..536fe93 100644 --- a/src/screens/screen_home.c +++ b/src/screens/screen_home.c @@ -11,10 +11,11 @@ #include "gui_event.h" #include "ds_rtc.h" #include "app_io.h" +#include "app_config.h" static uint32_t last_time = 0; static struct rtc_time rtc_time = {}; -static uint16_t moisture = 0; +static uint16_t moisture_pt = 0; static uint8_t showbuf_wp = 0; @@ -26,18 +27,17 @@ void screen_home(GuiEvent event) switch (event) { case GUI_EVENT_SCREEN_INIT: // pass - case GUI_EVENT_SCREEN_TICK: + case GUI_EVENT_SCREEN_TICK_10MS: if (elapsed >= 100) { last_time = now; rtc_get_time(&rtc_time); - moisture = moisture_read(); + moisture_pt = moisture_convert(moisture_read()); request_paint(); } break; case GUI_EVENT_PAINT:; // TODO - uint32_t moisture_pt = ((uint32_t)(moisture > 2800 ? 0 : (2800 - moisture)) * 100) / (2800 - 1600); sprintf(stmp, " %2d:%02d:%02d %3d%% 🌢 ", rtc_time.hour, rtc_time.minute, rtc_time.second, moisture_pt); LcdBuffer_Write(&lcd, 0, 0, stmp); diff --git a/src/screens/screen_moisture_calib.c b/src/screens/screen_moisture_calib.c new file mode 100644 index 0000000..54d9f74 --- /dev/null +++ b/src/screens/screen_moisture_calib.c @@ -0,0 +1,62 @@ +#include +#include "app_gui.h" +#include "gui_event.h" +#include "app_io.h" +#include "app_config.h" + +static int phase = 0; +static int tick_counter = 0; +static uint16_t last_moisture = 0; + +static uint16_t sample_dry = 0; + +void screen_moisture_calib(GuiEvent event) +{ + char buf[100]; + + switch (event) { + case GUI_EVENT_SCREEN_INIT: + phase = 0; + tick_counter = 0; + // pass through + case GUI_EVENT_SCREEN_TICK_10MS: + tick_counter++; + if (tick_counter == 25) { + request_paint(); // will read new measurement + tick_counter = 0; + } + last_moisture = moisture_read(); + break; + + case GUI_EVENT_PAINT: + if (phase == 0) { + LcdBuffer_Write(&lcd, 0, 0, "Dej čidlo do vyschlé"); + LcdBuffer_Write(&lcd, 1, 0, "půdy (vlhkost 0%)"); + } else { + LcdBuffer_Write(&lcd, 0, 0, "Dej čidlo do mokré"); + LcdBuffer_Write(&lcd, 1, 0, "půdy (vlhkost 100%)"); + } + + snprintf(buf, 100, "Vlhkost %4d/4095", last_moisture); + LcdBuffer_Write(&lcd, 2, 0, buf); + + LcdBuffer_Write(&lcd, 3, 0, "🅳Zrušit 🅰Potvrdit"); + break; + + case GUI_EVENT_KEY_A: // Confirm + if (phase == 0) { + sample_dry = last_moisture; + phase++; + LcdBuffer_Clear(&lcd); // make sure there isnt overlap with previous step + } else { + settings_scratch.moisture_dry = sample_dry; + settings_scratch.moisture_wet = last_moisture; + switch_screen(screen_settings, false); + } + break; + + case GUI_EVENT_KEY_D: // CANCEL + switch_screen(screen_settings, false); + break; + } +} diff --git a/src/screens/screen_settings.c b/src/screens/screen_settings.c index 16a15ff..0532bd7 100644 --- a/src/screens/screen_settings.c +++ b/src/screens/screen_settings.c @@ -125,7 +125,7 @@ void screen_settings(GuiEvent event) break; case GUI_EVENT_KEY_2: // Kalibrace vlhkomeru - // TODO + switch_screen(screen_moisture_calib, true); break; case GUI_EVENT_KEY_3: // Nastavit prah