Skip to content

Commit 1afdd6f

Browse files
committed
adafruits better example
but still no color because its not implemented on gfx driver yet
1 parent 0cc280d commit 1afdd6f

File tree

2 files changed

+178
-88
lines changed

2 files changed

+178
-88
lines changed

examples/gfx_menu/gfx_menu.ino

Lines changed: 119 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ http://www.r-site.net/?at=//op%5B%40id=%273090%27%5D
77
Sept. 2014 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
88
99
creative commons license 3.0: Attribution-ShareAlike CC BY-SA
10-
This software is furnished "as is", without technical support, and with no
10+
This software is furnished "as is", without technical support, and with no
1111
warranty, express or implied, as to its usefulness for any purpose.
1212
1313
Thread Safe: No
@@ -32,6 +32,7 @@ ruihfazevedo@[email protected]
3232
#include <Adafruit_GFX.h> // Core graphics library
3333
#include <Adafruit_ST7735.h> // Hardware-specific library
3434
#include <menuGFX.h>
35+
#include <menuFields.h>
3536

3637
#if defined(__AVR_ATmega2560__)
3738
///////////////////////////////////////////////////////////////////////////
@@ -48,43 +49,121 @@ ruihfazevedo@[email protected]
4849
#elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
4950
///////////////////////////////////////////////////////////////////////////
5051
//TFT + SD
52+
//TFT + SD
5153
//#define sdCS 9//not using SD card
52-
#define tftCS 10
53-
#define dc 7
54-
#define rst 8
54+
#define tftCS A1
55+
#define dc A0
56+
#define rst A2
5557
////////////////////////////////////////////
5658
// ENCODER (aka rotary switch) PINS
57-
#define encA A2
58-
#define encB A1
59-
#define encBtn A3
59+
#define encA 2
60+
#define encB 3
61+
#define encBtn 4
6062
#else
6163
#error "Uknown pinout"
6264
#endif
6365

6466
Adafruit_ST7735 tft(tftCS, dc, rst);
67+
68+
//wire led here because default led is already used by hardware SPI
69+
#define LEDPIN A4
70+
71+
//aux vars
72+
int ledCtrl=0;
73+
bool runMenu=false;
74+
bool scrSaverEnter=true;
75+
int percent;//just testing changing this var
76+
double fps=0;
77+
unsigned long lastFpsChk=0;
78+
int counter=0;
79+
6580
///////////////////////////////////////////////////////////////////////////
6681
//functions to wire as menu actions
82+
bool pauseMenu() {
83+
runMenu=false;
84+
scrSaverEnter=true;
85+
}
86+
bool ledOn() {
87+
Serial.println("set led on!");
88+
digitalWrite(LEDPIN,ledCtrl=1);
89+
return false;
90+
}
6791

68-
//aux function
69-
bool nothing() {return false;}
92+
bool ledOff() {
93+
Serial.println("set led off!");
94+
digitalWrite(LEDPIN,ledCtrl=0);
95+
return false;
96+
}
7097

71-
bool setValue(int &value,prompt &p,menuOut &o, Stream &i,const char* text,const char* units="",int sensivity=5,int low=0,int hi=100,int steps=0,bool (*func)()=nothing);
98+
bool quit() {
99+
Serial.println("Quiting after action call");
100+
return true;
101+
}
72102

73103
/////////////////////////////////////////////////////////////////////////
74104
// MENU DEFINITION
75105
// here we define the menu structure and wire actions functions to it
76-
MENU(subMenu,"Sub-Menu",
77-
OP("Op1",nothing),
78-
OP("Op2",nothing),
79-
OP("Op3",nothing)
106+
// empty options are just for scroll testing
107+
108+
/*bool setLed() {
109+
digitalWrite(LEDPIN,ledCtrl);
110+
return false;
111+
}*/
112+
TOGGLE(ledCtrl,setLed,"Led: ",
113+
VALUE("On",HIGH,ledOn),
114+
VALUE("Off",LOW,ledOff)
115+
);
116+
117+
int selTest=0;
118+
SELECT(selTest,selMenu,"Select",
119+
VALUE("Zero",0),
120+
VALUE("One",1),
121+
VALUE("Two",2)
80122
);
81123

82-
MENU(mainMenu,"Sistema",
83-
OP("A",nothing),
84-
OP("B",nothing),
85-
SUBMENU(subMenu)
124+
int chooseTest=-1;
125+
CHOOSE(chooseTest,chooseMenu,"Choose ",
126+
VALUE("First",1),
127+
VALUE("Second",2),
128+
VALUE("Third",3),
129+
VALUE("Last",-1)
86130
);
87131

132+
MENU(subMenu,"SubMenu"
133+
,OP("A",quit)
134+
,OP("B",quit)
135+
,OP("C",quit)
136+
,OP("D",quit)
137+
,OP("E",quit)
138+
,OP("F",quit)
139+
,OP("G",quit)
140+
,OP("H",quit)
141+
);
142+
143+
MENU(mainMenu,"Main menu",
144+
SUBMENU(setLed),
145+
OP("LED On",ledOn),
146+
OP("LED Off",ledOff),
147+
SUBMENU(selMenu),
148+
SUBMENU(chooseMenu),
149+
SUBMENU(subMenu),
150+
FIELD(percent,"Percent","%",0,100,10,1),
151+
FIELD(fps,"fps [","]",0,0,0,0),
152+
FIELD(counter,"counter [","]",0,0,0,0),
153+
OP("Exit",pauseMenu)
154+
);
155+
156+
void scrSaver() {
157+
if (scrSaverEnter) {
158+
tft.fillScreen(ST7735_BLACK);
159+
tft.print("|www.r-site.net|");
160+
tft.setCursor(0,1);
161+
tft.print("|click to enter|");
162+
scrSaverEnter=false;
163+
}
164+
}
165+
166+
88167
//the quadEncoder
89168
quadEncoder encoder(encA,encB);//simple quad encoder driver
90169
quadEncoderStream enc(encoder,5);// simple quad encoder fake Stream
@@ -98,7 +177,7 @@ Stream* in[]={&enc,&encButton};
98177
chainStream<2> quadEncoder_button(in);
99178

100179
//alternative to previous but now we can input from Serial too...
101-
Stream* in3[]={&enc,&encButton};
180+
Stream* in3[]={&enc,&encButton,&Serial};
102181
chainStream<3> allIn(in3);
103182

104183
//describing a menu output, alternatives so far are Serial or LiquidCrystal LCD
@@ -111,30 +190,41 @@ void setup() {
111190
tft.setRotation(3);
112191
tft.setTextWrap(false);
113192
tft.setTextColor(ST7735_RED,ST7735_BLACK);
114-
tft.setTextSize(2);
115-
gfx.resX*=2;//update resolution after font size change
116-
gfx.resY*=2;//update resolution after font size change
193+
tft.setTextSize(1);
194+
gfx.resX*=1;//update resolution after font size change
195+
gfx.resY*=1;//update resolution after font size change
117196
tft.fillScreen(ST7735_BLACK);
118197
tft.print("Menu test on GFX");
119198
//testing menu limits (not using all the screen)
120199
//size is within screen limits even after rotation
121200
//this limits are not constrained, please ensure your text fits
122-
gfx.maxX=8;
123-
gfx.maxY=3;
201+
gfx.maxX=16;
202+
gfx.maxY=10;
124203
gfx.bgColor=SILVER;
125204

126-
pinMode(encBtn, INPUT);
127-
digitalWrite(encBtn,1);
128-
205+
pinMode(encBtn, INPUT_PULLUP);
206+
129207
encoder.begin();
130208

131209
delay(300);
132210
tft.fillScreen(GREEN);
211+
212+
pinMode(LEDPIN,OUTPUT);
133213
}
134214

135215
///////////////////////////////////////////////////////////////////////////////
136216
// testing the menu system
137217
void loop() {
138-
mainMenu.poll(gfx,allIn);
218+
if (runMenu) mainMenu.poll(gfx,allIn);
219+
else if (allIn.read()==menu::enterCode) runMenu=true;
220+
else scrSaver();
221+
//simulate the delay of your program... if this number rises too much the menu will have bad navigation experience
222+
//if so, then the menu can be wired into a timmer... leaving the shorter end to your code while it is running
223+
counter=millis()/1000%60;
224+
int d=micros()-lastFpsChk;
225+
if (d>0) {
226+
fps=1000000.0/d;
227+
lastFpsChk+=d;
228+
}
229+
delay(50);
139230
}
140-

0 commit comments

Comments
 (0)