??? 07/02/10 15:37 Read: times |
#177066 - State machine can be small. Responding to: ???'s previous message |
I often have a little state machine for multi-level menues.
Instead I use some form of pointer mechanism (could be real pointers or indices) to keep track of where I am. Each menu has a "back" entry. If NULL, then I'm on the root menu level. Each menu item has a flag telling if it is a terminal menu choice or starts a new menu. If it starts a new menu, then the entry gives a reference to the new menu. If it is a command, then it may store a function pointer (for some processors) or a command identifier. For an 8051, I would try to keep down the number of real menues and instead do all references as integers. And I would use command identifiers and have a large switch statement converting from a command identifier to calling the actual function. Having a switch statement with 170 case statements - each having a single function call - may not be elegant, but this makes sure that the Keil compiler/linker will see the full span of the call tree. The state machine need not be so large since it basically only need to care about four buttons - back (escape) forward (enter) up (prev) down (next). Note that the full menu tree can be designed and stored as a one-dimensional array of menu choices, by having each menu choice point to either: from parent menu choice: identifier of top entry of the sub-menu. from leaf menu choice: command identifier selecting what cmd function to call. The back pointers can be dynamically stored in a small array (a menu shouldn't be more than 4-5 levels deep anyway) so whenever the user makes a select of a non-leaf menu choice, the current "back" pointer (array index of top entry of (sub)menu is pushed, and then the top index of the current menu is remembered as back index. Then the forward index of the selected entry is remembered as new first entry of current menu. |