Skip to content

Commit b870685

Browse files
committed
introduce idle background compile option
added stm32 example some cosmetics on stm32 example
1 parent f7e544f commit b870685

File tree

5 files changed

+220
-34
lines changed

5 files changed

+220
-34
lines changed

examples/Serial/ansiSerial/ansiSerial/ansiSerial.ino

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ www.r-site.net
2020
#include <menu.h>
2121
#include <menuIO/ansiSerialOut.h>
2222
#include <menuIO/serialIn.h>
23+
// #include <streamFlow.h>
24+
#include <AnsiStream.h>
2325

2426
using namespace Menu;
27+
using namespace ANSI;
2528

2629
#ifndef DEBUG
2730
Print& operator<<(Print&o, Menu::prompt&p) {
@@ -276,8 +279,8 @@ result alert(menuOut& o,idleEvent e) {
276279
if (e==idling)
277280
o <<ANSI::xy(0,0)
278281
<<ANSI::setBackgroundColor(BLACK)
279-
<<ANSI::setForegroundColor(WHITE)
280-
<<"alert test"
282+
<<ANSI::setForegroundColor(WHITE);
283+
Serial<<"alert test"
281284
<<endl
282285
<<"press [select] to continue..."
283286
<<endl;
@@ -294,11 +297,11 @@ result idle(menuOut& o,idleEvent e) {
294297
switch(e) {
295298
//case idleStart:o<<"suspending menu!"<<endl;break;
296299
case idling:
297-
o
298-
<<ANSI::xy(0,0)
299-
<<ANSI::setBackgroundColor(BLACK)
300-
<<ANSI::setForegroundColor(WHITE)
301-
<<"suspended..."<<endl;
300+
Serial
301+
<<ANSI::xy(0,0)
302+
<<ANSI::setBackgroundColor(BLACK)
303+
<<ANSI::setForegroundColor(WHITE);
304+
Serial<<"suspended..."<<endl;
302305
break;
303306
default: break;
304307
//case idleEnd:o<<"resuming menu."<<endl;break;

examples/Serial/ansiSerial/platformio.ini

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
src_dir=ansiSerial
1212
; lib_dir=~/Arduino/Libraries
1313

14-
[env:redbear_blenano2]
15-
platform = nordicnrf52
16-
board = redbear_blenano2
17-
framework = arduino
18-
build_flags =-DNODEBUG -DARDUINO=10805
19-
20-
; [env:nanoatmega328]
21-
; platform = atmelavr
22-
; board = nanoatmega328
14+
; [env:redbear_blenano2]
15+
; platform = nordicnrf52
16+
; board = redbear_blenano2
2317
; framework = arduino
24-
; upload_port=/dev/ttyUSB*
25-
; upload_flags=-V
26-
; build_flags = -DNODEBUG
18+
; build_flags =-DNODEBUG -DARDUINO=10805
19+
20+
[env:nanoatmega328]
21+
platform = atmelavr
22+
board = nanoatmega328
23+
framework = arduino
24+
upload_port=/dev/ttyUSB*
25+
upload_flags=-V
26+
build_flags = -DNODEBUG
2727

2828
; [env:nanoatmega328]
2929
; platform = atmelavr

examples/lolin32/lolin32/lolin32.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle
8686
,VALUE("Last",-1,doNothing,noEvent)
8787
);
8888

89-
// //customizing a prompt look!
90-
// //by extending the prompt class
91-
// class altPrompt:public prompt {
92-
// public:
93-
// altPrompt(constMEM promptShadow& p):prompt(p) {}
94-
// Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) override {
95-
// return out.printRaw("special prompt!",len);;
96-
// }
97-
// };
89+
//customizing a prompt look!
90+
//by extending the prompt class
91+
class altPrompt:public prompt {
92+
public:
93+
altPrompt(constMEM promptShadow& p):prompt(p) {}
94+
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) override {
95+
return out.printRaw("special prompt!",len);;
96+
}
97+
};
9898

9999
MENU(subMenu,"Sub-Menu",doNothing,noEvent,noStyle
100100
,OP("Sub1",doNothing,noEvent)
101-
// ,altOP(altPrompt,"",doNothing,noEvent)
101+
,altOP(altPrompt,"",doNothing,noEvent)
102102
,EXIT("<Back")
103103
);
104104

@@ -113,8 +113,8 @@ altMENU(menu,time,"Time",doNothing,noEvent,noStyle,(systemStyles)(_asPad|Menu::_
113113
);
114114

115115
char* constMEM hexDigit MEMMODE="0123456789ABCDEF";
116-
char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit};
117-
char buf1[]="0x11";
116+
char* constMEM hexNr[] MEMMODE={hexDigit,hexDigit};
117+
char buf1[]="00";
118118

119119
MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
120120
,OP("Op1",doNothing,noEvent)

examples/stm32/stm32/stm32.ino

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/********************
2+
Arduino generic menu system
3+
4+
Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
5+
6+
output: Serial3
7+
input: Serial3
8+
mcu: stm32f103 (blue pill)
9+
*/
10+
11+
#include <menu.h>
12+
#include <menuIO/serialOut.h>
13+
#include <menuIO/serialIn.h>
14+
15+
using namespace Menu;
16+
17+
#define LEDPIN PC13
18+
19+
result showEvent(eventMask e,navNode& nav,prompt& item) {
20+
Serial3.print("event: ");
21+
Serial3.println(e);
22+
return proceed;
23+
}
24+
25+
float test=55;
26+
27+
result action1(eventMask e) {
28+
Serial3.print(e);
29+
Serial3.println(" action1 executed, proceed menu");
30+
Serial3.flush();
31+
return proceed;
32+
}
33+
34+
result action2(eventMask e, prompt &item) {
35+
Serial3.print(e);
36+
Serial3.print(" action2 executed, quiting menu");
37+
return quit;
38+
}
39+
40+
int ledCtrl=LOW;
41+
42+
result myLedOn() {
43+
ledCtrl=HIGH;
44+
return proceed;
45+
}
46+
result myLedOff() {
47+
ledCtrl=LOW;
48+
return proceed;
49+
}
50+
51+
TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle
52+
,VALUE("On",HIGH,doNothing,noEvent)
53+
,VALUE("Off",LOW,doNothing,noEvent)
54+
);
55+
56+
int selTest=0;
57+
SELECT(selTest,selMenu,"Select",doNothing,noEvent,noStyle
58+
,VALUE("Zero",0,doNothing,noEvent)
59+
,VALUE("One",1,doNothing,noEvent)
60+
,VALUE("Two",2,doNothing,noEvent)
61+
);
62+
63+
int chooseTest=-1;
64+
CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle
65+
,VALUE("First",1,doNothing,noEvent)
66+
,VALUE("Second",2,doNothing,noEvent)
67+
,VALUE("Third",3,doNothing,noEvent)
68+
,VALUE("Last",-1,doNothing,noEvent)
69+
);
70+
71+
//customizing a prompt look!
72+
//by extending the prompt class
73+
class altPrompt:public prompt {
74+
public:
75+
altPrompt(constMEM promptShadow& p):prompt(p) {}
76+
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override {
77+
return out.printRaw("special prompt!",len);
78+
}
79+
};
80+
81+
MENU(subMenu,"Sub-Menu",showEvent,anyEvent,noStyle
82+
,OP("Sub1",showEvent,anyEvent)
83+
,OP("Sub2",showEvent,anyEvent)
84+
,OP("Sub3",showEvent,anyEvent)
85+
,altOP(altPrompt,"",showEvent,anyEvent)
86+
,EXIT("<Back")
87+
);
88+
89+
uint16_t year=2017;
90+
uint16_t month=10;
91+
uint16_t day=7;
92+
93+
//define a pad style menu (single line menu)
94+
//here with a set of fields to enter a date in YYYY/MM/DD format
95+
//altMENU(menu,birthDate,"Birth",doNothing,noEvent,noStyle,(systemStyles)(_asPad|Menu::_menuData|Menu::_canNav|_parentDraw)
96+
PADMENU(birthDate,"Birth",doNothing,noEvent,noStyle
97+
,FIELD(year,"","/",1900,3000,20,1,doNothing,noEvent,noStyle)
98+
,FIELD(month,"","/",1,12,1,0,doNothing,noEvent,wrapStyle)
99+
,FIELD(day,"","",1,31,1,0,doNothing,noEvent,wrapStyle)
100+
);
101+
102+
char* constMEM hexDigit MEMMODE="0123456789ABCDEF";
103+
char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit};
104+
char buf1[]="0x11";
105+
106+
MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
107+
,OP("Op1",action1,anyEvent)
108+
,OP("Op2",action2,enterEvent)
109+
,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle)
110+
,SUBMENU(subMenu)
111+
,SUBMENU(setLed)
112+
,OP("LED On",myLedOn,enterEvent)
113+
,OP("LED Off",myLedOff,enterEvent)
114+
,SUBMENU(selMenu)
115+
,SUBMENU(chooseMenu)
116+
,OP("Alert test",doAlert,enterEvent)
117+
,EDIT("Hex",buf1,hexNr,doNothing,noEvent,noStyle)
118+
,SUBMENU(birthDate)
119+
,EXIT("<Back")
120+
);
121+
122+
#define MAX_DEPTH 2
123+
124+
MENU_OUTPUTS(out,MAX_DEPTH
125+
,SERIAL_OUT(Serial3)
126+
,NONE//must have 2 items at least
127+
);
128+
129+
serialIn serial(Serial3);
130+
NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out);
131+
132+
result alert(menuOut& o,idleEvent e) {
133+
if (e==idling) {
134+
o.setCursor(0,0);
135+
o.print("alert test");
136+
o.setCursor(0,1);
137+
o.print("press [select]");
138+
o.setCursor(0,2);
139+
o.print("to continue...");
140+
}
141+
return proceed;
142+
}
143+
144+
result doAlert(eventMask e, prompt &item) {
145+
nav.idleOn(alert);
146+
return proceed;
147+
}
148+
149+
//when menu is suspended
150+
result idle(menuOut &o, idleEvent e) {
151+
// o.clear();
152+
switch(e) {
153+
case idleStart:o.println("suspending menu!");break;
154+
case idling:o.println("suspended...");break;
155+
case idleEnd:o.println("resuming menu.");break;
156+
}
157+
return proceed;
158+
}
159+
160+
void setup() {
161+
pinMode(LEDPIN,OUTPUT);
162+
digitalWrite(LEDPIN,!ledCtrl);
163+
delay(500);
164+
Serial3.begin(115200);
165+
// while(!Serial);//this locks on stm32
166+
Serial3.println("menu 4.x test");Serial3.flush();
167+
nav.idleTask=idle;//point a function to be used when menu is suspended
168+
// nav.idleOn();//this menu will start on idle state, press select to enter menu
169+
//nav.doInput("323");
170+
nav.timeOut=60;
171+
}
172+
173+
void loop() {
174+
nav.poll();
175+
digitalWrite(LEDPIN, !ledCtrl);
176+
delay(100);//simulate a delay when other tasks are done
177+
}

src/nav.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@
122122
inline navNode& node() const {return path[level];}
123123
inline menuNode& active() const {return *node().target;}
124124
inline prompt& selected() const {return active()[node().sel];}
125-
inline int drawLevel() const {
126-
return level-(navFocus->has((systemStyles)(_parentDraw|_asPad))&&navFocus->has(_menuData));
127-
}
128125
bool changed(const menuOut& out);
129126
inline bool changed(idx_t n) {return changed(out[n]);}
130127
#ifdef MENU_ASYNC
@@ -155,6 +152,9 @@
155152
else {
156153
idleChanged=false;//turn it off here so that sleepTask can force it on again
157154
out.idle(sleepTask,idling);
155+
#ifdef MENU_IDLE_BKGND
156+
if (idleTask!=sleepTask) out.idle(idleTask,idling);
157+
#endif
158158
}
159159
}
160160
inline void poll() {doInput();doOutput();};//fire and forget mode
@@ -169,9 +169,15 @@
169169
idleChanged=true;
170170
active().dirty=true;
171171
out.idle(sleepTask,idleStart);
172+
#ifdef MENU_IDLE_BKGND
173+
if (idleTask!=sleepTask) out.idle(idleTask,idleStart);
174+
#endif
172175
}
173176
inline void idleOff() {
174177
out.idle(sleepTask,idleEnd);
178+
#ifdef MENU_IDLE_BKGND
179+
if (idleTask!=sleepTask) out.idle(idleTask,idleEnd);
180+
#endif
175181
sleepTask=NULL;
176182
active().dirty=true;
177183
out.clear();

0 commit comments

Comments
 (0)