String List

This is a basic example, it will show you how to use lists in the LcdMenu library.

Requested, inspired and based on previous work by @thijstriemstra 🙏

The supported datatype for the list is String. This can be used for other primitive datatypes, you just need to pass it as a string and then parse the result to the desired datatype.

Includes

// other includes ...
#include <ItemList.h>
#include <LcdMenu.h> // Always comes after every item type import

Declare the array

extern String colors[];

Initialize the array

String colors[] = {"Red",  "Green",  "Blue",   "Orange",
                   "Aqua", "Yellow", "Purple", "Pink"};

Add the array to your MenuItem

ITEM_STRING_LIST("Col", colors, 9, ),

You must add the size of the array in order for the menu to know when to stop or loop while cycling through the items in the list.

Constructor reference:

ItemList(const char key, String* items, const uint8_t itemCount, fptrInt callback)
  • key: The key of the menu item.

  • items: The array of items to display.

  • itemCount: The number of items in the array.

  • callback: A pointer to the callback function to execute when this menu item is selected.

Enter edit mode

menu.enter()

Cycle through the list

Use menu.left() and/or menu.right() to cycle through the items

  • When left() is invoked the view cycles down the list

  • When right() is invoked the view cycles up the list, you can use only right() if you have a single button, because once the menu reaches the end of the list, it automatically goes to the beginning.

5. Run when an item is selected

When back() is invoked, the command (callback) bound to the item is invoked.

void colorsCallback(uint16_t pos) {
    // do something with the index
    Serial.println(colors[pos]);
}

Full example 👉 .../examples/List/List.ino

Last updated