Skip to content

Commit f5a54ba

Browse files
committed
dont call funtions on initialization
static adata should not call functions otherwise we get this weird initialization order problem
1 parent 669c241 commit f5a54ba

File tree

3 files changed

+190
-6
lines changed

3 files changed

+190
-6
lines changed

examples/btnNav/btnNav/btnNav.ino

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/* Arduino menu library example
2+
Oct. 2016 Rui Azevedo ([email protected]) www.r-site.net
3+
4+
Use the menu library with user code ctrl command
5+
6+
calling doNav with user command mode:
7+
8+
noCmd - clamp field values or do nothing
9+
escCmd - exit
10+
enterCmd - enter current option or validate field and exit
11+
upCmd - move up or increment field value
12+
downCmd - move down or decrement field value
13+
leftCmd - move left or escape (not tested yet)
14+
rightCmd - move right or enter (not tested yet)
15+
idxCmd - enter option by index
16+
17+
this mode allows you to implement ANY input device
18+
19+
on this example only using
20+
21+
*/
22+
23+
#include <Arduino.h>
24+
#include <menu.h>
25+
#include <menuIO/serialIn.h>
26+
#include <menuIO/serialOut.h>
27+
#include <menuIO/keyIn.h>
28+
#include <menuIO/chainStream.h>
29+
30+
using namespace Menu;
31+
32+
#define LEDPIN 13
33+
#define MAX_DEPTH 2
34+
35+
#define BTN_SEL 6 // Select button
36+
#define BTN_UP 7 // Up
37+
#define BTN_DOWN 8 // Down
38+
39+
result doAlert(eventMask e, prompt &item);
40+
41+
result showEvent(eventMask e) {
42+
Serial.print("event: ");
43+
Serial.println(e);
44+
return proceed;
45+
}
46+
47+
int test=55;
48+
49+
result action1(eventMask e,navNode& nav, prompt &item) {
50+
Serial.print("action1 event:");
51+
Serial.println(e);
52+
Serial.flush();
53+
return proceed;
54+
}
55+
56+
result action2(eventMask e) {
57+
Serial.print("actikon2 event:");
58+
Serial.println(e);
59+
Serial.flush();
60+
return quit;
61+
}
62+
63+
int ledCtrl=LOW;
64+
65+
result ledOn() {
66+
ledCtrl=HIGH;
67+
return proceed;
68+
}
69+
result ledOff() {
70+
ledCtrl=LOW;
71+
return proceed;
72+
}
73+
74+
TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,wrapStyle//,doExit,enterEvent,noStyle
75+
,VALUE("On",HIGH,doNothing,noEvent)
76+
,VALUE("Off",LOW,doNothing,noEvent)
77+
);
78+
79+
int selTest=0;
80+
SELECT(selTest,selMenu,"Select",doNothing,noEvent,wrapStyle
81+
,VALUE("Zero",0,doNothing,noEvent)
82+
,VALUE("One",1,doNothing,noEvent)
83+
,VALUE("Two",2,doNothing,noEvent)
84+
);
85+
86+
int chooseTest=-1;
87+
CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,wrapStyle
88+
,VALUE("First",1,doNothing,noEvent)
89+
,VALUE("Second",2,doNothing,noEvent)
90+
,VALUE("Third",3,doNothing,noEvent)
91+
,VALUE("Last",-1,doNothing,noEvent)
92+
);
93+
94+
//customizing a prompt look!
95+
//by extending the prompt class
96+
class altPrompt:public prompt {
97+
public:
98+
altPrompt(constMEM promptShadow& p):prompt(p) {}
99+
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override {
100+
return out.printRaw("special prompt!",len);;
101+
}
102+
};
103+
104+
MENU(subMenu,"Sub-Menu",doNothing,anyEvent,wrapStyle
105+
,OP("Sub1",showEvent,enterEvent)
106+
,OP("Sub2",showEvent,enterEvent)
107+
,OP("Sub3",showEvent,enterEvent)
108+
,altOP(altPrompt,"",showEvent,enterEvent)
109+
,EXIT("<Back")
110+
);
111+
112+
MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
113+
,OP("Op1",action1,anyEvent)
114+
,OP("Op2",action2,enterEvent)
115+
//,SUBMENU(togOp)
116+
,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle)
117+
,SUBMENU(subMenu)
118+
,SUBMENU(setLed)
119+
,OP("LED On",ledOn,enterEvent)
120+
,OP("LED Off",ledOff,enterEvent)
121+
,SUBMENU(selMenu)
122+
,SUBMENU(chooseMenu)
123+
,OP("Alert test",doAlert,enterEvent)
124+
,EXIT("<Back")
125+
);
126+
127+
keyMap joystickBtn_map[]={
128+
{-BTN_SEL, defaultNavCodes[enterCmd].ch} ,
129+
{-BTN_UP, defaultNavCodes[upCmd].ch} ,
130+
{-BTN_DOWN, defaultNavCodes[downCmd].ch} ,
131+
};
132+
keyIn<3> joystickBtns(joystickBtn_map);
133+
134+
serialIn serial(Serial);
135+
menuIn* inputsList[]={&joystickBtns,&serial};
136+
chainStream<2> in(inputsList);//3 is the number of inputs
137+
138+
MENU_OUTPUTS(out,MAX_DEPTH
139+
,SERIAL_OUT(Serial)
140+
,NONE
141+
);
142+
143+
NAVROOT(nav,mainMenu,MAX_DEPTH,in,out);
144+
145+
result alert(menuOut& o,idleEvent e) {
146+
if (e==idling) {
147+
o.setCursor(0,0);
148+
o.print("alert test");
149+
o.setCursor(0,1);
150+
o.print("[select] to continue...");
151+
}
152+
return proceed;
153+
}
154+
155+
result doAlert(eventMask e, prompt &item) {
156+
nav.idleOn(alert);
157+
return proceed;
158+
}
159+
160+
void setup() {
161+
Serial.begin(115200);
162+
while(!Serial);
163+
pinMode(LEDPIN, OUTPUT);
164+
// pinMode(BTN_SEL,INPUT_PULLUP);
165+
// pinMode(BTN_UP,INPUT_PULLUP);
166+
// pinMode(BTN_DOWN,INPUT_PULLUP);
167+
joystickBtns.begin();
168+
Serial<<"options->navCodes: 0x"<<hex((int)options->navCodes)<<endl;
169+
Serial<<"options->navCodes["<<noCmd<<"]: 0x"<<hex(options->navCodes[0].ch)<<" 0x"<<hex(options->getCmdChar(noCmd))<<endl;
170+
Serial<<"options->navCodes["<<escCmd<<"]: 0x"<<hex(options->navCodes[1].ch)<<" 0x"<<hex(options->getCmdChar(escCmd))<<endl;
171+
Serial<<"options->navCodes["<<enterCmd<<"]: 0x"<<hex(options->navCodes[2].ch)<<" 0x"<<hex(options->getCmdChar(enterCmd))<<endl;
172+
for(int n=0;n<3;n++) {
173+
Serial<<joystickBtn_map[n].pin<<": 0x"<<hex(joystickBtn_map[n].code)<<"("<<(char)joystickBtn_map[n].code<<")"<<endl;
174+
}
175+
}
176+
177+
#define SOFT_DEBOUNCE_MS 100
178+
179+
void loop() {
180+
nav.poll();//also do serial input
181+
digitalWrite(LEDPIN, ledCtrl);
182+
}

src/menuBase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const navCodesDef Menu::defaultNavCodes={
4343
{scrlUpCmd,0x36}
4444
};
4545

46-
config defaultOptions={'>','-',Menu::defaultNavCodes,false};
46+
config Menu::defaultOptions={'>','-',Menu::defaultNavCodes,false};
4747
config* Menu::options=&defaultOptions;
4848

4949
#ifdef DEBUG
@@ -60,7 +60,7 @@ config* Menu::options=&defaultOptions;
6060
return o;
6161
}
6262

63-
const char* showEvent(eventMask e) {
63+
const char* eventName(eventMask e) {
6464
switch(e) {
6565
case noEvent:return "noEvent";
6666
case activateEvent:return "activateEvent";
@@ -77,12 +77,12 @@ config* Menu::options=&defaultOptions;
7777
}
7878

7979
Print& Menu::operator<<(Print& o,eventMask e) {
80-
if (e==noEvent||e==anyEvent) {o<<showEvent(e); return o;}
80+
if (e==noEvent||e==anyEvent) {o<<eventName(e); return o;}
8181
bool first=true;
8282
for(int n=0;n<=6;n++) {
8383
eventMask t=(eventMask)(1<<n);
8484
if (t&e) {
85-
o<<(first?"":"|")<<showEvent(t);
85+
o<<(first?"":"|")<<eventName(t);
8686
first=false;
8787
}
8888
}

src/menuBase.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ www.r-site.net
195195
inline char getCmdChar(navCmds cmd) const {return navCodes[cmd].ch;}//return character assigned to this command
196196
};
197197

198+
extern config defaultOptions;
199+
extern config* options;
200+
201+
198202
#ifdef DRAW_2D
199203
typedef class Area {
200204
protected:
@@ -225,8 +229,6 @@ www.r-site.net
225229
typedef idx_t Used;
226230
#endif
227231

228-
extern config* options;
229-
230232
#ifdef DEBUG
231233
Print& operator<<(Print& o,bool b);
232234
Print& operator<<(Print& o,navCmds cmd);

0 commit comments

Comments
 (0)