|
| 1 | +/******************** |
| 2 | +Sept. 2014 ~ Oct 2016 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 3 | +creative commons license 4.0: Attribution-ShareAlike CC BY-SA |
| 4 | +This software is furnished "as is", without technical support, and with no |
| 5 | +warranty, express or implied, as to its usefulness for any purpose. |
| 6 | +
|
| 7 | +Thread Safe: No |
| 8 | +Extensible: Yes |
| 9 | +
|
| 10 | +menu with UTFT (tested on arduino due) |
| 11 | +output: 3.2″ TFT LCD Module Display 240X320 |
| 12 | +input: Serial + Touch Panel |
| 13 | +www.r-site.net |
| 14 | +
|
| 15 | +UTFT library from: |
| 16 | + http://www.rinkydinkelectronics.com/library.php?id=51 |
| 17 | + http://henningkarlsen.com/electronics/library.php?id=50 (old address) |
| 18 | +
|
| 19 | +
|
| 20 | +***/ |
| 21 | + |
| 22 | +#include <Arduino.h> |
| 23 | +#include <menu.h> |
| 24 | +#include <menuIO/utftOut.h> |
| 25 | +#include <menuIO/utouchIn.h> |
| 26 | +#include <menuIO/serialOut.h> |
| 27 | +#include <menuIO/chainStream.h> |
| 28 | + |
| 29 | +using namespace Menu; |
| 30 | + |
| 31 | +UTFT tft(CTE28,25,26,27,28); |
| 32 | +//extern uint8_t SmallFont[]; |
| 33 | +extern uint8_t BigFont[]; |
| 34 | +//extern uint8_t SevenSegNumFont[]; |
| 35 | + |
| 36 | +#define LEDPIN 13 |
| 37 | + |
| 38 | +result showEvent(eventMask e,navNode& nav,prompt& item) { |
| 39 | + Serial.print("event: "); |
| 40 | + Serial.println(e); |
| 41 | + return proceed; |
| 42 | +} |
| 43 | + |
| 44 | +int test=55; |
| 45 | + |
| 46 | +result action1(eventMask e) { |
| 47 | + Serial.print(e); |
| 48 | + Serial.println(" action1 executed, proceed menu");Serial.flush(); |
| 49 | + return proceed; |
| 50 | +} |
| 51 | + |
| 52 | +result action2(eventMask e, navNode& nav, prompt &item, Stream &in, menuOut &out) { |
| 53 | + Serial.print(e); |
| 54 | + Serial.println(" action2 executed, quiting menu"); |
| 55 | + return quit; |
| 56 | +} |
| 57 | + |
| 58 | +int ledCtrl=LOW; |
| 59 | + |
| 60 | +result ledOn() { |
| 61 | + ledCtrl=HIGH; |
| 62 | + return proceed; |
| 63 | +} |
| 64 | +result ledOff() { |
| 65 | + ledCtrl=LOW; |
| 66 | + return proceed; |
| 67 | +} |
| 68 | + |
| 69 | +TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle |
| 70 | + ,VALUE("On",HIGH,doNothing,noEvent) |
| 71 | + ,VALUE("Off",LOW,doNothing,noEvent) |
| 72 | +); |
| 73 | + |
| 74 | +int selTest=0; |
| 75 | +SELECT(selTest,selMenu,"Select",doNothing,noEvent,noStyle |
| 76 | + ,VALUE("Zero",0,doNothing,noEvent) |
| 77 | + ,VALUE("One",1,doNothing,noEvent) |
| 78 | + ,VALUE("Two",2,doNothing,noEvent) |
| 79 | +); |
| 80 | + |
| 81 | +int chooseTest=-1; |
| 82 | +CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle |
| 83 | + ,VALUE("First",1,doNothing,noEvent) |
| 84 | + ,VALUE("Second",2,doNothing,noEvent) |
| 85 | + ,VALUE("Third",3,doNothing,noEvent) |
| 86 | + ,VALUE("Last",-1,doNothing,noEvent) |
| 87 | +); |
| 88 | + |
| 89 | +//customizing a prompt look! |
| 90 | +//by extending the prompt class |
| 91 | +class altPrompt:public prompt { |
| 92 | +public: |
| 93 | + altPrompt(const promptShadow& p):prompt(p) {} |
| 94 | + idx_t printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len) override { |
| 95 | + return out.printRaw("special prompt!",len);; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +MENU(subMenu,"Sub-Menu",showEvent,anyEvent,noStyle |
| 100 | + ,OP("Sub1",showEvent,anyEvent) |
| 101 | + ,OP("Sub2",showEvent,anyEvent) |
| 102 | + ,OP("Sub3",showEvent,anyEvent) |
| 103 | + ,altOP(altPrompt,"",showEvent,anyEvent) |
| 104 | + ,EXIT("<Back") |
| 105 | +); |
| 106 | + |
| 107 | +result alert(menuOut& o,idleEvent e) { |
| 108 | + if (e==idling) { |
| 109 | + o.setColor(fgColor); |
| 110 | + o.println("alert test"); |
| 111 | + o.println("press [select]"); |
| 112 | + o.println("to continue..."); |
| 113 | + } |
| 114 | + return proceed; |
| 115 | +} |
| 116 | + |
| 117 | +result doAlert(eventMask e, navNode& nav, prompt &item, Stream &in, menuOut &out) { |
| 118 | + nav.root->idleOn(alert); |
| 119 | + return proceed; |
| 120 | +} |
| 121 | + |
| 122 | +MENU(mainMenu,"Main menu",doNothing,noEvent,noStyle |
| 123 | + ,OP("Op1",action1,anyEvent) |
| 124 | + ,OP("Op2",action2,enterEvent) |
| 125 | + ,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,noStyle) |
| 126 | + ,SUBMENU(subMenu) |
| 127 | + ,SUBMENU(setLed) |
| 128 | + ,OP("LED On",ledOn,enterEvent) |
| 129 | + ,OP("LED Off",ledOff,enterEvent) |
| 130 | + ,SUBMENU(selMenu) |
| 131 | + ,SUBMENU(chooseMenu) |
| 132 | + ,OP("Alert test",doAlert,enterEvent) |
| 133 | + ,EXIT("<Back") |
| 134 | +); |
| 135 | + |
| 136 | +// define menu colors -------------------------------------------------------- |
| 137 | +// {{disabled normal,disabled selected},{enabled normal,enabled selected, enabled editing}} |
| 138 | +//monochromatic color table |
| 139 | + |
| 140 | +const colorDef<uint16_t> colors[] MEMMODE={ |
| 141 | + {{VGA_BLACK,VGA_BLACK},{VGA_BLACK,VGA_BLUE,VGA_BLUE}},//bgColor |
| 142 | + {{VGA_GRAY,VGA_GRAY},{VGA_WHITE,VGA_WHITE,VGA_WHITE}},//fgColor |
| 143 | + {{VGA_WHITE,VGA_BLACK},{VGA_YELLOW,VGA_YELLOW,VGA_RED}},//valColor |
| 144 | + {{VGA_WHITE,VGA_BLACK},{VGA_WHITE,VGA_YELLOW,VGA_YELLOW}},//unitColor |
| 145 | + {{VGA_WHITE,VGA_GRAY},{VGA_BLACK,VGA_BLUE,VGA_WHITE}},//cursorColor |
| 146 | + {{VGA_WHITE,VGA_YELLOW},{VGA_BLUE,VGA_RED,VGA_RED}},//titleColor |
| 147 | +}; |
| 148 | + |
| 149 | +//PANELS(serial_panels,{0,0,40,10});//or use default |
| 150 | +//serialOut outSerial(Serial);//,serial_panels);//the output device (just the serial port) |
| 151 | + |
| 152 | +#define MAX_DEPTH 2 |
| 153 | + |
| 154 | +PANELS(gfx_panels,{0,0,12,8},{13,0,12,8}); |
| 155 | +idx_t gfx_tops[MAX_DEPTH]; |
| 156 | +utftOut outGfx(tft,colors,gfx_tops,gfx_panels,16,16);//output device, latter set resolution from font measure |
| 157 | + |
| 158 | +MENU_OUTLIST(out,&outGfx); |
| 159 | + |
| 160 | +extern navRoot nav; |
| 161 | +UTouch uTouch( 6, 5, 4, 3, 2); |
| 162 | +menuUTouch touchPanel(uTouch,nav,outGfx); |
| 163 | + |
| 164 | +NAVROOT(nav,mainMenu,MAX_DEPTH,touchPanel,out); |
| 165 | + |
| 166 | +//when menu is suspended |
| 167 | +result idle(menuOut& o,idleEvent e) { |
| 168 | + if (e==idling) { |
| 169 | + o.setColor(fgColor); |
| 170 | + o.println("suspended..."); |
| 171 | + o.println("press [select]"); |
| 172 | + o.println("to continue"); |
| 173 | + o.println(millis()%1000); |
| 174 | + } |
| 175 | + return proceed; |
| 176 | +} |
| 177 | + |
| 178 | +config myOptions('>','-',false,false,defaultNavCodes); |
| 179 | + |
| 180 | +void setup() { |
| 181 | + options=&myOptions; |
| 182 | + pinMode(LEDPIN,OUTPUT); |
| 183 | + while(!Serial); |
| 184 | + Serial.begin(115200); |
| 185 | + Serial.println("menu 3.x");Serial.flush(); |
| 186 | + nav.idleTask=idle;//point a function to be used when menu is suspended |
| 187 | + //mainMenu[1].enabled=disabledStatus; |
| 188 | + |
| 189 | + tft.InitLCD(); |
| 190 | + tft.setBrightness(4); |
| 191 | + tft.clrScr(); |
| 192 | + |
| 193 | + uTouch.InitTouch(); |
| 194 | + uTouch.setPrecision(PREC_MEDIUM);//LOW, MEDIUM, HI, EXTREME |
| 195 | + |
| 196 | + tft.setFont(BigFont); |
| 197 | + tft.setColor(0, 255, 0); |
| 198 | + tft.setBackColor(0, 0, 0); |
| 199 | + |
| 200 | + //outGfx.resX=tft.getFontXsize()+1; |
| 201 | + //outGfx.resY=tft.getFontYsize()+1; |
| 202 | + outGfx.println("Menu 3.x on UTFT"); |
| 203 | + delay(1000); |
| 204 | + tft.clrScr(); |
| 205 | +} |
| 206 | + |
| 207 | +void loop() { |
| 208 | + nav.poll();//this device only draws when needed |
| 209 | + digitalWrite(LEDPIN, ledCtrl); |
| 210 | + delay(100);//simulate a delay when other tasks are done |
| 211 | +} |
0 commit comments