Skip to content

Commit 8fed71c

Browse files
committed
Fixing serial menu support for optional Exit option on main menus
1 parent ab84752 commit 8fed71c

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

Examples/menu_test/menu_test.ino

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
#define LCDWIRE_DIRECT 4
1717

1818
//how the LCD is wired
19-
//#define LCD_WIRE LCDWIRE_NONE
19+
#define LCD_WIRE LCDWIRE_NONE
2020
//#define LCD_WIRE LCDWIRE_VPINS_I2C
21-
#define LCD_WIRE LCDWIRE_VPINS_SPI//on shift registers thru vpins (same library)
21+
//#define LCD_WIRE LCDWIRE_VPINS_SPI//on shift registers thru vpins (same library)
2222
//#define LCD_WIRE LCDWIRE_I2C//not tested
2323
//#define LCD_WIRE LCDWIRE_DIRECT//not tested
2424

25-
#define USE_TFT YES//YES|NO
25+
#define USE_TFT 0//0|1
2626

27-
#if (USE_TFT == YES)
27+
#if (USE_TFT == 1)
2828
#include <Adafruit_GFX.h> // Co1re graphics library
2929
#include <Adafruit_ST7735.h> // Hardware-specific library
3030
#include <menuGFX.h>
@@ -77,12 +77,14 @@
7777
#define encB 4
7878
//this encoder has a button here
7979
#define encBtn A0
80-
#define LEDPIN A3//on uno use pin 13
80+
#define LEDPIN 13//on uno use pin 13
8181

8282
///////////////////////////////////////////////////////////////////////////
8383
//functions to wire as menu actions
8484

8585
//aux function
86+
void nothing() {}
87+
8688
void setValue(prompt &p,menuOut &o, Stream &i,int &value,const char* text,const char* units="",int sensivity=5,int low=0,int hi=100,int steps=0,void (*func)()=nothing);
8789

8890
void ledOn() {digitalWrite(LEDPIN,1);}
@@ -155,31 +157,12 @@ menuPrint serial(Serial);
155157
#if (LCD_WIRE!=LCDWIRE_NONE)
156158
menuLCD lcd(lcd1,16,2);
157159
#endif
158-
#if (USE_TFT == YES)
160+
#if (USE_TFT == 1)
159161
menuGFX gfx(tft);
160162
//menuGFX gfx(tft,BLUE,BLACK,WHITE,SILVER,5,8);
161163
#endif
162164
menuPrint menuSerialOut(Serial);//describe output device
163165

164-
/*template<int N>
165-
class chainOut:public Print {
166-
public:
167-
menuOut* out[N];
168-
chainOut(Print* out[N]):out(out) {}
169-
virtual void clear() {for(int n=0;n<N;n++) out[n]->clear();}
170-
virtual void setCursor(int x,int y) {for(int n=0;n<N;n++) out[n]->setCursor(x,y);}
171-
virtual void print(char ch) {for(int n=0;n<N;n++) out[n]->print(ch);}
172-
virtual void print(const char *text) {for(int n=0;n<N;n++) out[n]->print(text);}
173-
virtual void println(const char *text) {for(int n=0;n<N;n++) out[n]->print(text);}
174-
virtual void print(int i) {for(int n=0;n<N;n++) out[n]->print(i);}
175-
virtual void println(int i) {for(int n=0;n<N;n++) out[n]->print(i);}
176-
virtual void print(prompt &o,bool selected,int idx,int posY,int width) {for(int n=0;n<N;n++) out[n]->print(o,selected,idx,posY,width);}
177-
virtual void printMenu(menu&m,bool drawExit) {for(int n=0;n<N;n++) out[n]->print(m,drawExit);}
178-
};
179-
180-
menuOut* out[]={&lcd,&gfx};
181-
chainOut<2> allOut(out);*/
182-
183166
///////////////////////////////////////////////////////////////////////////////
184167

185168
void setup() {
@@ -188,7 +171,7 @@ void setup() {
188171
Serial.begin(9600);
189172
Serial.println("menu system test");
190173

191-
#if ((LCD_WIRE != LCDWIRE_NONE) || (USE_TFT == YES))
174+
#if ((LCD_WIRE != LCDWIRE_NONE) || (USE_TFT == 1))
192175
pinMode(vpinsSPI_CS,OUTPUT);
193176
digitalWrite(vpinsSPI_CS,LOW);
194177
#endif
@@ -208,7 +191,7 @@ void setup() {
208191

209192
#endif
210193

211-
#if (USE_TFT == YES)
194+
#if (USE_TFT == 1)
212195
digitalWrite(vpinsSPI_CS,HIGH);
213196
digitalWrite(tftCS,HIGH);
214197
tft.initR(INITR_BLACKTAB);
@@ -233,28 +216,25 @@ void setup() {
233216
///////////////////////////////////////////////////////////////////////////////
234217
// testing the menu system
235218
void loop() {
236-
//mainMenu.activate(menuSerialOut,Serial);//show menu to Serial and read keys from Serial
237-
//Serial.println("");
238-
//Serial.println("Restarting...");
239-
//mainMenu.activate(Serial,enc);//bad combination! shopw menu on serial and navigate using quadEncoder
219+
mainMenu.activate(menuSerialOut,Serial);//show menu to Serial and read keys from Serial
220+
Serial.println("");
221+
Serial.println("Restarting...");
240222

241223
#if (LCD_WIRE != LCDWIRE_NONE)
242-
digitalWrite(vpinsSPI_CS,LOW);
243-
digitalWrite(tftCS,LOW);
224+
//digitalWrite(vpinsSPI_CS,LOW);
225+
//digitalWrite(tftCS,LOW);
244226
//mainMenu.activate(lcd,allIn);//show menu on LCD and use multiple inputs to navigate (defined encoder, encoder button, serial)
245227
//mainMenu.activate(lcd,Serial);//very bad combination!
246228
#endif
247229

248-
#if (USE_TFT == YES)
249-
digitalWrite(vpinsSPI_CS,HIGH);
250-
digitalWrite(tftCS,HIGH);
251-
mainMenu.activate(gfx,allIn);//show menu on LCD and use multiple inputs to navigate (defined encoder, encoder button, serial)
230+
#if (USE_TFT == 1)
231+
//digitalWrite(vpinsSPI_CS,HIGH);
232+
//digitalWrite(tftCS,HIGH);
233+
//mainMenu.activate(gfx,allIn);//show menu on LCD and use multiple inputs to navigate (defined encoder, encoder button, serial)
252234
#endif
253235

254236
}
255237

256-
void nothing() {}
257-
258238
void percentBar(menuOut &o,int percent) {
259239
int i=map(percent, 0, 100, 0, o.maxX);
260240
for(int n=0;n<o.maxX;n++)

menu.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Arduino generic menu system
33
Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
44
*/
5+
#include <Arduino.h>
56
#include "menu.h"
67

78
const char* menu::exit="Exit";
@@ -12,7 +13,7 @@ prompt menu::exitOption(menu::exit);
1213
int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
1314
int op=-2;
1415
do {
15-
while(!c.available());// delay(20);
16+
while(!c.available());
1617
if (c.peek()!=13) {
1718
if(c.available()) {
1819
int ch=c.read();
@@ -31,13 +32,14 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
3132
} else if (ch==27) {
3233
op=-1;
3334
} else op=ch-49;
34-
if (c.peek()==13||c.peek()==10) c.read();//discard ENTER and CR
3535
}
36-
} else {
37-
c.read();
36+
} else
3837
op=sel==sz?-1:sel;
39-
}
40-
} while(canExit&&(op<-1||op>=sz));
38+
if (!((op>=0&&op<sz)||(canExit&&op==-1))) op=-2;//validate the option
39+
//add some delays to be sure we do not have more characters NL or CR on the way
40+
//delay might be adjusted to vope with stream speed
41+
delay(50);while (c.peek()==13||c.peek()==10) {c.read();delay(50);}//discard ENTER and CR
42+
} while(op==-2);//repeat until we have a valid option (can be Exit/Esc)
4143
return op;
4244
}
4345

@@ -47,14 +49,13 @@ void menu::activate(menuOut& p,Stream& c,bool canExit) {
4749
int op=-1;
4850
do {
4951
printMenu(p,canExit);
50-
c.flush();//reset the encoder
51-
while(c.available()) c.read();//clean the stream
5252
op=menuKeys(p,c,canExit);
5353
if (op>=0&&op<sz) {
5454
sel=op;
55-
if (data[op]->enabled)
55+
if (data[op]->enabled) {
5656
data[op]->activate(p,c,true);
5757
p.drawn=0;//redraw menu
58+
}
5859
}
5960
} while(op!=-1);
6061
}

0 commit comments

Comments
 (0)