Thanks
What you are saying confirms that I have the basic understanding right.
My confusion seem to stem from me not having a clear enough grasp of the difference between functions, variables and data types.
Here are some code examples along with my assumptions. The examples occur in exact order top to bottom.
I don't understand exactly what the second line does.
#include <MenuBackend.h>
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
I'm guessing this is where variables containing menu items are defined. Is this a function, if so how come there are no curly brackets? Also, how do I know which parts of this expression are variables I can alter and which are not?
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
MenuItem miView1 = MenuItem("Main View");
MenuItem miView2 = MenuItem("Secondary View");
MenuItem miSetup = MenuItem("SETUP");
I understand that this function sets up the relationships between the menu elements, and that it is run only once.
void menuSetup(){
Serial.println("Setting up menu...");
//add the Main View to the menu root
//when add is used, as opposed to addX as you see below, the item is added below the item it's added to
menu.getRoot().add(miView1); miView1.addRight(miSetup); miSetup.addRight(miView2);
miView1.addLeft(miView2); miView2.addLeft(miSetup);
}//END of menu function
This is the bit I'm having trouble understanding. This occurs before the loop.
void menuUseEvent(MenuUseEvent used){
Serial.print("Menu use: ");
Serial.println(used.item.getName());
if (used.item == "SETUP") //comparison using a string literal
{
Serial.println("menuUseEvent found: SETUP");
}
if (used.item == "SETUP") //String comparison to set MenuUse behaviour
{
Serial.println("Menu DOWN & RIGHT");
rootMenu.moveDown();
menu.moveRight();
}
//Copy the above and alter to expand menu
}//END of menu use function
This is the function which prints changes to serial. This too occurs before loop.
void menuChangeEvent(MenuChangeEvent changed)
{
//Serial.print("Menu change: ");
//Serial.print(changed.from.getName());
Serial.print("-->");
Serial.println(changed.to.getName());
}
This is the setup()
void setup()
{
Serial.begin(9600);
menuSetup();
Serial.println("Starting navigation (see source for description):")
}
Finally the loop. This is it, no references to any of the previous functions as far as I can tell.
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;
}
}