About memory allocation. Good point. I wrote an allocation and garbage collector system. Unfortunately it was too large for Arduino.
You must consider that most part of memory consumption is due to literals (i.e. "strings"). You could deallocate menu object(s), but in your program the strings still got space at compile time, even without any object instantiation. I think that the actual memory constraints of Arduino UNO will be over soon. Some clones today provide more available ram to the users. I suggest to use the freeRam function to test new sketches, same as to trace error messages during debugging with getErrorMessage function. Anyway I agree, some more sofisticated memory management is required.
About multiple menus. In MENWIZ you can have multiple menu (hierarchies), each one with its own navigation device, lcd and so on.
You only need to create multiple istances of menwiz class.
The key points in MENWIZ are:
- MUNWIZ is (near) asinchronous. I need it with real time system were I cannot suspend the sketch execution waiting for user responses.
- MENWIZ allows to add (just one code line) splash screen and to set the elapsed time.
- MENWIZ allows to add (just one code line) a "default" user screen. The library show automatically the default screen after a user defined delay since the last button push. It is very usefull when you have sensors and you need continuous monitoring.
- MENWIZ allows user defined callback to be fired inside a menu (see the following example). I could even easily implement triggers to be fired when binded variables satisfy certain condictions, but I'm not sure such a feature is really usefull
How do you manage the above reqs ?
However there is not the killer solution for Menu management, and MENWIZ for sure is not (and never will be) the ultimate solution.
You have to balance the tradeoff between simple coding, power, flexibility, size, user model (sync/async) and device support.
It is not a simple task, and you know well... ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
Here following please find an example I wrote for an other thread (Help creating menu for golf robot - #25 by brunialti - Displays - Arduino Forum), were an action is required to be fired from menu. It can be done in very few lines ...
Thanks for your interesting remarks!
p.s. sorry for my english.
//MENWIZ GOLF ROBOT EXAMPLE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <buttons.h>
#include <MENWIZ.h>
// DEFINE ARDUINO PINS FOR THE NAVIGATION BUTTONS
#define UP_BOTTON_PIN 9
#define DOWN_BOTTON_PIN 10
#define LEFT_BOTTON_PIN 7
#define RIGHT_BOTTON_PIN 8
#define CONFIRM_BOTTON_PIN 12
#define ESCAPE_BOTTON_PIN 11
//Create global object LCD and MENU
menwiz menu;
LiquidCrystal_I2C lcd(0x27,20,4);
//instantiate global variables to bind to menu
byte m=0;
int fd=0,yd=0;
void setup(){
_menu *r,*s1;
Serial.begin(19200);
// inizialize the menu object (20 colums x 4 rows LCD)
menu.begin(&lcd,20,4);
//create the menu tree
r=menu.addMenu(MW_ROOT,NULL,"GOLF ROBOT"); //create a root menu at first (required)
s1=menu.addMenu(MW_VAR,r,"Modes"); //add a terminal node in the menu tree (that is "variable");
s1->addVar(MW_LIST,&m); //create the terminal node variable of type OPTION LIST and bind it to the app variable "m"
s1->addItem(MW_LIST,"Drive"); //add an option to the OPTION LIST
s1->addItem(MW_LIST,"Punch"); //add an other option to the OPTION LIST
s1->addItem(MW_LIST,"Chip"); //add the third option to the OPTION LIST
s1->addItem(MW_LIST,"Putt"); //add the last option to the OPTION LIST
s1=menu.addMenu(MW_VAR,r,"Putt Dist.(feets)"); //create an other "variable" menu terminal mode
s1->addVar(MW_AUTO_INT,&fd,0,100,1); //int type, fd binded variable, rage 0-100, step 1
s1=menu.addMenu(MW_VAR,r,"Other Dist. (yrds)");
s1->addVar(MW_AUTO_INT,&yd,0,300,5); //int type, yd binded variable, rage 0-300, step 5
s1=menu.addMenu(MW_VAR,r,"Fire action"); //latest menu entry
s1->addVar(MW_ACTION,fireAction); // associate an action (variable of type function) to the menu entry
//declare navigation buttons (required)
// equivalent shorter call: menu.navButtons(9,10,7,8,11,12);
menu.navButtons(UP_BOTTON_PIN,DOWN_BOTTON_PIN,LEFT_BOTTON_PIN,RIGHT_BOTTON_PIN,ESCAPE_BOTTON_PIN,CONFIRM_BOTTON_PIN);
}
void loop(){
// NAVIGATION MANAGEMENT & DRAWING ON LCD. NOT BLOCKING
menu.draw();
//PUT APPLICATION CODE HERE
// if any .... :-)
}
// user defined action for fire action
void fireAction(){
Serial.print("FIRED ");
switch (m){
case 0:
Serial.print("Drive to ");Serial.print(yd); Serial.println(" yrds");
break;
case 1:
Serial.print("Punch to ");Serial.print(yd); Serial.println(" yrds");
break;
case 2:
Serial.print("Chip to ");Serial.print(yd); Serial.println(" yrds");
break;
case 3:
Serial.print("Put to "); Serial.print(fd); Serial.println(" feets");
break;
default:
break;
}
}