Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

Is there a pattern out there for a simple menu system in C for a text LCD. I find myself re-writing code a lot for handling simple text LCD menus.

I find most systems have a main menu and some sub-menus that when selected allow you to set a parameter with within some minimum and maximum value.

Ideally this menu system could be navigated with 4 simple keys such as enter, cancel, up, and down.

In my application I'm using a 2 line x 16 character text LCD though an ideal solution should be able to be applied to any NxM display.

share|improve this question
Nothing to do with electronic design! Question will be closed. – Leon Heller Oct 13 '11 at 20:01
5  
I was under the impression programming questions if they applied to embedded systems could be asked here too. Maybe I was wrong... – mjh2007 Oct 13 '11 at 20:11
I was under similar impression too. Is there an official note about that? – AndrejaKo Oct 13 '11 at 20:30
Only very low-level stuff, according to the FAQ. This question is about the user interface. – Leon Heller Oct 13 '11 at 20:39
3  
+1 I would be interested too...I've always hand-brewed them. Are you using a console output or a NxM character display....not that I have an answer for either :) On several embedded 2xN display systems, I've implemented two button interfaces with combining Enter/Cancel and have Next which is a ring buffer and eliminates the need for up/next & down/back. – kenny Oct 13 '11 at 21:17
show 3 more comments

1 Answer

up vote 2 down vote accepted

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 menus each containing menuitems and pointers to child submenus. Up and down moves through the currently selected array of menuitems. 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.