-
Notifications
You must be signed in to change notification settings - Fork 7
/
menu.cpp
499 lines (442 loc) · 14.1 KB
/
menu.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
// This software, known as CarbOnBal is
// Copyright, 2017 L.L.M. (Dennis) Meulensteen. dennis@meulensteen.nl
//
// This file is part of CarbOnBal. A combination of software and hardware.
// I hope it may be of some help to you in balancing your carburetors and throttle bodies.
// Always be careful when working on a vehicle or electronic project like this.
// Your life and health are your sole responsibility, use wisely.
//
// CarbOnBal hardware is covered by the Cern Open Hardware License v1.2
// a copy of the text is incuded with the source code.
//
// CarbOnBal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CarbOnBal 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CarbOnBal. If not, see <http://www.gnu.org/licenses/>.
#include "menu.h"
#include "globals.h"
#include LANGUAGE
#include "lang_generic.h"
#include "lcdWrapper.h"
#include "menuActions.h"
#include "functions.h"
#include "utils.h"
bool quitMenu = false;
uint8_t mainMenuLine = 0;
uint8_t settingsMenuLine = 0;
uint8_t buttonsMenuLine = 0;
uint8_t displayClearCalibrationMenuLine = 0;
uint8_t displayCalibrationSensorMenuLine = 0;
uint8_t displayExtraMenuLine = 0;
uint8_t displaySoftwareSettingsMenuLine = 0;
uint8_t displayHardwareSettingsMenuLine = 0;
uint8_t displayDisplayMenuLine = 0;
uint8_t displayCalibrationMenuLine = 0;
uint8_t displayViewCalibrationMenuLine = 0;
// an array of strings contains the menu titles
// an array of function pointers, the same number and order as the above array, determines the actions to be taken when that title is selected
// a size is passed which must be equal to the number of menu entries
void actionDisplayMainMenu() {
setInterrupt(false);
quitMenu = false;
const char *menu[] = { txtDisplay, txtSettings, txtCalibration };
void (*actions[])() = {&actionDisplayDisplayMenu, &actionDisplaySettingsMenu, &actionDisplayCalibrationMenu};
uint8_t menuSize = 3;
handleMenu(menu, actions, menuSize, mainMenuLine);
setInterrupt(true);
}
void actionDisplaySettingsMenu() {
const char *const menu[] = { txtSoftware, txtHardware, txtButtons,
txtExtraMenu };
void (*actions[])() = {&actionDisplaySoftwareSettingsMenu, &actionDisplayHardwareSettingsMenu, &actionDisplayButtonsMenu, &actionDisplayExtraMenu};
uint8_t menuSize = 4;
handleMenu(menu, actions, menuSize, settingsMenuLine);
}
void actionDisplayButtonsMenu() {
const char *const menu[] = { txtContrastButton, txtBrightnessButton,
txtCancelButton };
void (*actions[])() = {&actionContrastButton, &actionBrightnessButton, &actionCancelButton};
uint8_t menuSize = 3;
handleMenu(menu, actions, menuSize, buttonsMenuLine);
}
void actionDisplayExtraMenu() {
const char *const menu[] = { txtDeviceInfo, txtAdvancedMenu,
txtSplashScreen, txtFactoryReset };
void (*actions[])() = {&doDeviceInfo, &doAdvanced, &doSplashScreen, &actionReset};
uint8_t menuSize = 4;
handleAdvancedMenu(menu, actions, menuSize, B1100, displayExtraMenuLine);
}
void actionDisplaySoftwareSettingsMenu() {
const char *const menu[] = { txtDamping, txtRpmDamping };
void (*actions[])() = {&actionEmaDamping, &actionRpmEmaDamping};
uint8_t menuSize = 2;
handleMenu(menu, actions, menuSize, displaySoftwareSettingsMenuLine);
}
void actionDisplayHardwareSettingsMenu() {
const char *const menu[] = { txtCylinderCount, txtMasterCylinder };
void (*actions[])() = {&actionCylinders, &actionMaster};
uint8_t menuSize = 2;
handleMenu(menu, actions, menuSize, displayHardwareSettingsMenuLine);
}
void actionDisplayDisplayMenu() {
const char *const menu[] = { txtGraphType, txtContrast, txtBrightness,
txtDetails, txtRpmDisplay, txtUnits, txtMaxZoomRange };
void (*actions[])() = {&actionGraphing, &actionContrast, &actionBrightness, &actionSilent,
&doRevs, &doUnits ,&doMaxZoom};
uint8_t menuSize = 7;
handleAdvancedMenu(menu, actions, menuSize, B1110100,
displayDisplayMenuLine);
}
void actionDisplayCalibrationMenu() {
const char *const menu[] = { txtViewCalibrations, txtCalibrateNow,
txtClearCalibration };
void (*actions[])() = {&actionDisplayViewCalibrationMenu, &actionDisplayCalibrationSensorMenu, &actionDisplayClearCalibrationMenu};
uint8_t menuSize = 3;
handleAdvancedMenu(menu, actions, menuSize, B110,
displayCalibrationMenuLine);
}
void actionDisplayViewCalibrationMenu() {
if (settings.cylinders == 4) {
const char *const menu[] = { txtViewSensor2, txtViewSensor3,
txtViewSensor4, txtSetCalibrationMax };
void (*actions[])() = {&doViewCalibration1, &doViewCalibration2, &doViewCalibration3, &actionCalibrationMax};
uint8_t menuSize = 4;
handleAdvancedMenu(menu, actions, menuSize, B1110,
displayViewCalibrationMenuLine);
} else if (settings.cylinders == 3) {
const char *const menu[] = { txtViewSensor2, txtViewSensor3,
txtSetCalibrationMax };
void (*actions[])() = {&doViewCalibration1, &doViewCalibration2, &actionCalibrationMax};
uint8_t menuSize = 3;
handleAdvancedMenu(menu, actions, menuSize, B110,
displayViewCalibrationMenuLine);
} else if (settings.cylinders == 2) {
const char *const menu[] = { txtViewSensor2, txtSetCalibrationMax };
void (*actions[])() = {&doViewCalibration1, &actionCalibrationMax};
uint8_t menuSize = 2;
handleAdvancedMenu(menu, actions, menuSize, B10,
displayViewCalibrationMenuLine);
}
}
void actionDisplayClearCalibrationMenu() {
if (settings.cylinders == 4) {
const char *const menu[] = { txtClearSensor2, txtClearSensor3,
txtClearSensor4, txtClearAllSensors };
void (*actions[])() = {&doClearCalibration1, &doClearCalibration2, &doClearCalibration3, &doZeroCalibrations};
uint8_t menuSize = 4;
handleMenu(menu, actions, menuSize, displayClearCalibrationMenuLine);
} else if (settings.cylinders == 3) {
const char *const menu[] = { txtClearSensor2, txtClearSensor3,
txtClearAllSensors };
void (*actions[])() = {&doClearCalibration1, &doClearCalibration2, &doZeroCalibrations};
uint8_t menuSize = 3;
handleMenu(menu, actions, menuSize, displayClearCalibrationMenuLine);
} else if (settings.cylinders == 2) {
const char *const menu[] = { txtClearSensor2 };
void (*actions[])() = {&doClearCalibration1};
uint8_t menuSize = 1;
handleMenu(menu, actions, menuSize, displayClearCalibrationMenuLine);
}
}
void actionDisplayCalibrationSensorMenu() {
if (settings.cylinders == 4) {
const char *const menu[] = { txtCalibrateSensor2, txtCalibrateSensor3,
txtCalibrateSensor4 };
void (*actions[])() = {&doCalibrate1, &doCalibrate2, &doCalibrate3};
uint8_t menuSize = 3;
handleMenu(menu, actions, menuSize, displayCalibrationSensorMenuLine);
} else if (settings.cylinders == 3) {
const char *const menu[] = { txtCalibrateSensor2, txtCalibrateSensor3 };
void (*actions[])() = {&doCalibrate1, &doCalibrate2};
uint8_t menuSize = 2;
handleMenu(menu, actions, menuSize, displayCalibrationSensorMenuLine);
} else if (settings.cylinders == 2) {
const char *const menu[] = { txtCalibrateSensor2 };
void (*actions[])() = {&doCalibrate1};
uint8_t menuSize = 1;
handleMenu(menu, actions, menuSize, displayCalibrationSensorMenuLine);
}
}
// display a basic setting change screen that does not have to call a function
int doBasicSettingChanger(const char *valueName, int minimum, int maximum,
int startValue, int steps) {
return doSettingChanger(valueName, minimum, maximum, startValue, steps,
NULL);
}
int doSettingChanger(const char *valueName, int minimum, int maximum,
int startValue, int steps, void (*func)(int i)) {
return doSettingChangerDelay(valueName, minimum, maximum, startValue, steps,
func, 0);
}
// display a settings change screen that calls a function every time the value changes for an immediate response
// normally used for setting contrast and brightness
int doSettingChangerDelay(const char *valueName, int minimum, int maximum,
int startValue, int steps, void (*func)(int i), unsigned int delayMs) {
unsigned long startTime = millis();
int value = startValue;
lcd_clear();
lcd_setCursor(0, 0);
lcd_print(valueName);
lcd_setCursor(0, 1);
lcd_printInt(startValue);
while (true) {
if ((delayMs > 0) && ((millis() - startTime) >= delayMs))
break;
int segmentsInCharacter = 5;
int TotalNumberOfLitSegments = 100000L / maximum * value / 1000;
int numberOfLitBars = TotalNumberOfLitSegments / segmentsInCharacter;
int numberOfLitSegments = TotalNumberOfLitSegments
% segmentsInCharacter;
char bars[DISPLAY_COLS + 1];
makeBars(bars, numberOfLitBars, 0);
byte bar[8];
makeChar(bar, numberOfLitSegments);
lcd_createChar(6, bar);
printLcdInteger(value, 0, 1, 6);
lcd_setCursor(0, 2);
lcd_print(bars);
if (numberOfLitBars < DISPLAY_COLS) {
lcd_setCursor(numberOfLitBars, 2);
lcd_write(byte(6));
}
lcd_setCursor(0, 3);
lcd_print(bars);
if (numberOfLitBars < DISPLAY_COLS) {
lcd_setCursor(numberOfLitBars, 3);
lcd_write(byte(6));
}
switch (buttonPressed()) {
case SELECT:
return value;
break;
case RIGHT:
if (value < maximum - steps) {
value += steps;
} else {
value = maximum;
}
startTime = millis();
if (func)
(*func)(value);
break;
case LEFT:
if (value > minimum + steps) {
value -= steps;
} else {
value = minimum;
}
startTime = millis();
if (func)
(*func)(value);
break;
case CANCEL:
if (func)
(*func)(startValue);
return startValue;
}
delay(50);
}
lcd_clear();
return startValue;
}
// allows the user to choose a value from an array of strings,
// the selected array index is returned.
int doSettingChooser(const char *valueName, const char *settings[], int count,
int startIndex) {
int index = startIndex;
boolean settingChanged = true;
lcd_clear();
lcd_setCursor(0, 0);
lcd_print(valueName);
lcd_setCursor(0, 1);
lcd_printInt(startIndex);
while (true) {
if (settingChanged) {
lcd_setCursor(0, 1);
lcd_print(txtSpace20);
lcd_setCursor(0, 3);
lcd_print(txtSpace20);
if (index > 0) {
lcd_setCursor(0, 3);
lcd_printChar(char(MENUCARET + 1)); //little arrow to the left
}
lcd_setCursor(0, 1);
lcd_print(settings[index]);
if (index < count - 1) {
lcd_setCursor(19, 3);
lcd_printChar(char(MENUCARET)); //little arrow to the right
}
settingChanged = false;
}
switch (buttonPressed()) {
case SELECT:
return index;
break;
case RIGHT:
if (index < count - 1) {
index += 1;
settingChanged = true;
}
break;
case LEFT:
if (index > 0) {
index -= 1;
settingChanged = true;
}
break;
case CANCEL:
return startIndex;
}
delay(50);
}
return startIndex;
}
// displays a menu screen
// menu[] = an array of menu option strings
// *func[] = an array of pointers to the corresponding functions
// menuSize = a counter to say how many values are in both arrays
// display the resulting menu, used by handleMenu()
void drawMenu(const char *const pointerTable[], int count, int offset) {
lcd_clear();
for (uint8_t line = 0; ((line < count) && (line < DISPLAY_ROWS)); line++) {
lcd_setCursor(1, line);
lcd_print((const char*) (pointerTable[line + offset]));
}
if (offset > 0) {
byte upArrow[8] = {
B00100,
B01110,
B10101,
B00100,
B00100,
B00000,
B00000, };
lcd_createChar(2, upArrow);
lcd_setCursor(19, 0);
lcd_write(byte(0x02));
}
if (offset < count - DISPLAY_ROWS) {
byte downArrow[8] = {
B00000,
B00000,
B00100,
B00100,
B10101,
B01110,
B00100,
};
lcd_createChar(3, downArrow);
lcd_setCursor(19, 3);
lcd_write(byte(0x03));
}
drawCaret(0);
}
void handleMenu(const char *const pointerTable[], void (*func[])(),
int menuSize, uint8_t menuLine) {
int cursorLine = menuLine % 4;
int offset = menuLine / 4;
bool refresh = true;
drawMenu(pointerTable, menuSize, offset);
while (!quitMenu) {
switch (buttonPressed()) {
case SELECT:
menuLine = cursorLine + offset;
(*func[cursorLine + offset])();
refresh = true;
break;
case LEFT:
if (cursorLine > 0) {
cursorLine -= 1;
} else if ((cursorLine == 0) && (offset > 0)) {
offset -= 1;
}
refresh = true;
break;
case RIGHT:
if (cursorLine < DISPLAY_ROWS - 1 && cursorLine < menuSize - 1) {
cursorLine += 1;
} else if ((cursorLine == DISPLAY_ROWS - 1)
&& (offset < menuSize - 4)) {
offset += 1;
}
refresh = true;
break;
case CANCEL:
lcd_clear();
return;
}
if (refresh) {
drawMenu(pointerTable, menuSize, offset);
drawCaret(cursorLine);
refresh = false;
}
}
}
void handleAdvancedMenu(const char *const pointerTable[], void (*func[])(),
int menuSize, unsigned int mask, uint8_t menuLine) {
if (settings.advanced) {
handleMenu(pointerTable, func, menuSize, menuLine);
} else {
const char *menu[16];
void (*actions[16])();
int menuIndex = 0;
for (int i = 0; i < menuSize; i++) {
if (mask & (1 << (menuSize - i - 1))) {
menu[menuIndex] = pointerTable[i];
actions[menuIndex] = func[i];
menuIndex++;
}
}
handleMenu(menu, actions, menuIndex, menuLine);
}
}
// puts the menu cursor in the right place
void drawCaret(uint8_t line) {
for (uint8_t i = 0; i < DISPLAY_ROWS; i++) {
lcd_setCursor(0, i);
lcd_printChar(' ');
}
lcd_setCursor(0, line);
lcd_printChar(char(MENUCARET)); //little arrow to the right
}
// builds a custom character used for smooth display (less than 5 stripes)
void makeChar(byte *character, int value) {
byte line = 0x00;
if (value >= 0) {
for (int i = 0; i < value; i++) {
line >>= 1;
line |= B10000;
}
} else {
for (int i = 0; i > value; i--) {
line <<= 1;
line |= B00001;
}
}
for (int i = 0; i < 8; i++) {
character[i] = line;
}
}
// generates a single bar graph
void makeBars(char *bars, uint8_t number, uint8_t skip) {
if (number > 20)
number = 20;
for (uint8_t i = 0; i < skip; i++) {
bars[i] = ' ';
}
for (uint8_t i = skip; i < number; i++) {
bars[i] = 0xff;
}
for (uint8_t i = number; i < 20; i++) {
bars[i] = ' ';
}
bars[20] = 0x00;
}