Standard text LCD menu system
The pattern I use for menu systems in C is something like this:
struct menuitem
{
const char *name; // name to be rendered
functionPointer handlerFunc; // handler for this leaf node (optionally NULL)
struct menu *child; // pointer to child submenu (optionally NULL)
};
struct menu
{
struct menu *parent; // pointer to parent menu
struct **menuitem; // array of menu items, NULL terminated
};
I then declare an array of menu
s each containing menuitem
s and pointers to child
submenus. Up and down moves through the currently selected array of menuitem
s. Back moves to the parent
menu and forward/select either moves to a child
submenu or calls a handlerFunc
for a leaf node.
Rendering a menu just involves iterating through its items.
The advantage of this scheme is that it's fully data driven, the menu structures can be statically declared in ROM independent of the renderer and handler functions.
Toby's answer is a very good starting point. The structures mentioned assume that the menus are static and you just navigate through them.
If you want dymanic menus (e.g. displaying certain values, such as temperature, time, etc), then you need to be able to generate that.
One way could be to have register a function to build your string.
struct menuitem
{
const char *name; // name to be rendered
const char * (*builderFunc)( const char *name ); // callback to generate string, if not null.
functionPointer handlerFunc; // handler for this leaf node (optionally NULL)
struct menu *child; // pointer to child submenu (optionally NULL)
};