-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.c
295 lines (261 loc) · 6.51 KB
/
ui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "ui.h"
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include "buttons.h"
#include "config.h"
#include "display.h"
#include "throbber.h"
static const uint32_t kUiSleepTimeout = 7200000UL;
static const uint8_t kUiMaxSampleSize = 22;
static uint32_t gUiSampleTime = 0;
static uint32_t gUiSleepTime = 0;
enum UIStateEvent {
kUIStateEnter,
kUIStateUpdate,
kUIStateTimer,
kUIStateExit
};
struct MenuItem {
char label[5];
uint8_t (*menuFunc)(void);
};
void ui_state_count(enum UIStateEvent);
void ui_state_ounces(enum UIStateEvent);
void ui_state_gallons(enum UIStateEvent);
void ui_state_elapsed(enum UIStateEvent);
void ui_configure_menu(void);
void ui_change_state(void (*state)(enum UIStateEvent));
void ui_menu(char title[5],struct MenuItem *menu, uint8_t size);
uint8_t ui_menu_exit(void);
uint8_t ui_configure_brightness(void);
uint8_t ui_configure_size(void);
uint8_t ui_configure_count(void);
uint8_t ui_clear_count(void);
void ui_display_test(void);
void (*gUiStateFunc)(enum UIStateEvent) = 0;
void ui_init(void)
{
throbber_init();
display_init();
buttons_init();
if (config_init())
ui_display_test();
display_set_brightness(config_get_brightness());
ui_change_state(&ui_state_count);
}
void ui_update()
{
if(!gUiStateFunc) {
//Sleep Logic
if (button_press(kButtonSelect | kButtonSample))
ui_change_state(&ui_state_count);
return;
}
uint32_t timestamp = millis();
uint16_t lastCount = config_get_count();
static uint32_t refreshTime = 0;
if (timestamp > gUiSleepTime) {
throbber_init();
display_clear();
gUiStateFunc = 0;
return;
}
if (button_short(kButtonSample)) {
config_set_count(lastCount + 1);
gUiSampleTime = timestamp;
throbber_set(gUiSampleTime);
gUiSleepTime = timestamp + kUiSleepTimeout;
}
if (button_long(kButtonSample) && lastCount)
config_set_count(lastCount - 1);
if (button_short(kButtonSelect))
(*gUiStateFunc)(kUIStateExit);
if (button_long(kButtonSelect)) {
ui_configure_menu();
(*gUiStateFunc)(kUIStateEnter);
}
if (lastCount != config_get_count())
(*gUiStateFunc)(kUIStateUpdate);
if ((timestamp - refreshTime) / 1000) {
(*gUiStateFunc)(kUIStateTimer);
refreshTime = timestamp;
}
throbber_update(timestamp);
}
void ui_change_state(void (*state)(enum UIStateEvent))
{
gUiSleepTime = millis() + kUiSleepTimeout;
gUiStateFunc = state;
display_clear();
(*gUiStateFunc)(kUIStateEnter);
}
void ui_state_count(enum UIStateEvent event)
{
switch (event) {
case kUIStateEnter:
ui_state_count(kUIStateUpdate);
display_write_string(1, " Cnt");
break;
case kUIStateUpdate:
display_write_number(0, config_get_count(), 0);
break;
case kUIStateTimer:
break;
case kUIStateExit:
ui_change_state(&ui_state_ounces);
break;
}
}
void ui_state_ounces(enum UIStateEvent event)
{
switch (event) {
case kUIStateEnter:
ui_state_ounces(kUIStateUpdate);
display_write_string(1, " oZ");
break;
case kUIStateUpdate:
display_write_number(0, config_get_count() * config_get_sample_size(), 0);
break;
case kUIStateTimer:
break;
case kUIStateExit:
ui_change_state(&ui_state_gallons);
break;
}
}
void ui_state_gallons(enum UIStateEvent event)
{
switch (event) {
case kUIStateEnter:
ui_state_gallons(kUIStateUpdate);
display_write_string(1, " GAL");
break;
case kUIStateUpdate:
display_write_number(0, (uint32_t)config_get_count() * config_get_sample_size() * 100 / 128, 2);
break;
case kUIStateTimer:
break;
case kUIStateExit:
ui_change_state(&ui_state_elapsed);
break;
}
}
void ui_state_elapsed(enum UIStateEvent event)
{
switch (event) {
case kUIStateEnter:
ui_state_elapsed(kUIStateUpdate);
display_write_string(1, "ELAP");
break;
case kUIStateUpdate:
case kUIStateTimer:
display_write_time(0, millis() - gUiSampleTime);
break;
case kUIStateExit:
ui_change_state(&ui_state_count);
break;
}
}
void ui_configure_menu(void)
{
struct MenuItem configMenu[] = {
{ "dISP", &ui_configure_brightness },
{ "SIZE", &ui_configure_size },
{ " Cnt", &ui_configure_count },
{ "EHit", &ui_menu_exit },
};
ui_menu("CFG", &configMenu[0], sizeof(configMenu) / sizeof(configMenu[0]));
}
void ui_menu(char title[5],struct MenuItem *menu, uint8_t size)
{
uint8_t cursor = 0;
uint8_t lastCursor = 1;
while(1) {
if (button_short(kButtonSelect)) {
++cursor;
if (cursor == size)
cursor = 0;
}
if (button_short(kButtonSample)) {
if (menu[cursor].menuFunc())
return;
lastCursor = cursor + 1;
}
if (cursor != lastCursor) {
display_clear();
display_write_string(1, title);
display_write_string(0, menu[cursor].label);
lastCursor = cursor;
}
}
}
uint8_t ui_menu_exit(void)
{
return 1;
}
uint8_t ui_configure_brightness(void)
{
uint8_t brightness = config_get_brightness();
uint8_t lastValue = brightness + 1;
while(1) {
if (button_short(kButtonSelect)) {
++brightness;
if (brightness > kDisplayMaxBrightness)
brightness = 0;
}
if (button_short(kButtonSample)) {
config_set_brightness(brightness);
return 0;
}
if (brightness != lastValue) {
display_clear();
display_write_string(1, "dISP");
display_write_number(0, brightness, 0);
lastValue = brightness;
display_set_brightness(brightness);
}
}
}
uint8_t ui_configure_size(void)
{
uint8_t size = config_get_sample_size();
uint8_t lastSize = size + 1;
while(1) {
if (button_short(kButtonSelect)) {
++size;
if (size > kUiMaxSampleSize)
size = 1;
}
if (button_short(kButtonSample)) {
config_set_sample_size(size);
return 0;
}
if (size != lastSize) {
display_clear();
display_write_string(1, "Size");
display_write_number(0, size, 0);
lastSize = size;
}
}
}
uint8_t ui_configure_count(void)
{
struct MenuItem clearMenu[] = {
{ "CnCL", &ui_menu_exit },
{ " CLr", &ui_clear_count }
};
ui_menu("Cnt", &clearMenu[0], sizeof(clearMenu) / sizeof(clearMenu[0]));
return 0;
}
uint8_t ui_clear_count(void)
{
config_set_count(0);
return 1;
}
void ui_display_test(void)
{
uint8_t allOn[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
display_write_raw(0, allOn);
while (!(button_press(kButtonSelect | kButtonSample))) {}
}