From 79962742c6f025a2b173b560958de6f5ecfb4d8f Mon Sep 17 00:00:00 2001 From: Forntoh Thomas Date: Tue, 24 Aug 2021 01:18:12 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=E2=98=BA=20value=20moved=20to=20execution?= =?UTF-8?q?=20site=20of=20callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keywords.txt | 2 ++ src/LcdMenu.h | 11 +++++++---- src/MenuItem.h | 35 +++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 14 deletions(-) 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 0ada5e10..ff341d1f 100644 --- a/src/LcdMenu.h +++ b/src/LcdMenu.h @@ -180,7 +180,7 @@ class LcdMenu { break; case MENU_ITEM_LIST: // - // append the value of the item at current list position + // append the value of the item at current list position // lcd->print(":"); lcd->print(item->getItems()[item->itemIndex].substring( @@ -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 0a74a184..07b0cce9 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(char* text, MenuItem* subMenu, byte type) : text(text), subMenu(subMenu), type(type) {} - MenuItem(char* text, String value, fptr callback, byte type) - : text(text), callback(callback), type(type), value(value) {} - MenuItem(char* text, char* textOn, char* textOff, fptr callback, byte type) + MenuItem(char* text, String value, fptrStr callback, byte type) + : text(text), callbackStr(callback), type(type), value(value) {} + MenuItem(char* text, char* textOn, char* textOff, fptrInt callback, + byte type) : text(text), textOn(textOn), textOff(textOff), - callback(callback), + callbackInt(callback), type(type) {} - MenuItem(char* text, String* items, uint8_t itemCount, fptr callback, + MenuItem(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,7 +260,7 @@ class ItemInput : public MenuItem { * @param value the input value * @param callback reference to callback function */ - ItemInput(char* text, String value, fptr callback) + ItemInput(char* text, String value, fptrStr callback) : MenuItem(text, value, callback, MENU_ITEM_INPUT) {} }; @@ -282,7 +297,7 @@ class ItemToggle : public MenuItem { * @param key key of the item * @param callback reference to callback function */ - ItemToggle(char* key, fptr callback) + ItemToggle(char* key, fptrInt callback) : MenuItem(key, (char*)"ON", (char*)"OFF", callback, MENU_ITEM_TOGGLE) { } /** @@ -291,7 +306,7 @@ class ItemToggle : public MenuItem { * @param textOff display text when OFF * @param callback reference to callback function */ - ItemToggle(char* key, char* textOn, char* textOff, fptr callback) + ItemToggle(char* key, char* textOn, char* textOff, fptrInt callback) : MenuItem(key, textOn, textOff, callback, MENU_ITEM_TOGGLE) {} }; @@ -340,7 +355,7 @@ class ItemList : public MenuItem { * @param itemCount number of items in `items` * @param callback reference to callback function */ - ItemList(char* key, String* items, uint8_t itemCount, fptr callback) + ItemList(char* key, String* items, uint8_t itemCount, fptrInt callback) : MenuItem(key, items, itemCount, callback, MENU_ITEM_LIST) {} }; From ae2852b7b8340ba9897da32075910e7975c5e89e Mon Sep 17 00:00:00 2001 From: Forntoh Thomas Date: Tue, 24 Aug 2021 01:25:32 +0200 Subject: [PATCH 2/4] renamed callbacks --- src/MenuItem.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MenuItem.h b/src/MenuItem.h index 07b0cce9..48ba8fa9 100644 --- a/src/MenuItem.h +++ b/src/MenuItem.h @@ -262,6 +262,10 @@ class ItemInput : public MenuItem { */ ItemInput(char* text, String value, fptrStr callback) : MenuItem(text, value, callback, MENU_ITEM_INPUT) {} + /** + */ + ItemInput(char* text, fptrStr callback) + : MenuItem(text, "", callback, MENU_ITEM_INPUT) {} }; /** From 2af50ad5879a1ff420c5c49eb72abd591a0fb2d4 Mon Sep 17 00:00:00 2001 From: Forntoh Thomas Date: Thu, 26 Aug 2021 22:31:53 +0200 Subject: [PATCH 3/4] fixed callback example --- examples/Callbacks/Callbacks.ino | 7 ++----- examples/Callbacks/README.md | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) 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) From 32c4b3590ba8c4e6381216556bbd7cc60f07bf1a Mon Sep 17 00:00:00 2001 From: Forntoh Thomas Date: Thu, 26 Aug 2021 22:33:56 +0200 Subject: [PATCH 4/4] fixed list example --- examples/List/List.ino | 14 ++++++-------- examples/List/README.md | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) 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)