3333#include < Adafruit_GFX.h> // Core graphics library
3434#include < Adafruit_ST7735.h> // Hardware-specific library
3535#include < menuGFX.h>
36+ #include < menuFields.h>
3637
3738
3839#if defined(__AVR_ATmega2560__)
5152 #define LEDPIN A3
5253 // /////////////////////////////////////////////////////////////////////////
5354 // TFT + SD
54- #define TFT_DC A0
55- #define TFT_CS A1
56- #define TFT_RST A2
55+ // TFT + SD
56+ // #define sdCS 9//not using SD card
57+ #define tftCS A1
58+ #define dc A0
59+ #define rst A2
5760 // //////////////////////////////////////////
5861 // ENCODER (aka rotary switch) PINS
59- #define encA 2
60- #define encB 3
61- #define encBtn 4
62+ #define encA 2
63+ #define encB 3
64+ #define encBtn 4
6265#else
6366 #error "Uknown pinout"
6467#endif
6568
66- Adafruit_ST7735 tft (TFT_CS, TFT_DC, TFT_RST);
69+ Adafruit_ST7735 tft (tftCS, dc, rst);
70+
71+ // wire led here because default led is already used by hardware SPI
72+ #define LEDPIN A3
6773
6874// aux vars
6975int ledCtrl=0 ;
76+ bool runMenu=false ;
77+ bool scrSaverEnter=true ;
7078int percent;// just testing changing this var
79+ double fps=0 ;
80+ unsigned long lastFpsChk=0 ;
7181int counter=0 ;
7282
7383// /////////////////////////////////////////////////////////////////////////
7484// functions to wire as menu actions
85+ bool pauseMenu () {
86+ runMenu=false ;
87+ scrSaverEnter=true ;
88+ }
7589bool ledOn () {
7690 Serial.println (" set led on!" );
7791 digitalWrite (LEDPIN,ledCtrl=1 );
@@ -136,9 +150,23 @@ MENU(mainMenu,"Main menu",
136150 SUBMENU(selMenu),
137151 SUBMENU(chooseMenu),
138152 SUBMENU(subMenu),
139- FIELD(percent," Percent" ," %" ,0 ,100 ,10 ,1 )
153+ FIELD(percent," Percent" ," %" ,0 ,100 ,10 ,1 ),
154+ FIELD(fps," fps [" ," ]" ,0 ,0 ,0 ,0 ),
155+ FIELD(counter," counter [" ," ]" ,0 ,0 ,0 ,0 ),
156+ OP(" Exit" ,pauseMenu)
140157);
141158
159+ void scrSaver () {
160+ if (scrSaverEnter) {
161+ tft.fillScreen (ST7735_BLACK);
162+ tft.print (" |www.r-site.net|" );
163+ tft.setCursor (0 ,1 );
164+ tft.print (" |click to enter|" );
165+ scrSaverEnter=false ;
166+ }
167+ }
168+
169+
142170// the quadEncoder
143171quadEncoder encoder (encA,encB);// simple quad encoder driver
144172quadEncoderStream enc (encoder,5 );// simple quad encoder fake Stream
@@ -152,7 +180,7 @@ Stream* in[]={&enc,&encButton};
152180chainStream<2 > quadEncoder_button (in);
153181
154182// alternative to previous but now we can input from Serial too...
155- Stream* in3[]={&enc,&encButton};
183+ Stream* in3[]={&enc,&encButton,&Serial };
156184chainStream<3 > allIn (in3);
157185
158186// describing a menu output, alternatives so far are Serial or LiquidCrystal LCD
@@ -166,16 +194,16 @@ void setup() {
166194 tft.setRotation (3 );
167195 tft.setTextWrap (false );
168196 tft.setTextColor (ST7735_RED,ST7735_BLACK);
169- // tft.setTextSize(2 );
170- // gfx.resX*=2 ;//update resolution after font size change
171- // gfx.resY*=2 ;//update resolution after font size change
197+ tft.setTextSize (1 );
198+ gfx.resX *=1 ;// update resolution after font size change
199+ gfx.resY *=1 ;// update resolution after font size change
172200 tft.fillScreen (ST7735_BLACK);
173201 tft.print (" Menu test on GFX" );
174202 // testing menu limits (not using all the screen)
175203 // size is within screen limits even after rotation
176204 // this limits are not constrained, please ensure your text fits
177205 gfx.maxX =16 ;
178- gfx.maxY =5 ;
206+ gfx.maxY =10 ;
179207 gfx.bgColor =SILVER;
180208 pinMode (encBtn, INPUT_PULLUP);
181209 encoder.begin ();
@@ -184,6 +212,16 @@ void setup() {
184212// /////////////////////////////////////////////////////////////////////////////
185213// testing the menu system
186214void loop () {
187- mainMenu.poll (gfx,allIn);
188- digitalWrite (LEDPIN, ledCtrl);
215+ if (runMenu) mainMenu.poll (gfx,allIn);
216+ else if (allIn.read ()==menu::enterCode) runMenu=true ;
217+ else scrSaver ();
218+ // simulate the delay of your program... if this number rises too much the menu will have bad navigation experience
219+ // if so, then the menu can be wired into a timmer... leaving the shorter end to your code while it is running
220+ counter=millis ()/1000 %60 ;
221+ int d=micros ()-lastFpsChk;
222+ if (d>0 ) {
223+ fps=1000000.0 /d;
224+ lastFpsChk+=d;
225+ }
226+ delay (50 );
189227}
0 commit comments