implement moisture calib
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
+20
-3
@@ -7,24 +7,41 @@
|
||||
#include <hardware/adc.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user