-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyMenu.cpp
703 lines (621 loc) · 15.5 KB
/
PropertyMenu.cpp
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
PropertyMenu.cpp - Arduino lcd menu with property editing library
Written by Yuri Valentini <yuroller [at] gmail.com>
Copyright (c) 2013 Yuri Valentini, All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "PropertyMenu.h"
const char SEL_LEFT = '[';
const char SEL_RIGHT = ']';
const char CHECKED = 'v';
const char UNCHECKED = ' ';
const char CHK_LEFT = '(';
const char CHK_RIGHT = ')';
const char SPACE = ' ';
const char CURSOR = '>';
const char REPLY_YES = 'Y';
const char REPLY_NO = 'N';
const char PREV_MENU[] = "..";
static void pad00Print(LCD *lcd, uint8_t n)
{
assert(lcd != NULL);
assert(n < 100);
if (n < 10) {
lcd->print('0');
}
lcd->print(n);
}
static void padMulti0Print(LCD *lcd, uint16_t n, uint8_t width)
{
assert(lcd != NULL);
uint16_t m = 10;
for (uint8_t i = 1; i < width; ++i) {
if (n < m) {
lcd->print('0');
}
m *= 10;
}
assert(n < m);
lcd->print(n);
}
template<typename T>
static void clipValue(T *n, T low, T high)
{
assert(n != NULL);
assert(low < high);
if (*n < low) {
*n = low;
} else if (*n > high) {
*n = high;
}
}
template<typename T>
static void wrapDecrease(T *n, T low, T high)
{
assert(n != NULL);
assert(low < high);
if (*n == low) {
*n = high;
} else {
(*n)--;
}
}
template<typename T>
static void wrapIncrease(T *n, T low, T high)
{
assert(n != NULL);
assert(low < high);
if (*n == high) {
*n = low;
} else {
(*n)++;
}
}
///////////////////////////////////////////////////////////////////////////
// Screen
///////////////////////////////////////////////////////////////////////////
Screen::Screen(LCD *lcd, uint8_t cols, uint8_t rows)
: _lcd(lcd),
_cols(cols),
_rows(rows)
{
assert(_lcd != NULL);
assert(_cols > 0);
assert(_rows > 0);
_lcd->begin(cols, rows);
}
///////////////////////////////////////////////////////////////////////////
// Property
///////////////////////////////////////////////////////////////////////////
Property::Property(const __FlashStringHelper *name, uint8_t maxFocusParts)
: _name(name),
_focusPart(0),
_maxFocusParts(maxFocusParts)
{
assert(name != NULL);
assert(maxFocusParts > 0);
}
Property::~Property()
{
}
void Property::nextFocusPart()
{
_focusPart++;
if (_focusPart > _maxFocusParts) {
_focusPart = 0;
onExitEdit();
}
}
void Property::paintLabel(LCD *lcd) const
{
assert(lcd != NULL);
lcd->print(_name);
}
void Property::enterEdit()
{
onEnterEdit();
_focusPart = 1;
}
void Property::onEnterEdit()
{
}
void Property::onExitEdit()
{
}
///////////////////////////////////////////////////////////////////////////
// PropertyTime
///////////////////////////////////////////////////////////////////////////
PropertyTime::PropertyTime(const __FlashStringHelper *name, PropertyTime::Time *var)
: Property(name, 2),
_var(var)
{
assert(var != NULL);
if (_var->hour > 23) {
_var->hour = 23;
}
if (_var->mins > 59) {
_var->hour = 59;
}
}
void PropertyTime::paintEdit(LCD *lcd) const
{
assert(lcd != NULL);
assert(getFocusPart() <= 2);
uint8_t focusPart = getFocusPart();
lcd->print(focusPart == 1 ? SEL_LEFT : SPACE );
pad00Print(lcd, _var->hour);
switch (focusPart) {
case 1:
lcd->print(SEL_RIGHT);
break;
case 2:
lcd->print(SEL_LEFT);
break;
default:
lcd->print(':');
break;
}
pad00Print(lcd, _var->mins);
lcd->print(focusPart == 2 ? SEL_RIGHT : SPACE);
}
bool PropertyTime::processEditInput(ButtonPress button)
{
uint8_t focusPart = getFocusPart();
assert(0 < focusPart && focusPart <= 2);
if (button == BUTTON_PRESS_DOWN) {
if (focusPart == 1) {
wrapDecrease<uint8_t>(&_var->hour, 0, 23);
} else {
wrapDecrease<uint8_t>(&_var->mins, 0, 59);
}
return true;
} else if (button == BUTTON_PRESS_UP) {
if (focusPart == 1) {
wrapIncrease<uint8_t>(&_var->hour, 0, 23);
} else {
wrapIncrease<uint8_t>(&_var->mins, 0, 59);
}
return true;
} else if (button == BUTTON_PRESS_ENTER) {
nextFocusPart();
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////
// PropertyDate
///////////////////////////////////////////////////////////////////////////
PropertyDate::PropertyDate(const __FlashStringHelper *name, PropertyDate::Date *var)
: Property(name, 3),
_var(var)
{
assert(var != NULL);
clipValue<uint8_t>(&_var->day, 1, 31);
clipValue<uint8_t>(&_var->month, 1, 12);
if (_var->year2000 > 99) {
_var->year2000 = 99;
}
}
void PropertyDate::onExitEdit()
{
// TODO: adjust date to a correct value
}
void PropertyDate::paintEdit(LCD *lcd) const
{
assert(lcd != NULL);
assert(getFocusPart() <= 3);
uint8_t focusPart = getFocusPart();
lcd->print(focusPart == 1 ? SEL_LEFT : SPACE );
pad00Print(lcd, _var->day);
switch (focusPart) {
case 1:
lcd->print(SEL_RIGHT);
break;
case 2:
lcd->print(SEL_LEFT);
break;
default:
lcd->print('/');
break;
}
pad00Print(lcd, _var->month);
switch (focusPart) {
case 2:
lcd->print(SEL_RIGHT);
break;
case 3:
lcd->print(SEL_LEFT);
break;
default:
lcd->print('/');
break;
}
lcd->print("20");
pad00Print(lcd, _var->year2000);
lcd->print(focusPart == 3 ? SEL_RIGHT : SPACE);
}
bool PropertyDate::processEditInput(ButtonPress button)
{
uint8_t focusPart = getFocusPart();
assert(0 < focusPart && focusPart <= 3);
if (button == BUTTON_PRESS_DOWN) {
if (focusPart == 1) {
wrapDecrease<uint8_t>(&_var->day, 1, 31);
} else if (focusPart == 2) {
wrapDecrease<uint8_t>(&_var->month, 1, 12);
} else {
wrapDecrease<uint8_t>(&_var->year2000, 0, 99);
}
return true;
} else if (button == BUTTON_PRESS_UP) {
if (focusPart == 1) {
wrapIncrease<uint8_t>(&_var->day, 1, 31);
} else if (focusPart == 2) {
wrapIncrease<uint8_t>(&_var->month, 1, 12);
} else {
wrapIncrease<uint8_t>(&_var->year2000, 0, 99);
}
return true;
} else if (button == BUTTON_PRESS_ENTER) {
nextFocusPart();
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////
// PropertyU16
///////////////////////////////////////////////////////////////////////////
struct LimitWidth {
uint16_t moreThan;
uint8_t width;
};
const LimitWidth limitWidth[] = {
{ 9999, 5 },
{ 999, 4 },
{ 99, 3 },
{ 9, 2 },
{ 0, 1 }
};
PropertyU16::PropertyU16(const __FlashStringHelper *name, uint16_t *var, uint16_t limitMin, uint16_t limitMax)
: Property(name, 1),
_var(var),
_limitMin(limitMin),
_limitMax(limitMax)
{
assert(var != NULL);
assert(limitMax > limitMin);
for (const LimitWidth *lw = &limitWidth[0]; ; ++lw) {
if (lw->moreThan < limitMax) {
_displayWidth = lw->width;
break;
}
}
clipValue(_var, _limitMin, _limitMax);
}
void PropertyU16::paintEdit(LCD *lcd) const
{
assert(lcd != NULL);
assert(getFocusPart() <= 1);
uint8_t focusPart = getFocusPart();
lcd->print(focusPart == 1 ? SEL_LEFT : SPACE);
padMulti0Print(lcd, *_var, _displayWidth);
lcd->print(focusPart == 1 ? SEL_RIGHT : SPACE);
}
bool PropertyU16::processEditInput(ButtonPress button)
{
assert(getFocusPart() == 1);
if (button == BUTTON_PRESS_DOWN) {
wrapDecrease(_var, _limitMin, _limitMax);
return true;
} else if (button == BUTTON_PRESS_UP) {
wrapIncrease(_var, _limitMin, _limitMax);
return true;
} else if (button == BUTTON_PRESS_ENTER) {
nextFocusPart();
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////
// PropertyBool
///////////////////////////////////////////////////////////////////////////
PropertyBool::PropertyBool(const __FlashStringHelper *name, bool *var)
: Property(name, 1),
_var(var)
{
assert(var != NULL);
}
void PropertyBool::paintEdit(LCD *lcd) const
{
assert(lcd != NULL);
assert(getFocusPart() <= 1);
uint8_t focusPart = getFocusPart();
lcd->print(focusPart == 1 ? SEL_LEFT : SPACE);
lcd->print(CHK_LEFT);
lcd->print(*_var ? CHECKED : UNCHECKED);
lcd->print(CHK_RIGHT);
lcd->print(focusPart == 1 ? SEL_RIGHT : SPACE);
}
bool PropertyBool::processEditInput(ButtonPress button)
{
assert(getFocusPart() == 1);
if (button == BUTTON_PRESS_DOWN || button == BUTTON_PRESS_UP) {
*_var = !*_var;
return true;
} else if (button == BUTTON_PRESS_ENTER) {
nextFocusPart();
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////
// PropertyAction
///////////////////////////////////////////////////////////////////////////
PropertyAction::PropertyAction(const __FlashStringHelper *name, Callback callback)
: Property(name, 1),
_callback(callback),
_confirm(false)
{
assert(callback != NULL);
}
void PropertyAction::paintEdit(LCD *lcd) const
{
assert(lcd != NULL);
assert(getFocusPart() <= 1);
uint8_t focusPart = getFocusPart();
if (focusPart == 1) {
lcd->print(SEL_LEFT);
lcd->print(_confirm ? REPLY_YES : REPLY_NO);
lcd->print(SEL_RIGHT);
} else {
lcd->print(SPACE);
lcd->print(SPACE);
lcd->print(SPACE);
}
}
bool PropertyAction::processEditInput(ButtonPress button)
{
assert(getFocusPart() == 1);
if (button == BUTTON_PRESS_DOWN || button == BUTTON_PRESS_UP) {
_confirm = !_confirm;
return true;
} else if (button == BUTTON_PRESS_ENTER) {
if (_confirm) {
_callback();
}
nextFocusPart();
return true;
}
return false;
}
void PropertyAction::onEnterEdit()
{
_confirm = false;
}
///////////////////////////////////////////////////////////////////////////
// Page
///////////////////////////////////////////////////////////////////////////
Page::Page()
{
}
Page::~Page()
{
}
void Page::reset()
{
}
void Page::paint(Screen *screen) const
{
assert(screen != NULL);
screen->getLcd()->clear();
}
uint8_t Page::buttonInput(ButtonPress /*button*/, Screen * /*screen*/)
{
return INVALID_LINE;
}
///////////////////////////////////////////////////////////////////////////
// ScrollablePage
///////////////////////////////////////////////////////////////////////////
ScrollablePage::ScrollablePage()
: _maxLines(0)
{
reset();
}
void ScrollablePage::reset()
{
_topIndex = 0;
_cursorRow = 0;
}
void ScrollablePage::setMaxLines(uint8_t maxLines)
{
assert(_maxLines == 0);
_maxLines = maxLines;
}
void ScrollablePage::paint(Screen *screen) const
{
assert(screen != NULL);
LCD *lcd = screen->getLcd();
lcd->clear();
for (uint8_t i = 0; i < screen->getRows(); ++i) {
uint8_t idx = _topIndex + i;
if (idx == 0) {
lcd->setCursor(COL_CONTENTS, i);
lcd->print(PREV_MENU);
} else {
uint8_t line = idx - 1;
if (line < _maxLines) {
paintLine(line, i, screen);
}
}
}
paintCursor(screen);
}
uint8_t ScrollablePage::buttonInput(ButtonPress button, Screen *screen)
{
assert(screen != NULL);
uint8_t idx = getCurIdx();
switch (button) {
case BUTTON_PRESS_ENTER:
if (idx == 0) {
return 0;
} else {
focusLine(idx - 1);
paintLine(idx - 1, _cursorRow, screen);
}
break;
case BUTTON_PRESS_DOWN:
if (idx < _maxLines) {
if (_cursorRow < screen->getRows() - 1) {
_cursorRow++;
paintCursor(screen);
} else {
_topIndex++;
paint(screen);
}
}
break;
case BUTTON_PRESS_UP:
if (getCurIdx() > 0) {
if (_cursorRow == 0) {
_topIndex--;
paint(screen);
} else {
_cursorRow--;
paintCursor(screen);
}
}
break;
default:
break;
}
return INVALID_LINE;
}
void ScrollablePage::paintCursor(Screen *screen) const
{
assert(screen != NULL);
LCD *lcd = screen->getLcd();
for (uint8_t i = 0; i < screen->getRows(); ++i) {
lcd->setCursor(COL_CURSOR, i);
lcd->print(_cursorRow == i ? CURSOR : SPACE);
}
}
void ScrollablePage::focusLine(uint8_t /*line*/)
{
}
///////////////////////////////////////////////////////////////////////////
// PropertyPage
///////////////////////////////////////////////////////////////////////////
PropertyPage::PropertyPage(Property **propertiesAry)
: _propertiesAry(propertiesAry),
_maxPropNameLen(0)
{
assert(propertiesAry != NULL);
assert(propertiesAry[0] != NULL);
reset();
size_t maxLen = strlen(PREV_MENU);
uint8_t i = 0;
const Property *p = _propertiesAry[0];
while (p != NULL) {
size_t l = strlen(reinterpret_cast<const char *>(p->getName()));
if (l > maxLen) {
maxLen = l;
}
i++;
p = _propertiesAry[i];
}
_maxPropNameLen = static_cast<uint8_t>(maxLen);
setMaxLines(i);
}
void PropertyPage::reset()
{
_focusLine = INVALID_LINE;
}
uint8_t PropertyPage::buttonInput(ButtonPress button, Screen *screen)
{
assert(screen != NULL);
if (_focusLine == INVALID_LINE) {
return ScrollablePage::buttonInput(button, screen);
}
Property *p = _propertiesAry[_focusLine];
LCD *lcd = screen->getLcd();
assert(p->getFocusPart() != 0);
if (p->processEditInput(button)) {
screen->getLcd()->setCursor(_maxPropNameLen + 2, getCursorRow());
p->paintEdit(lcd);
}
bool hasFocus = p->getFocusPart() != 0;
if (!hasFocus) {
_focusLine = INVALID_LINE;
}
return INVALID_LINE;
}
void PropertyPage::paintLine(uint8_t line, uint8_t row, Screen *screen) const
{
assert(screen != NULL);
Property *p = _propertiesAry[line];
if (p != NULL) {
LCD *lcd = screen->getLcd();
lcd->setCursor(COL_CONTENTS, row);
p->paintLabel(lcd);
lcd->setCursor(_maxPropNameLen + 2, row);
p->paintEdit(lcd);
}
}
void PropertyPage::focusLine(uint8_t line)
{
Property *p = _propertiesAry[line];
p->enterEdit();
_focusLine = line;
}
///////////////////////////////////////////////////////////////////////////
// MenuItem
///////////////////////////////////////////////////////////////////////////
MenuItem::MenuItem(const __FlashStringHelper *name, Page *page)
: _name(name),
_page(page)
{
assert(name != NULL);
assert(page != NULL);
}
///////////////////////////////////////////////////////////////////////////
// MenuItemPage
///////////////////////////////////////////////////////////////////////////
MenuItemPage::MenuItemPage(MenuItem *menuItemAry[])
: _menuItemAry(menuItemAry)
{
assert(menuItemAry != NULL);
uint8_t i = 0;
const MenuItem *p = _menuItemAry[0];
while (p != NULL) {
i++;
p = _menuItemAry[i];
}
setMaxLines(i);
}
uint8_t MenuItemPage::buttonInput(ButtonPress button, Screen *screen)
{
if (button == BUTTON_PRESS_ENTER) {
return getCurIdx();
}
return ScrollablePage::buttonInput(button, screen);
}
void MenuItemPage::paintLine(uint8_t line, uint8_t row, Screen *screen) const
{
assert(screen != NULL);
MenuItem *p = _menuItemAry[line];
if (p != NULL) {
LCD *lcd = screen->getLcd();
lcd->setCursor(COL_CONTENTS, row);
lcd->print(p->getName());
}
}