Arduino menu system library

2 minute read Published: 2015-08-16

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.

If you're interested in learning more about design patterns, I highly recommend Design Patterns: Elements of Reusable Object-Oriented Software by Eric Gamma, Richard Helm, Ralph Johnson, and John Vlissides.

Menu

A Menu represents an item in the menu that contains other Menu's and MenuItem's. Use add_menu_item() and add_menu() to build its contents.

MenuItem

A MenuItem represents an action. When select() is called the MenuItem's callback function is called.

MenuSystem

The MenuSystem contains the core functions for interacting with the menu system: next(), prev(), select(), and back().

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.


    #include <MenuSystem.h>

    MenuSystem ms;
    Menu mm("");
    MenuItem mi_time("TIME");
    MenuItem mi_date("DATE");
    MenuItem mi_alarm("ALARM");
    Menu mu_disp("DISP");
    MenuItem mi_disp_brightness("BRTNS");

In your Setup() function, put the menu system together.


    void setup()
    {
      // Menus
      mm.add_item(&mi_time, &on_menu_set_time);
      mm.add_item(&mi_date, &on_menu_set_date);
      mm.add_item(&mi_alarm, &on_menu_set_alarm);
      mm.add_menu(&mu_disp);
      mu_disp.add_item(&mi_disp_brightness, &on_menu_set_brightness);
      ms.set_root_menu(&mm);
    }

Define your callback functions (only one is shown here).


    void on_menu_set_alarm(MenuItem* pMenuItem)
    {
      // Set the alarm
    }

And finally, interact with your menu system using the functions listed below. This will usually happen when responding to user input.


    ms.select(); // select the current menuitem/menu
    ms.back(); // go back to the menu one level up
    ms.next(); // go to the next item in the current menu
    ms.prev(); // go to the previous item in the current menu
    ms.get_current_menu_name(); // get the current menu name
    ms.get_num_menu_items(); // get the number of menu items in the current menu
    ms.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, including one using a Sure Electronics LED Matrix, as shown in the cover image.