|
@@ -19,18 +19,90 @@
|
19
|
19
|
#include "pico/cyw43_arch.h"
|
20
|
20
|
|
21
|
21
|
#include "config.h"
|
|
22
|
+#include "buttons.h"
|
22
|
23
|
#include "lcd.h"
|
23
|
24
|
#include "log.h"
|
|
25
|
+#include "lcd.h"
|
|
26
|
+#include "textbox.h"
|
24
|
27
|
#include "mem.h"
|
|
28
|
+#include "util.h"
|
25
|
29
|
#include "wifi.h"
|
26
|
30
|
|
|
31
|
+static int16_t lcd_off = 0;
|
|
32
|
+static size_t text_window = 120;
|
|
33
|
+static size_t text_off = 0;
|
|
34
|
+static size_t prev_len = 0;
|
|
35
|
+static bool redraw = false;
|
|
36
|
+
|
|
37
|
+static void lcd_write(const void *buf, size_t len) {
|
|
38
|
+ char tmp[len + 1];
|
|
39
|
+ memcpy(tmp, buf, len);
|
|
40
|
+ tmp[len] = '\0';
|
|
41
|
+ lcd_off = text_box(tmp, false,
|
|
42
|
+ "fixed_10x20",
|
|
43
|
+ 0, LCD_WIDTH,
|
|
44
|
+ lcd_off, LCD_HEIGHT - lcd_off,
|
|
45
|
+ 0);
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+static void log_dump_to_lcd(void) {
|
|
49
|
+ // TODO length is not good as indicator.
|
|
50
|
+ // TODO will stop working when log buffer is filled.
|
|
51
|
+ size_t len = rb_len(log_get());
|
|
52
|
+ if (redraw || (len == prev_len)) {
|
|
53
|
+ return;
|
|
54
|
+ }
|
|
55
|
+ prev_len = len;
|
|
56
|
+ redraw = false;
|
|
57
|
+
|
|
58
|
+ lcd_off = 0;
|
|
59
|
+
|
|
60
|
+ text_off = 0;
|
|
61
|
+ if (len > text_window) {
|
|
62
|
+ text_off = len - text_window;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ rb_dump(log_get(), lcd_write, text_off);
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+static void ota_buttons(enum buttons btn, bool state) {
|
|
69
|
+ if (state && (btn == BTN_Y)) {
|
|
70
|
+ reset_to_main();
|
|
71
|
+ } else if (state && (btn == BTN_LEFT)) {
|
|
72
|
+ if (text_window > 10) {
|
|
73
|
+ text_window -= 10;
|
|
74
|
+ redraw = true;
|
|
75
|
+ }
|
|
76
|
+ } else if (state && (btn == BTN_RIGHT)) {
|
|
77
|
+ if (text_window < 1000) {
|
|
78
|
+ text_window += 10;
|
|
79
|
+ redraw = true;
|
|
80
|
+ }
|
|
81
|
+ } else if (state && (btn == BTN_UP)) {
|
|
82
|
+ if (text_off > 10) {
|
|
83
|
+ text_off -= 10;
|
|
84
|
+ redraw = true;
|
|
85
|
+ }
|
|
86
|
+ } else if (state && (btn == BTN_DOWN)) {
|
|
87
|
+ if (text_off < (prev_len - 10)) {
|
|
88
|
+ text_off += 10;
|
|
89
|
+ redraw = true;
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+void picowota_poll(void) {
|
|
95
|
+ buttons_run();
|
|
96
|
+ cyw43_arch_poll();
|
|
97
|
+ wifi_run();
|
|
98
|
+ log_dump_to_lcd();
|
|
99
|
+}
|
|
100
|
+
|
27
|
101
|
int picowota_network_init(void) {
|
28
|
|
- debug("mem_load");
|
|
102
|
+ buttons_init();
|
|
103
|
+ buttons_callback(ota_buttons);
|
29
|
104
|
mem_load();
|
30
|
|
-
|
31
|
|
- debug("lcd_init");
|
32
|
105
|
lcd_init();
|
33
|
|
-
|
34
|
106
|
lcd_set_backlight(mem_data()->backlight);
|
35
|
107
|
log_dump_to_lcd();
|
36
|
108
|
|
|
@@ -51,27 +123,26 @@ int picowota_network_init(void) {
|
51
|
123
|
|
52
|
124
|
const char *prev = NULL;
|
53
|
125
|
while (!wifi_ready()) {
|
54
|
|
- cyw43_arch_poll();
|
55
|
|
- wifi_run();
|
56
|
|
-
|
57
|
126
|
const char *state = wifi_state();
|
58
|
127
|
if (state != prev) {
|
59
|
128
|
prev = state;
|
60
|
129
|
debug("new state: %s", state);
|
61
|
130
|
}
|
62
|
131
|
|
63
|
|
- log_dump_to_lcd();
|
|
132
|
+ picowota_poll();
|
64
|
133
|
|
65
|
134
|
// TODO open AP when timed out?
|
66
|
135
|
}
|
67
|
136
|
|
68
|
137
|
debug("wifi ready");
|
69
|
|
- log_dump_to_lcd();
|
|
138
|
+ picowota_poll();
|
70
|
139
|
|
71
|
140
|
return 0;
|
72
|
141
|
}
|
73
|
142
|
|
74
|
143
|
void picowota_network_deinit(void) {
|
75
|
144
|
debug("wifi_deinit");
|
|
145
|
+ log_dump_to_lcd();
|
|
146
|
+
|
76
|
147
|
wifi_deinit();
|
77
|
148
|
}
|