-
Notifications
You must be signed in to change notification settings - Fork 49
/
Menu.cpp
154 lines (119 loc) · 5.06 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
// ----------------------------------------------------------------------------
// MicroMenu
// Copyright (c) 2014 [email protected]
// All rights reserved.
// License: MIT
// ----------------------------------------------------------------------------
#include <Menu.h>
// ----------------------------------------------------------------------------
namespace Menu {
// ----------------------------------------------------------------------------
const Item_t NullItem PROGMEM = { (const Menu::Item_s *)NULL, (const Menu::Item_s *)NULL, (const Menu::Item_s *)NULL, (const Menu::Item_s *)NULL, (const Menu::Callback_t)NULL, (const char *)NULL };
// ----------------------------------------------------------------------------
Engine::Engine()
: currentItem(&Menu::NullItem), previousItem(&Menu::NullItem), lastInvokedItem(&Menu::NullItem)
{
}
Engine::Engine(const Item_t *initialItem)
: currentItem(initialItem), previousItem(&Menu::NullItem), lastInvokedItem(&Menu::NullItem)
{
}
// ----------------------------------------------------------------------------
const char * Engine::getLabel(const Item_t * item) const {
return (const char *)pgm_read_word((item == NULL) ? ¤tItem->Label : &item->Label);
}
const Item_t * Engine::getPrev(const Item_t * item) const {
return (const Item_t *)pgm_read_word((item == NULL) ? ¤tItem->Previous : &item->Previous);
}
const Item_t * Engine::getNext(const Item_t * item) const {
return (const Item_t *)pgm_read_word((item == NULL) ? ¤tItem->Next : &item->Next);
}
const Item_t * Engine::getParent(const Item_t * item) const {
return (const Item_t *)pgm_read_word((item == NULL) ? ¤tItem->Parent : &item->Parent);
}
const Item_t * Engine::getChild(const Item_t * item) const {
return (const Item_t *)pgm_read_word((item == NULL) ? ¤tItem->Child : &item->Child);
}
// ----------------------------------------------------------------------------
void Engine::navigate(const Item_t * targetItem) {
uint8_t commit = true;
if (targetItem && targetItem != &Menu::NullItem) {
if (targetItem == getParent(currentItem)) { // navigating back to parent
commit = executeCallbackAction(actionParent); // exit/save callback
lastInvokedItem = &Menu::NullItem;
}
if (commit) {
previousItem = currentItem;
currentItem = targetItem;
executeCallbackAction(actionLabel);
}
}
}
// ----------------------------------------------------------------------------
void Engine::invoke(void) {
bool preventTrigger = false;
if (lastInvokedItem != currentItem) { // prevent 'invoke' twice in a row
lastInvokedItem = currentItem;
preventTrigger = true; // don't invoke 'trigger' at first Display event
executeCallbackAction(actionDisplay);
}
const Item_t *child = getChild();
if (child && child != &Menu::NullItem) { // navigate to registered submenuitem
navigate(child);
}
else { // call trigger in already selected item that has no child
if (!preventTrigger) {
executeCallbackAction(actionTrigger);
}
}
}
// ----------------------------------------------------------------------------
bool Engine::executeCallbackAction(const Action_t action) const {
if (currentItem && currentItem != NULL) {
Callback_t callback = (Callback_t)pgm_read_word(¤tItem->Callback);
if (callback != NULL) {
return (*callback)(action);
}
}
return true;
}
// ----------------------------------------------------------------------------
Info_t Engine::getItemInfo(const Item_t * item) const {
Info_t result = { 0, 0 };
const Item_t * i = getChild(getParent());
for (; i && i != &Menu::NullItem && &i->Next && i->Next != &Menu::NullItem; i = getNext(i)) {
result.siblings++;
if (i == item) {
result.position = result.siblings;
}
}
return result;
}
// ----------------------------------------------------------------------------
void Engine::render(const RenderCallback_t render, uint8_t maxDisplayedMenuItems) const {
if (!currentItem || currentItem == &Menu::NullItem) {
return;
}
uint8_t start = 0;
uint8_t itemCount = 0;
const uint8_t center = maxDisplayedMenuItems >> 1;
Info_t mi = getItemInfo(currentItem);
if (mi.position >= (mi.siblings - center)) { // at end
start = mi.siblings - maxDisplayedMenuItems;
}
else {
start = mi.position - center;
if (maxDisplayedMenuItems & 0x01) start--; // center if odd
}
if (start & 0x80) start = 0; // prevent overflow
// first item in current menu level
const Item_t * i = getChild(getParent());
for (; i && i != &Menu::NullItem && &i->Next && i->Next != &Menu::NullItem; i = getNext(i)) {
if (itemCount - start >= maxDisplayedMenuItems) break;
if (itemCount >= start) render(i, itemCount - start);
itemCount++;
}
}
// ----------------------------------------------------------------------------
}; // end namespace
// ----------------------------------------------------------------------------