/
tui.c
executable file
·474 lines (408 loc) · 10.9 KB
/
tui.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "main.h"
#include "relay.h"
#include "adc.h"
#include "buttons.h"
#include "timer.h"
#include "lcd.h"
#include "lib.h"
#include "backlight.h"
#include "saver.h"
#include "tui.h"
#include "time.h"
#include "dallas.h"
uint8_t tui_mp_mods[4][TUI_MODS_MAXDEPTH];
static uint8_t tui_mp_modidx[4];
static uint8_t tui_force_draw;
static uint8_t tui_next_refresh;
static uint8_t tui_refresh_interval=TUI_DEFAULT_REFRESH_INTERVAL; // by default 1s
uint8_t tui_pollkey(void) {
uint8_t v = buttons_get();
mini_mainloop();
return v;
}
uint8_t tui_waitforkey(void) {
uint8_t v;
for(;;) {
v = tui_pollkey();
if (v) return v;
}
}
void tui_gen_menuheader(unsigned char* line, unsigned char* buf, PGM_P header) {
uint8_t y,z;
strcpy_P((char*)buf, header);
memset(line,'=',16);
line[16] = 0;
y = strlen((char*)buf);
z = (16 - y) / 2;
memcpy(&(line[z]),buf,y);
lcd_gotoxy(0,0);
lcd_puts(line);
}
int32_t tui_gen_menupart(unsigned char* line, unsigned char* buf, printval_func_t *printer, int32_t min, int32_t max, int32_t start, int32_t step, uint8_t delay) {
int32_t idx=start;
uint8_t z,y;
for (;;) {
uint8_t key;
y = printer(buf,idx);
memset(line,' ',16);
z = (16 - y) / 2;
memcpy(&(line[z]),buf,y);
lcd_gotoxy(0,1);
lcd_puts(line);
if (delay) timer_delay_ms(delay);
key = tui_waitforkey();
switch (key) {
case BUTTON_S1:
if ((idx+step) > max) idx = min;
else idx += step;
break;
case BUTTON_S2:
if ((idx-step) < min) idx = max;
else idx -= step;
break;
case BUTTON_BOTH:
return idx;
}
}
}
static PGM_P const * tui_pgm_menu_table;
static uint8_t tui_pgm_menupart_printer(unsigned char* buf, int32_t val) {
strcpy_P((char*)buf, (PGM_P)pgm_read_word(&(tui_pgm_menu_table[val])));
return strlen((char*)buf);
}
uint8_t tui_pgm_menupart(unsigned char* line, unsigned char* buf, PGM_P const menu_table[], uint8_t itemcnt, uint8_t start) {
tui_pgm_menu_table = menu_table;
return tui_gen_menupart(line,buf,tui_pgm_menupart_printer,0,itemcnt-1,start,1,50);
}
int32_t tui_gen_adjmenu(PGM_P header, printval_func_t *printer,int32_t min, int32_t max, int32_t start, int32_t step) {
unsigned char line[17];
unsigned char buf[17];
tui_gen_menuheader(line,buf,header);
return tui_gen_menupart(line,buf,printer,min,max,start,step,0);
}
uint8_t tui_gen_listmenu(PGM_P header, PGM_P const menu_table[], uint8_t itemcnt, uint8_t start) {
unsigned char line[17];
unsigned char buf[17];
tui_gen_menuheader(line,buf,header);
return tui_pgm_menupart(line,buf,menu_table,itemcnt,start);
}
static uint8_t tui_voltmenu_printer(unsigned char* buf, int32_t val) {
adc_print_dV(buf,(uint16_t)val);
return strlen((char*)buf);
}
static uint16_t tui_gen_voltmenu(PGM_P header, uint16_t start) {
return adc_from_dV(
tui_gen_adjmenu(header,tui_voltmenu_printer,0,1600,adc_to_dV(start),1)
);
}
static uint8_t tui_nummenu_printer(unsigned char* buf, int32_t val) {
return uint2str(buf,(uint16_t)val);
}
uint16_t tui_gen_nummenu(PGM_P header, uint16_t min, uint16_t max, uint16_t start) {
return (uint16_t)tui_gen_adjmenu(header,tui_nummenu_printer,min,max,start,1);
}
static void tui_gen_message_start(PGM_P l1) {
lcd_clear();
lcd_gotoxy((16 - strlen_P(l1))/2,0);
lcd_puts_P(l1);
}
static void tui_gen_message_end(void) {
timer_delay_ms(100);
tui_waitforkey();
}
void tui_gen_message(PGM_P l1, PGM_P l2) {
tui_gen_message_start(l1);
lcd_gotoxy((16 - strlen_P(l2))/2,1);
lcd_puts_P(l2);
tui_gen_message_end();
}
void tui_gen_message_m(PGM_P l1, const unsigned char* l2m) {
tui_gen_message_start(l1);
lcd_gotoxy((16 - strlen((const char*)l2m))/2,1);
lcd_puts(l2m);
tui_gen_message_end();
}
const unsigned char tui_q_s1[] PROGMEM = "NO";
const unsigned char tui_q_s2[] PROGMEM = "YES";
PGM_P const tui_q_table[] PROGMEM = {
(PGM_P)tui_q_s1,
(PGM_P)tui_q_s2,
};
uint8_t tui_are_you_sure(void) {
return tui_gen_listmenu(PSTR("ARE YOU SURE?"), tui_q_table, 2, 0);
}
//Generic Exit Menu Item
const unsigned char tui_exit_menu[] PROGMEM = "EXIT MENU";
const unsigned char tui_rm_s1[] PROGMEM = "OFF";
const unsigned char tui_rm_s2[] PROGMEM = "ON";
const unsigned char tui_rm_s3[] PROGMEM = "AUTO";
PGM_P const tui_rm_table[] PROGMEM = {
(PGM_P)tui_rm_s1,
(PGM_P)tui_rm_s2,
(PGM_P)tui_rm_s3,
};
static void tui_relaymenu(void) {
uint8_t sel;
sel = tui_gen_listmenu(PSTR("RELAY MODE:"), tui_rm_table, 3, relay_get_mode());
relay_set(sel);
}
const unsigned char tui_blsm_name[] PROGMEM = "DISPLAY CFG";
const unsigned char tui_blsm_s1[] PROGMEM = "BL BRIGHTNESS";
const unsigned char tui_blsm_s2[] PROGMEM = "BL DRV BRIGHT";
const unsigned char tui_blsm_s3[] PROGMEM = "BL TIMEOUT";
const unsigned char tui_blsm_s4[] PROGMEM = "LCD CONTRAST";
PGM_P const tui_blsm_table[] PROGMEM = { // BL Settings Menu
(PGM_P)tui_blsm_s1,
(PGM_P)tui_blsm_s2,
(PGM_P)tui_blsm_s3,
(PGM_P)tui_blsm_s4,
(PGM_P)tui_exit_menu
};
static uint8_t tui_contrast_set_util_printer(unsigned char* buf, int32_t val) {
backlight_set_contrast((uint8_t)val);
return uchar2str(buf,(uint8_t)val);
}
static void tui_contrast_set_util(void) {
tui_gen_adjmenu((PGM_P)tui_blsm_s4,tui_contrast_set_util_printer,CONTRAST_MIN,CONTRAST_MAX,backlight_get_contrast(),1);
}
static void tui_blsettingmenu(void) {
uint8_t sel = 0;
for(;;) {
sel = tui_gen_listmenu((PGM_P)tui_blsm_name, tui_blsm_table, 5, sel);
switch (sel) {
case 0: {
uint8_t v = tui_gen_nummenu((PGM_P)tui_blsm_s1, 0, 16, backlight_get());
backlight_set(v);
}
break;
case 1: {
uint8_t v = tui_gen_nummenu((PGM_P)tui_blsm_s2, 0, backlight_get(), backlight_get_dv());
backlight_set_dv(v);
}
break;
case 2: {
uint8_t v = tui_gen_nummenu((PGM_P)tui_blsm_s3, 1, 255, backlight_get_to());
backlight_set_to(v);
}
break;
case 3:
tui_contrast_set_util();
break;
default:
return;
}
}
}
const unsigned char tui_setclock_name[] PROGMEM = "SET CLOCK";
void tui_set_clock(void) {
struct mtm tm;
timer_get_time(&tm);
tm.sec = 0;
uint16_t year = tui_gen_nummenu(PSTR("YEAR"),TIME_EPOCH_YEAR,TIME_EPOCH_YEAR+130,tm.year+TIME_EPOCH_YEAR);
uint8_t month = tui_gen_nummenu(PSTR("MONTH"),1,12,tm.month);
year = year - TIME_EPOCH_YEAR;
if ((tm.month != month)||(tm.year != year)) { // Day count in the month possibly changed, cannot start from old day.
tm.day = 1;
}
tm.day = tui_gen_nummenu(PSTR("DAY"),1,month_days(year,month-1),tm.day);
tm.year = year;
tm.month = month;
tm.hour = tui_gen_nummenu(PSTR("HOURS"), 0, 23, tm.hour);
tm.min = tui_gen_nummenu(PSTR("MINUTES"),0, 59, tm.min);
timer_set_time(&tm);
}
const unsigned char tui_sm_name[] PROGMEM = "SETTINGS";
const unsigned char tui_sm_s1[] PROGMEM = "RLY VOLTTHRES";
const unsigned char tui_sm_s2[] PROGMEM = "RLY AUTO KEEPON";
// BL Settings menu (3)
const unsigned char tui_sm_s4[] PROGMEM = "TUI CONFIG";
// Set Clock (5)
const unsigned char tui_sm_s6[] PROGMEM = "SAVE SETTINGS";
const unsigned char tui_sm_s7[] PROGMEM = "LOAD SETTINGS";
#ifdef ALARMCLOCK
const unsigned char tui_sm_s8[] PROGMEM = "SET RELAY MODE";
#else
const unsigned char tui_sm_s8[] PROGMEM = "ALARM MENU";
#endif
const unsigned char tui_sm_s9[] PROGMEM = "ADC CALIBRATION";
// Exit Menu (9)
PGM_P const tui_sm_table[] PROGMEM = { // Settings Menu
(PGM_P)tui_sm_s1,
(PGM_P)tui_sm_s2,
(PGM_P)tui_blsm_name,
(PGM_P)tui_sm_s4,
(PGM_P)tui_setclock_name,
(PGM_P)tui_sm_s6,
(PGM_P)tui_sm_s7,
(PGM_P)tui_sm_s8,
(PGM_P)tui_sm_s9,
(PGM_P)tui_exit_menu
};
static void tui_settingsmenu(void) {
uint8_t sel = 0;
for(;;) {
sel = tui_gen_listmenu((PGM_P)tui_sm_name, tui_sm_table, 10, sel);
switch (sel) {
case 0: {
uint16_t v = tui_gen_voltmenu((PGM_P)tui_sm_s1, relay_get_autovoltage());
relay_set_autovoltage(v);
}
break;
case 1: {
uint8_t v = tui_gen_nummenu((PGM_P)tui_sm_s2, 1, 255, relay_get_keepon());
relay_set_keepon(v);
}
break;
case 2:
tui_blsettingmenu();
break;
case 3:
tui_config_menu();
break;
case 4:
tui_set_clock();
break;
case 5:
saver_save_settings();
tui_gen_message((PGM_P)tui_sm_name,PSTR("SAVED"));
return;
case 6:
saver_load_settings();
tui_gen_message((PGM_P)tui_sm_name,PSTR("LOADED"));
return;
case 7:
#ifndef ALARMCLOCK
tui_alarm_menu();
#else
tui_relaymenu();
#endif
break;
case 8:
tui_adc_calibrate();
break;
default:
return;
}
}
}
#ifdef ALARMCLOCK
const unsigned char tui_mm_s1[] PROGMEM = "ALARM MENU";
#else
const unsigned char tui_mm_s1[] PROGMEM = "SET RELAY MODE";
#endif
// Settings Menu (2)
const unsigned char tui_mm_s3[] PROGMEM = "OTHERS";
// Exit Menu (4)
PGM_P const tui_mm_table[] PROGMEM = {
(PGM_P)tui_mm_s1,
(PGM_P)tui_sm_name,
(PGM_P)tui_mm_s3,
(PGM_P)tui_exit_menu
};
static void tui_mainmenu(void) {
uint8_t sel=0;
for (;;) {
sel = tui_gen_listmenu(PSTR("MAIN MENU"), tui_mm_table, 4, sel);
switch (sel) {
case 0:
#ifdef ALARMCLOCK
tui_alarm_menu();
#else
tui_relaymenu();
#endif
break;
case 1:
tui_settingsmenu();
break;
case 2:
tui_othermenu();
break;
default:
return;
}
}
}
static void tui_draw_mainpage(uint8_t forced) {
uint8_t d;
unsigned char line[17];
unsigned char buf[16];
line[16]=0;
if (!forced) {
static uint8_t drawcnt=0;
drawcnt++;
if ((drawcnt&1)==0) {
uint8_t i;
for (i=0;i<4;i++) {
uint8_t c = tui_mp_modidx[i] + 1;
if (c >= TUI_MODS_MAXDEPTH) c=0;
if (tui_mp_mods[i][c] == 255) c=0;
tui_mp_modidx[i] = c;
}
}
}
// Line 0
memset(line,' ',16);
d = tui_run_mod(tui_mp_mods[0][tui_mp_modidx[0]],buf,10);
memcpy(line,buf,d);
d = tui_run_mod(tui_mp_mods[1][tui_mp_modidx[1]],buf,15-d);
memcpy(&(line[16-d]),buf,d);
lcd_gotoxy(0,0);
lcd_puts(line);
// Line 1
memset(line,' ',16);
d = tui_run_mod(tui_mp_mods[2][tui_mp_modidx[2]],buf,10);
memcpy(line,buf,d);
d = tui_run_mod(tui_mp_mods[3][tui_mp_modidx[3]],buf,15-d);
memcpy(&(line[16-d]),buf,d);
lcd_gotoxy(0,1);
lcd_puts(line);
tui_force_draw = 0;
if (!forced) {
tui_refresh_interval = tui_update_refresh_interval();
tui_next_refresh = timer_get_5hz_cnt()+tui_refresh_interval;
}
}
void tui_init(void) {
uint8_t i;
for(i=0;i<4;i++) {
uint8_t n;
for(n=1;n<TUI_MODS_MAXDEPTH;n++) {
tui_mp_mods[i][n] = 255;
}
}
tui_mp_mods[0][0] = 0; // Main Bat
#ifdef ALARMCLOCK
tui_mp_mods[1][0] = 3; // Clock
tui_mp_mods[2][0] = 10; // Date
tui_mp_mods[3][0] = 2; // Relay State
#else
tui_mp_mods[1][0] = 6; // Temp 0
tui_mp_mods[2][0] = 1; // Sec Bat
tui_mp_mods[3][0] = 2; // Relay State
#endif
tui_draw_mainpage(0);
}
void tui_run(void) {
uint8_t k = buttons_get();
if (k & BUTTON_S2) {
tui_mainmenu();
tui_draw_mainpage(0);
return;
} else {
if (tui_force_draw) {
tui_draw_mainpage(tui_force_draw);
return;
} else {
if (timer_get_5hzp()) {
signed char update = (signed char)(timer_get_5hz_cnt() - tui_next_refresh);
if (update>=0) {
tui_draw_mainpage(0);
return;
}
}
}
}
}
void tui_activate(void) {
tui_force_draw=1;
}