-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes.h
308 lines (257 loc) · 6.97 KB
/
frames.h
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
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef TIMER_FRAMES_H
#define TIMER_FRAMES_H
#include "config.h"
#include "lcd.h"
#define TIMER_DISABLE 0
#define TIMER_ENABLE 1
#define TIMER_CONTROL_SCHEDULE 0
#define TIMER_SET_SCHEDULE 1
uint8_t _alarm1_called = 0,
_alarm2_called = 0;
uint8_t alarm1_called() {
if (_alarm1_called) {
_alarm1_called = 0;
return 1;
}
return 0;
}
uint8_t alarm2_called() {
if (_alarm2_called) {
_alarm2_called = 0;
return 1;
}
return 0;
}
/* frame 1 stuff */
uint8_t frame1_alarm_state = 0,
frame1_state = 0;
/* frame 2 stuff */
alarm_t user_timer;
uint8_t frame2_state = 0;
unsigned long frame2_blink_time = 0;
uint8_t frame2_blink_state = 0;
uint8_t frame3_state = 0,
frame3_pump_runtime = 0;
void frame0_draw_alarm1() {
lcd_clear_row(ROWS - 1, 8);
lcd_print_two_digit(hour(current_time));
lcd.print(':');
lcd_print_two_digit(minute(current_time));
lcd.print(':');
lcd_print_two_digit(second(current_time));
}
/* gets called as soon as the alarm occurs, no matter
which frame is active */
void handle_alarm1() {
current_time = now();
_alarm1_called = 1;
}
/* gets called as soon as the alarm occurs, no matter
which frame is active */
void handle_alarm2() {
_alarm2_called = 1;
}
void frame0_draw_alarm2() {
lcd_clear_row(ROWS - 1, 8, 8);
lcd.print(" PMP: ");
lcd.print(get_pump_count());
}
void frame0_draw() {
lcd_clear_row(0);
lcd.print("WATER PUMPER 1.0");
lcd_clear_row(1);
frame0_draw_alarm1();
frame0_draw_alarm2();
}
void frame0() {
if (frame_needs_redraw()) {
frame0_draw();
}
if (alarm1_called()) {
frame0_draw_alarm1();
}
if (alarm2_called()) {
/* this would be the place we we'd start the pump */
pump_on();
frame0_draw_alarm2();
}
int8_t btn = button_handle();
if (btn == 0) {
switch_frame(FRAME_3);
} else if (btn == 1) {
switch_frame(FRAME_1);
} else if (btn == 2) {
switch_frame(FRAME_2);
}
}
void frame1_draw() {
lcd_clear_row(0);
if (is_alarm_active(ALARM_2)) {
lcd.print("Timer enabled.");
frame1_alarm_state = TIMER_DISABLE;
} else {
lcd.print("Timer disabled.");
frame1_alarm_state = TIMER_ENABLE;
}
lcd_clear_row(1);
frame1_state = TIMER_CONTROL_SCHEDULE;
frame2_blink_time = 0;
frame2_blink_state = 0;
}
void frame1() {
if (frame_needs_redraw()) {
frame1_draw();
}
switch (frame1_state) {
case TIMER_CONTROL_SCHEDULE:
{
char* types[] = {
"Disable timer.",
"Enable timer."
};
frame_blink_text(&frame2_blink_time, &frame2_blink_state, types[frame1_alarm_state], 0, 1);
frame_button_handle(&frame1_alarm_state, TIMER_DISABLE, TIMER_ENABLE, 0, 1, &frame1_state, TIMER_SET_SCHEDULE);
}
break;
case TIMER_SET_SCHEDULE:
RTC.alarmInterrupt(ALARM_2, frame1_alarm_state);
switch_frame(FRAME_0);
break;
}
}
void frame2_draw() {
lcd_clear_row(0);
lcd.print("Set timer");
lcd_clear_row(1);
user_timer = alarm2_def;
frame2_state = TIMER_STATE_SCHEDULE;
frame2_blink_time = 0;
frame2_blink_state = 0;
}
void frame2() {
if (frame_needs_redraw()) {
frame2_draw();
}
if ((millis() - get_last_button_activity()) > 10000) {
switch_frame(0);
return;
}
/* 0 => select hour, 1 => select minute, 2 => select second, 3 => cancel */
switch (frame2_state) {
case TIMER_STATE_SCHEDULE:
{
frame_blink_text(&frame2_blink_time, &frame2_blink_state, alarm2s(user_timer.type), 0, 1);
#define TYPES_SIZE 3
uint8_t types[TYPES_SIZE] = {
(uint8_t)ALM2_EVERY_MINUTE,
(uint8_t)ALM2_MATCH_MINUTES,
(uint8_t)ALM2_MATCH_HOURS
};
if (frame_button_handle_enum((uint8_t*)&user_timer.type, types, TYPES_SIZE, 0, 1, &frame2_state, TIMER_STATE_HOUR)) {
switch (user_timer.type) {
case ALM2_MATCH_HOURS:
{
lcd_print_two_digit(user_timer.hours);
lcd.print(':');
lcd_print_two_digit(user_timer.minutes);
lcd.print(':');
lcd_print_two_digit(user_timer.seconds);
}
break;
case ALM2_EVERY_MINUTE:
{
frame2_state = TIMER_STATE_CONFIRM;
lcd_clear_row(0);
lcd.print("Confirm time");
}
break;
case ALM2_MATCH_MINUTES:
{
lcd.print(" ");
lcd_print_two_digit(user_timer.minutes);
lcd.print(':');
lcd_print_two_digit(user_timer.seconds);
frame2_state = TIMER_STATE_MINUTE;
}
break;
}
}
}
break;
case TIMER_STATE_HOUR:
frame_blink_text(&frame2_blink_time, &frame2_blink_state, user_timer.hours, 0, 1);
frame_button_handle(&user_timer.hours, 0, 23, 0, 1, &frame2_state, TIMER_STATE_MINUTE);
break;
case TIMER_STATE_MINUTE:
frame_blink_text(&frame2_blink_time, &frame2_blink_state, user_timer.minutes, 3, 1);
frame_button_handle(&user_timer.minutes, 0, 59, 3, 1, &frame2_state, TIMER_STATE_SECOND);
break;
case TIMER_STATE_SECOND:
frame_blink_text(&frame2_blink_time, &frame2_blink_state, user_timer.seconds, 6, 1);
if (frame_button_handle(&user_timer.seconds, 0, 59, 6, 1, &frame2_state, TIMER_STATE_CONFIRM)) {
lcd_clear_row(0);
lcd.print("Confirm time");
}
break;
case TIMER_STATE_CONFIRM:
{
int8_t btn = button_handle();
// CANCEL
if (btn == 0 || btn == 1) {
switch_frame(FRAME_0);
return;
} else if (btn == 2) {
// CONFIRM
lcd_clear_row(1);
lcd.print("Time set.");
frame2_state = TIMER_STATE_SET;
setAlarm(ALARM_2, &user_timer);
alarm2_def = user_timer;
}
}
break;
case TIMER_STATE_SET:
{
int8_t btn = button_handle();
if (btn == 2) {
switch_frame(FRAME_0);
}
}
break;
}
}
void frame3_draw() {
frame3_state = PUMP_SET_TIME;
frame3_pump_runtime = (uint8_t)(get_pump_runtime() / 1000);
lcd_clear_row(0);
lcd.print("Set pump runtime");
lcd_clear_row(1);
lcd_print_two_digit(frame3_pump_runtime);
lcd.print(" seconds");
}
void frame3() {
if (frame_needs_redraw()) {
frame3_draw();
}
switch (frame3_state) {
case PUMP_SET_TIME:
frame_blink_text(&frame2_blink_time, &frame2_blink_state, frame3_pump_runtime, 0, 1);
frame_button_handle(&frame3_pump_runtime, (uint8_t)(WATER_MIN_RUNTIME/1000), (uint8_t)(WATER_MAX_RUNTIME/1000), 0, 1, &frame3_state, PUMP_TIME_CONFIRM);
break;
case PUMP_TIME_CONFIRM:
{
int8_t btn = button_handle();
// CANCEL
if (btn == 0 || btn == 1) {
switch_frame(FRAME_0);
return;
} else if (btn == 2) {
// CONFIRM
set_pump_runtime((uint16_t)(frame3_pump_runtime * 1000));
switch_frame(0);
}
}
break;
}
}
#endif