Skip to content

Commit 7d76142

Browse files
committed
implementing menu width foir GFX consistent selection draw
1 parent 702fd11 commit 7d76142

File tree

6 files changed

+63
-27
lines changed

6 files changed

+63
-27
lines changed

Examples/menu_test/menu_test.ino

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ void setValue(prompt &p,menuOut &o, Stream &i,int &value,const char* text,const
8585
void ledOn() {digitalWrite(LEDPIN,1);}
8686
void ledOff() {digitalWrite(LEDPIN,0);}
8787
void disabledTest(prompt &p,menuOut &o,Stream &i) {
88+
o.clear();
89+
o.setCursor(0,0);
8890
o.print("THIS IS AN ERROR, this option should never be called as it is disabled");
8991
while(i.read()!=13);
9092
}
@@ -97,6 +99,7 @@ void setDutty(prompt &p,menuOut &o,Stream &i) {setValue(p,o,i,dutty,"Dutty:","%"
9799

98100
void completeHandlerTest(prompt &p,menuOut &o,Stream &i) {
99101
o.clear();
102+
o.setCursor(0,0);
100103
o.print("Handler test ok!");
101104
while(i.read()!=13);
102105
}
@@ -114,6 +117,17 @@ MENU(mainMenu,"Sistema",
114117
OP("Dutty",setDutty),
115118
OP("Disabled",disabledTest),
116119
OP("Handler test",completeHandlerTest),
120+
OP("Handler test",completeHandlerTest),
121+
OP("Handler test",completeHandlerTest),
122+
OP("Handler test",completeHandlerTest),
123+
OP("Handler test",completeHandlerTest),
124+
OP("Handler test",completeHandlerTest),
125+
OP("Handler test",completeHandlerTest),
126+
OP("Handler test",completeHandlerTest),
127+
OP("Handler test",completeHandlerTest),
128+
OP("Handler test",completeHandlerTest),
129+
OP("Handler test",completeHandlerTest),
130+
OP("Handler test",completeHandlerTest),
117131
SUBMENU(subMenu)
118132
);
119133

@@ -144,6 +158,8 @@ menuGFX gfx(tft);
144158
///////////////////////////////////////////////////////////////////////////////
145159

146160
void setup() {
161+
mainMenu.data[2]->enabled=false;//disabling option
162+
147163
Serial.begin(9600);
148164
Serial.println("menu system test");
149165

@@ -166,7 +182,7 @@ void setup() {
166182
lcd1.print("Menu test");
167183

168184
digitalWrite(vpinsSPI_CS,HIGH);
169-
digitalWrite(tftCS,LOW);
185+
digitalWrite(tftCS,HIGH);
170186
tft.initR(INITR_BLACKTAB);
171187
tft.setRotation(3);
172188
tft.setTextWrap(false);
@@ -175,13 +191,22 @@ void setup() {
175191
tft.fillScreen(ST7735_BLACK);
176192
tft.print("Menu test on GFX");
177193
tft.setCursor(0,10);
194+
195+
digitalWrite(vpinsSPI_CS,LOW);
196+
digitalWrite(tftCS,LOW);
197+
lcd1.setCursor(0,1);
198+
lcd1.print(mainMenu.width);
199+
digitalWrite(vpinsSPI_CS,HIGH);
200+
digitalWrite(tftCS,HIGH);
201+
202+
//update limits after screen rotation
203+
gfx.maxX=tft.width()/gfx.resX;
204+
gfx.maxY=tft.height()/gfx.resY;
178205

179206
pinMode(encBtn, INPUT);
180207
digitalWrite(encBtn,1);
181208

182209
pinMode(LEDPIN,OUTPUT);
183-
184-
mainMenu.data[2]->enabled=false;
185210
}
186211

187212
///////////////////////////////////////////////////////////////////////////////
@@ -211,7 +236,7 @@ void setValue(prompt &p,menuOut &o, Stream &i,int &value,const char* text,const
211236
int at=strlen(text);//.length();
212237
o.setCursor(0,0);
213238
o.print(text);
214-
if (o.style==menuOut::enumerated) {//probably a Serial terminal -------------------------------------
239+
/*if (o.style==menuOut::enumerated) {//probably a Serial terminal -------------------------------------
215240
//long timeout because some terminals send data righ away when typed, and parseInt would parse a partial number
216241
i.setTimeout(10000);//lib gives no access to previous timeout value :( ... cant restore it then, i would wait forever if possible
217242
value=i.parseInt();//assuming data was all delivered to the buffer (we had a large timeout)
@@ -221,7 +246,7 @@ void setValue(prompt &p,menuOut &o, Stream &i,int &value,const char* text,const
221246
i.setTimeout(1000);//assuming it was default
222247
Serial.println(value);//feed back
223248
while(i.available()) i.read();//clean up extra characters to avoid reentry
224-
} else {// then we assume its some kind of encoder ---------------------------------------------------
249+
} else {*/// then we assume its some kind of encoder ---------------------------------------------------
225250
if (!steps) steps=(hi-low)/(float)o.maxX;
226251
float fact=((float)sensivity)/((float)steps);//sensivity factor
227252
float pos=quadEncoder.pos*fact;
@@ -243,7 +268,7 @@ void setValue(prompt &p,menuOut &o, Stream &i,int &value,const char* text,const
243268
last=pos;
244269
}
245270
//func();
246-
}
271+
//}
247272
delay(100);
248273
while(encButton.read()==13);
249274
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ example of menu definition (c++ macros)
2222

2323
input is read from generic streams, included simple streams for encoders and keyboards
2424

25-
output to menuOut devices, included derivations to support serial and lcd
25+
output to menuOut devices, included derivations to support serial, GFX and lcd
2626

2727
multiple stream packing for input allowing mixing encoder stream with encoder keyboard (usually 1 or 2 keys)

menu.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ char menu::enabledCursor='>';
99
char menu::disabledCursor='-';
1010
prompt menu::exitOption(menu::exit);
1111

12-
void menuOut::print(prompt &o,bool selected) {
13-
print(selected?(o.enabled?menu::enabledCursor:menu::disabledCursor):' ');
14-
print(o.text);
15-
}
16-
1712
int menu::menuKeys(menuOut &p,Stream& c) {
1813
int op=-2;
1914
do {
@@ -48,27 +43,27 @@ int menu::menuKeys(menuOut &p,Stream& c) {
4843

4944
void menu::printMenu(menuOut& p) {
5045
p.clear();
51-
if (p.style==menuOut::enumerated) {p.print(text); p.print("==========================");}
46+
//if (p.style==menuOut::enumerated) {p.print(text); p.print("==========================");}
5247
if (sel-p.top>=p.maxY) p.top=sel-p.maxY+1;//selected option outside device (bottom)
5348
else if (sel<p.top) p.top=sel;//selected option outside device (top)
5449
int i=0;for(;i<sz;i++) {
5550
if ((i>=p.top)&&((i-p.top)<p.maxY)) {
56-
p.setCursor(0,i-p.top);
51+
//p.setCursor(0,i-p.top);
5752
if(i-p.top>=p.maxY) break;
58-
if (p.style==menuOut::enumerated) {
53+
/*if (p.style==menuOut::enumerated) {
5954
p.print(i<10?" ":"");
6055
p.print(i+1);
61-
}
62-
p.print(*data[i],i==sel);
56+
}*/
57+
p.print(*data[i],i==sel,i+1,i-p.top,width);
6358
/*p.print((i==sel)?data[i]->enabled?menu::enabledCursor:menu::disabledCursor:' ');
6459
p.print(data[i]->text);*/
6560
}
6661
}
6762
if (i-p.top<p.maxY) {
68-
p.setCursor(0,i-p.top);
69-
if (p.style==menuOut::enumerated)
70-
p.print(" 0");
71-
p.print(exitOption,sel==sz);
63+
//p.setCursor(0,i-p.top);
64+
/*if (p.style==menuOut::enumerated)
65+
p.print(" 0");*/
66+
p.print(exitOption,sel==sz,0,i-p.top,width);
7267
/*p.print(sel==sz?menu::enabledCursor:' ');
7368
p.println(menu::exit);*/
7469
}

menu.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,23 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
107107
//device resolution
108108
int resX;
109109
int resY;
110-
enum styles {
110+
/*enum styles {
111111
enumerated, //print numbers or references for options, adequated keyboard or file input
112112
cursor,//print a cursor at selected option, tracking device (encoder)
113113
point,//pointing device (not implemented)
114-
} style;
114+
} style;*/
115115
//menuOut(menuOut::styles style=menuOut::enumerated):maxX(0),maxY(0),style(style),top(0) {}
116-
menuOut(menuOut::styles style=menuOut::enumerated,int x=0x7F,int y=0x7F,int resX=1,int resY=1):maxX(x),maxY(y),style(style),top(0),resX(resX),resY(resY) {}
116+
//menuOut(menuOut::styles style=menuOut::enumerated,int width=0x7F,int x=0x7F,int y=0x7F,int resX=1,int resY=1):maxX(x),maxY(y),style(style),top(0),resX(resX),resY(resY) {}
117+
menuOut(int x=0x7F,int y=0x7F,int resX=1,int resY=1)
118+
:maxX(x),maxY(y),top(0),resX(resX),resY(resY) {}
117119
virtual void clear()=0;
118120
virtual void setCursor(int x,int y)=0;
119121
virtual void print(char ch)=0;
120122
virtual void print(const char *text)=0;
121123
virtual void println(const char *text)=0;
122124
virtual void print(int)=0;
123125
virtual void println(int)=0;
124-
virtual void print(prompt &o,bool selected);
126+
virtual void print(prompt &o,bool selected,int idx,int posY,int width)=0;
125127
};
126128

127129
////////////////////////////////////////////////////////////////////
@@ -165,14 +167,15 @@ for encoders, joysticks, keyboards or touch a stream must be made out of them
165167
//a menu or sub-menu
166168
class menu:public prompt {
167169
public:
170+
int width;//menu width
168171
static const char *exit;//text used for exit option
169172
static char enabledCursor;//character to be used as navigation cursor
170173
static char disabledCursor;//to be used when navigating over disabled options
171174
static prompt exitOption;//option tro append to every menu allowing exit when no escape button/key is available
172175
const int sz;
173176
int sel;//selection
174177
prompt* const* data;
175-
menu(const char * text,int sz,prompt* const data[]):prompt(text),sz(sz),data(data),sel(0) {}
178+
menu(const char * text,int sz,prompt* const data[]):prompt(text),sz(sz),data(data),sel(0),width(16) {}
176179

177180
int menuKeys(menuOut &p,Stream& c);
178181
void printMenu(menuOut& p);

menuLCD.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
class menuLCD:public menuOut {
77
public:
88
LiquidCrystal& lcd;
9-
menuLCD(LiquidCrystal& lcd,int x=16,int y=1):lcd(lcd),menuOut(menuOut::cursor,x,y) {}
9+
//menuLCD(LiquidCrystal& lcd,int x=16,int y=1):lcd(lcd),menuOut(menuOut::cursor,x,y) {}
10+
menuLCD(LiquidCrystal& lcd,int x=16,int y=1):lcd(lcd),menuOut(x,y) {}
1011
virtual void clear() {lcd.clear();}
1112
virtual void setCursor(int x,int y) {lcd.setCursor(x*resX,y*resY);}
1213
virtual void print(char ch) {lcd.print(ch);}
1314
virtual void print(const char *text) {lcd.print(text);}
1415
virtual void println(const char *text) {lcd.print(text);};
1516
virtual void print(int i) {lcd.print(i);};
1617
virtual void println(int i) {lcd.println(i);};
18+
virtual void print(prompt &o,bool selected,int idx,int posY,int width) {
19+
lcd.setCursor(0,posY);
20+
print(selected?(o.enabled?menu::enabledCursor:menu::disabledCursor):' ');
21+
print(o.text);
22+
}
1723
};
1824
#endif RSITE_ARDUINOP_MENU_LCD
1925

menuPrint.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
virtual void println(const char *text) {device.println(text);}
1515
virtual void print(int i) {device.print(i);};
1616
virtual void println(int i) {device.println(i);};
17+
virtual void print(prompt &o,bool selected,int idx,int posY,int width) {
18+
//setCursor(0,posY);
19+
print(idx<10?" ":"");
20+
print(idx);
21+
print(selected?(o.enabled?menu::enabledCursor:menu::disabledCursor):' ');
22+
println(o.text);
23+
}
1724
};
1825

1926
#endif RSITE_ARDUINOP_MENU_PRINT

0 commit comments

Comments
 (0)