Hi, I've written code that uses MenuBackend http://forum.arduino.cc/index.php/topic,38053.0.html with a change as suggested by coagula Tutorial: manage menu and LCD display with Arduino – Coagula – Giuseppe Di Cillo. This change inserts the function toRoot in the MenuBackEnd class of the library.
void toRoot() {
setCurrent( &getRoot() );
}
This works as intended (when running the command in the loop()). I need to use it differently, I check for a condition when a menuChangeEvent occurs and if that condition is true, I want to reset the menu back to "MenuRoot". So, for this simple example, I test the condition if (changed.to == mAlarmHour) as you can see. The problem is that although the menu appears to go back to root (as seen by the subsequent menuChangeEvent and serial monitor readout) the pointer is in fact still pointed to the "hour" menu. Output from menu.moveDown() shows the pointer going from "hour" to "minute" not "MenuRoot" to "time" as would be expected. I've looked at the library and don't know enough about pointers to functions to know why setCurrent() isn't working as expected. Any help would be much appreciated. I will include Brevig's MenuBackend.h with the modification by coagula as an attachment.
#include <MenuBackend.h>
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
MenuItem mTime = MenuItem("time");
MenuItem mYear = MenuItem("year");
MenuItem mAlarm = MenuItem("alarm");
MenuItem mAlarmFreq = MenuItem("frequency");
MenuItem mAlarmHour = MenuItem("hour");
MenuItem mAlarmMinute = MenuItem("minute");
void menuSetup()
{
menu.getRoot().add(mTime);
mTime.addRight(mYear);
mTime.addAfter(mAlarm);
mAlarm.addAfter(mTime);
mAlarm.addRight(mAlarmFreq);
mAlarmFreq.addAfter(mAlarmHour);
mAlarmHour.addAfter(mAlarmMinute);
}
void menuUseEvent(MenuUseEvent used)
{
Serial.print("Menu use ");
Serial.println(used.item.getName());
menu.toRoot();
}
void menuChangeEvent(MenuChangeEvent changed)
{
Serial.print("Menu change ");
Serial.print(changed.from.getName());
Serial.print(" ");
Serial.println(changed.to.getName());
//***************************PROBLEM IS HERE**************************
if (changed.to == mAlarmHour)
{
menu.toRoot();
}
}
void setup()
{
Serial.begin(9600);
menuSetup();
Serial.println("Starting navigation:\r\nUp: w Down: s Left: a Right: d ToRoot: b Use: e");
}
void loop()
{
if (Serial.available()) {
byte read = Serial.read();
switch (read) {
case 'w': menu.moveUp(); break;
case 's': menu.moveDown(); break;
case 'd': menu.moveRight(); break;
case 'a': menu.moveLeft(); break;
case 'e': menu.use(); break;
case 'b': menu.toRoot(); break;
}
}
delay(10);
}
MenuBackend.h (6.5 KB)