-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.h
More file actions
286 lines (246 loc) · 6.38 KB
/
Menu.h
File metadata and controls
286 lines (246 loc) · 6.38 KB
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
#ifndef _MENU_H
#define _MENU_H
#include <itoa.h>
#include <LiquidCrystal_I2C_STM32.h>
#include <SD.h>
struct MenuItem {
MenuItem(const char *name_): name(name_), parent(NULL) {}
virtual const char * line1() {
return name;
}
virtual void onEnter() {}
virtual void onExit() {}
virtual const char * line2() = 0;
virtual MenuItem * onKeyPressed(int keys) = 0;
const char *name;
MenuItem *parent = 0;
protected:
static char displayBuffer[17];
};
struct MenuList: public MenuItem {
template<typename... SubItems>
MenuList(const char *name_, MenuItem &subItem_, SubItems&... subItems_): MenuItem(name_), curItem(0) {
addSubItems(0, subItem_, subItems_...);
}
virtual void onEnter() {
curItem = 0;
}
virtual const char * line2();
virtual MenuItem * onKeyPressed(int keys);
private:
void addSubItems(int) {}
template<typename... SubItems>
void addSubItems(int position, MenuItem &subItem, SubItems&... subItems_) {
subItem.parent = this;
subItems[position] = &subItem;
subItems[position + 1] = NULL;
addSubItems(position + 1, subItems_...);
}
MenuItem *subItems[8];
int curItem;
};
struct Menu {
static const int KEY_UP = 1 << 0;
static const int KEY_DOWN = 1 << 1;
static const int KEY_LEFT = 1 << 2;
static const int KEY_RIGHT = 1 << 3;
Menu(MenuItem &mainMenu_, int upKeyPin_, int downKeyPin_, int leftKeyPin_, int rightKeyPin_):
currentMenu(&mainMenu_),
mainMenu(&mainMenu_),
upKeyPin(upKeyPin_),
downKeyPin(downKeyPin_),
leftKeyPin(leftKeyPin_),
rightKeyPin(rightKeyPin_),
lastKeyState(0),
lcd(0x27, 16, 2)
{}
void init() {
pinMode(upKeyPin, INPUT_PULLUP);
pinMode(downKeyPin, INPUT_PULLUP);
pinMode(leftKeyPin, INPUT_PULLUP);
pinMode(rightKeyPin, INPUT_PULLUP);
lcd.begin();
lcd.noCursor();
currentMenu->onEnter();
displayRefresh();
wallClock = millis() + REFRESH_PERIOD;
}
// Call in the main loop
void poll() {
// Read pressed keys
readKeys();
}
void switchTo(MenuItem &newMenu) {
if(currentMenu != &newMenu)
newMenu.onEnter();
currentMenu = &newMenu;
}
void switchToMain() {
while(currentMenu != mainMenu) {
currentMenu->onExit();
currentMenu = currentMenu->parent;
}
displayRefresh();
}
private:
static const int REFRESH_PERIOD = 50; // Screen refresh period in milliseconds
static const int KEY_REPEAT_FIRST = 12; // Refresh periods before starting key repeat
static const int KEY_REPEAT_NEXT = 4; // Refresh periods between 2 key repeats
static const int KEY_REPEAT_ACCEL_THRES = 5; // Repeats before going to fast mode
static const int KEY_REPEAT_FAST = 1; // Refresh periods between 2 key repeats
void onKeyPressed(int keys);
void readKeys();
void displayRefresh();
int upKeyPin;
int downKeyPin;
int leftKeyPin;
int rightKeyPin;
int lastKeyState; // Pressed keys
int keyRepeat; // Frame counter for key repeat
int keyRepeatCount;
int wallClock;
LiquidCrystal_I2C_STM32 lcd;
MenuItem *currentMenu;
MenuItem *mainMenu;
};
struct MenuNumberSelect: public MenuItem {
MenuNumberSelect(const char *name_, MenuItem &subMenu_, int minimum_, int maximum_):
MenuItem(name_),
subMenu(subMenu_),
minimum(minimum_),
maximum(maximum_) {
if(&subMenu != this)
subMenu.parent = this;
}
virtual void onEnter() {
number = minimum;
}
virtual const char * line2() {
itoa(number, displayBuffer, 10);
return displayBuffer;
}
virtual MenuItem * onKeyPressed(int keys) {
if(keys == Menu::KEY_LEFT && number > minimum)
--number;
else if(keys == Menu::KEY_RIGHT && number < maximum)
++number;
else if(keys == Menu::KEY_DOWN)
return &subMenu;
return this;
}
MenuItem &subMenu;
int minimum;
int maximum;
int number;
};
struct MenuConfirm: public MenuItem {
MenuConfirm(const char *name_, const char *message_):
MenuItem(name_),
message(message_)
{}
MenuConfirm(const char *name_, const char *message_, MenuItem& parent_):
MenuItem(name_),
message(message_)
{
parent = &parent_;
}
virtual const char * line2() {
return message;
}
virtual MenuItem * onKeyPressed(int keys);
virtual void onConfirmed() {}
const char *message;
};
struct MenuFileSelect: public MenuItem {
MenuFileSelect(const char *name_, MenuItem &subMenu_, int csPin_, const char *path_):
MenuItem(name_),
csPin(csPin_),
path(path_),
subMenu(subMenu_) {
if(&subMenu != this)
subMenu.parent = this;
}
virtual void onEnter() {
// Clear any selection and reinit the SD card each time the menu is entered
// That allows for more robust operation when hot-switching cards
if(SD.begin(csPin)) {
dir = SD.open(path);
if(!dir) {
SD.mkdir(path);
dir = SD.open(path);
}
}
fileIndex = 0;
fileCount = -1; // Number of files unknown
openFileAtIndex();
}
void onExit() {
if(file)
file.close();
if(dir)
dir.close();
SD.end();
}
virtual const char * line2() {
if(!dir) {
return "SD CARD ERROR";
}
if(!file) {
return "NO FILE";
}
return file.name();
}
void openFileAtIndex() {
if(!dir)
return;
dir.rewindDirectory();
for(int i = 0; i <= fileIndex; ++i) {
openNextFile();
}
}
void openNextFile() {
if(!dir)
return;
do {
if(file)
file.close();
file = dir.openNextFile();
} while(file && file.isDirectory());
}
virtual MenuItem * onKeyPressed(int keys) {
if(!dir)
// Not ready, don't react to any keypress
return this;
if(keys == Menu::KEY_LEFT && fileIndex > 0) {
--fileIndex;
openFileAtIndex();
} else if(keys == Menu::KEY_RIGHT) {
if(fileIndex < fileCount - 1 || fileCount == -1) {
++fileIndex;
openNextFile();
}
if(fileCount == -1) {
if(!file) {
fileCount = fileIndex;
--fileIndex;
openFileAtIndex();
}
}
}
// Only allow entering submenu if a file was selected
else if(keys == Menu::KEY_DOWN && file) {
// Go to the beginning of the file (in case the same file is selected more than once in a row)
file.seek(0);
return &subMenu;
}
return this;
}
MenuItem &subMenu;
int csPin;
const char *path;
int fileIndex;
int fileCount;
File dir;
File file;
};
#endif