-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickMenu.cpp
397 lines (349 loc) · 12.4 KB
/
QuickMenu.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
#include "QuickMenu.h"
QuickMenu::QuickMenu(Keypad *_keypad, LiquidCrystal_I2C *_lcd_i2c, QMenu *_main_menu, uint8_t _menu_size, uint8_t _variable_monitor_length){
TQDF_keypad = _keypad;
TQDF_lcd = _lcd_i2c;
TQDF_mainMenu = _main_menu;
TQDF_mainMenu_size = _menu_size;
if (_variable_monitor_length > 255) _variable_monitor_length = 255;
if (_variable_monitor_length < 1) _variable_monitor_length = 1;
VAR_Monitor_temp = (unsigned long *) malloc(_variable_monitor_length * sizeof(unsigned long));
}
void QuickMenu::begin(){
TQDF_lcd->clear();
}
void QuickMenu::idle(){
if(MENU_Idle_enable){
TQDF_lcd->clear();
TQDF_lcd->setCursor(0, 0);
TQDF_lcd->print("Press # to");
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print("enter the QMenu");
}
}
QMLevel* QuickMenu::MENULevel_now(){
return &MENULevel[MENULevel_index];
}
QMenu* QuickMenu::MENU_now(int index){
if(index == 0){
index = MENULevel_index;
}else{
index = MENULevel_index + index;
}
return &MENULevel[index].menu[MENULevel[index].index];
}
bool QuickMenu::eventChange(u_int16_t var){
bool _return = false;
if(VAR_Monitor_temp[VAR_Monitor_index] != var){
_return = true;
}
VAR_Monitor_temp[VAR_Monitor_index] = var;
VAR_Monitor_index++;
return _return;
}
void QuickMenu::displayMenu(){
MENU_Display_initial = true;
if(MENULevel_index == -1){
idle();
}else if(MENULevel_index == 0){
MENULevel_now()->index = 0;
MENULevel_now()->menu = TQDF_mainMenu;
MENULevel_now()->menuLength = TQDF_mainMenu_size;
MENULevel_now()->dropDownLength = 0;
MENU_Title_index = 0;
TQDF_lcd->clear();
}else{
TQDF_lcd->clear();
TQDF_lcd->setCursor(0, 0);
TQDF_lcd->print(MENU_now(-1)->title);
MENULevel_now()->dropDownLength = 0;
}
}
void QuickMenu::displayText(){
if(MENULevel_index >= 0){
if((millis() - MENU_DisplayRow0_millis > 1000) || MENU_Display_initial){
MENU_DisplayRow0_millis = millis();
MENU_DisplayRow1_millis = millis();
MENU_DisplayRow1_begin = true;
if(KEYPAD_isKeyInput){
TQDF_lcd->setCursor(0, 0);
if(((MENU_TitleRotation_index % 8) == 0) || ((MENU_TitleRotation_index % 8) == 1)){
TQDF_lcd->print(MENU_Title_text);
}else if(((MENU_TitleRotation_index % 8) == 2) || ((MENU_TitleRotation_index % 8) == 3)){
TQDF_lcd->print(MENU_Title_desc);
}else if(((MENU_TitleRotation_index % 8) == 4) || ((MENU_TitleRotation_index % 8) == 5)){
TQDF_lcd->print("# to Save ");
}else{
TQDF_lcd->print("* to Cancel ");
}
MENU_TitleRotation_index++;
}else if(MENULevel_index == 0){
TQDF_lcd->setCursor(0, 0);
if(((MENU_TitleRotation_index % 4) == 0) || ((MENU_TitleRotation_index % 4) == 1)){
TQDF_lcd->setCursor(0, 0);
TQDF_lcd->print("Choose [1..");
TQDF_lcd->print(TQDF_mainMenu_size);
TQDF_lcd->print("] ");
}else{
TQDF_lcd->print("#/* to close ");
}
MENU_TitleRotation_index++;
}
}
if(((millis() - MENU_DisplayRow1_millis > 200) || MENU_Display_initial) && MENU_DisplayRow1_begin ){
MENU_Display_initial = false;
MENU_DisplayRow1_begin = false;
if(!KEYPAD_isKeyInput){
if(MENULevel_index != -1){
if(MENU_Title_index >= MENULevel_now()->menuLength)
{
MENU_Title_index = 0;
}
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print(MENULevel_now()->menu[MENU_Title_index++].title);
}
}
switch(MENU_now()->dataType){
case textDropDown:
if(MENULevel_now()->dropDownLength > 0){
if(MENU_Title_index >= MENULevel_now()->dropDownLength)
{
MENU_Title_index = 0;
}
TQDF_lcd->setCursor(0, 1);
char *dropdown_item = MENULevel_now()->dropDown + (MENU_Title_index++ * (LCDString_WIDTH));
*(dropdown_item + 14) = '\0';
TQDF_lcd->print(dropdown_item);
}
break;
}
}
}
}
void QuickMenu::loop(){
char key = TQDF_keypad->getKey();
if (key){
if(key == '#'){
if(KEYPAD_isKeyInput){
KEYPAD_isKeyInput = false;
uint16_t KEYPAD_KeyInput_int = KEYPAD_KeyInput_char.toInt();
if((KEYPAD_KeyInput_int >= MENU_now()->valueMax) && (KEYPAD_KeyInput_int <= MENU_now()->valueMin)){
switch(MENU_now()->dataType){
case UInt8:
*(uint8_t*)MENU_now()->dataVariable = KEYPAD_KeyInput_int;
break;
case UInt16:
*(uint16_t*)MENU_now()->dataVariable = KEYPAD_KeyInput_int;
break;
case textDropDown:
MENULevel_now()->dropDownLength = 0;
*(byte*)MENU_now()->dataVariable = KEYPAD_KeyInput_int;
MENU_Title_index = 0;
break;
}
TQDF_lcd->clear();
TQDF_lcd->print(MENU_Title_text);
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print(KEYPAD_KeyInput_int);
TQDF_lcd->print(" saved!");
delay(1000);
}else{
TQDF_lcd->clear();
TQDF_lcd->print(MENU_Title_text);
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print("* !Out Range!");
delay(1000);
}
}else if(MENULevel_index < 0){
MENULevel_index = 0; // enter main menu
}else{
MENULevel_index--;
}
displayMenu();
}else if(key == '*'){
if(KEYPAD_isKeyInput){
KEYPAD_isKeyInput = false;
displayMenu();
}else if(MENULevel_index >= 0){
MENULevel_index--;
displayMenu();
}
}else if((MENULevel_index >= 0) && ((key >= '0') || (key <= '9'))){
if(KEYPAD_isKeyInput){
switch(MENU_now()->dataType){
case UInt8:
case UInt16:
KEYPAD_KeyInput_char += key;
TQDF_lcd->setCursor(KEYPAD_KeyInput_position++, 1);
TQDF_lcd->print(key);
break;
case textDropDown:
KEYPAD_KeyInput_char = key;
TQDF_lcd->setCursor(KEYPAD_KeyInput_position, 1);
TQDF_lcd->print(key);
break;
}
}else{ // if input not required
// we are selecting the menu
if((key != '0') && (key - '0' <= MENULevel_now()->menuLength)){ // just check that the key input between menuLength
MENULevel_now()->index = key - '1'; // change the level index parameter
MENU_Title_text = MENU_now()->title;
MENU_Title_desc = MENU_now()->desc;
TQDF_lcd->clear();
TQDF_lcd->print(MENU_Title_text);
TQDF_lcd->setCursor(0, 1);
MENU_TitleRotation_index = 0;
KEYPAD_KeyInput_position = 8;
MENU_Display_initial = true;
uint16_t nilaiUInt8;
uint16_t nilaiUInt16;
uint16_t nilaiUInt32;
int16_t nilaiFloat;
KEYPAD_KeyInput_char = "";
switch(MENU_now()->dataType){
case UInt8:
KEYPAD_isKeyInput = true;
TQDF_lcd->print(*(byte*)MENU_now()->dataVariable);
TQDF_lcd->setCursor(6, 1);
TQDF_lcd->print("=>0");
break;
case UInt16:
KEYPAD_isKeyInput = true;
TQDF_lcd->print(*(uint16_t*)MENU_now()->dataVariable);
TQDF_lcd->setCursor(6, 1);
TQDF_lcd->print("=>0");
break;
case textDropDown:
KEYPAD_isKeyInput = true;
MENULevel_now()->dropDownLength = MENU_now()->param_a;
MENULevel_now()->dropDown = MENU_now()->menuSub;
MENU_Title_index = 0;
TQDF_lcd->print(*(uint8_t*)MENU_now()->dataVariable);
TQDF_lcd->setCursor(14, 1);
TQDF_lcd->print(">0");
KEYPAD_KeyInput_position = 15;
// delay(1000);
break;
case subMenu:
KEYPAD_isKeyInput = false;
MENULevel_index++;
MENULevel_now()->index = 0; // reset the next level index, current level index (set above) remain the same
MENULevel_now()->menu = (QMenu *)MENU_now(-1)->menuSub;
MENULevel_now()->menuLength = MENU_now(-1)->param_a;
MENULevel_now()->dropDownLength = 0;
MENU_Title_index = 0;
break;
case systemState:
KEYPAD_isKeyInput = false;
SYS_State = MENU_now()->param_a;
break;
}
}
}
}
}
displayText();
VAR_Monitor_index = 0;
}
bool QuickMenu::isMenuActive(){
return (MENULevel_index != -1);
}
int QuickMenu::getState(){
return SYS_State;
}
void QuickMenu::showIdleText(bool opt){
MENU_Idle_enable = opt;
}
bool QuickMenu::resetState(){
char key = TQDF_keypad->getKey();
if(key == '#'){
SYS_State = 0;
MENULevel_index = -1;
TQDF_lcd->clear();
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print("Stopping...");
delay(1000);
displayMenu();
return true;
}
return false;
}
/*
void QuickMenu::begin(){
TQDF_lcd->clear();
}
char msg[17];
Switch menu mechanism
MENULevel_index = 0;
MENULevel[MENULevel_index].index = 0;
MENULevel[MENULevel_index].menu = TQDF_mainMenu;
MENULevel[MENULevel_index].menuLength = array_sizeof(TQDF_mainMenu);
MENULevel[MENULevel_index].dropDownLength = 0;
MENULevel_index++;
MENULevel[MENULevel_index].index = 0;
// MENULevel[MENULevel_index].menu = (QMenu *)MENULevel[MENULevel_index-1].menu[MENULevel[MENULevel_index-1].index].menuSub;
MENULevel[MENULevel_index].menu = (QMenu *) MENU_now(-1)->menuSub;
MENULevel[MENULevel_index].menuLength = MENU_now(-1)->param_a;
MENULevel[MENULevel_index].dropDownLength = 0;
sprintf(msg, "%p", MENULevel[MENULevel_index].menuLength);
TQDF_lcd->setCursor(0, 0);
TQDF_lcd->print(msg);
sprintf(msg, "%p", MENU_now(-1)->param_a);
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print(msg);
LOG
0x200000d0 &MENUAlarm_items
0x200000ac &MENUActive_items
0x200000d0 MENUAlarm_items
0x200000ac MENUActive_items
0x2000016c &MENUMain_items
0x2000016c MENUMain_items
0x200000ac MENUMain_items[0].menuSub <- Real pointer to MENUActive_items
0x20000198 &MENUMain_items[0].menuSub
0x20000000 MENUMain_items[1].menuSub <- Real pointer
0x200001cc &MENUMain_items[1].menuSub
0x200000d0 MENUMain_items[2].menuSub <- Real pointer to MENUAlarm_items
0x20000200 &MENUMain_items[2].menuSub
0x20000000 MENUMain_items[3].menuSub <- Real pointer
0x20000234 &MENUMain_items[3].menuSub
0x200000ac TQDF_mainMenu[0].menuSub <- Real pointer
0x20000198 &TQDF_mainMenu[0].menuSub
0x00000000 TQDF_mainMenu[1].menuSub <- Real pointer
0x200001cc &TQDF_mainMenu[1].menuSub
0x200000d0 TQDF_mainMenu[2].menuSub <- Real pointer
0x20000200 &TQDF_mainMenu[2].menuSub
0x00000000 TQDF_mainMenu[3].menuSub <- Real pointer
0x20000234 &TQDF_mainMenu[3].menuSub
0x2000016c TQDF_mainMenu <- correct!
QMenu level debug
0x6341RAND MENULevel[0].menu[MENULevel[0].index] !thats the pointer address
0x2000016c &MENULevel[0].menu[MENULevel[0].index] <- means that level 0 has TQDF_mainMenu items
0x2000016c &MENULevel[0].menu[0]
0x200001a0 &MENULevel[0].menu[1]
0x200001d4 &MENULevel[0].menu[2]
0x200000ac MENULevel[0].menu[0].menuSub <- Real pointer
0x20000198 &MENULevel[0].menu[0].menuSub
0x20000000 MENULevel[0].menu[1].menuSub <- Real pointer
0x200001cc &MENULevel[0].menu[1].menuSub
0x200000d0 MENULevel[0].menu[2].menuSub <- Real pointer
0x200001d4 &MENULevel[0].menu[2].menuSub
0x20000000 MENULevel[0].menu[3].menuSub <- Real pointer
0x20000234 &MENULevel[0].menu[3].menuSub
0x200000ac &MENULevel[1].menu[MENULevel[1].index]
0x200000ac &MENULevel[1].menu[0]
0x200000ac (QMenu *)MENULevel[0].menu[MENULevel[0].index].menuSub
0x200006e0 &MENULevel[MENULevel_index].menu
0x200000ac MENULevel[MENULevel_index].menu
delay(3000);
MENULevel_index++;
MENULevel[MENULevel_index].index = 0;
MENULevel[MENULevel_index].menu = (QMenu *)MENULevel[MENULevel_index-1].menu[MENULevel[MENULevel_index-1].index].subMenu;
MENULevel[MENULevel_index].menuLength = MENULevel[MENULevel_index-1].menu[MENULevel[MENULevel_index-1].index].jumlahBaris;
MENULevel[MENULevel_index].dropDownLength = 0;
TQDF_lcd->clear();
sprintf(msg, "%p", TestMENU_now(1));
TQDF_lcd->setCursor(0, 0);
TQDF_lcd->print(msg);
sprintf(msg, "%p", MENULevel[1].menu[MENULevel[1].index]);
TQDF_lcd->setCursor(0, 1);
TQDF_lcd->print(msg);
*/