Command & Toggle

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

Callbacks can be used with menu items of type ItemCommand, ItemInput, ItemList, ItemProgress or ItemToggle

Includes

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

Declare the callback function

// For ItemToggle
void myCallbackFunction(uint16_t isOn);
// For ItemCommand
void myCallbackFunction();

Add a callback to MenuItem

MAIN_MENU(
    ITEM_BASIC("Start service"),
    ITEM_BASIC("Connect to WiFi"),
    //
    // Include callback in ItemToggle
    //
    ITEM_TOGGLE("Backlight", myCallbackFunction),
    ITEM_BASIC("Blink SOS"),
    ITEM_BASIC("Blink random")
);

Define the callback function

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

/**
 * Define the callback function
 */
void myCallbackFunction(uint16_t isOn) {
    menu.setBacklight(isOn);
}

Constructor reference

ItemCommand(const char* key, fptr callback)
ItemToggle(const char* key, fptrInt callback)
ItemToggle(const char* key, const char* textOn, const char* textOff, fptrInt callback)
  • key: display text of the item

  • textOn: display text when ON

  • textOff: display text when OFF

  • callback: reference to a callback function

Full code 👉 .../examples/Callbacks/Callbacks.ino

Last updated