Skip to content

Commit

Permalink
Merge pull request #17 from forntoh/fix-string-to-char-ptr
Browse files Browse the repository at this point in the history
Code optimization + Compiler warnings fix #6
  • Loading branch information
forntoh authored Mar 23, 2021
2 parents d2a5bc5 + c829f2f commit af8bb17
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pio
.vscode/
src/app.ino
debug.log
src/main.cpp
debug.log
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LcdMenu
version=1.1.0
version=1.1.1
author=Forntoh Thomas <[email protected]>
maintainer=Forntoh Thomas <[email protected]>
sentence=Display navigable menu items on your LCD display with Arduino.
Expand Down
17 changes: 17 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
chris--a/Keypad@^3.1.1
marcoschwartz/LiquidCrystal_I2C@^1.1.4
21 changes: 10 additions & 11 deletions src/LcdMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class LcdMenu {
/**
* Last visible item's position in the menu array
*/
uint8_t bottom;
uint8_t previousBottom;
uint8_t bottom = 0;
uint8_t previousBottom = 0;
/**
* Rows on the LCD Display
*/
Expand Down Expand Up @@ -142,14 +142,14 @@ class LcdMenu {
// append textOn or textOff depending on the state
//
lcd->print(":");
lcd->print(item->isOn ? item->textOn : item->textOff);
lcd->print(item->isOn ? item->getTextOn() : item->getTextOff());
break;
case MENU_ITEM_INPUT:
//
// append the value the value of the input
//
lcd->print(":");
lcd->print(item->value.substring(
lcd->print(item->getValue().substring(
0, maxCols - ((String)item->getText()).length() - 2));
break;
default:
Expand Down Expand Up @@ -238,8 +238,7 @@ class LcdMenu {
* @relatesalso MenuItem
*/
void placeCursorAtEnd(MenuItem* item) {
uint8_t col =
((String)item->getText()).length() + 2 + item->value.length();
uint8_t col = item->getText().length() + 2 + item->getValue().length();
lcd->setCursor(constrain(col, 0, maxCols - 1), cursorPosition - top);
}

Expand Down Expand Up @@ -487,7 +486,7 @@ class LcdMenu {
//
// set the value
//
item->value = text;
item->getValue() = text;
//
// repaint menu
//
Expand All @@ -510,12 +509,12 @@ class LcdMenu {
* @param message message to display
* @param duration how long to display the message
*/
void displayNotification(char* message, unsigned int duration) {
void displayNotification(String message, unsigned int duration) {
/**
* Calculate the position to start writing
* (centralize text)
*/
uint8_t centerPos = maxCols / 2 - (strlen(message) / 2);
uint8_t centerPos = maxCols / 2 - (message.length() / 2);
/**
* Set cursor potion and clear lane
*/
Expand All @@ -526,8 +525,8 @@ class LcdMenu {
* Draw each independent character
*/
lcd->write(0xA5);
for (unsigned int i = 0; i < strlen(message); i++) {
char character = message[i];
for (unsigned int i = 0; i < message.length(); i++) {
char character = message.charAt(i);
lcd->write(character);
}
lcd->write(0xA5);
Expand Down
54 changes: 32 additions & 22 deletions src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,56 @@ const byte MENU_ITEM_END_OF_MENU = 7;
//
class MenuItem {
private:
char* text = (char*)"";
String text;
String value;
String textOn;
String textOff;
fptr callback = NULL;
MenuItem* subMenu = NULL;
byte type = MENU_ITEM_NONE;

public:
//
//
//
boolean isOn = false;
//
// constructors
//
MenuItem() = default;
MenuItem(char* text) : text(text) {}
MenuItem(char* text, fptr callback, byte type)
MenuItem(String text) : text(text) {}
MenuItem(String text, fptr callback, byte type)
: text(text), callback(callback), type(type) {}
MenuItem(char* text, fptr callback, MenuItem* subMenu, byte type)
MenuItem(String text, fptr callback, MenuItem* subMenu, byte type)
: text(text), callback(callback), subMenu(subMenu), type(type) {}
MenuItem(MenuItem* subMenu, byte type) : subMenu(subMenu), type(type) {}
MenuItem(char* text, MenuItem* subMenu, byte type)
MenuItem(String text, MenuItem* subMenu, byte type)
: text(text), subMenu(subMenu), type(type) {}
MenuItem(char* text, char* value, fptr callback, byte type)
MenuItem(String text, String value, fptr callback, byte type)
: text(text), value(value), callback(callback), type(type) {}
MenuItem(String text, String textOn, String textOff, fptr callback,
byte type)
: text(text),
textOn(textOn),
textOff(textOff),
callback(callback),
type(type) {}
//
// getters
//
char* getText() { return text; }
String getText() { return text; }
fptr getCallback() { return callback; }
MenuItem* getSubMenu() { return subMenu; }
byte getType() { return type; }
String getValue() { return value; }
String getTextOn() { return textOn; }
String getTextOff() { return textOff; }
//
// setters
//
void setText(char* text) { this->text = text; }
void setText(String text) { this->text = text; }
void setCallBack(fptr callback) { this->callback = callback; }
void setSubMenu(MenuItem* subMenu) { this->subMenu = subMenu; }
//
//
//
boolean isOn = false;
char* textOn = (char*)"ON";
char* textOff = (char*)"OFF";
String value = "";
};

class ItemHeader : public MenuItem {
Expand All @@ -104,27 +114,27 @@ class ItemFooter : public MenuItem {

class ItemInput : public MenuItem {
public:
ItemInput(char* text, char* value, fptr callback)
ItemInput(String text, String value, fptr callback)
: MenuItem(text, value, callback, MENU_ITEM_INPUT) {}
};

class ItemSubMenu : public MenuItem {
public:
ItemSubMenu(char* text, MenuItem* parent)
ItemSubMenu(String text, MenuItem* parent)
: MenuItem(text, parent, MENU_ITEM_SUB_MENU) {}
};

class ItemToggle : public MenuItem {
public:
ItemToggle(char* key, fptr callback)
: MenuItem(key, callback, MENU_ITEM_TOGGLE) {}
ItemToggle(char* key, char* textOn, char* textOff, fptr callback)
: MenuItem(key, callback, MENU_ITEM_TOGGLE) {}
ItemToggle(String key, fptr callback)
: MenuItem(key, "", "", callback, MENU_ITEM_TOGGLE) {}
ItemToggle(String key, String textOn, String textOff, fptr callback)
: MenuItem(key, textOn, textOff, callback, MENU_ITEM_TOGGLE) {}
};

class ItemCommand : public MenuItem {
public:
ItemCommand(char* key, fptr callback)
ItemCommand(String key, fptr callback)
: MenuItem(key, callback, MENU_ITEM_COMMAND) {}
};

Expand Down

0 comments on commit af8bb17

Please sign in to comment.