Introducing the arduino-menusystem library that makes it easy to incorporate a menu system into an Arduino project.
Design
The library is implemented according to the composite design pattern. When using this library
you need to create Menu
’s, MenuItem
’s, and a single MenuSystem
.
- Menu
- A
Menu
represents an item in the menu that contains otherMenu
’s andMenuItem
’s. Useadd_menu_item()
andadd_menu()
to build its contents. - MenuItem
- A
MenuItem
represents an action. Whenselect()
is called theMenuItem
’s callback function is called. - MenuSystem
- The
MenuSystem
contains the core functions for interacting with the menu system:next()
,prev()
,select()
, andback()
.
Tutorial
First create the various Menu
’s, MenuItem
’s, and MenuSystem
at the top of your source file.
You will also need to create a root Menu
to hold the top- level menu items. The root item is never
displayed.
1#include <MenuSystem.h>
2
3MenuSystem ms;
4Menu mm("");
5MenuItem mi_time("TIME");
6MenuItem mi_date("DATE");
7MenuItem mi_alarm("ALARM");
8Menu mu_disp("DISP");
9MenuItem mi_disp_brightness("BRTNS");
In your Setup()
function, put the menu system together.
1void setup()
2{
3 // Menus
4 mm.add_item(&mi_time, &on_menu_set_time);
5 mm.add_item(&mi_date, &on_menu_set_date);
6 mm.add_item(&mi_alarm, &on_menu_set_alarm);
7 mm.add_menu(&mu_disp);
8 mu_disp.add_item(&mi_disp_brightness, &on_menu_set_brightness);
9 ms.set_root_menu(&mm);
10}
Define your callback functions (only one is shown here).
1void on_menu_set_alarm(MenuItem* pMenuItem)
2{
3 // Set the alarm
4}
And finally, interact with your menu system using the functions listed below. This will usually happen when responding to user input.
1ms.select(); // select the current menuitem/menu
2ms.back(); // go back to the menu one level up
3ms.next(); // go to the next item in the current menu
4ms.prev(); // go to the previous item in the current menu
5ms.get_current_menu_name(); // get the current menu name
6ms.get_num_menu_items(); // get the number of menu items in the current menu
7ms.get_cur_menu_item(); // get the current menu item number in the current menu
Examples
There are several examples in the examples folder in the repository.