diff --git a/examples/Callbacks/Callbacks.ino b/examples/Callbacks/Callbacks.ino index 87ed6c15..15ea2e18 100644 --- a/examples/Callbacks/Callbacks.ino +++ b/examples/Callbacks/Callbacks.ino @@ -33,7 +33,7 @@ #define CLEAR 46 // NUMPAD . // Declare the call back function -void toggleBacklight(); +void toggleBacklight(uint8_t isOn); extern MenuItem mainMenu[]; extern MenuItem settingsMenu[]; @@ -88,10 +88,7 @@ void loop() { /** * Define callback */ -void toggleBacklight() { - // This way (when you need the value for something else) - bool isOn = menu.getItemAt(menu.getCursorPosition())->isOn; - // +void toggleBacklight(uint8_t isOn) { menu.lcd->setBacklight(isOn); // Or this way menu.toggleBacklight(); diff --git a/examples/Callbacks/README.md b/examples/Callbacks/README.md index 1307ebc9..5c361978 100644 --- a/examples/Callbacks/README.md +++ b/examples/Callbacks/README.md @@ -30,7 +30,7 @@ Callbacks can be used with menu items of type `ItemCommand`, `ItemInput`, `ItemL When `enter()` is invoked, the command _(callback)_ bound to the item is invoked. ```cpp -// ../../examples/Callbacks/Callbacks.ino#L85-L95 +// ../../examples/Callbacks/Callbacks.ino#L88-L95 ``` Full code 👉 [.../examples/Callbacks/Callbacks.ino](https://github.com/forntoh/LcdMenu/tree/master/examples/Callbacks/Callbacks.ino) diff --git a/examples/List/List.ino b/examples/List/List.ino index 70008e78..c5056ccb 100644 --- a/examples/List/List.ino +++ b/examples/List/List.ino @@ -34,8 +34,8 @@ #define CLEAR 46 // NUMPAD . // Declare the calbacks -void colorsCallback(); -void numsCallback(); +void colorsCallback(uint8_t pos); +void numsCallback(uint8_t pos); // Declare the array extern String colors[]; @@ -94,14 +94,12 @@ void loop() { } // Define the calbacks -void colorsCallback() { - uint8_t activeListItem = menu[menu.getCursorPosition()]->itemIndex; +void colorsCallback(uint8_t pos) { // do something with the index - Serial.println(colors[activeListItem]); + Serial.println(colors[pos]); } -void numsCallback() { - uint8_t activeListItem = menu[menu.getCursorPosition()]->itemIndex; +void numsCallback(uint8_t pos) { // do something with the index - Serial.println(nums[activeListItem]); + Serial.println(nums[pos]); } diff --git a/examples/List/README.md b/examples/List/README.md index 15006016..68c51a77 100644 --- a/examples/List/README.md +++ b/examples/List/README.md @@ -45,7 +45,7 @@ Use `menu.left()` and/or `menu.right()` to cycle through the items When `enter()` is invoked, the command _(callback)_ bound to the item is invoked. ```cpp -// ../../examples/List/List.ino#L97-L101 +// ../../examples/List/List.ino#L97-L100 ``` Full example 👉 [.../examples/List/List.ino](https://github.com/forntoh/LcdMenu/tree/master/examples/List/List.ino) diff --git a/keywords.txt b/keywords.txt index 05e1c2fc..23aa09a3 100644 --- a/keywords.txt +++ b/keywords.txt @@ -48,6 +48,8 @@ getItemAt KEYWORD2 toggleBacklight KEYWORD2 getText KEYWORD2 getCallback KEYWORD2 +getCallbackInt KEYWORD2 +getCallbackStr KEYWORD2 getSubMenu KEYWORD2 getType KEYWORD2 getTextOn KEYWORD2 diff --git a/src/LcdMenu.h b/src/LcdMenu.h index ef7ebdc7..6760101d 100644 --- a/src/LcdMenu.h +++ b/src/LcdMenu.h @@ -446,7 +446,8 @@ class LcdMenu { // // execute the menu item's function // - if (item->getCallback() != NULL) (item->getCallback())(); + if (item->getCallbackInt() != NULL) + (item->getCallbackInt())(item->isOn); // // display the menu again // @@ -457,14 +458,16 @@ class LcdMenu { // // execute the menu item's function // - if (item->getCallback() != NULL) (item->getCallback())(); + if (item->getCallbackStr() != NULL) + (item->getCallbackStr())(item->value); break; } case MENU_ITEM_LIST: { // // execute the menu item's function // - if (item->getCallback() != NULL) (item->getCallback())(); + if (item->getCallbackInt() != NULL) + (item->getCallbackInt())(item->itemIndex); break; } } diff --git a/src/MenuItem.h b/src/MenuItem.h index 3ae23559..a85d3d09 100644 --- a/src/MenuItem.h +++ b/src/MenuItem.h @@ -30,6 +30,8 @@ #include typedef void (*fptr)(); +typedef void (*fptrInt)(uint8_t); +typedef void (*fptrStr)(String); // // menu item types // @@ -51,6 +53,8 @@ class MenuItem { char* textOn = NULL; char* textOff = NULL; fptr callback = NULL; + fptrInt callbackInt = NULL; + fptrStr callbackStr = NULL; MenuItem* subMenu = NULL; byte type = MENU_ITEM_NONE; String* items = NULL; @@ -86,18 +90,19 @@ class MenuItem { MenuItem(MenuItem* subMenu, byte type) : subMenu(subMenu), type(type) {} MenuItem(const char* text, MenuItem* subMenu, byte type) : text(text), subMenu(subMenu), type(type) {} - MenuItem(const char* text, String value, fptr callback, byte type) - : text(text), callback(callback), type(type), value(value) {} - MenuItem(const char* text, char* textOn, char* textOff, fptr callback, byte type) + MenuItem(const char* text, String value, fptrStr callback, byte type) + : text(text), callbackStr(callback), type(type), value(value) {} + MenuItem(const char* text, char* textOn, char* textOff, fptrInt callback, + byte type) : text(text), textOn(textOn), textOff(textOff), - callback(callback), + callbackInt(callback), type(type) {} - MenuItem(const char* text, String* items, uint8_t itemCount, fptr callback, + MenuItem(const char* text, String* items, uint8_t itemCount, fptrInt callback, byte type) : text(text), - callback(callback), + callbackInt(callback), type(type), items(items), itemCount(itemCount) {} @@ -115,6 +120,16 @@ class MenuItem { * @return `ftpr` - Item's callback */ fptr getCallback() { return callback; } + /** + * Get the callback of the item + * @return `fptrInt` - Item's callback + */ + fptrInt getCallbackInt() { return callbackInt; } + /** + * Get the callback of the item + * @return `fptrStr` - Item's callback + */ + fptrStr getCallbackStr() { return callbackStr; } /** * Get the sub menu at item * @return `MenuItem*` - Submenu at item @@ -245,8 +260,12 @@ class ItemInput : public MenuItem { * @param value the input value * @param callback reference to callback function */ - ItemInput(const char* text, String value, fptr callback) + ItemInput(const char* text, String value, fptrStr callback) : MenuItem(text, value, callback, MENU_ITEM_INPUT) {} + /** + */ + ItemInput(char* text, fptrStr callback) + : MenuItem(text, "", callback, MENU_ITEM_INPUT) {} }; /** @@ -282,7 +301,7 @@ class ItemToggle : public MenuItem { * @param key key of the item * @param callback reference to callback function */ - ItemToggle(const char* key, fptr callback) + ItemToggle(const char* key, fptrInt callback) : MenuItem(key, (char*)"ON", (char*)"OFF", callback, MENU_ITEM_TOGGLE) { } /** @@ -291,7 +310,7 @@ class ItemToggle : public MenuItem { * @param textOff display text when OFF * @param callback reference to callback function */ - ItemToggle(const char* key, char* textOn, char* textOff, fptr callback) + ItemToggle(const char* key, char* textOn, char* textOff, fptrInt callback) : MenuItem(key, textOn, textOff, callback, MENU_ITEM_TOGGLE) {} }; @@ -340,7 +359,7 @@ class ItemList : public MenuItem { * @param itemCount number of items in `items` * @param callback reference to callback function */ - ItemList(const char* key, String* items, uint8_t itemCount, fptr callback) + ItemList(const char* key, String* items, uint8_t itemCount, fptrInt callback) : MenuItem(key, items, itemCount, callback, MENU_ITEM_LIST) {} };