LCD menu
This page explains how to implement a menu system using the existing TextLCD library (found here: http://mbed.org/cookbook/Text-LCD) and RPG library (found here: http://mbed.org/users/canderson199/notebook/rotary-pulse-generator-library). This was motivated by a need for an easy to implement and modular menu system. It lessens the difficulties of hardcoding menus. The system keeps track of cursor positions, input logic, and displays. Inspired in part by the Menbed project by Kyle Gilpin. His project can be seen here: http://circuitcellar.com/contests/nxpmbeddesignchallenge/winners/HM3915.htm. Our system is a lot more lightweight, and slightly limited, but is easier to understand.
Below is an example.
Parts
- The RPG used for interfacing with menus can be found here: https://www.sparkfun.com/products/9117
- An 2x16 LCD with a Hitachi HD44780U driver
Summary
The image above details an example set up.
Each menu that appears on the lcd screen are Menu objects, and Menu contains Selections. You can create as many menus, sub menus, and selections as you want and each selection can be tied to different functions. The navigator is what allows you to scroll through these menus and select selections through the aid of the RPG. The navigator can read whether the user wants to move up (CCW) or down (CW) and adjust the selection on the screen accordingly.
The basic calls are:
- Menu menuName(menuID) This creates a menu object. Note, menuID is a character array (string):
- Selection selection(function, position, child, text) This creates a selectable item, but is not attached to any menu.
- Selections can be tied to functions, which is how this system incorporates program flow. Simply create a "function" for a set of code you want to execute, and tie it to a selection to have it execute when the RPG is depressed and the cursor is pointing that selection.
- menuName.add(selection) This adds a selection to a menu.
- It is easier to add and declare a selection at the same time: like so menuName.add(Selection(function, position, child, text))
- The position parameter will soon be deprecated, but it's a good idea to increment as you add selections
- It is easier to add and declare a selection at the same time: like so menuName.add(Selection(function, position, child, text))
- Navigator navigator(rootMenu, RPG, LCD) A menu configuration must always include a root menu.
(Refer to the source for more details. Some objects must be passed by reference, in particular the LCD)
Basic Steps
To create the system similar the example program
- Create a root menu Menu rootMenu("root menu");
- Create any submenus Menu ledMenu("LED menu");
- Add any selections ledMenu.add(Selection(&toggleLED1, 0, NULL, "Toggle LED1"));
- Remember to add a selection to go back up in the menu hierarchy. Do this by letting the child menu point to the previous menu *ledMenu.add(Selection(NULL, 1, &rootMenu, "Go back");
- Add submenus to the root (as selections with child parameters set to the submenus) rootMenu.add(Selection(NULL, 0, &ledMenu, "LED menu");
- Assuming RPG and LCD are set up as below, create the Navigator Navigator navigator(&rootMenu, rpg, &lcd);
- In a while loop (not recommended with a ticker, at least not yet), call navigator.poll(), which polls the system for user input
Sample program
Library
Import libraryMenu
Library of menu structure
Connections
Pin number | TextLCD pins | mbed pins |
---|---|---|
1 | GND | 0V |
2 | VCC | 3.3V |
3 | VO | 0V, via 1k resistor |
4 | RS | p15 |
5 | RW | 0V |
6 | E | p16 |
7 | D0 | not connected |
8 | D1 | not connected |
9 | D2 | not connected |
10 | D3 | not connected |
11 | D4 | p17 |
12 | D5 | p18 |
13 | D6 | p19 |
14 | D7 | p20 |
Pin number | RPG pins | mbed pins |
1 | A | p21 |
2 | B | p22 |
3 | C | GND |
4 | 1 | p23 |
5 | 2 | GND |
- When looking from the top, the RPG pin are:
[A] [C] [B]
RPG
[1] [2]
References
Tect LCD Cookbook: http://mbed.org/cookbook/Text-LCD
RPG Cookbook: http://mbed.org/users/canderson199/notebook/rotary-pulse-generator-library/
Author's notes
Comments welcome!
Possible changes in the future:
- Compatibility with other LCDs
- Allow selections to be associated with variables for watching or modifying (similar to Kyle Gilpin's project)
- More functionality such as methods for opening files
2 comments on LCD menu:
Please log in to post comments.
This reads a lot like a menu system I intend to develop, in my case I'll be using a VFD with a few "bells and whistles" that regular LCD modules don't support and I'll need to support numeric parameters.
I do wonder if its possible to declare some of the data as constants rather than having to build the data-structure at run-time?