1s and 100ms tick events, auto return to home

master
Ondřej Hruška 1 year ago
parent 9905033ca9
commit 4018e27c6b
  1. 1
      src/app_config.h
  2. 37
      src/screens/app_gui.c
  3. 11
      src/screens/app_gui.h
  4. 8
      src/screens/gui_event.h
  5. 11
      src/screens/screen_cyklus.c
  6. 23
      src/screens/screen_home.c
  7. 5
      src/screens/screen_set_time.c

@ -10,6 +10,7 @@
#define CIRCUIT_COUNT 4 #define CIRCUIT_COUNT 4
#define SCHEDULE_COUNT 4 #define SCHEDULE_COUNT 4
#define MENU_AUTOEXIT_TIME_S 120
struct __attribute__((packed)) ScheduleTime { struct __attribute__((packed)) ScheduleTime {
bool enable; bool enable;

@ -5,8 +5,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "app_gui.h" #include "app_gui.h"
#include "../lcd/lcdbuf.h" #include "lcd/lcdbuf.h"
#include "lcd.h" #include "lcd.h"
#include "app_config.h"
struct State s_app = {}; struct State s_app = {};
@ -26,18 +27,38 @@ char sbuf[100];
void gui_init() void gui_init()
{ {
switch_screen(screen_home, true); switch_screen(screen_home, true);
s_app.last_tick_time = timestamp(); rtc_get_time(&s_app.rtc_time);
LcdBuffer_Init(&lcd, CGROM_A00, CGRAM_CZ); LcdBuffer_Init(&lcd, CGROM_A00, CGRAM_CZ);
} }
void gui_loop_iter(GuiEvent message) { void gui_loop_iter(GuiEvent message) {
uint32_t tickNow = timestamp(); uint32_t tickNow = timestamp_ms();
// screen auto-close
if (s_app.screen != screen_home && s_app.screen != screen_cyklus) {
if ((tickNow - s_app.screen_open_time) >= (MENU_AUTOEXIT_TIME_S * 1000)) {
switch_screen(screen_home, true);
}
}
// Read RTC every second
if (tickNow - s_app.last_1s_time >= 1000) {
s_app.screen(GUI_EVENT_SCREEN_TICK_1S);
rtc_get_time(&s_app.rtc_time); // now is a good time to update timestamp - we could just increment it though
s_app.last_1s_time = tickNow;
}
// 100ms tick event
if (tickNow - s_app.last_100ms_time >= 100) {
s_app.screen(GUI_EVENT_SCREEN_TICK_100MS);
s_app.last_100ms_time = tickNow;
}
// 10ms tick event // 10ms tick event
if (tickNow - s_app.last_tick_time > 10) { if (tickNow - s_app.last_10ms_time >= 10) {
s_app.screen(GUI_EVENT_SCREEN_TICK_10MS); s_app.screen(GUI_EVENT_SCREEN_TICK_10MS);
s_app.last_tick_time = tickNow; s_app.last_10ms_time = tickNow;
} }
if (message != GUI_EVENT_NONE) { if (message != GUI_EVENT_NONE) {
@ -68,12 +89,18 @@ void switch_screen(screen_t pScreen, bool init) {
return; return;
} }
s_app.screen_open_time = timestamp_ms();
s_app.screen = pScreen; s_app.screen = pScreen;
LcdBuffer_Clear(&lcd); LcdBuffer_Clear(&lcd);
LcdBuffer_SetCursor(&lcd, 0, 0, CURSOR_NONE); // always start with a hidden cursor. If the page wants a visible cursor, it should do that in PAINT LcdBuffer_SetCursor(&lcd, 0, 0, CURSOR_NONE); // always start with a hidden cursor. If the page wants a visible cursor, it should do that in PAINT
request_paint(); request_paint();
// Reset the timers, so the screen has nicely aligned events
s_app.last_10ms_time = s_app.last_100ms_time = s_app.last_1s_time = timestamp_ms();
// also read time so we have the latest greatest
rtc_get_time(&s_app.rtc_time);
if (init) { if (init) {
pScreen(GUI_EVENT_SCREEN_INIT); pScreen(GUI_EVENT_SCREEN_INIT);
} }

@ -10,6 +10,7 @@
#include <pico/time.h> #include <pico/time.h>
#include "gui_event.h" #include "gui_event.h"
#include "lcd/lcdbuf.h" #include "lcd/lcdbuf.h"
#include "ds_rtc.h"
// 🌢🅰🅱🅲🅳❶❷❸❹⊛¤▌↑↓✔ // 🌢🅰🅱🅲🅳❶❷❸❹⊛¤▌↑↓✔
@ -25,7 +26,7 @@ extern struct LcdBuffer lcd;
typedef void (*screen_t)(GuiEvent event); typedef void (*screen_t)(GuiEvent event);
static inline uint32_t timestamp() { static inline uint32_t timestamp_ms() {
return to_ms_since_boot(get_absolute_time()); return to_ms_since_boot(get_absolute_time());
} }
@ -64,13 +65,19 @@ void screen_program_edit(GuiEvent event);
// XXX other prototypes // XXX other prototypes
struct State { struct State {
struct rtc_time rtc_time;
/// Repaint was requested from the screen code /// Repaint was requested from the screen code
bool paint_needed; bool paint_needed;
/// Pointer to the currently active screen func /// Pointer to the currently active screen func
screen_t screen; screen_t screen;
uint32_t last_tick_time; uint32_t last_10ms_time;
uint32_t last_100ms_time;
uint32_t last_1s_time;
uint32_t screen_open_time;
}; };
extern struct State s_app; extern struct State s_app;

@ -10,12 +10,14 @@ typedef enum GuiEvent {
/// No event, zero; This is a default value. /// No event, zero; This is a default value.
GUI_EVENT_NONE = 0, GUI_EVENT_NONE = 0,
/// Cause redraw /// Cause redraw
GUI_EVENT_PAINT = 0, GUI_EVENT_PAINT,
/// Used as the argument when initing a screen /// Used as the argument when initing a screen
GUI_EVENT_SCREEN_INIT = 1, GUI_EVENT_SCREEN_INIT,
/// Time tick, used to carry timing to the screen functions. /// Time tick, used to carry timing to the screen functions.
/// This tick has 10ms interval /// This tick has 10ms interval
GUI_EVENT_SCREEN_TICK_10MS = 2, GUI_EVENT_SCREEN_TICK_10MS,
GUI_EVENT_SCREEN_TICK_100MS,
GUI_EVENT_SCREEN_TICK_1S,
/// Keypad /// Keypad
GUI_EVENT_KEY_0 = '0', GUI_EVENT_KEY_0 = '0',
GUI_EVENT_KEY_1, GUI_EVENT_KEY_1,

@ -10,15 +10,11 @@
#include "gui_event.h" #include "gui_event.h"
#include "app_io.h" #include "app_io.h"
#include "app_config.h" #include "app_config.h"
#include "ds_rtc.h"
static int phase = 0; static int phase = 0;
static uint32_t phase_seconds = 0; static uint32_t phase_seconds = 0;
static uint32_t phase_seconds_max = 0; static uint32_t phase_seconds_max = 0;
static uint8_t last_secs = 0;
static struct rtc_time time;
static void end_cycle() { static void end_cycle() {
open_valve(0); open_valve(0);
} }
@ -36,10 +32,8 @@ void screen_cyklus(GuiEvent event)
phase_seconds = 0; phase_seconds = 0;
start_branch(); start_branch();
break; break;
case GUI_EVENT_SCREEN_TICK_10MS:
last_secs = time.second; case GUI_EVENT_SCREEN_TICK_1S:
rtc_get_time(&time);
if (time.second != last_secs) {
phase_seconds += 1; phase_seconds += 1;
if (phase_seconds >= phase_seconds_max) { if (phase_seconds >= phase_seconds_max) {
@ -54,7 +48,6 @@ void screen_cyklus(GuiEvent event)
phase_seconds = 0; phase_seconds = 0;
} }
request_paint(); request_paint();
}
break; break;
case GUI_EVENT_PAINT: case GUI_EVENT_PAINT:

@ -9,37 +9,28 @@
#include <string.h> #include <string.h>
#include "app_gui.h" #include "app_gui.h"
#include "gui_event.h" #include "gui_event.h"
#include "ds_rtc.h"
#include "app_io.h" #include "app_io.h"
#include "app_config.h" #include "app_config.h"
static uint32_t last_time = 0;
static struct rtc_time rtc_time = {};
static uint16_t moisture_pt = 0; static uint16_t moisture_pt = 0;
static uint8_t showbuf_wp = 0;
void screen_home(GuiEvent event) void screen_home(GuiEvent event)
{ {
uint32_t now = timestamp();
uint32_t elapsed = now - last_time;
switch (event) { switch (event) {
case GUI_EVENT_SCREEN_INIT: case GUI_EVENT_SCREEN_INIT:
// pass // pass
case GUI_EVENT_SCREEN_TICK_10MS: case GUI_EVENT_SCREEN_TICK_100MS:
if (elapsed >= 100) {
last_time = now;
rtc_get_time(&rtc_time);
moisture_pt = moisture_convert(moisture_read()); moisture_pt = moisture_convert(moisture_read());
request_paint(); request_paint();
}
break; break;
case GUI_EVENT_PAINT:; case GUI_EVENT_PAINT:
// TODO sprintf(sbuf, " %2d:%02d:%02d %3d%% 🌢 ",
s_app.rtc_time.hour,
s_app.rtc_time.minute,
s_app.rtc_time.second,
moisture_pt);
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, 0, 0, sbuf);
LcdBuffer_Write(&lcd, 1, 0, "Plán. závlaha 7:15"); LcdBuffer_Write(&lcd, 1, 0, "Plán. závlaha 7:15");

@ -12,9 +12,8 @@ void screen_set_time(GuiEvent event)
{ {
switch (event) { switch (event) {
case GUI_EVENT_SCREEN_INIT: case GUI_EVENT_SCREEN_INIT:
rtc_get_time(&time); // so it's shown in the preview time.hour = s_app.rtc_time.hour;
// time.hour = 0; time.minute = s_app.rtc_time.minute;
// time.minute = 0;
time.second = 0; time.second = 0;
cursor = 0; cursor = 0;
break; break;

Loading…
Cancel
Save