Skip to content

Commit ad01887

Browse files
committed
auto selection of pgm mode if available for board
1 parent 7e2118f commit ad01887

File tree

8 files changed

+83
-42
lines changed

8 files changed

+83
-42
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ SUBMENU(id)
208208
209209
link in a submenu as option of the current one
210210
211-
**id**: the submenu id
211+
**id**: of element to be used as a submenu, can be a menu, toggle, select or choose
212212
213213
#### Menu
214214
```c++
@@ -227,17 +227,21 @@ define menu structure
227227

228228
**name**: menu name to use as submenu title
229229

230+
## Saving RAM
231+
232+
By default PROGMEM is used on platforms where it is available.
233+
230234
## Include files
231235

232-
**chainStream.h** - join multiple input stream to be used as one
236+
**chainStream.h** - join multiple input stream to be used as one, this is used to join quadEncoder stream with a keyboard (encoder button).
233237

234-
**ClickEncoderStream.h** - using encoder https://github.com/0xPIT/encoder
238+
**ClickEncoderStream.h** - using alternative encoder https://github.com/0xPIT/encoder
235239

236-
**genericKeyboard.h** - use a custom keyboard by providing a reader function.
240+
**genericKeyboard.h** - make a custom keyboard by providing a reader function.
237241

238242
**keyStream.h** - simple digital keyboard driver doing digitalRead of pins. this driver can miss clicks on heavy load.
239243

240-
**macros.h** - macro definitions to help building complex menu structure (included by menu.h)
244+
_**macros.h** - macro definitions to help building complex menu structure (included by menu.h)_
241245

242246
**menuFields.h** - allows the menu system to alter/monitor your variables, this file defines the behavior of FIELD, TOGGLE, SELECT and CHOOSE.
243247

examples/LCD_Demo/LCD_Demo.ino

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/********************
32
Arduino generic menu system
43
LCD menu - unsing arduino classic LCD library
@@ -16,8 +15,6 @@ Extensible: Yes
1615

1716
//#define DEBUG
1817

19-
#define USEPGM
20-
2118
#include <HardwareSerial.h>
2219
#include <LiquidCrystal.h>
2320
#include <menu.h>//menu macros and objects
@@ -68,16 +65,6 @@ double fps=0;
6865
unsigned long lastFpsChk=0;
6966
int counter=0;
7067

71-
void scrSaver() {
72-
if (scrSaverEnter) {
73-
lcd1.clear();
74-
lcd1.print("|www.r-site.net|");
75-
lcd1.setCursor(0,1);
76-
lcd1.print("|click to enter|");
77-
scrSaverEnter=false;
78-
}
79-
}
80-
8168
///////////////////////////////////////////////////////////////////////////
8269
//functions to wire as menu actions
8370
bool pauseMenu() {
@@ -96,6 +83,11 @@ bool ledOff() {
9683
return false;
9784
}
9885

86+
bool quit() {
87+
Serial.println("Quiting after action call");
88+
return true;
89+
}
90+
9991
/////////////////////////////////////////////////////////////////////////
10092
// MENU DEFINITION
10193
// here we define the menu structure and wire actions functions to it
@@ -125,18 +117,40 @@ CHOOSE(chooseTest,chooseMenu,"Choose ",
125117
VALUE("Last",-1)
126118
);
127119

120+
MENU(subMenu,"SubMenu"
121+
,OP("A",quit)
122+
,OP("B",quit)
123+
,OP("C",quit)
124+
,OP("D",quit)
125+
,OP("E",quit)
126+
,OP("F",quit)
127+
,OP("G",quit)
128+
,OP("H",quit)
129+
);
130+
128131
MENU(mainMenu,"Main menu",
129132
SUBMENU(setLed),
130133
OP("LED On",ledOn),
131134
OP("LED Off",ledOff),
132135
SUBMENU(selMenu),
133136
SUBMENU(chooseMenu),
137+
SUBMENU(subMenu),
134138
FIELD(percent,"Percent","%",0,100,10,1),
135139
FIELD(fps,"fps [","]",0,0,0,0),
136140
FIELD(counter,"counter [","]",0,0,0,0),
137141
OP("Exit",pauseMenu)
138142
);
139143

144+
void scrSaver() {
145+
if (scrSaverEnter) {
146+
lcd1.clear();
147+
lcd1.print("|www.r-site.net|");
148+
lcd1.setCursor(0,1);
149+
lcd1.print("|click to enter|");
150+
scrSaverEnter=false;
151+
}
152+
}
153+
140154
//the quadEncoder
141155
quadEncoder quadEncoder(encA,encB);//simple quad encoder driver
142156
quadEncoderStream enc(quadEncoder,2);// simple quad encoder fake Stream
@@ -172,8 +186,8 @@ void setup() {
172186

173187
pinMode(LEDPIN,OUTPUT);
174188

175-
((prompt*)pgm_read_ptr_near(&mainMenu.data[6]))->enabled=false;
176-
((prompt*)pgm_read_ptr_near(&mainMenu.data[7]))->enabled=false;
189+
mainMenu[7].disable();
190+
mainMenu[8].disable();
177191

178192
delay(300);
179193
}

examples/UTFT_menu/UTFT_menu.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ UTFT library from:
1616
UTouch library from:
1717
http://henningkarlsen.com/electronics/library.php?id=56
1818
*/
19-
#define DEBUG
19+
//#define DEBUG
2020
#include <UTFT.h>
2121
#include <UTouch.h>
2222
#ifdef DEBUG
23-
//#include <streamFlow.h>
23+
#include <streamFlow.h>
2424
#endif
2525
#include <menu.h>
2626
#include <menuUTFT.h>

src/macros.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#ifdef USEPGM
1+
//#include <avr/pgmspace.h>
2+
3+
#ifdef pgm_read_ptr_near
24
//storing some values into avr flash memory (saving ram space)
3-
#include <avr/pgmspace.h>
45
#define MEMMODE PROGMEM
56
#define pgmPtrNear(addr) pgm_read_ptr_near(addr)
67
#define pgmByteNear(addr) (pgm_read_byte_near(addr))
7-
//#error USEPGM (just checking)
88
#else
99
//use ram for non-avr devices
1010
#define MEMMODE
1111
#define pgmPtrNear(addr) (addr)
12-
#define pgmByteNear(addr) (*(uint8_t*)addr)
13-
#error NO USEPGM (this mem mode is under development)
12+
#define pgmByteNear(addr) (*addr)
1413
#endif
1514

1615
class prompt;

src/menu.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ v2.0 mods - calling action on every element
1616
#include <Arduino.h>
1717
#include "menu.h"
1818

19+
//#define DEBUG
20+
#ifdef DEBUG
21+
#include <streamFlow.h>
22+
#include <menuPrint.h>
23+
menuPrint debugOut(Serial);
24+
#endif
25+
1926
const char menu::exit[] MEMMODE="Exit";
2027
char menu::escCode='/';
2128
char menu::enterCode='*';
@@ -54,10 +61,16 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
5461
if (sel<0) {
5562
if (wrapMenus) {
5663
sel=sz-(canExit?0:1);
57-
if (sel+1>=p.maxY) p.top=sel-p.maxY+1;
64+
if (sel+1>=p.maxY) {
65+
p.top=sel-p.maxY+1;
66+
Serial<<"menuKeys top1:"<<p.top<<endl;
67+
}
5868
} else sel=0;
5969
}
60-
if (p.top>sel) p.top=sel;
70+
if (p.top>sel) {
71+
p.top=sel;
72+
Serial<<"menuKeys top2:"<<p.top<<endl;
73+
}
6174
} else if (ch==menu::upCode) {
6275
sel++;
6376
if (sel>(sz-(canExit?0:1))) {
@@ -66,7 +79,10 @@ int menu::menuKeys(menuOut &p,Stream& c,bool canExit) {
6679
p.top=0;
6780
} else sel=sz-(canExit?0:1);
6881
}
69-
if (sel+1>p.maxY) p.top=sel-p.maxY+1;
82+
if (sel+1-p.top>p.maxY) {
83+
p.top=sel-p.maxY+1;
84+
Serial<<"menuKeys top3:"<<p.top<<endl;
85+
}
7086
} else if (ch==menu::escCode) {
7187
op=-1;
7288
} else {
@@ -106,6 +122,10 @@ promptFeedback menu::activate(menuOut& p,Stream& c,bool canExit) {
106122
if (cp->activate(p,c,true)) {
107123
p.clear();
108124
activeNode=previousMenu;
125+
//print_P(debugOut,this->text);
126+
//Serial<<"->";
127+
//print_P(debugOut,this->operator[](sel).text);
128+
//Serial<<" quiting... "<<p.lastSel<<endl;
109129
return true;
110130
}
111131
}

src/menu.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
2525
#ifndef RSITE_ARDUINO_MENU_SYSTEM
2626
#define RSITE_ARDUINO_MENU_SYSTEM
2727

28-
//PGM activated by default, non pgm mode is not working ok
29-
#define USEPGM
30-
3128
#include <Stream.h>
3229
#include <HardwareSerial.h>
3330

@@ -119,13 +116,15 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
119116
//a menu is also a prompt so we can have sub-menus
120117
class prompt {
121118
public:
122-
const char* text MEMMODE;
119+
const char* text;// MEMMODE; memmode is defined by the source, here its only a pointer
123120
promptAction action;
124121
static promptFeedback nothing() {return false;}
125122
bool enabled;
126123
inline prompt(const char * text):text(text),enabled(true),action(nothing) {}
127124
inline prompt(const char * text,promptAction action)
128125
:text(text),action(action),enabled(true) {}
126+
inline void enable() {enabled=true;}
127+
inline void disable() {enabled=false;}
129128
virtual void printTo(menuOut& p) {
130129
//p.print(text);
131130
print_P(p,text);
@@ -170,11 +169,13 @@ v2.1 - Add full support of SetPosition(x,y) to move the menu inside the screen (
170169
bool canExit;//store last canExit value for inner reference
171170
menu(const char * text,int sz,prompt* const data[]):menuNode(text),sz(sz),data(data),canExit(false) {}
172171

172+
inline prompt& operator[](int i) {return *(prompt*)pgmPtrNear(&data[i]);}
173173
inline void setPosition(int x,int y) {ox=x;oy=y;}
174174
int menuKeys(menuOut &p,Stream& c,bool drawExit);
175-
inline void printMenu(menuOut& p,bool drawExit) {
175+
inline void printMenu(menuOut& p,bool drawExit=false) {
176176
p.printMenu(*this,drawExit);
177177
}
178+
//virtual bool needRedraw(menuOut& o,bool) {return o.drawn!=true;}
178179
//force menu redraw on selected device
179180
inline void redraw(menuOut& p,Stream& c,bool canExit=false) {p.drawn=NULL;poll(p,c,canExit);}
180181
//set the focus to menu->Option idx, the focused menu will exit to the current

src/menuLCD.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ www.r-site.net
3939
}
4040
virtual void printMenu(menu& m,bool drawExit) {
4141
if (drawn!=&m) clear();//clear screen when changing menu
42-
if (m.sel-top>=maxY) top=m.sel-maxY+1;//selected option outside device (bottom)
43-
else if (m.sel<top) top=m.sel;//selected option outside device (top)
42+
if (m.sel<top) top=m.sel;//selected option outside device (top)
43+
else if (m.sel-top>=maxY) top=m.sel-maxY+1;//selected option outside device (bottom)
4444
int i=0;for(;i<m.sz;i++) {
4545
if ((i>=top)&&((i-top)<maxY)) {
4646
if (needRedraw(m,i)) {

src/menuU8G.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ www.r-site.net
8080
o.printTo(*this);
8181
}
8282
virtual void printMenu(menu& m,bool drawExit) {
83-
//if (drawn!=&m) clear();//clear all screen when changing menu - not ok for U8Glib
84-
if (m.sel-top >= maxY)
85-
top = m.sel - maxY + resY + 1; //selected option outside device (bottom)
86-
else if (m.sel < top)
87-
top = m.sel + resY; //selected option outside device (top)
83+
//Serial<<"printing menu "<<m<<" top:"<<top<<endl;
84+
//Serial<<*menu::activeNode<<" <-> "<<*drawn<<endl;
85+
if (m.sel < top) {
86+
top = m.sel;//+ resY; //selected option outside device (top)
87+
} else if (m.sel-top >= maxY) {
88+
top = m.sel - maxY + 1;//resY-1; //selected option outside device (bottom)
89+
}
8890
int i = top;
8991
for (; i < m.sz; i++) {
9092
if (i-top >= maxY) break;
@@ -96,6 +98,7 @@ www.r-site.net
9698
printPrompt(menu::exitOption,m.sel==m.sz,0,m.ox,(i-top)+m.oy,m.width);
9799
lastTop = top;
98100
lastSel = m.sel;
101+
//Serial<<"menuU8G::printMenu top:"<<top<<endl;
99102
//drawn=&m; // if commented, equals to have gfx.redraw() in loop - not ok for U8Glib
100103
}
101104
};

0 commit comments

Comments
 (0)