-
Notifications
You must be signed in to change notification settings - Fork 112
/
OLEDMenuManager.cpp
407 lines (392 loc) · 13.5 KB
/
OLEDMenuManager.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
/*
See https://github.com/PSHarold/OLED-SSD1306-Menu
for original code, license and documentation
*/
#include "OLEDMenuManager.h"
#include "OLEDMenuFonts.h"
OLEDMenuManager::OLEDMenuManager(SSD1306Wire *display)
: display(display)
, rootItem(registerItem(nullptr, 0, nullptr))
{
pushItem(rootItem);
randomSeed(millis()); // does this work?
}
OLEDMenuItem *OLEDMenuManager::allocItem()
{
OLEDMenuItem *newItem = nullptr;
for (int i = 0; i < OLED_MENU_MAX_ITEMS_NUM; ++i) {
if (!this->allItems[i].used) {
memset(&this->allItems[i], 0, sizeof(OLEDMenuItem));
newItem = &this->allItems[i];
newItem->used = true;
break;
}
}
if (!newItem) {
char msg[40];
sprintf(msg, "Maximum number of items reached: %d", OLED_MENU_MAX_ITEMS_NUM);
panicAndDisable(msg);
}
return newItem;
}
OLEDMenuItem *OLEDMenuManager::registerItem(
OLEDMenuItem *parent,
uint16_t tag,
uint16_t imageWidth,
uint16_t imageHeight,
const uint8_t *xbmImage,
MenuItemHandler handler,
OLEDDISPLAY_TEXT_ALIGNMENT alignment)
{
OLEDMenuItem *newItem = allocItem();
if (alignment == OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER_BOTH) {
alignment = OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER;
}
newItem->imageWidth = imageWidth;
newItem->imageHeight = imageHeight;
newItem->xbmImage = xbmImage;
newItem->tag = tag;
newItem->handler = handler;
newItem->alignment = alignment;
if (parent) {
parent->addSubItem(newItem);
if (parent == rootItem) {
cursor = 0;
itemUnderCursor = rootItem->subItems[0];
}
}
return newItem;
}
OLEDMenuItem *OLEDMenuManager::registerItem(
OLEDMenuItem *parent,
uint16_t tag,
const char *string,
MenuItemHandler handler,
const uint8_t *font,
OLEDDISPLAY_TEXT_ALIGNMENT alignment)
{
OLEDMenuItem *newItem = allocItem();
newItem->str = string;
if (font == nullptr) {
font = DejaVu_Sans_Mono_12;
}
newItem->font = font;
newItem->tag = tag;
newItem->handler = handler;
newItem->alignment = alignment;
if (parent) {
if (parent->numSubItem == OLED_MENU_MAX_SUBITEMS_NUM) {
char msg[50];
sprintf(msg, "Maximum number of sub items reached: %d", OLED_MENU_MAX_SUBITEMS_NUM);
panicAndDisable(msg);
}
parent->addSubItem(newItem);
if (parent == rootItem) {
cursor = 0;
itemUnderCursor = rootItem->subItems[0];
}
}
return newItem;
}
void OLEDMenuManager::drawOneItem(OLEDMenuItem *item, uint16_t yOffset, bool negative)
{
int16_t curScrollOffset = 0;
if (negative) {
this->display->setColor(OLEDDISPLAY_COLOR::WHITE); // display in negative, so fill white first, then draw image in black
this->display->fillRect(0, yOffset, OLED_MENU_WIDTH, item->imageHeight);
this->display->setColor(OLEDDISPLAY_COLOR::BLACK);
} else {
this->display->setColor(OLEDDISPLAY_COLOR::WHITE);
}
uint16_t wrappingOffset = OLED_MENU_WIDTH;
if (item->imageWidth < OLED_MENU_WIDTH) {
// item width <= screen width, no scrolling
curScrollOffset = item->alignOffset;
} else if (item->imageWidth > OLED_MENU_WIDTH && (item == itemUnderCursor || item->alwaysScrolls)) {
#if OLED_MENU_RESET_ALWAYS_SCROLL_ON_SELECTION
// scrolling items will have lead in frames (they stay still for some frames before scrolling)
if (leadInFramesLeft == 0) {
curScrollOffset = item->scrollOffset--;
} else {
--leadInFramesLeft;
}
#else
if (item == itemUnderCursor && !item->alwaysScrolls) {
// non always-scrolling items will have lead in frames (they stay still for some frames before scrolling)
if (leadInFramesLeft == 0) {
curScrollOffset = item->scrollOffset--;
} else {
--leadInFramesLeft;
}
} else {
curScrollOffset = item->scrollOffset--;
}
#endif
int16_t left = item->imageWidth + item->scrollOffset + 1;
wrappingOffset = left + OLED_MENU_WRAPPING_SPACE;
if (left <= 0) {
curScrollOffset = item->scrollOffset = wrappingOffset;
wrappingOffset = OLED_MENU_WIDTH + 1;
}
} else {
// already zeroed, but assign again for easy understanding
curScrollOffset = 0;
}
if (item->str) {
// let OLEDDisplay handle text alignment
this->display->setTextAlignment(item->alignment);
this->display->setFont(item->font);
this->display->drawString(curScrollOffset, yOffset, item->str);
if (wrappingOffset < OLED_MENU_WIDTH) {
this->display->drawString(wrappingOffset, yOffset, item->str);
}
} else {
this->display->drawXbm(curScrollOffset, yOffset, item->imageWidth, item->imageHeight, item->xbmImage);
if (wrappingOffset < OLED_MENU_WIDTH) {
this->display->drawXbm(wrappingOffset, yOffset, item->imageWidth, item->imageHeight, item->xbmImage);
}
}
}
void OLEDMenuManager::drawSubItems(OLEDMenuItem *parent)
{
display->clear();
drawStatusBar(itemUnderCursor == nullptr);
uint16_t yOffset = OLED_MENU_STATUS_BAR_HEIGHT;
uint8_t targetPage = itemUnderCursor == nullptr ? 0 : itemUnderCursor->pageInParent;
for (int i = 0; i < parent->numSubItem; ++i) {
OLEDMenuItem *subItem = parent->subItems[i];
#if OLED_MENU_OVER_DRAW
if (subItem->pageInParent >= targetPage) {
#else
if (subItem->pageInParent == targetPage) {
#endif
bool negative = subItem == itemUnderCursor;
drawOneItem(subItem, yOffset, negative);
yOffset += subItem->imageHeight;
if (itemUnderCursor == parent->subItems[i]) {
cursor = i;
}
if (subItem->pageInParent > targetPage) {
// over-draw one item that belongs to the next page in case it is big and leaves a huge blank on current page
break;
}
}
}
display->display();
}
void OLEDMenuManager::goBack(bool preserveCursor)
{
// go back one page
if (peakItem() != rootItem) {
// cannot pop root
if (state == OLEDMenuState::FREEZING) {
unfreeze();
}
popItem(preserveCursor);
}
resetScroll();
state = OLEDMenuState::GOING_BACK;
}
void OLEDMenuManager::goMain(bool preserveCursor)
{
while (peakItem() != rootItem) {
goBack(preserveCursor);
}
}
void OLEDMenuManager::nextItem()
{
resetScroll();
OLEDMenuItem *parentItem = peakItem();
if (!parentItem->numSubItem) {
// shouldn't happen
return;
}
if (itemUnderCursor == nullptr) {
// status bar is selected
itemUnderCursor = parentItem->subItems[cursor = 0];
} else if (cursor == parentItem->numSubItem - 1) {
if (parentItem == rootItem) {
// cannot select status bar on main menu, wrap around
itemUnderCursor = parentItem->subItems[cursor = 0];
} else {
// select status bar next
itemUnderCursor = nullptr;
}
} else {
itemUnderCursor = parentItem->subItems[++cursor];
}
}
void OLEDMenuManager::prevItem()
{
resetScroll();
OLEDMenuItem *parentItem = peakItem();
if (!parentItem->numSubItem) {
// shouldn't happen
return;
}
if (itemUnderCursor == nullptr) {
// status bar is selected
cursor = parentItem->numSubItem - 1;
itemUnderCursor = parentItem->subItems[cursor];
} else if (cursor == 0) {
if (parentItem == rootItem) {
// cannot select status bar on main menu, wrap around
cursor = parentItem->numSubItem - 1;
itemUnderCursor = parentItem->subItems[cursor];
} else {
// select status bar next
itemUnderCursor = nullptr;
}
} else {
itemUnderCursor = parentItem->subItems[--cursor];
}
}
void OLEDMenuManager::resetScroll()
{
leadInFramesLeft = OLED_MENU_SCROLL_LEAD_IN_FRAMES;
if (itemUnderCursor && itemUnderCursor->parent) {
OLEDMenuItem *parent = itemUnderCursor->parent;
for (int i = 0; i < itemUnderCursor->parent->numSubItem; ++i) {
if (!OLED_MENU_RESET_ALWAYS_SCROLL_ON_SELECTION && parent->subItems[i]->alwaysScrolls) {
continue;
}
parent->subItems[i]->scrollOffset = 0;
}
}
lastUpdateTime = 0;
}
void OLEDMenuManager::enterItem(OLEDMenuItem *item, OLEDMenuNav btn, bool isFirstTime)
{
bool willEnter = true;
if (this->state == OLEDMenuState::IDLE) {
pushItem(item);
this->state = OLEDMenuState::ITEM_HANDLING;
}
if (item->handler) {
// Notify the handler that we're about to enter the sub menu and draw sub items of this item, or we're now freezing.
// You can dynamically edit the sub items, or clear all the items and register new ones.
// Handlers can return "false" to stop the manager from entering the sub menu if some conditions are not met.
// If there are no sub items, then the return value does not matter.
// In addition, a handler can call freeze() to block the manager and print custom stuff. If so,
// the manager will continously call the handler until unfreeze() is called.
// And until then, the handler is responsible for handling inputs.
//
willEnter = item->handler(this, item, btn, isFirstTime);
if (this->state == OLEDMenuState::FREEZING) {
return;
}
if (this->state == OLEDMenuState::GOING_BACK) {
state = OLEDMenuState::IDLE;
return;
}
}
this->state = OLEDMenuState::IDLE;
if (!willEnter || !item->numSubItem) {
popItem();
return;
}
resetScroll();
cursor = 0;
this->itemUnderCursor = item->subItems[0];
}
void OLEDMenuManager::drawStatusBar(bool negative)
{
if (negative) {
this->display->setColor(OLEDDISPLAY_COLOR::WHITE); // display in negative, so fill white first, then draw in black
this->display->fillRect(0, 0, OLED_MENU_WIDTH, OLED_MENU_STATUS_BAR_HEIGHT);
this->display->setColor(OLEDDISPLAY_COLOR::BLACK);
} else {
this->display->setColor(OLEDDISPLAY_COLOR::WHITE);
}
if (peakItem() != rootItem) {
// not on main menu, draw back button
this->display->drawXbm(0, 0, IMAGE_ITEM(OM_STATUS_BAR_BACK));
} else {
// main menu, draw some custom info
this->display->drawXbm(0, 0, IMAGE_ITEM(OM_STATUS_CUSTOM));
}
static uint8_t totalItems = 0;
uint8_t curIndex = 1;
this->display->setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_RIGHT);
if (itemUnderCursor) {
curIndex = cursor + 1;
// itemUnderCursor must have a parent
totalItems = itemUnderCursor->parent->numSubItem;
// TODO Adjust to OLED_MENU_STATUS_BAR_HEIGHT
this->display->setFont(DejaVu_Sans_Mono_10);
this->display->drawStringf(OLED_MENU_WIDTH, 1, statusBarBuffer, "%d/%d", curIndex, totalItems);
} else {
this->display->drawString(OLED_MENU_WIDTH, 0, " ");
}
this->display->setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_LEFT);
}
OLEDMenuItem *OLEDMenuManager::popItem(bool preserveCursor)
{
if (itemSP == 1) {
return rootItem; // cannot pop root
}
OLEDMenuItem *top = itemStack[--itemSP];
if (preserveCursor) {
for (int i = 0; i < top->parent->numSubItem; ++i) {
// May need a better way. Maybe store indexes in OLEDMenuItems? But is it worth it?
if (top == top->parent->subItems[i]) {
cursor = i;
itemUnderCursor = top;
return top;
}
}
panicAndDisable("invalid item: item cannot be found in its parent's subItems");
return nullptr;
}
cursor = 0;
itemUnderCursor = top->parent->subItems[0];
return top;
}
void OLEDMenuManager::tick(OLEDMenuNav nav)
{
if (this->disabled) {
return;
}
if (state == OLEDMenuState::FREEZING) {
// handlers do not get affected by OLED_MENU_REFRESH_INTERVAL_IN_MS and will get called with every tick
enterItem(peakItem(), nav, false);
return;
}
unsigned long curMili = millis();
if (nav == OLEDMenuNav::IDLE) {
if (curMili - lastKeyPressedTime > OLED_MENU_SCREEN_SAVER_KICK_IN_SECONDS * 1000) {
if (curMili - screenSaverLastUpdateTime > OLED_MENU_SCREEN_SAVER_REFRESH_INTERVAL_IN_MS) {
drawScreenSaver();
screenSaverLastUpdateTime = curMili;
}
return;
} else if (curMili - this->lastUpdateTime < OLED_MENU_REFRESH_INTERVAL_IN_MS) {
return;
}
} else {
lastKeyPressedTime = curMili;
}
switch (nav) {
case OLEDMenuNav::UP:
prevItem();
break;
case OLEDMenuNav::DOWN:
nextItem();
break;
case OLEDMenuNav::ENTER:
if (itemUnderCursor) {
enterItem(itemUnderCursor, OLEDMenuNav::IDLE, true);
} else {
// status bar is selected
goBack();
this->state = OLEDMenuState::IDLE;
}
break;
default:
break;
}
if (this->state != OLEDMenuState::FREEZING) {
drawSubItems(peakItem());
this->lastUpdateTime = curMili;
}
}