|
| 1 | +/******************** |
| 2 | +Arduino generic menu system |
| 3 | +
|
| 4 | +Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 5 | +
|
| 6 | +output: Serial3 |
| 7 | +input: Serial3 |
| 8 | +mcu: stm32f103 (blue pill) |
| 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 PC13 |
| 18 | + |
| 19 | +result showEvent(eventMask e,navNode& nav,prompt& item) { |
| 20 | + Serial3.print("event: "); |
| 21 | + Serial3.println(e); |
| 22 | + return proceed; |
| 23 | +} |
| 24 | + |
| 25 | +float test=55; |
| 26 | + |
| 27 | +result action1(eventMask e) { |
| 28 | + Serial3.print(e); |
| 29 | + Serial3.println(" action1 executed, proceed menu"); |
| 30 | + Serial3.flush(); |
| 31 | + return proceed; |
| 32 | +} |
| 33 | + |
| 34 | +result action2(eventMask e, prompt &item) { |
| 35 | + Serial3.print(e); |
| 36 | + Serial3.print(" action2 executed, quiting menu"); |
| 37 | + return quit; |
| 38 | +} |
| 39 | + |
| 40 | +int ledCtrl=LOW; |
| 41 | + |
| 42 | +result myLedOn() { |
| 43 | + ledCtrl=HIGH; |
| 44 | + return proceed; |
| 45 | +} |
| 46 | +result myLedOff() { |
| 47 | + ledCtrl=LOW; |
| 48 | + return proceed; |
| 49 | +} |
| 50 | + |
| 51 | +TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle |
| 52 | + ,VALUE("On",HIGH,doNothing,noEvent) |
| 53 | + ,VALUE("Off",LOW,doNothing,noEvent) |
| 54 | +); |
| 55 | + |
| 56 | +int selTest=0; |
| 57 | +SELECT(selTest,selMenu,"Select",doNothing,noEvent,noStyle |
| 58 | + ,VALUE("Zero",0,doNothing,noEvent) |
| 59 | + ,VALUE("One",1,doNothing,noEvent) |
| 60 | + ,VALUE("Two",2,doNothing,noEvent) |
| 61 | +); |
| 62 | + |
| 63 | +int chooseTest=-1; |
| 64 | +CHOOSE(chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle |
| 65 | + ,VALUE("First",1,doNothing,noEvent) |
| 66 | + ,VALUE("Second",2,doNothing,noEvent) |
| 67 | + ,VALUE("Third",3,doNothing,noEvent) |
| 68 | + ,VALUE("Last",-1,doNothing,noEvent) |
| 69 | +); |
| 70 | + |
| 71 | +//customizing a prompt look! |
| 72 | +//by extending the prompt class |
| 73 | +class altPrompt:public prompt { |
| 74 | +public: |
| 75 | + altPrompt(constMEM promptShadow& p):prompt(p) {} |
| 76 | + Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override { |
| 77 | + return out.printRaw("special prompt!",len); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +MENU(subMenu,"Sub-Menu",showEvent,anyEvent,noStyle |
| 82 | + ,OP("Sub1",showEvent,anyEvent) |
| 83 | + ,OP("Sub2",showEvent,anyEvent) |
| 84 | + ,OP("Sub3",showEvent,anyEvent) |
| 85 | + ,altOP(altPrompt,"",showEvent,anyEvent) |
| 86 | + ,EXIT("<Back") |
| 87 | +); |
| 88 | + |
| 89 | +uint16_t year=2017; |
| 90 | +uint16_t month=10; |
| 91 | +uint16_t day=7; |
| 92 | + |
| 93 | +//define a pad style menu (single line menu) |
| 94 | +//here with a set of fields to enter a date in YYYY/MM/DD format |
| 95 | +//altMENU(menu,birthDate,"Birth",doNothing,noEvent,noStyle,(systemStyles)(_asPad|Menu::_menuData|Menu::_canNav|_parentDraw) |
| 96 | +PADMENU(birthDate,"Birth",doNothing,noEvent,noStyle |
| 97 | + ,FIELD(year,"","/",1900,3000,20,1,doNothing,noEvent,noStyle) |
| 98 | + ,FIELD(month,"","/",1,12,1,0,doNothing,noEvent,wrapStyle) |
| 99 | + ,FIELD(day,"","",1,31,1,0,doNothing,noEvent,wrapStyle) |
| 100 | +); |
| 101 | + |
| 102 | +char* constMEM hexDigit MEMMODE="0123456789ABCDEF"; |
| 103 | +char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit}; |
| 104 | +char buf1[]="0x11"; |
| 105 | + |
| 106 | +MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle |
| 107 | + ,OP("Op1",action1,anyEvent) |
| 108 | + ,OP("Op2",action2,enterEvent) |
| 109 | + ,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle) |
| 110 | + ,SUBMENU(subMenu) |
| 111 | + ,SUBMENU(setLed) |
| 112 | + ,OP("LED On",myLedOn,enterEvent) |
| 113 | + ,OP("LED Off",myLedOff,enterEvent) |
| 114 | + ,SUBMENU(selMenu) |
| 115 | + ,SUBMENU(chooseMenu) |
| 116 | + ,OP("Alert test",doAlert,enterEvent) |
| 117 | + ,EDIT("Hex",buf1,hexNr,doNothing,noEvent,noStyle) |
| 118 | + ,SUBMENU(birthDate) |
| 119 | + ,EXIT("<Back") |
| 120 | +); |
| 121 | + |
| 122 | +#define MAX_DEPTH 2 |
| 123 | + |
| 124 | +MENU_OUTPUTS(out,MAX_DEPTH |
| 125 | + ,SERIAL_OUT(Serial3) |
| 126 | + ,NONE//must have 2 items at least |
| 127 | +); |
| 128 | + |
| 129 | +serialIn serial(Serial3); |
| 130 | +NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out); |
| 131 | + |
| 132 | +result alert(menuOut& o,idleEvent e) { |
| 133 | + if (e==idling) { |
| 134 | + o.setCursor(0,0); |
| 135 | + o.print("alert test"); |
| 136 | + o.setCursor(0,1); |
| 137 | + o.print("press [select]"); |
| 138 | + o.setCursor(0,2); |
| 139 | + o.print("to continue..."); |
| 140 | + } |
| 141 | + return proceed; |
| 142 | +} |
| 143 | + |
| 144 | +result doAlert(eventMask e, prompt &item) { |
| 145 | + nav.idleOn(alert); |
| 146 | + return proceed; |
| 147 | +} |
| 148 | + |
| 149 | +//when menu is suspended |
| 150 | +result idle(menuOut &o, idleEvent e) { |
| 151 | + // o.clear(); |
| 152 | + switch(e) { |
| 153 | + case idleStart:o.println("suspending menu!");break; |
| 154 | + case idling:o.println("suspended...");break; |
| 155 | + case idleEnd:o.println("resuming menu.");break; |
| 156 | + } |
| 157 | + return proceed; |
| 158 | +} |
| 159 | + |
| 160 | +void setup() { |
| 161 | + pinMode(LEDPIN,OUTPUT); |
| 162 | + digitalWrite(LEDPIN,!ledCtrl); |
| 163 | + delay(500); |
| 164 | + Serial3.begin(115200); |
| 165 | + // while(!Serial);//this locks on stm32 |
| 166 | + Serial3.println("menu 4.x test");Serial3.flush(); |
| 167 | + nav.idleTask=idle;//point a function to be used when menu is suspended |
| 168 | + // nav.idleOn();//this menu will start on idle state, press select to enter menu |
| 169 | + //nav.doInput("323"); |
| 170 | + nav.timeOut=60; |
| 171 | +} |
| 172 | + |
| 173 | +void loop() { |
| 174 | + nav.poll(); |
| 175 | + digitalWrite(LEDPIN, !ledCtrl); |
| 176 | + delay(100);//simulate a delay when other tasks are done |
| 177 | +} |
0 commit comments