|
| 1 | +/******************** |
| 2 | +Arduino generic menu system |
| 3 | +printing events and navigation info |
| 4 | +
|
| 5 | +Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 6 | +
|
| 7 | +output: Serial |
| 8 | +input: Serial |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <menu.h> |
| 12 | +#include <menuIO/serialOut.h> |
| 13 | +#include <menuIO/serialIn.h> |
| 14 | + |
| 15 | +using namespace Menu; |
| 16 | + |
| 17 | +#define LEDPIN LED_BUILTIN |
| 18 | + |
| 19 | +void showPath(navRoot& root) { |
| 20 | + Serial.print("nav level:"); |
| 21 | + Serial.print(root.level); |
| 22 | + Serial.print(" path:["); |
| 23 | + for(int n=0;n<=root.level;n++) { |
| 24 | + Serial.print(n?",":""); |
| 25 | + Serial.print(root.path[n].sel); |
| 26 | + } |
| 27 | + Serial.println("]"); |
| 28 | +} |
| 29 | + |
| 30 | +result showEvent(eventMask e,navNode& nav,prompt& item) { |
| 31 | + Serial.println(); |
| 32 | + Serial.println("========"); |
| 33 | + Serial.print("Event for target: 0x"); |
| 34 | + Serial.println((long)nav.target,HEX); |
| 35 | + showPath(*nav.root); |
| 36 | + Serial.print(e); |
| 37 | + switch(e) { |
| 38 | + case noEvent://just ignore all stuff |
| 39 | + Serial.println(" noEvent");break; |
| 40 | + case activateEvent://this item is about to be active (system event) |
| 41 | + Serial.println(" activateEvent");break; |
| 42 | + case enterEvent://entering navigation level (this menu is now active) |
| 43 | + Serial.println(" enterEvent");break; |
| 44 | + case exitEvent://leaving navigation level |
| 45 | + Serial.println(" exitEvent");break; |
| 46 | + case returnEvent://TODO:entering previous level (return) |
| 47 | + Serial.println(" returnEvent");break; |
| 48 | + case focusEvent://element just gained focus |
| 49 | + Serial.println(" focusEvent");break; |
| 50 | + case blurEvent://element about to lose focus |
| 51 | + Serial.println(" blurEvent");break; |
| 52 | + case selFocusEvent://TODO:child just gained focus |
| 53 | + Serial.println(" selFocusEvent");break; |
| 54 | + case selBlurEvent://TODO:child about to lose focus |
| 55 | + Serial.println(" selBlurEvent");break; |
| 56 | + case updateEvent://Field value has been updated |
| 57 | + Serial.println(" updateEvent");break; |
| 58 | + case anyEvent: |
| 59 | + Serial.println(" anyEvent");break; |
| 60 | + } |
| 61 | + return proceed; |
| 62 | +} |
| 63 | + |
| 64 | +float test=55; |
| 65 | + |
| 66 | +int ledCtrl=LOW; |
| 67 | + |
| 68 | +result myLedOn() { |
| 69 | + ledCtrl=HIGH; |
| 70 | + return proceed; |
| 71 | +} |
| 72 | +result myLedOff() { |
| 73 | + ledCtrl=LOW; |
| 74 | + return proceed; |
| 75 | +} |
| 76 | + |
| 77 | +TOGGLE(ledCtrl,setLed,"Led: ",showEvent,anyEvent,noStyle//,doExit,enterEvent,noStyle |
| 78 | + ,VALUE("On",HIGH,showEvent,anyEvent) |
| 79 | + ,VALUE("Off",LOW,showEvent,anyEvent) |
| 80 | +); |
| 81 | + |
| 82 | +int selTest=0; |
| 83 | +SELECT(selTest,selMenu,"Select",showEvent,anyEvent,noStyle |
| 84 | + ,VALUE("Zero",0,showEvent,anyEvent) |
| 85 | + ,VALUE("One",1,showEvent,anyEvent) |
| 86 | + ,VALUE("Two",2,showEvent,anyEvent) |
| 87 | +); |
| 88 | + |
| 89 | +int chooseTest=-1; |
| 90 | +CHOOSE(chooseTest,chooseMenu,"Choose",showEvent,anyEvent,noStyle |
| 91 | + ,VALUE("First",1,showEvent,anyEvent) |
| 92 | + ,VALUE("Second",2,showEvent,anyEvent) |
| 93 | + ,VALUE("Third",3,showEvent,anyEvent) |
| 94 | + ,VALUE("Last",-1,showEvent,anyEvent) |
| 95 | +); |
| 96 | + |
| 97 | +//customizing a prompt look! |
| 98 | +//by extending the prompt class |
| 99 | +class altPrompt:public prompt { |
| 100 | +public: |
| 101 | + // altPrompt(constMEM promptShadow& p):prompt(p) {} |
| 102 | + using prompt::prompt; |
| 103 | + Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override { |
| 104 | + return out.printRaw(F("special prompt!"),len); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +MENU(subMenu,"Sub-Menu",showEvent,anyEvent,noStyle |
| 109 | + ,OP("Sub1",showEvent,anyEvent) |
| 110 | + ,OP("Sub2",showEvent,anyEvent) |
| 111 | + ,OP("Sub3",showEvent,anyEvent) |
| 112 | + ,altOP(altPrompt,"",showEvent,anyEvent) |
| 113 | + ,EXIT("<Back") |
| 114 | +); |
| 115 | + |
| 116 | +uint16_t year=2017; |
| 117 | +uint16_t month=10; |
| 118 | +uint16_t day=7; |
| 119 | + |
| 120 | +//define a pad style menu (single line menu) |
| 121 | +//here with a set of fields to enter a date in YYYY/MM/DD format |
| 122 | +//altMENU(menu,birthDate,"Birth",showEvent,anyEvent,noStyle,(systemStyles)(_asPad|Menu::_menuData|Menu::_canNav|_parentDraw) |
| 123 | +PADMENU(birthDate,"Birth",showEvent,anyEvent,noStyle |
| 124 | + ,FIELD(year,"","/",1900,3000,20,1,showEvent,anyEvent,noStyle) |
| 125 | + ,FIELD(month,"","/",1,12,1,0,showEvent,anyEvent,wrapStyle) |
| 126 | + ,FIELD(day,"","",1,31,1,0,showEvent,anyEvent,wrapStyle) |
| 127 | +); |
| 128 | + |
| 129 | +char* constMEM hexDigit MEMMODE="0123456789ABCDEF"; |
| 130 | +char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit}; |
| 131 | +char buf1[]="0x11"; |
| 132 | + |
| 133 | +char* constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\|!\"#$%&/()=?~*^+-{}[]€"; |
| 134 | +char* constMEM alphaNumMask[] MEMMODE={alphaNum}; |
| 135 | +char name[]=" "; |
| 136 | + |
| 137 | +MENU(mainMenu,"Main menu",showEvent,anyEvent,wrapStyle |
| 138 | + ,OP("Op1",showEvent,anyEvent) |
| 139 | + ,OP("Op2",showEvent,anyEvent) |
| 140 | + ,FIELD(test,"Test","%",0,100,10,1,showEvent,anyEvent,wrapStyle) |
| 141 | + ,SUBMENU(subMenu) |
| 142 | + ,SUBMENU(setLed) |
| 143 | + ,OP("LED On",myLedOn,enterEvent) |
| 144 | + ,OP("LED Off",myLedOff,enterEvent) |
| 145 | + ,SUBMENU(selMenu) |
| 146 | + ,SUBMENU(chooseMenu) |
| 147 | + ,OP("Alert test",doAlert,enterEvent) |
| 148 | + ,EDIT("Name",name,alphaNumMask,showEvent,anyEvent,noStyle) |
| 149 | + ,EDIT("Hex",buf1,hexNr,showEvent,anyEvent,noStyle) |
| 150 | + ,SUBMENU(birthDate) |
| 151 | + ,EXIT("<Back") |
| 152 | +); |
| 153 | + |
| 154 | +#define MAX_DEPTH 2 |
| 155 | + |
| 156 | +MENU_OUTPUTS(out,MAX_DEPTH |
| 157 | + ,SERIAL_OUT(Serial) |
| 158 | + ,NONE//must have 2 items at least |
| 159 | +); |
| 160 | + |
| 161 | +serialIn serial(Serial); |
| 162 | +NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out); |
| 163 | + |
| 164 | +result alert(menuOut& o,idleEvent e) { |
| 165 | + if (e==idling) { |
| 166 | + o.setCursor(0,0); |
| 167 | + o.print("alert test"); |
| 168 | + o.setCursor(0,1); |
| 169 | + o.print("press [select]"); |
| 170 | + o.setCursor(0,2); |
| 171 | + o.print("to continue..."); |
| 172 | + } |
| 173 | + return proceed; |
| 174 | +} |
| 175 | + |
| 176 | +result doAlert(eventMask e, prompt &item) { |
| 177 | + nav.idleOn(alert); |
| 178 | + return proceed; |
| 179 | +} |
| 180 | + |
| 181 | +result idle(menuOut &o, idleEvent e) { |
| 182 | + // o.clear(); |
| 183 | + switch(e) { |
| 184 | + case idleStart:o.println("suspending menu!");break; |
| 185 | + case idling:o.println("suspended...");break; |
| 186 | + case idleEnd:o.println("resuming menu."); |
| 187 | + nav.reset(); |
| 188 | + break; |
| 189 | + } |
| 190 | + return proceed; |
| 191 | +} |
| 192 | + |
| 193 | +void setup() { |
| 194 | + pinMode(LEDPIN,OUTPUT); |
| 195 | + digitalWrite(LEDPIN,ledCtrl); |
| 196 | + delay(500); |
| 197 | + Serial.begin(115200); |
| 198 | + while(!Serial); |
| 199 | + Serial.println("menu 4.x test");Serial.flush(); |
| 200 | + nav.timeOut=120; |
| 201 | + nav.idleTask=idle;//point a function to be used when menu is suspended |
| 202 | + // nav.idleOn();//this menu will start on idle state, press select to enter menu |
| 203 | + //nav.doInput("323"); |
| 204 | + // nav.useAccel=false; |
| 205 | +} |
| 206 | + |
| 207 | +void loop() { |
| 208 | + nav.poll(); |
| 209 | + digitalWrite(LEDPIN, ledCtrl); |
| 210 | + delay(100);//simulate a delay when other tasks are done |
| 211 | +} |
0 commit comments