|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +/******************** |
| 4 | +Arduino generic menu system |
| 5 | +lolin32 menu example |
| 6 | +
|
| 7 | +output: onboard oled (i2c ssd1306 u8g2) |
| 8 | +input: Serial |
| 9 | +mcu: esp32 lolin with builtin oled |
| 10 | +
|
| 11 | +*/ |
| 12 | + |
| 13 | +#include <menu.h> |
| 14 | +#include <menuIO/u8g2Out.h> |
| 15 | +// #include <menuIO/encoderIn.h> |
| 16 | +// #include <menuIO/keyIn.h> |
| 17 | +#include <menuIO/chainStream.h> |
| 18 | +#include <menuIO/serialOut.h> |
| 19 | +#include <menuIO/serialIn.h> |
| 20 | + |
| 21 | +using namespace Menu; |
| 22 | + |
| 23 | +// #define LEDPIN LED_BUILTIN |
| 24 | + |
| 25 | +#include <Wire.h> |
| 26 | +#define fontName u8g2_font_7x13_mf |
| 27 | +#define fontX 7 |
| 28 | +#define fontY 16 |
| 29 | +#define offsetX 0 |
| 30 | +#define offsetY 3 |
| 31 | +#define U8_Width 128 |
| 32 | +#define U8_Height 64 |
| 33 | +#define USE_HWI2C |
| 34 | +// U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, 4, 5); |
| 35 | +U8G2_SSD1306_128X64_VCOMH0_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, 4, 5);//allow contrast change |
| 36 | +// U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, 4, 5); |
| 37 | + |
| 38 | + |
| 39 | +// define menu colors -------------------------------------------------------- |
| 40 | +//each color is in the format: |
| 41 | +// {{disabled normal,disabled selected},{enabled normal,enabled selected, enabled editing}} |
| 42 | +// this is a monochromatic color table |
| 43 | +const colorDef<uint8_t> colors[]={ |
| 44 | + {{0,0},{0,1,1}},//bgColor |
| 45 | + {{1,1},{1,0,0}},//fgColor |
| 46 | + {{1,1},{1,0,0}},//valColor |
| 47 | + {{1,1},{1,0,0}},//unitColor |
| 48 | + {{0,1},{0,0,1}},//cursorColor |
| 49 | + {{1,1},{1,0,0}},//titleColor |
| 50 | +}; |
| 51 | + |
| 52 | +result doAlert(eventMask e, prompt &item); |
| 53 | + |
| 54 | +int test=55; |
| 55 | + |
| 56 | +int ledCtrl=HIGH; |
| 57 | + |
| 58 | +result myLedOn() { |
| 59 | + ledCtrl=HIGH; |
| 60 | + return proceed; |
| 61 | +} |
| 62 | +result myLedOff() { |
| 63 | + ledCtrl=LOW; |
| 64 | + return proceed; |
| 65 | +} |
| 66 | + |
| 67 | +TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle |
| 68 | + ,VALUE("On",HIGH,doNothing,noEvent) |
| 69 | + ,VALUE("Off",LOW,doNothing,noEvent) |
| 70 | +); |
| 71 | + |
| 72 | +int selTest=0; |
| 73 | +SELECT(selTest,selMenu,"Select",doNothing,noEvent,noStyle |
| 74 | + ,VALUE("Zero",0,doNothing,noEvent) |
| 75 | + ,VALUE("One",1,doNothing,noEvent) |
| 76 | + ,VALUE("Two",2,doNothing,noEvent) |
| 77 | +); |
| 78 | + |
| 79 | +int chooseTest=-1; |
| 80 | +CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle |
| 81 | + ,VALUE("First",1,doNothing,noEvent) |
| 82 | + ,VALUE("Second",2,doNothing,noEvent) |
| 83 | + ,VALUE("Third",3,doNothing,noEvent) |
| 84 | + ,VALUE("Last",-1,doNothing,noEvent) |
| 85 | +); |
| 86 | + |
| 87 | +// //customizing a prompt look! |
| 88 | +// //by extending the prompt class |
| 89 | +// class altPrompt:public prompt { |
| 90 | +// public: |
| 91 | +// altPrompt(constMEM promptShadow& p):prompt(p) {} |
| 92 | +// Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t panelNr) override { |
| 93 | +// return out.printRaw("special prompt!",len);; |
| 94 | +// } |
| 95 | +// }; |
| 96 | + |
| 97 | +MENU(subMenu,"Sub-Menu",doNothing,noEvent,noStyle |
| 98 | + ,OP("Sub1",doNothing,noEvent) |
| 99 | + // ,altOP(altPrompt,"",doNothing,noEvent) |
| 100 | + ,EXIT("<Back") |
| 101 | +); |
| 102 | + |
| 103 | +uint16_t hrs=0; |
| 104 | +uint16_t mins=0; |
| 105 | + |
| 106 | +//define a pad style menu (single line menu) |
| 107 | +//here with a set of fields to enter a date in YYYY/MM/DD format |
| 108 | +altMENU(menu,time,"Time",doNothing,noEvent,noStyle,(systemStyles)(_asPad|Menu::_menuData|Menu::_canNav|_parentDraw) |
| 109 | + ,FIELD(hrs,"",":",0,11,1,0,doNothing,noEvent,noStyle) |
| 110 | + ,FIELD(mins,"","",0,59,10,1,doNothing,noEvent,wrapStyle) |
| 111 | +); |
| 112 | + |
| 113 | +char* constMEM hexDigit MEMMODE="0123456789ABCDEF"; |
| 114 | +char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit}; |
| 115 | +char buf1[]="0x11"; |
| 116 | + |
| 117 | +MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle |
| 118 | + ,OP("Op1",doNothing,noEvent) |
| 119 | + ,OP("Op2",doNothing,noEvent) |
| 120 | + //,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle) |
| 121 | + ,SUBMENU(time) |
| 122 | + ,SUBMENU(subMenu) |
| 123 | + ,SUBMENU(setLed) |
| 124 | + ,OP("LED On",myLedOn,enterEvent) |
| 125 | + ,OP("LED Off",myLedOff,enterEvent) |
| 126 | + ,SUBMENU(selMenu) |
| 127 | + ,SUBMENU(chooseMenu) |
| 128 | + ,OP("Alert test",doAlert,enterEvent) |
| 129 | + ,EDIT("Hex",buf1,hexNr,doNothing,noEvent,noStyle) |
| 130 | + ,EXIT("<Exit") |
| 131 | +); |
| 132 | + |
| 133 | +#define MAX_DEPTH 2 |
| 134 | + |
| 135 | +serialIn serial(Serial); |
| 136 | +MENU_INPUTS(in,&serial); |
| 137 | + |
| 138 | +MENU_OUTPUTS(out,MAX_DEPTH |
| 139 | + ,U8G2_OUT(u8g2,colors,fontX,fontY,offsetX,offsetY,{0,0,U8_Width/fontX,U8_Height/fontY}) |
| 140 | + ,SERIAL_OUT(Serial) |
| 141 | +); |
| 142 | + |
| 143 | +NAVROOT(nav,mainMenu,MAX_DEPTH,in,out); |
| 144 | + |
| 145 | +result alert(menuOut& o,idleEvent e) { |
| 146 | + if (e==idling) { |
| 147 | + o.setCursor(0,0); |
| 148 | + o.print("alert test"); |
| 149 | + o.setCursor(0,1); |
| 150 | + o.print("press [select]"); |
| 151 | + o.setCursor(0,2); |
| 152 | + o.print("to continue..."); |
| 153 | + } |
| 154 | + return proceed; |
| 155 | +} |
| 156 | + |
| 157 | +result doAlert(eventMask e, prompt &item) { |
| 158 | + nav.idleOn(alert); |
| 159 | + return proceed; |
| 160 | +} |
| 161 | + |
| 162 | +//when menu is suspended |
| 163 | +result idle(menuOut& o,idleEvent e) { |
| 164 | + o.clear(); |
| 165 | + switch(e) { |
| 166 | + case idleStart:o.println("suspending menu!");break; |
| 167 | + case idling:o.println("suspended...");break; |
| 168 | + case idleEnd:o.println("resuming menu.");break; |
| 169 | + } |
| 170 | + return proceed; |
| 171 | +} |
| 172 | + |
| 173 | +void setup() { |
| 174 | + // pinMode(LEDPIN,OUTPUT); |
| 175 | + Serial.begin(115200); |
| 176 | + while(!Serial); |
| 177 | + Serial.println("menu 4.x test");Serial.flush(); |
| 178 | + Wire.begin(5,4); |
| 179 | + u8g2.begin(); |
| 180 | + u8g2.setFont(fontName); |
| 181 | + // disable second option |
| 182 | + mainMenu[1].enabled=disabledStatus; |
| 183 | + nav.idleTask=idle;//point a function to be used when menu is suspended |
| 184 | + Serial.println("setup done.");Serial.flush(); |
| 185 | + u8g2.firstPage(); |
| 186 | + do { |
| 187 | + u8g2.drawStr(0,fontY,"ArduinoMenu 4.x"); |
| 188 | + u8g2.drawStr(0,fontY<<1,"on lolin32"); |
| 189 | + u8g2.drawStr(0,fontY+(fontY<<1),"with buitin oled"); |
| 190 | + } while(u8g2.nextPage()); |
| 191 | + for(int c=256;c>0;c--) { |
| 192 | + u8g2.setContrast(255-255.0*log(c)/log(255)); |
| 193 | + delay(8); |
| 194 | + } |
| 195 | + u8g2.setContrast(255); |
| 196 | + delay(500); |
| 197 | +} |
| 198 | + |
| 199 | +void loop() { |
| 200 | + nav.doInput(); |
| 201 | + // digitalWrite(LEDPIN, !ledCtrl);//no led on this board |
| 202 | + if (nav.changed(0)) {//only draw if menu changed for gfx device |
| 203 | + u8g2.firstPage(); |
| 204 | + do nav.doOutput(); while(u8g2.nextPage()); |
| 205 | + } |
| 206 | + delay(100);//simulate other tasks delay |
| 207 | +} |
0 commit comments