-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuPCD8544.cpp
257 lines (228 loc) · 7.34 KB
/
MenuPCD8544.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
/*
* MenuPCD8544 - Simple menus for LCD4884 shield
* (Nokia 5110 display + 5-way analog button)
*
* Copyright (c) 2014 oryshed <oryshed@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Changelog:
* 2014-08-27:
* corrected bug where the display was not properly refreshed if the
* submenu contained more items then the parent menu
*/
#ifndef MenuPCD8544_h
#include "MenuPCD8544.h"
#endif
//#define DEBUG_MENU_PCD8544
int8_t MenuPCD8544::submenuTrail[] =
{ -1, -1, -1 };
MenuPCD8544::MenuPCD8544(const menu_t *menu)
{
this->numItems = pgm_read_byte(&(menu->numItems));
this->menu = (menuItem_t *) pgm_read_word(&(menu->items));
this->currentItem = 0;
}
//void MenuPCD8544::begin(PCD8544 *lcd)
void MenuPCD8544::begin(Adafruit_PCD8544 *lcd)
{
#ifdef DEBUG_MENU_PCD8544
Serial.println(F("begin:entered"));
#endif
this->lcd = lcd;
display();
}
void MenuPCD8544::stop()
{
clearSubmenuTrail();
clear();
}
int8_t MenuPCD8544::navigate(char keyCode, menuFunc_t *func )
{
if (submenuTrail[0] == -1) // first call to menu?
{
submenuTrail[0] = 0; // set first menu item as current item
currentItem = 0;
display(); // show the top menu level
#ifdef DEBUG_MENU_PCD8544
Serial.println(F("navigate: first call"));
#endif
}
#ifdef DEBUG_MENU_PCD8544
Serial.print(F("navigate:keyCode=")); Serial.println(keyCode);
#endif
int8_t result = navigateSubmenu(0, keyCode, func);
if (submenuTrail[0] == -1)
{ *func = canceled;
result = 1;
#ifdef DEBUG_MENU_PCD8544
Serial.println(F("navigate: canceled"));
#endif
}
if (result == 1)
{ clear(); // remove main menu from the screen
clearSubmenuTrail();
}
#ifdef DEBUG_MENU_PCD8544
Serial.print(F("navigate: result=")); Serial.println(result);
#endif
return result;
}
int8_t MenuPCD8544::navigateSubmenu(int8_t level, char keyCode, menuFunc_t *func)
{
int8_t oldItem;
int8_t result = 0;
#ifdef DEBUG_MENU_PCD8544
Serial.print(F("navigateSub:entered"));
Serial.print(F(", level="));
Serial.print(level);
Serial.print(F(", item="));
Serial.println(currentItem);
#endif
currentItem = submenuTrail[level];
if ((level < (MAX_MENU_DEPTH - 1)) && (submenuTrail[level + 1] != -1))
{
MenuPCD8544 submenu = MenuPCD8544(
(menu_t *) pgm_read_word( &(menu[currentItem].doit) ));
submenu.currentItem = submenuTrail[level+1];
submenu.begin(lcd);
result = submenu.navigateSubmenu(level + 1, keyCode, func);
if (submenuTrail[level+1] == -1) // was submenu canceled?
display();
}
else
switch (keyCode)
{
case 'U': // key UP
oldItem = currentItem;
currentItem--;
if (currentItem < 0)
currentItem = numItems - 1;
submenuTrail[level] = currentItem; // save the current item
displayItem(oldItem);
displayItem(currentItem);
result = 0;
break;
case 'D': // key DOWN
oldItem = currentItem;
currentItem = (currentItem + 1) % numItems;
submenuTrail[level] = currentItem; // save the current item
displayItem(oldItem);
displayItem(currentItem);
result = 0;
break;
case 'L': // key LEFT
clear();
submenuTrail[level] = -1; // return from submenu
result = 0;
break;
case 'R': // key RIGHT
case 'S': // key SELECT
{
PGM_P menuItemText = getItemText(currentItem);
if (isSubmenu(menuItemText))
{ submenuTrail[level+1] = 0; // remember we're in the submenu, select the first item
clear(); // now display the submenu
MenuPCD8544 submenu = MenuPCD8544((menu_t *) pgm_read_word( &(menu[currentItem].doit) ));
submenu.begin(lcd);
submenu.display();
#ifdef DEBUG_MENU_PCD8544
Serial.println(F("submenu displayed"));
#endif
result = 0;// no selection done yet
}
else
{ *func = (menuFunc_t) pgm_read_word( &(menu[currentItem].doit) );
clear();
result = 1;
}
break;
}
default:
result = 0; // unknown key, no selection is made
break;
} // end SELECT
#ifdef DEBUG_MENU_PCD8544
Serial.print(F("navigateSub:exit"));
Serial.print(F(", level="));
Serial.print(level);
Serial.print(F(", item="));
Serial.print(currentItem);
Serial.print(F(", result="));
Serial.println(result);
#endif
return result;
}
void MenuPCD8544::display()
{
for (int8_t i = 0; i < numItems; i++)
{
this->lcd->setCursor(0, i*8+1);
///lcd->clearLine();
this->lcd->print(" ");
displayItem(i);
}
}
void MenuPCD8544::clear()
{
for (int8_t i = 0; i < numItems; i++)
{ this->lcd->setCursor(0, i*8+1);
//lcd->clearLine();
this->lcd->print(" ");
}
}
void MenuPCD8544::clearSubmenuTrail()
{
currentItem = 0;
for (uint8_t i=0; i<MAX_MENU_DEPTH; i++)
submenuTrail[i] = -1; // no items selected at any level
}
void MenuPCD8544::displayItem(int8_t i)
{
PGM_P menuItemText;
//lcd->setInverse(i == currentItem);
if(i == currentItem)
lcd->setTextColor(WHITE, BLACK);
this->lcd->setCursor(0,i*8+1);
this->lcd->print(" ");
this->lcd->setCursor(0,i*8+1);
menuItemText = getItemText(i);
if (isSubmenu(menuItemText)) menuItemText++;
this->lcd->print(FS(menuItemText));
#ifdef DEBUG_MENU_PCD8544
Serial.write(i == currentItem ? '*' : ' ');
Serial.println(FS(menuItemText));
#endif
//lcd->setInverse(false); //TODO: restore the previous state instead of forcing the normal mode
this->lcd->setTextColor(BLACK, WHITE);
this->lcd->drawLine(0, (i+1)*8, lcd->width()-7, (i+1)*8, BLACK);
this->lcd->drawLine(lcd->width()-7, i*8, lcd->width()-7, (i+1)*8, BLACK);
this->lcd->display();
}
PGM_P MenuPCD8544::getItemText(int8_t i)
{
return (PGM_P)pgm_read_word( &(menu[i].item));
}
boolean MenuPCD8544::isSubmenu(PGM_P menuItemText)
{
if (pgm_read_byte(menuItemText) == ':')
return true;
else
return false;
}