@@ -16,13 +16,16 @@ user defined array menu (userMenu plugin)
1616
1717using namespace Menu ;
1818
19- # define MAX_DEPTH 4
19+ // ===========================================================================
2020
21+ // some values for user data record
2122enum SelTest {Zero=0 ,One,Two};
2223enum ChooseTest {First=1 ,Second=2 ,Third=3 ,Last=-1 };
2324constexpr int dataSz=3 ;
2425constexpr int nameSz=20 ;
2526
27+
28+ // some user data record type to be edited by the menu
2629struct Data {
2730 char name[nameSz+1 ]=" <name> " ;
2831 bool valid=0 ;
@@ -38,17 +41,20 @@ struct Data {
3841 // }
3942};
4043
44+ // THE DATA <-----------------------------------------
4145Data myTargets[dataSz];// our data
42- // a temp. to be edited
46+
47+ // a temporary to be edited
4348// the data type must have assignment operator
44- // because we will later on move data back and forth between the actual data and this template
45- // menu edit is wired to this temp. and menu eventd will deal with the copy-call
49+ // because we will later on move data back and forth between the actual data and this temporary
50+ // menu edit is wired to this temp. and menu events will deal with the copy-call (from/to)
4651Data target;
4752
48-
53+ // characters allowed on name field
4954char * constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\ |!\" #$%&/()=?~*^+-{}[]€" ;
5055char * constMEM alphaNumMask[] MEMMODE={alphaNum};
5156
57+ // some enumeration of values allowed on some fields
5258TOGGLE (target.valid,editValid," Valid: " ,doNothing,noEvent,noStyle// ,doExit,enterEvent,noStyle
5359 ,VALUE(" On" ,HIGH,doNothing,noEvent)
5460 ,VALUE(" Off" ,LOW,doNothing,noEvent)
@@ -67,14 +73,15 @@ CHOOSE(target.chooseTest,chooseMenu,"Choose",doNothing,noEvent,noStyle
6773 ,VALUE(" Last" ,Last,doNothing,noEvent)
6874);
6975
76+ // a function to save the edited data record
7077result saveTarget (eventMask e,navNode& nav) {
7178 _trace (MENU_DEBUG_OUT<<" saveTarget" <<endl);
7279 idx_t n=nav.root ->path [nav.root ->level -1 ].sel ;// get selection of previous level
7380 myTargets[n]=target;
7481 return quit;
7582}
7683
77- // if you want to print the recird name as the title,
84+ // if you want to print the data record name as the title,
7885// then you MUST create a customized print menu to replace this default one
7986MENU (targetEdit," Target edit" ,doNothing,noEvent,wrapStyle
8087 ,EDIT(" Name" ,target.name,alphaNumMask,doNothing,noEvent,noStyle)
@@ -86,27 +93,31 @@ MENU(targetEdit,"Target edit",doNothing,noEvent,wrapStyle
8693);
8794
8895// handling the user menu selection
89- //
96+ // this will copy the selected recotd into the temp var
9097result targetEvent (eventMask e,navNode& nav) {
9198 _trace (MENU_DEBUG_OUT<<" copy data to temp.\n " );
9299 target=myTargets[nav.sel ];
93100 // you can store nav.sel for future reference
94101 return proceed;
95102}
96103
104+ // the customized print of records
105+ // menu system wil use this to print the list of all records
97106struct TargetMenu :UserMenu{
98107 using UserMenu::UserMenu;
99108
100109 // override sz() function to have variable/custom size
101110 // inline idx_t sz() const override {return 0;}
102111
103- // customizing the print of user menu
112+ // customizing the print of user menu item (len is the availabe space)
104113 Used printItem (menuOut& out, int idx,int len) override {
114+ // just printing the string name to the menu output device
115+ // as an item representative
105116 return len?out.printText (myTargets[idx].name ,len):0 ;
106117 }
107118};
108119
109- // build the user menu object, optinally giving a sub menu
120+ // build the user menu object, optionally giving a sub menu
110121#ifdef MENU_USERAM
111122 // for non-AVR devices or when MENU_USERAM is defined at compiler level
112123 TargetMenu targetsMenu (" Targets" ,dataSz,targetEdit,targetEvent,enterEvent);
@@ -127,10 +138,15 @@ struct TargetMenu:UserMenu{
127138#endif
128139
129140MENU (mainMenu," Main menu" ,doNothing,noEvent,wrapStyle
130- ,OBJ(targetsMenu)// use in a macro built menu
141+ ,OBJ(targetsMenu)// attach the array edit menu on a macri build nenu
131142 ,EXIT(" <Back" )
132143);
133144
145+ // ////////////////////////////////////////////////////////////////////////////
146+ // menu IO and root navigation control
147+
148+ #define MAX_DEPTH 4
149+
134150MENU_OUTPUTS (out,MAX_DEPTH
135151 ,SERIAL_OUT(Serial)
136152 ,NONE// must have 2 items at least
@@ -139,6 +155,7 @@ MENU_OUTPUTS(out,MAX_DEPTH
139155serialIn serial (Serial);
140156NAVROOT (nav,mainMenu,MAX_DEPTH,serial,out);
141157
158+ // function for menu idle/timemout
142159result idle (menuOut &o, idleEvent e) {
143160 switch (e) {
144161 case idleStart:o.println (" suspending menu!" );break ;
0 commit comments