|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +/******************** |
| 4 | +Sept. 2014 ~ Jan 2019 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 5 | +
|
| 6 | +TFT_eSPI example |
| 7 | +output: TFT_eSPI + Serial |
| 8 | +input: Serial |
| 9 | +www.r-site.net |
| 10 | +***/ |
| 11 | + |
| 12 | +#include <TFT_eSPI.h> |
| 13 | + |
| 14 | +#include <menu.h> |
| 15 | +#include <menuIO/serialIO.h> |
| 16 | +#include <menuIO/TFT_eSPIOut.h> |
| 17 | +// #include <menuIO/chainStream.h> |
| 18 | + |
| 19 | +using namespace Menu; |
| 20 | + |
| 21 | +TFT_eSPI gfx; |
| 22 | + |
| 23 | +result doAlert(eventMask e, prompt &item); |
| 24 | + |
| 25 | +int test=55; |
| 26 | + |
| 27 | +int ledCtrl=LOW; |
| 28 | + |
| 29 | +result myLedOn() { |
| 30 | + ledCtrl=HIGH; |
| 31 | + return proceed; |
| 32 | +} |
| 33 | +result myLedOff() { |
| 34 | + ledCtrl=LOW; |
| 35 | + return proceed; |
| 36 | +} |
| 37 | + |
| 38 | +TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle |
| 39 | + ,VALUE("On",HIGH,doNothing,noEvent) |
| 40 | + ,VALUE("Off",LOW,doNothing,noEvent) |
| 41 | +); |
| 42 | + |
| 43 | +int selTest=0; |
| 44 | +SELECT(selTest,selMenu,"Select",doNothing,noEvent,noStyle |
| 45 | + ,VALUE("Zero",0,doNothing,noEvent) |
| 46 | + ,VALUE("One",1,doNothing,noEvent) |
| 47 | + ,VALUE("Two",2,doNothing,noEvent) |
| 48 | +); |
| 49 | + |
| 50 | +int chooseTest=-1; |
| 51 | +CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle |
| 52 | + ,VALUE("First",1,doNothing,noEvent) |
| 53 | + ,VALUE("Second",2,doNothing,noEvent) |
| 54 | + ,VALUE("Third",3,doNothing,noEvent) |
| 55 | + ,VALUE("Last",-1,doNothing,noEvent) |
| 56 | +); |
| 57 | + |
| 58 | +//customizing a prompt look! |
| 59 | +//by extending the prompt class |
| 60 | +class altPrompt:public prompt { |
| 61 | +public: |
| 62 | + altPrompt(constMEM promptShadow& p):prompt(p) {} |
| 63 | + Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override { |
| 64 | + return out.printRaw(F("special prompt!"),len);; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +MENU(subMenu,"Sub-Menu",doNothing,noEvent,noStyle |
| 69 | + ,altOP(altPrompt,"",doNothing,noEvent) |
| 70 | + ,OP("Op",doNothing,noEvent) |
| 71 | + ,EXIT("<Back") |
| 72 | +); |
| 73 | + |
| 74 | +char* constMEM hexDigit MEMMODE="0123456789ABCDEF"; |
| 75 | +char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit}; |
| 76 | +char buf1[]="0x11"; |
| 77 | + |
| 78 | +MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle |
| 79 | + ,OP("Op1",doNothing,noEvent) |
| 80 | + ,OP("Op2",doNothing,noEvent) |
| 81 | + // ,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle) |
| 82 | + ,SUBMENU(subMenu) |
| 83 | + ,SUBMENU(setLed) |
| 84 | + ,OP("LED On",myLedOn,enterEvent) |
| 85 | + ,OP("LED Off",myLedOff,enterEvent) |
| 86 | + ,SUBMENU(selMenu) |
| 87 | + ,SUBMENU(chooseMenu) |
| 88 | + //,OP("Alert test",doAlert,enterEvent) |
| 89 | + ,EDIT("Hex",buf1,hexNr,doNothing,noEvent,noStyle) |
| 90 | + ,EXIT("<Back") |
| 91 | +); |
| 92 | + |
| 93 | +// define menu colors -------------------------------------------------------- |
| 94 | +// {{disabled normal,disabled selected},{enabled normal,enabled selected, enabled editing}} |
| 95 | +//monochromatic color table |
| 96 | + |
| 97 | + |
| 98 | +#define Black RGB565(0,0,0) |
| 99 | +#define Red RGB565(255,0,0) |
| 100 | +#define Green RGB565(0,255,0) |
| 101 | +#define Blue RGB565(0,0,255) |
| 102 | +#define Gray RGB565(128,128,128) |
| 103 | +#define LighterRed RGB565(255,150,150) |
| 104 | +#define LighterGreen RGB565(150,255,150) |
| 105 | +#define LighterBlue RGB565(150,150,255) |
| 106 | +#define DarkerRed RGB565(150,0,0) |
| 107 | +#define DarkerGreen RGB565(0,150,0) |
| 108 | +#define DarkerBlue RGB565(0,0,150) |
| 109 | +#define Cyan RGB565(0,255,255) |
| 110 | +#define Magenta RGB565(255,0,255) |
| 111 | +#define Yellow RGB565(255,255,0) |
| 112 | +#define White RGB565(255,255,255) |
| 113 | + |
| 114 | +const colorDef<uint16_t> colors[] MEMMODE={ |
| 115 | + {{(uint16_t)Black,(uint16_t)Black}, {(uint16_t)Black, (uint16_t)Blue, (uint16_t)Blue}},//bgColor |
| 116 | + {{(uint16_t)Gray, (uint16_t)Gray}, {(uint16_t)White, (uint16_t)White, (uint16_t)White}},//fgColor |
| 117 | + {{(uint16_t)White,(uint16_t)Black}, {(uint16_t)Yellow,(uint16_t)Yellow,(uint16_t)Red}},//valColor |
| 118 | + {{(uint16_t)White,(uint16_t)Black}, {(uint16_t)White, (uint16_t)Yellow,(uint16_t)Yellow}},//unitColor |
| 119 | + {{(uint16_t)White,(uint16_t)Gray}, {(uint16_t)Black, (uint16_t)Blue, (uint16_t)White}},//cursorColor |
| 120 | + {{(uint16_t)White,(uint16_t)Yellow},{(uint16_t)Blue, (uint16_t)Red, (uint16_t)Red}},//titleColor |
| 121 | +}; |
| 122 | + |
| 123 | +#define MAX_DEPTH 4 |
| 124 | + |
| 125 | +serialIn serial(Serial); |
| 126 | + |
| 127 | +//MENU_INPUTS(in,&serial);its single, no need to `chainStream` |
| 128 | + |
| 129 | +//define serial output device |
| 130 | +idx_t serialTops[MAX_DEPTH]={0}; |
| 131 | +serialOut outSerial(Serial,serialTops); |
| 132 | + |
| 133 | +#define GFX_WIDTH 128 |
| 134 | +#define GFX_HEIGHT 64 |
| 135 | +#define fontW 6 |
| 136 | +#define fontH 9 |
| 137 | + |
| 138 | +constMEM panel panels[] MEMMODE = {{0, 0, GFX_WIDTH / fontW, GFX_HEIGHT / fontH}}; |
| 139 | +navNode* nodes[sizeof(panels) / sizeof(panel)]; //navNodes to store navigation status |
| 140 | +panelsList pList(panels, nodes, 1); //a list of panels and nodes |
| 141 | +idx_t eSpiTops[MAX_DEPTH]={0}; |
| 142 | +TFT_eSPIOut eSpiOut(gfx,colors,eSpiTops,pList,fontW,fontH+1); |
| 143 | +menuOut* constMEM outputs[] MEMMODE={&outSerial,&eSpiOut};//list of output devices |
| 144 | +outputsList out(outputs,sizeof(outputs)/sizeof(menuOut*));//outputs list controller |
| 145 | + |
| 146 | +NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out); |
| 147 | + |
| 148 | +//when menu is suspended |
| 149 | +result idle(menuOut& o,idleEvent e) { |
| 150 | + if (e==idling) { |
| 151 | + o.println(F("suspended...")); |
| 152 | + o.println(F("press [select]")); |
| 153 | + o.println(F("to continue")); |
| 154 | + } |
| 155 | + return proceed; |
| 156 | +} |
| 157 | + |
| 158 | +//config myOptions('*','-',defaultNavCodes,false); |
| 159 | + |
| 160 | +void setup() { |
| 161 | + //options=&myOptions;//can customize options |
| 162 | + // pinMode(LEDPIN,OUTPUT); |
| 163 | + Serial.begin(115200); |
| 164 | + while(!Serial); |
| 165 | + Serial.println("menu 4.x test"); |
| 166 | + Serial.flush(); |
| 167 | + nav.idleTask=idle;//point a function to be used when menu is suspended |
| 168 | + mainMenu[1].disable(); |
| 169 | + //outGfx.usePreview=true;//reserve one panel for preview? |
| 170 | + //nav.showTitle=false;//show menu title? |
| 171 | + |
| 172 | + SPI.begin(); |
| 173 | + // gfx.initR(INITR_BLACKTAB); |
| 174 | + gfx.setRotation(3); |
| 175 | + // gfx.setTextSize(textScale);//test scalling |
| 176 | + gfx.setTextWrap(false); |
| 177 | + gfx.fillScreen(Black); |
| 178 | + gfx.setTextColor(Red,Black); |
| 179 | + gfx.println("Menu 4.x test on GFX"); |
| 180 | + delay(1000); |
| 181 | +} |
| 182 | + |
| 183 | +void loop() { |
| 184 | + nav.poll();//this device only draws when needed |
| 185 | + // digitalWrite(LEDPIN, ledCtrl); |
| 186 | + delay(100);//simulate a delay when other tasks are done |
| 187 | +} |
0 commit comments