Link to home
Start Free TrialLog in
Avatar of SINEtist
SINEtist

asked on

creating a simple gui in C for a microcontroller-based project

Hello Experts,

First, some background...
I am working on C project that will run on a micro controller.  There are 4 buttons (+, -, enter, and cancel)  
and a 2x16 character LCD that will be used for the user interface.  I've got a handle using the LCD and input  
buttons so far.  Although this project is micro controller-based, my questions are not specific to the micro.  


where I'm stuck...
I am looking for help (example code would be great as I'm pretty new to this) on creating the best data  
structure for the menu and mechanism for navigating through it.  I want to maintain the ability to add and delete menus in my code (not at runtime) and still be able to use the code that navigates through the menus without doing a lot of recoding.  I did some experimentation, and was able to do this without a hitch with a simple array, but I'm thinking I will need to use a more flexible data type, so I can keep menu text and the  
associated parameters (which may vary in type) for the menu text together.


I came up with the following nested structure design.  I don't even know if I'm on the right track or if the  
code below is possible, but I thought it might be helpful to see what I've been working with.


struct menu
{
   struct mainMenu
   {
       char mainMenuText[15];  //this is just some kind of title screen
   } MAINMENU;
   
   struct menu_1
   {
       char menu_1_item_1_Text[15];  // for each setting in the gui, there is display text
       int  menu_1_item_1_Value;     //  ...and a corresponding value.
       char menu_1_item_2_Text[15];
       int  menu_1_item_2_Value;
       char menu_1_item_3_Text[15];
       int  menu_1_item_3_Value;

   } MENU_1;

   struct menu_2
   {
       char menu_2_item_1_Text[15];
       int  menu_2_item_1_Value;
       char menu_2_item_2_Text[15];
       int  menu_2_item_2_Value;
   } MENU_2;
} MAINMENU;
   


I intend for it to end up looking something like as follows:

Main Menu
Menu 1
 Menu 1 item 1 = (some number)
 Menu 1 item 2 = (some number)
 Menu 1 item 3 = (some number)
Menu 2
 Menu 2 item 1 = (some number)
 Menu 2 item 2 = (some number)


I want to be able to "scroll" through the choices with my + and - buttons, so if I hit the "-" button when  
the main menu loads, I would be at the last top level menu choice (Menu 2, in this case).  If I hit the "+"  
button, when the Main Menu loads, I would be on Menu 1.  The cancel button will go back a level and the enter  
button will confirm a choice.


Thanks in advance!

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Array won't be the best option for nested menus, better you use some kind of linked list structure, have you heard about it?
Your structure for any kind of menu would be:

struct menu
   {
       char menu_1_item_1_Text[15];  // for each setting in the gui, there is display text
       int  menu_1_item_1_Value;     //  ...and a corresponding value.
       struct rmenu *next_sibling;  // poinjter to next menu option at same level
       struct rmenu *first_child;     // pointer to next menu option at deeper level
   } MENU;

Sorry, variable names are unnecessarily complex:

struct menu
   {
       char text[15];  // for each setting in the gui, there is display text
       int  value;     //  ...and a corresponding value.
       struct rmenu *next_sibling;  // poinjter to next menu option at same level
       struct rmenu *first_child;     // pointer to next menu option at deeper level
   } MENU;

if you are using pure C you will need malloc function to dinamically create menu options at runtime, if you are using C++ you can work easier with the new operator, please specify.

Avatar of SINEtist
SINEtist

ASKER

Hi there,

Wow that was fast!

I am using C, as the micro does not support C++.  I was hoping to avoid malloc and free, because...well, frankly I guess I'm just  lazy and that's another new concept to learn.  ; )

I was considering linked lists, but I was under the impression that since each menu would have a different number of options, that a linked list was not appropriate.     Perhaps I'm just wrong about that.

Could you expand on how using the + and  - buttons will cycle through the dynamic data structure example you provided (with a few different menus to get me started).  Once I have that, I think I will be good to go... Go read up on malloc and free, that is.

Thank you!





ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial