forked from mst-solar-car/LiquidCrystalMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiquidCrystalMenu.cpp
433 lines (337 loc) · 8.78 KB
/
LiquidCrystalMenu.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
#include "LiquidCrystalMenu.h"
// Create glyphs
byte menuArrow[8] = {0b00000,0b00100,0b00010,0b11111,0b00010,0b00100,0b00000,0b00000};
/**
* Constructors
*/
LiquidCrystalMenu::LiquidCrystalMenu(
const uint8_t &rs, const uint8_t &rw, const uint8_t &en, const uint8_t &d4,
const uint8_t &d5, const uint8_t &d6, const uint8_t &d7
) {
// Create the LCD instance
this->lcd = new LiquidCrystal(rs, rw, en, d4, d5, d6, d7);
this->init();
}
LiquidCrystalMenu::LiquidCrystalMenu(
const uint8_t &rs, const uint8_t &en, const uint8_t &d4,
const uint8_t &d5, const uint8_t &d6, const uint8_t &d7
) {
this->lcd = new LiquidCrystal(rs, en, d4, d5, d6, d7);
this->init();
}
LiquidCrystalMenu::LiquidCrystalMenu(
const uint8_t &rs, const uint8_t &en, const uint8_t &d0,
const uint8_t &d1, const uint8_t &d2, const uint8_t &d3,
const uint8_t &d4, const uint8_t &d5, const uint8_t &d6,
const uint8_t &d7
) {
this->lcd = new LiquidCrystal(rs, en, d0, d1, d2, d3, d4, d5, d6, d7);
this->init();
}
LiquidCrystalMenu::LiquidCrystalMenu(
const uint8_t &rs, const uint8_t &rw, const uint8_t &en,
const uint8_t &d0, const uint8_t &d1, const uint8_t &d2,
const uint8_t &d3, const uint8_t &d4, const uint8_t &d5,
const uint8_t &d6, const uint8_t &d7
) {
this->lcd = new LiquidCrystal(rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7);
this->init();
}
/**
* Initializes the library (called by constructors)
*/
void LiquidCrystalMenu::init() {
// Register Glyphs with the LCD
this->lcd->createChar(MENU_ARROW_GLYPH, menuArrow);
// Create root node
this->root = newMenuNode("$", nullptr, nullptr);
this->menu = this->root;
}
/**
* Destructor
*/
LiquidCrystalMenu::~LiquidCrystalMenu() {
delete this->lcd;
this->lcd = nullptr;
// Delete everything
deleteMenuNode(this->root);
this->root = nullptr;
this->menu = nullptr;
}
/**
* Initialize and begin the library
*/
void LiquidCrystalMenu::begin(const uint8_t &cols, const uint8_t &rows) {
this->rows = rows;
this->cols = cols;
// Initialize and clear the LCD
this->lcd->begin(cols, rows);
this->lcd->clear();
this->draw();
}
/**
* Custom Splash Screen for the display
*/
void LiquidCrystalMenu::splash(const String contents[], const uint8_t &delayMs /*= 4000*/) {
this->lcd->clear();
// Display number of rows
for (int i = 0; i < this->rows; i++) {
this->lcd->setCursor(0, i);
this->lcd->print(contents[i]);
}
// Delay and then clear again
delay(delayMs);
this->lcd->clear();
}
/**
* A a menu to a specific menu
*/
MenuID LiquidCrystalMenu::addMenu(const MenuID &parent, const char *title) {
// Find parent
MenuNode *parentPtr = this->findNodeWithAddr(this->root, parent);
// Create new node
MenuNode *toAdd = newMenuNode(title, nullptr, nullptr);
// Add the menu node
this->addNode(parentPtr, toAdd);
// Redraw
this->draw();
return (MenuID)toAdd;
}
/**
* Add a menu to the root menu
*/
MenuID LiquidCrystalMenu::addMenu(const char *title) {
return this->addMenu((MenuID)(this->root), title);
}
/**
* Adds a value to the menu
*/
void LiquidCrystalMenu::addValue(const MenuID &parent, const char *title, String (*callback)(void), String *value /*= nullptr*/) {
// Find parent
MenuNode *parentPtr = this->findNodeWithAddr(this->root, parent);
// Create new node
MenuNode *toAdd = newMenuNode(title, callback, value);
// Add the node
this->addNode(parentPtr, toAdd);
this->draw();
}
/**
* Adds a value to a specific menu
*/
void LiquidCrystalMenu::addValue(const MenuID &parent, const char *title, String *value) {
return this->addValue(parent, title, nullptr, value);
}
/**
* Add a value to the root
*/
void LiquidCrystalMenu::addValue(const char *title, String (*callback)(void)) {
return this->addValue((MenuID)(this->root), title, callback);
}
/**
* Add a value to the root
*/
void LiquidCrystalMenu::addValue(const char *title, String *value) {
return this->addValue((MenuID)(this->root), title, value);
}
/**
* Generic add a node somewhere in the menu system
*/
void LiquidCrystalMenu::addNode(MenuNode *root, MenuNode *toAdd) {
MenuNode *tmp = nullptr;
toAdd->parent = root; // Give the new element a parent
// Handle adding something to the root menu
if (root == this->root) {
tmp = getLastNodeInList(root);
tmp->next = toAdd;
toAdd->previous = tmp;
return;
}
// Handle not adding to the root menu
if (root->submenu == nullptr) {
// First in submenu
root->submenu = toAdd;
}
else {
// Add at the end of the submenu
tmp = getLastNodeInList(root->submenu);
tmp->next = toAdd;
toAdd->previous = tmp;
}
}
/**
* Refresh the value currently being displayed
*/
void LiquidCrystalMenu::refreshValues() {
static unsigned long last_refresh = 0;
if (millis() - last_refresh > LCDMENU_REFRESH_INTERVAL) {
if (this->menu->valueFn != nullptr || this->menu->value != nullptr) {
this->draw();
}
last_refresh = millis();
}
}
/**
* Renders the active menu
*/
void LiquidCrystalMenu::draw() {
this->lcd->clear();
this->lcd->setCursor(0, 0);
// Just in case
if (this->menu == nullptr) {
this->menu = this->root;
}
// Show value if this is a value
if (this->menu->valueFn != nullptr || this->menu->value != nullptr) {
// Give section a title
this->lcd->print(this->menu->title);
this->lcd->print(":");
this->lcd->setCursor(0, 1);
String val;
if (this->menu->valueFn != nullptr) {
val = (this->menu->valueFn)();
}
else {
val = *(this->menu->value);
}
this->lcd->print(val);
return;
}
// Display the menu options
MenuNode *ptr = this->menu;
short row = 0;
// Do not show the $ROOT element
if (ptr == this->root) {
ptr = ptr->next;
// Force a change from the menu
if (ptr != nullptr) {
this->menu = ptr;
}
}
while (ptr != nullptr && row < this->rows) {
this->lcd->setCursor(0, row);
if (row == 0) {
this->lcd->write(MENU_ARROW_GLYPH);
}
this->lcd->print(ptr->title);
ptr = ptr->next;
row++;
}
}
/**
* Move Up
*/
void LiquidCrystalMenu::up() {
// Change to the previous menu item if there is one
if (this->menu->previous != nullptr) {
this->menu = this->menu->previous;
this->draw();
}
delay(LCDMENU_ACTION_DEBOUNCE);
}
/**
* Move Down
*/
void LiquidCrystalMenu::down() {
// Change to the next menu item if there is one
if (this->menu->next != nullptr) {
this->menu = this->menu->next;
this->draw();
}
delay(LCDMENU_ACTION_DEBOUNCE);
}
/**
* Move backwards up the navigation tree
*/
void LiquidCrystalMenu::back() {
// Change to the parent menu if there is one
if (this->menu->parent != nullptr) {
this->menu = this->menu->parent;
this->draw();
}
delay(LCDMENU_ACTION_DEBOUNCE);
}
/**
* Go into a submenu
*/
void LiquidCrystalMenu::select() {
// If no submenu then don't do anything
if (this->menu->submenu != nullptr) {
this->menu = this->menu->submenu;
this->draw();
}
delay(LCDMENU_ACTION_DEBOUNCE);
}
/**
* Finds a menu node with a certain memory address
*/
MenuNode* LiquidCrystalMenu::findNodeWithAddr(MenuNode *node, const MenuID &memAddr) {
MenuNode *tmp = nullptr;
// Check the node that is about to be searched
if ((MenuID)node == memAddr) {
return node;
}
// Check the next item
if (node->next != nullptr) {
tmp = this->findNodeWithAddr(node->next, memAddr);
if (tmp != nullptr) {
return tmp;
}
}
// Search submenus
if (node->submenu != nullptr) {
tmp = this->findNodeWithAddr(node->submenu, memAddr);
if (tmp != nullptr) {
return tmp;
}
}
return this->root; // Not found
}
/**
* Helper function for creating a new MenuNode and initializing values
*/
MenuNode* newMenuNode(const char *title, String (*fn)(void), String *val) {
MenuNode *node = new MenuNode;
node->title = title;
node->parent = nullptr;
node->submenu = nullptr;
node->previous = nullptr;
node->next = nullptr;
node->valueFn = nullptr;
node->value = nullptr;
if (fn != nullptr || val != nullptr) {
MenuNode* subNode = node->submenu = newMenuNode(title, nullptr, nullptr);
subNode->parent = node;
subNode->valueFn = fn;
subNode->value = val;
}
return node;
}
/**
* Destructor for a menu node
*/
void deleteMenuNode(MenuNode *node) {
if (node->next != nullptr) {
deleteMenuNode(node->next);
node->next = nullptr;
}
if (node->submenu != nullptr) {
deleteMenuNode(node->submenu);
node->submenu = nullptr;
}
delete node->title;
node->title = NULL;
node->parent = NULL;
node->previous = NULL;
node->valueFn = NULL;
node->value = NULL;
}
/**
* Returns the last node in a list
*/
MenuNode* getLastNodeInList(MenuNode *list) {
MenuNode *ptr = list;
while (ptr->next != nullptr) {
ptr = ptr->next;
}
return ptr;
}