Hi,
After a bit of researching and much experimenting still haven't been able to figure out how to "return" to each Callout Function that involves nested functions... multilevel menu system. Here's a picture and some code to help explain. Hope it makes sense
Observation:
No issues 'return'ing to the first Callout to the MainMenu, except when calling two or more functions... then it seems like the 'return' just goes to the most recent callout.
Is there a coding technique for managing through these functions?
Or am I approaching this all wrong?
Thanks,
void loop() {
// do things
MainMenu();
// do things
}
void MainMenu() {
do
{// Serial Terminal input choices 1,2,3
switch (input){
case 1:
SubMenu_1();
break;
case 2:
SubMenu_2();
break;
case 3:
return; //RETURN ONLY to original calling function
}
} while (input != 3);
return; //RETURN ONLY to original calling function
}
void SubMenu_1() {
// Terminal Serial input choices: 1,2,3)
switch (inputchoice){
case 1:
SubMenu_1a();
break;
case 2:
SubMenu_1b();
break;
case 3: // EXIT to MainMenu
//use "break;","return;" or "MainMenu();" ??
}
//use "return;", "MainMenu();" or NOTHING ??
}
void SubMenu_1a() {
// do things then
// Terminal Serial input choices: 1,2,3)
switch (inputchoice){
case 1:
//do things
break;
case 2:
//do things
break;
case 3: // EXIT to MainMenu
//use "break;","return;" or "MainMenu();" ??
}
//use "return;", "MainMenu();" or NOTHING ??
}
void SubMenu_2() {
//conitnues similar to Submen_1...
}
Tried that but didn't work. Return will kill the entire nested function process and go back to the void loop()... from what I have read and tested.
The Subx and Proc functions all need to go back directly to MainMenu. If I use 'return' in each of those, then it kicks out of the entire nested function... going directly back to void loop().
Want to have ONLY the MainMenu() control returning to the Functionx() that called it. While also being able to having all the Subx/Proc functions going only back to the MainMenu().
That is not good because every call to MainMenu that never returns with a 'return' is adding data to the stack. This will eventually fill all available memory and cause a crash. Each function, when it is done, should return to the caller.
I'm just starting to understand that issue as well, but thought the 'return'... when eventually used in MainMenu() would CLEAR the Stack issues? or is that not correct?
I guess I could rollup all of the Menu stuff into a single VERY long Function to call? That would eliminate all of the jumping around between functions. Was trying to break it out for easier coding/understanding.
i implemented a menu system where the code returns to the same menu item in the next higher level, from which that sub-menu was called using an array that keeps track of the menu item at each level and an idx that track the level
I would suggest reading up on finite state machines (FSM). Any other approach will end up getting unbearably complex for any but very trivial menu systems. A well-designed FSM can expand to an almost unlimited extent while remaining understandable and easily maintainable. I've done menu systems with dozens of menus, and hundreds of functions, even systems wth multiple cooperating FSMs for handling user input, motion control, messaging and other functions. Even years later, they are still easy to expand and maintain.