Skip to content

Commit 5404157

Browse files
committed
reuse menu structure example
1 parent 2e228bb commit 5404157

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

examples/reuse/reuse.ino

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/********************
2+
Arduino generic menu system
3+
Serial menu example
4+
http://www.r-site.net/?at=//op%5B%40id=%273090%27%5D
5+
6+
Sept.2014 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
7+
creative commons license 3.0: Attribution-ShareAlike CC BY-SA
8+
This software is furnished "as is", without technical support, and with no
9+
warranty, express or implied, as to its usefulness for any purpose.
10+
11+
Thread Safe: No
12+
Extensible: Yes
13+
*/
14+
#include <menu.h>//menu macros and objects
15+
#include <menuFields.h>
16+
#include <menuPrint.h>
17+
//#include <streamFlow.h>
18+
19+
struct Timer {
20+
bool use=false;
21+
enum {alarm,countDown} type=alarm;
22+
int h=0,m=0,s=0;
23+
};
24+
25+
Timer tmp;
26+
Timer alarms[3];
27+
28+
promptFeedback cancel() {return true;}
29+
promptFeedback setCurAlarm();
30+
promptFeedback setupAlarm();
31+
32+
/////////////////////////////////////////////////////////////////////////
33+
// MENU DEFINITION
34+
35+
// menu for setup an alarm, we will reuse this for all alarms
36+
TOGGLE(tmp.use,setUse,"Use",VALUE("Yes",true),VALUE("No",false));
37+
SELECT(tmp.type,setType,"Type"
38+
,VALUE("Alarm",Timer::alarm)
39+
,VALUE("Countdown",Timer::countDown)
40+
);
41+
MENU(alarmDef,"Alarm def"
42+
,SUBMENU(setUse)
43+
,SUBMENU(setType)
44+
,FIELD(tmp.h,"Hour","",0,24,1,0)
45+
,FIELD(tmp.m,"Minuts","",0,60,5,1)
46+
,FIELD(tmp.s,"Seconds","",0,60,5,1)
47+
,OP("Ok",setupAlarm)
48+
);
49+
50+
//menu with list of alarms
51+
MENU(mainMenu,"Alarms"
52+
,OP("Alarm 1",setCurAlarm)
53+
,OP("Alarm 2",setCurAlarm)
54+
,OP("Alarm 3",setCurAlarm)
55+
);
56+
57+
//when click ok on alarm
58+
promptFeedback setCurAlarm() {
59+
tmp=alarms[mainMenu.sel];
60+
menuNode::activeNode=&alarmDef;
61+
return false;
62+
}
63+
64+
//when an alarm is selected
65+
promptFeedback setupAlarm() {
66+
Serial.println("Ok");
67+
alarms[mainMenu.sel]=tmp;
68+
return cancel();
69+
}
70+
71+
menuPrint menu_out(Serial);//describe output device
72+
73+
void setup() {
74+
Serial.begin(115200);
75+
while(!Serial);
76+
Serial.println("menu system test");
77+
delay(1000);
78+
}
79+
80+
void loop() {
81+
mainMenu.poll(menu_out,Serial);
82+
delay(100);
83+
}

src/menuPrint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ www.r-site.net
3838
int i=0;
3939
for(;i<m.sz;i++)
4040
printPrompt(*(prompt*)pgmPtrNear(m.data[i]),i==m.sel,i+1,i-top,0,m.width);
41-
if (drawExit) printPrompt(menu::exitOption,m.sel==m.sz,i,0,i-top,m.width);
41+
if (drawExit) printPrompt(menu::exitOption,m.sel==m.sz,0,i-top,0,m.width);
4242
lastTop=top;
4343
lastSel=m.sel;
4444
drawn=&m;

0 commit comments

Comments
 (0)