-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuMaster.cpp
374 lines (359 loc) · 8.67 KB
/
MenuMaster.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
#include "MenuMaster.h"
#include <WString.h>
#include <SdFat.h>
//#include <avr/pgmspace.h>
#define BLOCK -1
#define NOTUSED 0
#define UPDOWN 1
#define ONOFF 2
#define CALLBACK 3
#define LIST 4
using namespace std;
/*
* Define Menu texts below - max size is 20
* This is not the same as the logger screen (Defined in main file)
* Naming is arbitrary, but help me keep sane. TL = Top Level; S = Sub; S = SubSub
*/
const char MenuMaster::TL1[] = { "Parameters" };
const char MenuMaster::TL1S1[] = { "TPS" };
const char MenuMaster::TL1S2[] = { "Accel Position" };
const char MenuMaster::TL1S3SS1[] = { "Brake" };
const char MenuMaster::TL1S3SS2[] = { "Brake Pressure" };
const char MenuMaster::TL1S4[] = { "Speed" };
const char MenuMaster::TL1S5[] = { "RPM" };
const char MenuMaster::TL1S6[] = { "Steering Angle" };
const char MenuMaster::TL1S7[] = { "Unknown Monitor" };
const char MenuMaster::TL1S8[] = { "Empty" };
const char MenuMaster::TL1S9[] = { "G-Force Lat" };
const char MenuMaster::TL1S10[] = { "G-Force Long" };
const char MenuMaster::TL2[] = { "Data Logger" };
const char MenuMaster::TL2S1[] = { "Start/Stop" };
const char MenuMaster::TL3[] = { "Track Select" };
const char MenuMaster::TL4[] = { "Soft Reset" };
/* Below is the Text Array for the menu structure
* This is confusing AS ALL HELL, but once you get it, you get it.
* Think of it as a flattened menustructure (see menutype array from header file)
*
* To further save memory, the text for all the menu items are stored in progmem as well
*
* ALL DEM SPACES NEED TO BE IN THERE TO KEEP ALIGNMENT
*/
const char* const MenuMaster::menuTable[] = { TL1, "", TL1S1, TL1S1, TL1S2, TL1S2, TL1S3SS1, TL1S3SS2, TL1S4, TL1S4, TL1S5, TL1S5, TL1S6, TL1S6, TL1S7, TL1S7, TL1S8, TL1S8, TL1S9, TL1S9, TL1S10, TL1S10, TL2, "", TL2S1, TL2, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", TL3, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", TL4, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
MenuMaster::MenuMaster(int depth, int index1, int index2, int index3)
{
mD = depth;
m1 = index1;
m2 = index2;
m3 = index3;
}
void MenuMaster::SetValues(int depth, int index1, int index2, int index3)
{
mD = depth;
m1 = index1;
m2 = index2;
m3 = index3;
}
//Converts multi-dimensional array location into single dimensional array location
short MenuMaster::calculateIndex(short index1, short index2, short index3)
{
short index;
short calc1 = index1 * dim2Size * dim3Size;
short calc2 = index2 * dim3Size;
index = calc1 + calc2 + index3;
return index;
}
int* MenuMaster::GetMenuSize()
{
/* This had to be altered since moving menu items to progmem
static int menuSize[3];
menuSize[0] = sizeof(menuText) / sizeof(*menuText);
menuSize[1] = (sizeof(*menuText) / sizeof(**menuText) - 1);
menuSize[2] = (sizeof(**menuText) / sizeof(***menuText) - 1);
return menuSize;
*/
static int menuSize[3];
menuSize[0] = dim1Size;
menuSize[1] = dim2Size - 1;
menuSize[2] = dim3Size - 1;
return menuSize;
}
//Returns Top Text of smallLCD based off menu indexes
String MenuMaster::GetLine1Text()
{
String top;
if (menuType[m1][m2][m3] == UPDOWN)
{
if (mD == -1)
top = "";
else if (mD == 0)
top = "Main Menu";
else if (mD == 1) {
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, 0, 0)]))); // Necessary casts and dereferencing,
top = buffer;
}
else if (mD == 2)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, 0)]))); // Necessary casts and dereferencing,
top = buffer;
}
else {
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, m3)]))); // Necessary casts and dereferencing,
top = buffer;
}
}
else if (menuType[m1][m2][m3] == CALLBACK)
{
if (mD == 0)
top = "Main Menu";
else if (mD == 1) {
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, 0, 0)]))); // Necessary casts and dereferencing,
top = buffer;
}
else if (mD == 2)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, 0)]))); // Necessary casts and dereferencing,
top = buffer;
}
else {
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, m3)]))); // Necessary casts and dereferencing,
top = buffer;
}
}
else if (menuType[m1][m2][m3] == ONOFF)
{
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, m3)]))); // Necessary casts and dereferencing,
top = buffer;
}
else if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex == 0)
{
top = String("> ") + trackNames[0];
}
else
{
top = trackNames[0];
}
}
return top;
}
//Returns Bottom Text of smallLCD based off menu indexes
String MenuMaster::GetLine2Text()
{
String bottom;
if (menuType[m1][m2][m3] == UPDOWN)
{
selectable = false;
callback = false;
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, m3)]))); // Necessary casts and dereferencing,
bottom = buffer;
}
else if (menuType[m1][m2][m3] == CALLBACK)
{
selectable = false;
callback = true;
strcpy_P(buffer, (PGM_P)pgm_read_word(&(menuTable[calculateIndex(m1, m2, m3)]))); // Necessary casts and dereferencing,
bottom = buffer;
}
else if (menuType[m1][m2][m3] == ONOFF)
{
selectable = true;
callback = false;
if (m1 == 0) {
loggerSelected = loggers[m2 - 1];
}
else if (m1 == 1) {
loggerSelected = enableLogging;
}
if (loggerSelected)
bottom = " [Yes] No";
else
bottom = " Yes [No]";
}
else if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex == 1)
{
bottom = String("> ") + trackNames[1];
}
else
{
bottom = trackNames[1];
}
}
return bottom;
}
//Returns Top Text of smallLCD based off menu indexes
String MenuMaster::GetLine3Text()
{
String line;
if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex == 2)
{
line = String("> ") + trackNames[2];
}
else
{
line = trackNames[2];
}
}
return line;
}
//Returns Bottom Text of smallLCD based off menu indexes
String MenuMaster::GetLine4Text()
{
String line;
if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex == 3)
{
line = String("> ") + trackNames[3];
}
else
{
line = trackNames[3];
}
}
return line;
}
//Returns Bool if yesno menu
bool MenuMaster::IsSelectable()
{
return selectable;
}
//Returns Bool if callback menu
bool MenuMaster::IsCallback()
{
return callback;
}
//Return Menu Type for a specified menu -
short MenuMaster::GetMenuType(short index1, short index2, short index3)
{
return menuType[index1][index2][index3];
}
//Select up in menu lists
int MenuMaster::SelectUp()
{
if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex != 0)
trackReadIndex--;
else
{
//trackReadIndex = 0;
if (trackSelectIndex != 0)
trackSelectIndex--;
return (trackSelectIndex);
}
}
else {
if (m1 == 0) {
if (!locked)
{
//This part sets a logger object (aligned with the menu text and text in master *hopefully*) to either true or false
if (loggerSelected)
loggers[m2 - 1] = false;
else
loggers[m2 - 1] = true;
}
}
else if (m1 == 1) {
if (loggerSelected)
enableLogging = false;
else
enableLogging = true;
}
return 0;
}
}
//Select down in menu lists
int MenuMaster::SelectDown()
{
if (menuType[m1][m2][m3] == LIST)
{
if (trackReadIndex != 3)
trackReadIndex++;
else
{
//trackReadIndex = 3;
if ((trackSelectIndex + 3) != trackCount)
trackSelectIndex++;
return (trackSelectIndex);
}
}
else {
if (m1 == 0) {
if (!locked)
{
if (loggerSelected)
loggers[m2 - 1] = false;
else
loggers[m2 - 1] = true;
}
}
else if (m1 == 1) {
if (loggerSelected)
enableLogging = false;
else
enableLogging = true;
}
return 0;
}
}
//Sets all loggers on
void MenuMaster::SetLoggersTrue()
{
for (int i = 0; i < (dim2Size - 1); i++)
{
loggers[i] = true;
}
}
//Locks Logger State
void MenuMaster::LockLoggers()
{
locked = true;
}
//Unlocks Logger State
void MenuMaster::UnlockLoggers()
{
locked = false;
}
void MenuMaster::LoadTracks(SdFat &sd, SdFile &file)
{
if (!sd.chdir("Tracks")) {
//error("chdir failed for track traps folder.\n");
}
byte maxTracks = trackCount;
if (trackCount >= 4)
{
maxTracks = 4;
}
//char fname[20];
if (trackSelectIndex == 0)
{
strcpy(trackNames[0], "None");
for (byte i = 1; i < maxTracks; i++)
{
file.openNext(sd.vwd(), O_READ);
file.getName(trackNames[i], 20);
file.close();
}
}
else
{
for (byte i = (trackSelectIndex - 1); i != 0; i--)
{
file.openNext(sd.vwd(), O_READ);
file.close();
}
for (byte i = 0; i < maxTracks; i++)
{
file.openNext(sd.vwd(), O_READ);
file.getName(trackNames[i], 20);
file.close();
}
}
sd.vwd()->rewind();
sd.chdir("/");
}