-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
executable file
·215 lines (178 loc) · 5.38 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
/*
* Arduino-Cocktail
*
* OLED Menu
* Copyright 2017 by Thomas Buck <[email protected]>
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return. Thomas Buck
* ----------------------------------------------------------------------------
*/
#include <Arduino.h>
#include "display.h"
#include "debounce.h"
#include "menu.h"
extern Debouncer s1;
extern Debouncer s2;
extern Debouncer s3;
MenuEntry *MenuEntry::currentMainMenuItem = NULL;
bool MenuEntry::firstDraw = false;
int MenuEntry::menu(MenuEntry **entry) {
int actRet = (*entry)->act(entry);
int drawRet = MENU_ERR_UNUSED;
if ((!firstDraw) || (actRet == MENU_SHOULD_DRAW)) {
firstDraw = true;
drawRet = (*entry)->draw();
Serial.print(F("Menu: drawing ("));
Serial.print(drawRet ? "0" : "1");
Serial.print(F(" / "));
Serial.print(actRet);
Serial.println(F(")"));
}
return (drawRet != MENU_ERR_UNUSED) ? drawRet : actRet;
}
int alwaysReturnActFunc(MenuEntry **entry, void *userData) {
int sw1 = s1.poll();
int sw2 = s2.poll();
int sw3 = s3.poll();
if ((sw1 == -1) || (sw2 == -1) || (sw3 == -1)) {
// always return to parent
*entry = (*entry)->getParent();
return MENU_SHOULD_DRAW;
}
return MENU_NO_ERR;
}
MenuEntry::MenuEntry(const char *_name, MenuEntry *_parent,
MenuDrawFunc _drawFunc, MenuActFunc _actFunc, void *_userData)
: name(_name), parent(_parent), drawFunc(_drawFunc), actFunc(_actFunc), userData(_userData) {
children[0] = NULL;
if (parent != NULL) {
if (parent->addChild(this) != MENU_NO_ERR) {
Serial.println(F("Menu-Error: couldn't add another child item!"));
parent = NULL;
}
}
if (currentMainMenuItem == NULL) {
// automatically set first item to be main menu root
currentMainMenuItem = this;
}
}
int MenuEntry::getChildCount(void) {
int childCount = 0;
while (children[childCount] != NULL) childCount++;
return childCount;
}
int MenuEntry::addChild(MenuEntry *child) {
int childCount = getChildCount();
if (childCount < MENU_ENTRY_MAX_CHILDREN) {
children[childCount] = child;
children[childCount + 1] = NULL;
return MENU_NO_ERR;
} else {
return MENU_ERR_OUT_OF_SPACE;
}
}
int MenuEntry::drawChildrenList(void) {
int realChildCount = getChildCount();
// Show "Back..." at end of menu when parent menu exists
int childCount = realChildCount;
if (parent != NULL) childCount++;
int i;
for (i = childOffset; (i < childCount) && (i < (childOffset + 7)); i++) {
String line;
if (selection == i) {
line += "= ";
} else {
line += " ";
}
if (i < realChildCount) {
line += children[i]->name;
} else {
line += "Back...";
}
writeLine(1 + i - childOffset, line.c_str());
}
// Clear screen below menu entries
i -= childOffset;
if (i < 7) {
for (int j = i + 1; j < OLED_LINES; j++) {
writeLine(j, "");
}
}
return MENU_NO_ERR;
}
int MenuEntry::draw(void) {
int ret = MENU_NO_ERR;
if (drawFunc != NULL) {
ret = drawFunc(this, userData);
} else if (children != NULL) {
writeLine(0, name);
ret = drawChildrenList();
DRAW_TEXT();
} else {
ret = MENU_ERR_INVALID_ENTRY;
}
return ret;
}
int MenuEntry::actChildrenList(MenuEntry **entry) {
bool redraw = false;
int sw1 = s1.poll();
int sw2 = s2.poll();
int sw3 = s3.poll();
int realChildCount = getChildCount();
// Show "Back..." at end of menu when parent menu exists
int childCount = realChildCount;
if (sw1 == -1) {
// Left button
selection--;
redraw = true;
Serial.println(F("Menu: moving left"));
}
if (sw3 == -1) {
// Right button
selection++;
redraw = true;
Serial.println(F("Menu: moving right"));
}
// Fix over- or under-flown selection index
if (parent != NULL) childCount++;
if (selection < 0) {
selection = (childCount > 0) ? (childCount - 1) : 0;
}
if (selection >= childCount) {
selection = 0;
}
while (selection < childOffset) {
childOffset--;
}
while (selection >= (childOffset + 7)) {
childOffset++;
}
if (sw2 == -1) {
// Center button
if (selection >= realChildCount) {
// Go up to parent menu...
*entry = parent;
Serial.println(F("Menu: entering parent menu"));
} else {
// Enter sub menu...
*entry = children[selection];
Serial.println(F("Menu: entering sub menu"));
}
redraw = true;
selection = 0;
}
return redraw ? MENU_SHOULD_DRAW : MENU_NO_ERR;
}
int MenuEntry::act(MenuEntry **entry) {
if (actFunc != NULL) {
return actFunc(entry, userData);
} else if (children != NULL) {
return actChildrenList(entry);
} else {
return MENU_ERR_INVALID_ENTRY;
}
}