Skip to content

Commit 1137891

Browse files
committed
userMenu exit option
1 parent c16837d commit 1137891

File tree

7 files changed

+76
-49
lines changed

7 files changed

+76
-49
lines changed

examples/targetSel/platformio.ini

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
[platformio]
22
src_dir=targetSel
33

4-
[env:seeeduino_lorawan]
5-
board=seeeduino_lorawan
6-
platform = atmelsam
7-
framework = arduino
8-
build_flags =
9-
-DMENU_DEBUG
10-
11-
; [env:nanoatmega328]
12-
; platform = atmelavr
13-
; board = nanoatmega328new
4+
; [env:seeeduino_lorawan]
5+
; board=seeeduino_lorawan
6+
; platform = atmelsam
147
; framework = arduino
15-
; ; upload_port=/dev/ttyUSB1
16-
; upload_flags=-V
178
; build_flags =
189
; -DMENU_DEBUG
19-
; -Wno-strict-aliasing -Wno-sign-compare -Wno-write-strings
10+
11+
[env:nanoatmega328]
12+
platform = atmelavr
13+
board = nanoatmega328
14+
; board = nanoatmega328new
15+
framework = arduino
16+
; upload_port=/dev/ttyUSB1
17+
upload_flags=-V
18+
build_flags =
19+
-DMENU_DEBUG
20+
-Wno-strict-aliasing -Wno-sign-compare -Wno-write-strings
2021

2122
; [env:esp12e]
2223
; platform = espressif8266

examples/targetSel/targetSel/targetSel.ino

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Data myTargets[dataSz];//our data
5151
Data target;
5252

5353
//characters allowed on name field
54-
char* constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\|!\"#$%&/()=?~*^+-{}[]€";
55-
char* constMEM alphaNumMask[] MEMMODE={alphaNum};
54+
const char* constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\|!\"#$%&/()=?~*^+-{}[]€";
55+
const char* constMEM alphaNumMask[] MEMMODE={alphaNum};
5656

5757
//some enumeration of values allowed on some fields
5858
TOGGLE(target.valid,editValid,"Valid: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle
@@ -75,8 +75,9 @@ CHOOSE(target.chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle
7575

7676
//a function to save the edited data record
7777
result saveTarget(eventMask e,navNode& nav) {
78-
_trace(MENU_DEBUG_OUT<<"saveTarget"<<endl);
79-
idx_t n=nav.root->path[nav.root->level-1].sel;//get selection of previous level
78+
trace(MENU_DEBUG_OUT<<"saveTarget"<<endl);
79+
navNode& nn=nav.root->path[nav.root->level-1];
80+
idx_t n=nn.sel;//get selection of previous level
8081
myTargets[n]=target;
8182
return quit;
8283
}
@@ -94,19 +95,15 @@ MENU(targetEdit,"Target edit",doNothing,noEvent,wrapStyle
9495

9596
//handling the user menu selection
9697
//this will copy the selected recotd into the temp var
97-
result targetEvent(eventMask e,navNode& nav) {
98-
_trace(MENU_DEBUG_OUT<<"copy data to temp.\n");
99-
target=myTargets[nav.sel];
100-
//you can store nav.sel for future reference
101-
return proceed;
102-
}
98+
result targetEvent(eventMask e,navNode& nav);
10399

104100
//the customized print of records
105101
//menu system wil use this to print the list of all records
106102
struct TargetMenu:UserMenu{
107103
using UserMenu::UserMenu;
108104

109105
// override sz() function to have variable/custom size
106+
// if using exit option the an extra element has to be considered...
110107
// inline idx_t sz() const override {return 0;}
111108

112109
//customizing the print of user menu item (len is the availabe space)
@@ -120,7 +117,8 @@ struct TargetMenu:UserMenu{
120117
//build the user menu object, optionally giving a sub menu
121118
#ifdef MENU_USERAM
122119
//for non-AVR devices or when MENU_USERAM is defined at compiler level
123-
TargetMenu targetsMenu("Targets",dataSz,targetEdit,targetEvent,enterEvent);
120+
TargetMenu targetsMenu("Targets",dataSz,"<Back",targetEdit,targetEvent,enterEvent);
121+
// TargetMenu targetsMenu("Targets",dataSz,targetEdit,targetEvent,enterEvent);//no exit option
124122
#else
125123
//menu allocation compatible with AVR flash ---------------------------------
126124
constMEM char targetsMenuTitle[] MEMMODE="Targets";
@@ -134,14 +132,23 @@ struct TargetMenu:UserMenu{
134132
NULL
135133
};
136134
constMEM menuNodeShadow& targetsMenuInfo=*(menuNodeShadow*)&targetsMenuInfoRaw;
137-
TargetMenu targetsMenu(targetsMenuInfo,targetEdit);
135+
TargetMenu targetsMenu(targetsMenuInfo,targetEdit,"<Back");
136+
// TargetMenu targetsMenu(targetsMenuInfo,targetEdit);//no exit option
138137
#endif
139138

140139
MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
141140
,OBJ(targetsMenu)//attach the array edit menu on a macri build nenu
142141
,EXIT("<Back")
143142
);
144143

144+
result targetEvent(eventMask e,navNode& nav) {
145+
trace(MENU_DEBUG_OUT<<"copy data to temp target:"<<(int)nav.target<<"\n");
146+
if(nav.target==&targetsMenu)//only if we are on targetsMenu
147+
target=myTargets[nav.sel];
148+
//you can store nav.sel for future reference
149+
return proceed;
150+
}
151+
145152
//////////////////////////////////////////////////////////////////////////////
146153
// menu IO and root navigation control
147154

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.21.2
2+
version=4.21.3
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=Generic menu/interactivity system

src/items.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
class menuNode:public navTarget {
247247
public:
248248
inline menuNode(constMEM menuNodeShadow& s):navTarget(s) {}
249-
inline menuNode(constText* text,idx_t sz,prompt* constMEM data[],action a=noAction,eventMask e=noEvent,styles style=wrapStyle,systemStyles ss=(systemStyles)(_menuData|_canNav))
249+
inline menuNode(const char* text,idx_t sz,prompt* constMEM data[],action a=noAction,eventMask e=noEvent,styles style=wrapStyle,systemStyles ss=(systemStyles)(_menuData|_canNav))
250250
:navTarget(*new menuNodeShadow(text,sz,data,a,e,style,ss)) {}
251251
#ifdef MENU_FMT_WRAPS
252252
virtual classes type() const;

src/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define USING_RAM
4646
#define MEMMODE
4747
#define constMEM
48-
#define constText char
48+
#define constText const char
4949
#define memPtr(src) (src)
5050
#define memByte(addr) (*addr)
5151
#define memWord(addr) (*addr)

src/plugin/userMenu.h

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,54 @@ namespace Menu {
1616
class UserMenu:public menuNode {
1717
public:
1818
menu* subMenu=NULL;
19-
UserMenu(constMEM menuNodeShadow& shadow):menuNode(shadow) {}
20-
UserMenu(constMEM menuNodeShadow& shadow,menu& subMenu):menuNode(shadow),subMenu(&subMenu) {}
21-
UserMenu(constText* text,idx_t size,menu& subMenu,action a=noAction,eventMask e=noEvent,styles style=wrapStyle)
19+
const char*backTitle=NULL;
20+
UserMenu(constMEM menuNodeShadow& shadow,const char*backTitle=NULL)
21+
:menuNode(shadow),backTitle(backTitle) {}
22+
UserMenu(constMEM menuNodeShadow& shadow,menu& subMenu,const char*backTitle=NULL)
23+
:menuNode(shadow),subMenu(&subMenu),backTitle(backTitle) {}
24+
UserMenu(const char* text,idx_t size,menu& subMenu,action a=noAction,eventMask e=noEvent,styles style=wrapStyle)
2225
:menuNode(text,size,NULL,a,e,style,(systemStyles)(_menuData|_canNav)),subMenu(&subMenu) {}
26+
UserMenu(const char* text,idx_t size,const char*backTitle,menu& subMenu,action a=noAction,eventMask e=noEvent,styles style=wrapStyle)
27+
:menuNode(text,size,NULL,a,e,style,(systemStyles)(_menuData|_canNav)),subMenu(&subMenu),backTitle(backTitle) {}
28+
29+
inline idx_t sz() const override {return menuNode::sz()+(backTitle?1:0);}
2330

2431
prompt& operator[](idx_t i) const override {return *(prompt*)this;}
2532
virtual Used printItem(menuOut& out, int idx,int len)=0;
2633
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t p) override {
2734
if(root.navFocus!=this) return menuNode::printTo(root,sel,out,idx,len,p);
28-
return idx<0?
29-
menuNode::printTo(root,sel,out,idx,len,p)://print the title
30-
printItem(out,out.tops[root.level]+idx,len);
35+
if(idx<0) return menuNode::printTo(root,sel,out,idx,len,p);//print the title
36+
if(backTitle&&idx==sz()-1) return out.printText(backTitle,len);
37+
trace(MENU_DEBUG_OUT<<"{"<<idx<<"}");
38+
return printItem(out,out.tops[root.level]+idx/*-(backTitle?1:0)*/,len);
3139
}
3240
void doNav(navNode& nav,navCmd cmd) {
33-
if(nav.root->navFocus==this&&cmd.cmd==enterCmd) {
34-
_trace(MENU_DEBUG_OUT<<"no sub levels! submenu:"<<(subMenu?"yes":"no")<<" level:"<<nav.root->level<<"\n");
35-
//just send the event, but do not open (as this menu is representing the item)
36-
// nav.root->path[nav.root->level-1].event(enterEvent);
37-
// nav.event(enterEvent);
38-
// if(enterEvent&&) operator()(enterEvent, nav, *this);
39-
nav.event(enterEvent);
40-
if(subMenu) {
41-
assert(nav.root->level<nav.root->maxDepth);
42-
nav.root->active().dirty=true;
43-
nav.root->level++;
44-
nav.root->navFocus=nav.root->node().target=subMenu;
45-
nav.root->node().sel=0;
41+
if(nav.root->navFocus==this) {
42+
trace(MENU_DEBUG_OUT<<" submenu:"<<(subMenu?"yes":"no")<<" level:"<<nav.root->level<<"\n");
43+
switch (cmd.cmd) {
44+
case selCmd:
45+
case idxCmd:
46+
trace(MENU_DEBUG_OUT<<"idxCmd "<<cmd.param<<endl);
47+
nav.sel=cmd.param;
48+
case enterCmd:
49+
_trace(MENU_DEBUG_OUT<<"enterCmd");
50+
if(backTitle&&nav.sel==sz()-1) nav.root->exit();
51+
else {
52+
nav.event(enterEvent);
53+
if(subMenu) {
54+
_trace(MENU_DEBUG_OUT<<"edit item");
55+
assert(nav.root->level<nav.root->maxDepth);
56+
nav.root->active().dirty=true;
57+
nav.root->level++;
58+
nav.root->navFocus=nav.root->node().target=subMenu;
59+
nav.root->node().sel=0;
60+
}
61+
}
62+
return;
63+
default:break;
4664
}
47-
} else menuNode::doNav(nav,cmd);
65+
}
66+
menuNode::doNav(nav,cmd);
4867
}
4968
};
5069

src/shadows.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
idx_t sz;
8686
prompt* constMEM* data;
8787
public:
88-
menuNodeShadow(constText* text,idx_t sz,prompt* constMEM* data,action a,eventMask e,styles style,systemStyles ss=(systemStyles)(_menuData|_canNav))
88+
menuNodeShadow(const char* text,idx_t sz,prompt* constMEM* data,action a,eventMask e,styles style,systemStyles ss=(systemStyles)(_menuData|_canNav))
8989
:promptShadow(text,a,e,style,ss),sz(sz),data(data) {}
9090
idx_t _sz() const {return (idx_t)memIdx(sz);}
9191
prompt* constMEM* _data() const {return (prompt* constMEM*)memPtr(data);}

0 commit comments

Comments
 (0)