jon black

"I am in a charming state of confusion"

Using the visitor design pattern in arduino-menusystem

Rendering a menu in arduino-menusystem version 2.1.1 isn't dictated by the API. For example, in the current_menu.ino example the menu is rendered in loop():

void loop()
{
  Serial.println("");
  
  // Display the menu
  Menu const* cp_menu = ms.get_current_menu();
  MenuComponent const* cp_menu_sel = cp_menu->get_selected();
  for (int i = 0; i < cp_menu->get_num_menu_components(); ++i)
  {
    MenuComponent const* cp_m_comp = cp_menu->get_menu_component(i);
    Serial.print(cp_m_comp->get_name());
    
    if (cp_menu_sel == cp_m_comp)
      Serial.print("<<< ");
    
    Serial.println("");
  }
  
  // Code ommitted
}

There are two problems with this approach:

  1. Complex decision blocks would be needed to render each MenuComponent type differently, resulting in code that's difficult to read and maintain;
  2. It's not obvious how the menu system should be rendered; the API provides

Arduino menu system library

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 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