Attaching Callbacks to an Item in an Array

Forgive me, I've been at this for hours with basically nothing to show for it. And I've exhausted all materials both here and elsewhere. So I'm at my last resort, which is asking for help. As will most likely be evident, I'm creating a really basic, rudimentary menu system for a very basic device. And C++, though my first language, pales in comparison to my skills with other languages. So I could use a bit of help.

I've been researching other Arduino menu libraries, most notably LcdMenu from which I am noting its structuring. Basically, a user defines and creates a menu, but can also attach a callback to that item as follows:

#include <LcdMenu.h>

#define LCD_ROWS 2
#define LCD_COLS 16

// Declare the call back function
void toggleBacklight(uint8_t isOn);

extern MenuItem mainMenu[];
extern MenuItem settingsMenu[];

MenuItem mainMenu[] = {ItemHeader(),
                       MenuItem("Start service"),
                       MenuItem("Connect to WiFi"),
                       ItemSubMenu("Settings", settingsMenu),
                       MenuItem("Blink SOS"),
                       MenuItem("Blink random"),
                       ItemFooter()};
/**
 * Create submenu and precise its parent
 */
MenuItem settingsMenu[] = {ItemHeader(mainMenu),
                           //
                           // Include callback in ItemToggle
                           //
                           ItemToggle("Backlight", toggleBacklight),
                           MenuItem("Contrast"), ItemFooter()};

LcdMenu menu(LCD_ROWS, LCD_COLS);

/**
 * Define callback
 */
void toggleBacklight(uint8_t isOn) {
    menu.lcd->setBacklight(isOn);
    // Or this way
    menu.toggleBacklight();
}

I did alter the original code slightly to better illuminate my desired goal, but nothing removed was essential to the code's function. But I have attached a hyperlink to the complete original code here.

This code is merely an example, so I'm not concerned with every piece of it. I'm mostly concerned with the way they have set up the mainMenu[ ] array so that it can accept a label and an attached callback. That's what I'm interested in, so that I can have an array of labels with a function name attached to them should I please. That way calling a function attached to a specific menu item is easier and more direct.

I'm sure this is very simple to do. I've just had trouble finding an example adequate of providing me with enough adaptable information to complete my task. I also have other ways I'm sure I could use to accomplish this, but they're... bulky. So I'm hoping to find a sleeker way to do this.

The only useful thing I've come across that may work in this situation is a multidimensional array. I'm just not sure how to set it up so that column 0 holds the labels, and column 1 holds the function names. If that's even a solution.

Thank you guys very much for the help. I'm more of a hardware engineer than a software engineer. I merely know enough C++ to be dangerous. :wink:

I'm sorry. I don't quite understand what you want to do with your "array of labels with a function name attached to them". Did you want a menu item for each label without your having to write a "MenuItem(label, callback)," for each one?

Hello

I can't see any difference between your modified code and the original code :roll_eyes:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.