Skip to content

Commit d0ad7c8

Browse files
committed
non-blocking poll function
also stated fiels...
1 parent 76298b9 commit d0ad7c8

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

Examples/I2C_menu/I2C_menu.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ void setup()
2525
void loop()
2626
{
2727
//serial can use numbers or +/- and Enter to navigate
28-
mainMenu.activate(menu_lcd,Serial);
28+
mainMenu.run(menu_lcd,Serial);
2929
}

Examples/serial_menu/serial_menu.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "menu.h"//menu macros and objects
22
#include "menuPrint.h"
33

4+
#define LEDPIN 13
5+
46
///////////////////////////////////////////////////////////////////////////
57
//functions to wire as menu actions
68

79
void none() {}
8-
void ledOn() {digitalWrite(13,1);}
9-
void ledOff() {digitalWrite(13,0);}
10+
void ledOn() {digitalWrite(LEDPIN,1);}
11+
void ledOff() {digitalWrite(LEDPIN,0);}
1012

1113
/////////////////////////////////////////////////////////////////////////
1214
// MENU DEFINITION
@@ -22,13 +24,13 @@ menuPrint menu_out(Serial);//describe output device
2224
void setup() {
2325
Serial.begin(9600);
2426
Serial.println("menu system test");
25-
pinMode(13,OUTPUT);
27+
pinMode(LEDPIN,OUTPUT);
2628
menu::exit="Saida";
2729
mainMenu.data[2]->enabled=false;
2830
}
2931

3032
void loop() {
31-
mainMenu.activate(menu_out,Serial);
33+
mainMenu.run(menu_out,Serial);
3234
Serial.println("");
3335
Serial.println("Restarting...");
3436
}

menu.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const char* menu::exit="Exit";
1616
char menu::enabledCursor='>';
1717
char menu::disabledCursor='-';
1818
prompt menu::exitOption(menu::exit);
19+
menu* menu::activeMenu=NULL;
1920

2021
//menu navigation engine
2122
//iteract with input until a selection is done, return the selection
2223
int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
2324
int op=-2;
24-
do {
25-
while(!c.available());
25+
//do {
26+
//while(!c.available());
27+
if (!c.available()) return op;
2628
if (c.peek()!=13) {
2729
if(c.available()) {
2830
int ch=c.read();
@@ -48,7 +50,7 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
4850
//add some delays to be sure we do not have more characters NL or CR on the way
4951
//delay might be adjusted to vope with stream speed
5052
delay(50);while (c.peek()==13||c.peek()==10) {c.read();delay(50);}//discard ENTER and CR
51-
} while(op==-2);//repeat until we have a valid option (can be Exit/Esc)
53+
//} while(op==-2);//repeat until we have a valid option (can be Exit/Esc)
5254
return op;
5355
}
5456

@@ -58,10 +60,14 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
5860
// draw: call target device object
5961
//input scan: call the navigation function (self)
6062
void menu::activate(menuOut& p,Stream& c,bool canExit) {
61-
sel=0;
62-
p.top=0;
63+
if (activeMenu!=this) {
64+
previousMenu=activeMenu;
65+
activeMenu=this;
66+
sel=0;
67+
p.top=0;
68+
}
6369
int op=-1;
64-
do {
70+
//do {
6571
printMenu(p,canExit);
6672
op=menuKeys(p,c,canExit);
6773
if (op>=0&&op<sz) {
@@ -71,8 +77,14 @@ void menu::activate(menuOut& p,Stream& c,bool canExit) {
7177
c.flush();//reset the encoder
7278
p.drawn=0;//redraw menu
7379
}
74-
}
75-
} while(op!=-1);
80+
} else if (op==-1) //then exit
81+
activeMenu=previousMenu;
82+
//} while(op!=-1);
83+
}
84+
85+
void menu::poll(menuOut& p,Stream& c,bool canExit) {
86+
if (!activeMenu) activeMenu=this;
87+
activeMenu->activate(p,c,activeMenu==this?canExit:true);
7688
}
7789

7890
//todo: preparing functions for touch screen support

menu.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
9797
menu id (text,sizeof(id##_data)/sizeof(prompt*),id##_data);
9898

9999
#define OP(...) OP_(__COUNTER__,__VA_ARGS__)
100+
#define FIELD(...) FIELD_(__COUNTER__,__VA_ARGS__)
101+
100102
#define DECL_OP_(cnt,...) prompt op##cnt(__VA_ARGS__);
103+
#define DECL_FIELD_(cnt,...) menuField _menuField##cnt(__VA_ARGS__);
101104
#define DECL_SUBMENU(id)
105+
102106
#define DEF_OP_(cnt,...) &op##cnt
107+
#define DEF_FIELD_(cnt,...) &_menuField##cnt
103108
#define DEF_SUBMENU(id) &id
104109

105110
/////////////////////////////////////////////////////////
@@ -175,17 +180,21 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
175180
static const char *exit;//text used for exit option
176181
static char enabledCursor;//character to be used as navigation cursor
177182
static char disabledCursor;//to be used when navigating over disabled options
178-
static prompt exitOption;//option tro append to every menu allowing exit when no escape button/key is available
183+
static prompt exitOption;//option to append to menu allowing exit when no escape button/key is available
184+
static menu* activeMenu;
185+
menu* previousMenu;
179186
const int sz;
180187
int sel;//selection
181188
prompt* const* data PROGMEM;
182-
menu(const char * text,int sz,prompt* const data[]):prompt(text),sz(sz),data(data),sel(0),width(16) {}
189+
menu(const char * text,int sz,prompt* const data[]):prompt(text),sz(sz),data(data),sel(0),width(16),previousMenu(NULL) {}
183190

184191
int menuKeys(menuOut &p,Stream& c,bool drawExit);
185192
inline void printMenu(menuOut& p,bool drawExit) {p.printMenu(*this,drawExit);}
186193

187194
void activate(menuOut& p,Stream& c,bool canExit=false);
188195

196+
void poll(menuOut& p,Stream& c,bool canExit=false);
197+
189198
//some funcs to support touch... TODO: test them
190199
// this functions will probably move to touch class...
191200
// will be here now for test while i wait for my touch screen to arrive.....

0 commit comments

Comments
 (0)