forked from DCC-EX/CommandStation-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuobject.cpp
266 lines (251 loc) · 6.92 KB
/
menuobject.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
/*
* La Box Project
* menu Classes
*
* Base class of the menu library.
* It allows recursively to select entries from lists and to chain menus.
* The up and down button allows you to scroll through the choices and the select button validates the choice.
*
* @Author : Cedric Bellec
* @Organization : Locoduino.org
*/
#include "defines.h"
#include "DCC.h"
#ifdef USE_HMI
#include "hmiGlobals.h"
#include "menuobject.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*!
@brief menuObject Constructor
@param Screen, an Adafruit_SSD1306 object to permit to display our menu
@param p, a parent object. All menu are chained between parent and sons
@param title, a menu has a title, a char[HMI_MenueMessageSize].
@param val, a integer which define the king of menu or the returned value after selection
@return None (void).
@note
*/
menuObject::menuObject()
{
parent = NULL;
}
menuObject::menuObject(Adafruit_SSD1306* screen, menuObject* p, const char* title, int val)
{
parent = p;
display = screen;
selectedMenu = NULL ;
nbSubMenu = 0;
value = val;
position = 0;
memset(caption, 0, HMI_MenueMessageSize);
if(title)
{
memcpy(caption, title, HMI_MenueMessageSize - 1);
}
if(parent)
{
_HMIDEBUG_LEVEL1_PRINT("menuObject: Add child to my parent : ");_HMIDEBUG_LEVEL1_PRINT(parent->caption);_HMIDEBUG_LEVEL1_PRINT("/");_HMIDEBUG_LEVEL1_PRINTLN(caption);
parent->addChild( (menuObject*) this);
}
}
/*!
@brief addChild, add a child to this menu object
@param sub, a menuObject
@return None (void).
@note
*/
void menuObject::addChild(menuObject* sub)
{
if(sub)
{
if(nbSubMenu < nbMaxSubmenu)
{
subMenu[nbSubMenu++] = sub;
}else
{
_HMIDEBUG_CRITICAL_PRINT("Critical error, you want add to much sub menus ! Menu ");_HMIDEBUG_CRITICAL_PRINT(caption);
_HMIDEBUG_CRITICAL_PRINT(", sub menu ");_HMIDEBUG_CRITICAL_PRINTLN(sub->caption);
}
}
}
/*!
@brief begin
@param None
@return None (void).
@note
*/
void menuObject::begin()
{
_HMIDEBUG_FCT_PRINTLN("menuObject::begin.. Begin");
resetMenu();
// Recursive call
for(int i=0; i < nbSubMenu; i++)
{
if(subMenu[i])
{
subMenu[i]->begin();
}
}
// Add endobject <come back> for lists
if(value == MENUTYPELIST)
{
endObject = new menuObject(display, this, TXT_MenuBack, MENUTYPECOMEBCK) ;
}
_HMIDEBUG_FCT_PRINTLN("menuObject::begin.. End");
}
/*!
@brief update, call to refresh screen
@param None
@return None (void).
@note
*/
void menuObject::update()
{
_HMIDEBUG_FCT_PRINTLN("menuObject::update.. Begin");
display->clearDisplay();
display->setTextSize(1);
display->setTextColor(WHITE);
display->setCursor(5, 53);
display->println(caption);
display->drawFastHLine(0,48,SCREEN_WIDTH, WHITE);
if(SelectListIndex < NbMaxLineVisible )
{
firstListIndex = 0 ;
}else
{
firstListIndex = (SelectListIndex - NbMaxLineVisible) +1;
}
if(nbSubMenu > NbMaxLineVisible)
{
ListIndexMaxVisible = firstListIndex + NbMaxLineVisible;
}else
{
ListIndexMaxVisible = nbSubMenu;
}
//_HMIDEBUG_LEVEL1_PRINT("firstListIndex : ");_HMIDEBUG_LEVEL1_PRINT(firstListIndex);_HMIDEBUG_LEVEL1_PRINT(", ListIndexMaxVisible : ");_HMIDEBUG_LEVEL1_PRINT(ListIndexMaxVisible);
//_HMIDEBUG_LEVEL1_PRINT(", SelectListIndex : ");_HMIDEBUG_LEVEL1_PRINT(SelectListIndex);_HMIDEBUG_LEVEL1_PRINT(", nbSubMenu : ");_HMIDEBUG_LEVEL1_PRINTLN(nbSubMenu);
for(int i=firstListIndex; i < ListIndexMaxVisible; i++)
{
if(subMenu[i])
{
if(subMenu[i] == selectedMenu) // selectedMenu
{
display->fillRect(0, (i-firstListIndex) * 10, SCREEN_WIDTH, 9 , WHITE);
display->setCursor(5, 1 + (i-firstListIndex) * 10);
display->setTextColor(BLACK);
display->println(subMenu[i]->caption);
display->setTextColor(WHITE);
}else
{
display->setCursor(5, 0 + (i-firstListIndex) * 10);
display->println(subMenu[i]->caption);
}
}
}
display->display();
_HMIDEBUG_FCT_PRINTLN("menuObject::update.. End");
}
/*!
@brief resetMenu,
@param None
@return None (void).
@note
*/
void menuObject::resetMenu()
{
_HMIDEBUG_FCT_PRINTLN("menuObject::resetMenu.. Begin");
// Recursive call
for(int i=0; i < nbSubMenu; i++)
{
if(subMenu[i])
{
subMenu[i]->resetMenu();
}
}
selectedMenu = NULL ;
position = 0 ;
firstListIndex = 0 ;
ListIndexMaxVisible = 0 ;
SelectListIndex = 0 ;
ListIndexMaxVisible = 5; // 5 entries max
selectedMenu = subMenu[firstListIndex];
SelectListIndex = 0 ;
_HMIDEBUG_FCT_PRINTLN("menuObject::resetMenu.. End");
}
/*!
@brief eventUp, Notification of a button event
@param None
@return None (void).
@note
*/
void menuObject::eventUp()
{
_HMIDEBUG_FCT_PRINTLN("menuObject::eventUp.. Begin");
if(SelectListIndex > 0) SelectListIndex--;
selectedMenu = subMenu[SelectListIndex];
_HMIDEBUG_FCT_PRINTLN("menuObject::eventUp.. End");
}
/*!
@brief eventDown, Notification of a button event
@param None
@return None (void).
@note
*/
void menuObject::eventDown()
{
_HMIDEBUG_FCT_PRINTLN("menuObject::eventDown.. Begin");
if(SelectListIndex < nbSubMenu-1) SelectListIndex++;
selectedMenu = subMenu[SelectListIndex];
_HMIDEBUG_FCT_PRINTLN("menuObject::eventDown.. End");
}
/*!
@brief eventSelect, Notification of a button event
@param None
@return None (void).
@note
*/
int menuObject::eventSelect()
{
_HMIDEBUG_CRITICAL_PRINTLN("menuObject::eventSelect.. Begin");
if (selectedMenu != NULL)
{
switch(selectedMenu->value)
{
case MENUTYPECOMEBCK: // User has selected come back
return MENUEXIT;
break;
case MENUTYPELIST: // User has selected a sub menu
return MENUCHANGETOCHILD;
break;
case MENUTRAINADDRREAD: // User has selected a sub menu
case MENUTRAINCVREAD: // User has selected a sub menu
case MENUTRAINCVWRITE: // User has selected a sub menu
case MENUTRAINIDENT: // User has selected a sub menu
case MENUSHUTTLESAMPLE: // User has selected a sub menu
case MENUDCDCCMODE: // User has selected a sub menu
return selectedMenu->value;
break;
default:
return MENUCHOSEN; // It's user selection like Yes or No for exemple
}
}
_HMIDEBUG_CRITICAL_PRINTLN("menuObject::eventSelect.. End");
return 0;
}
/*!
@brief Thing to do before opening the option
@param None
@return None (void).
@note
*/
void menuObject::start()
{
}
void menuObject::displayOptionString(const char *str, bool selected, int x, int y)
{
char message[40];
sprintf(message,"%c%s%c",selected?'>':' ', str, selected?'<':' ');
display->setCursor(x, y);
display->println(message);
}
#endif