Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/forntoh/LcdMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
forntoh committed Aug 26, 2021
2 parents 4280701 + 12cbea3 commit 6a700fb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 28 deletions.
7 changes: 2 additions & 5 deletions examples/Callbacks/Callbacks.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/Callbacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 6 additions & 8 deletions examples/List/List.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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]);
}
2 changes: 1 addition & 1 deletion examples/List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ getItemAt KEYWORD2
toggleBacklight KEYWORD2
getText KEYWORD2
getCallback KEYWORD2
getCallbackInt KEYWORD2
getCallbackStr KEYWORD2
getSubMenu KEYWORD2
getType KEYWORD2
getTextOn KEYWORD2
Expand Down
9 changes: 6 additions & 3 deletions src/LcdMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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;
}
}
Expand Down
39 changes: 29 additions & 10 deletions src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <Arduino.h>

typedef void (*fptr)();
typedef void (*fptrInt)(uint8_t);
typedef void (*fptrStr)(String);
//
// menu item types
//
Expand All @@ -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;
Expand Down Expand Up @@ -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) {}
Expand All @@ -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
Expand Down Expand Up @@ -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) {}
};

/**
Expand Down Expand Up @@ -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) {
}
/**
Expand All @@ -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) {}
};

Expand Down Expand Up @@ -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) {}
};

Expand Down

0 comments on commit 6a700fb

Please sign in to comment.