Skip to content

Commit

Permalink
Improved Debug Logging
Browse files Browse the repository at this point in the history
- Replaced printCmd with printLog for better debug messages
- Added debug flag in platformio.ini for debugging purposes
  • Loading branch information
forntoh committed Sep 20, 2024
1 parent 1af22dc commit 438a9c1
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 55 deletions.
8 changes: 8 additions & 0 deletions .scripts/pio_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

from SCons.Script import Import

Import("env")

if os.getenv("LOCAL_BUILD"):
env.Append(CPPDEFINES=["DEBUG"])
6 changes: 3 additions & 3 deletions examples/RTOS/RTOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ bool relay1State, relay2State, relay3State;

// Relay 1 Toggle
void toggleRelay1(uint16_t isOn) {
printCmd(F("relay1State"));
printLog(F("relay1State"));
}

// Relay 2 Toggle
void toggleRelay2(uint16_t isOn) {
printCmd(F("relay2State"));
printLog(F("relay2State"));
}

// Relay 3 Toggle
void toggleRelay3(uint16_t isOn) {
printCmd(F("relay3State"));
printLog(F("relay3State"));
}

LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
platform = atmelavr
board = uno
framework = arduino
extra_scripts = pre:.scripts/pio_config.py
lib_deps =
marcoschwartz/LiquidCrystal_I2C@^1.1.4
arduino-libraries/LiquidCrystal @ ^1.0.7
Expand Down
1 change: 1 addition & 0 deletions src/ItemCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ItemCommand : public MenuItem {
if (callback != NULL) {
callback();
}
printLog(F("ItemCommand::enter"), text);
return true;
};
};
Expand Down
12 changes: 7 additions & 5 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class ItemInput : public MenuItem {
display->setEditModeEnabled(true);
display->resetBlinker(constrainBlinkerPosition(display, strlen(text) + 2 + cursor - view));
display->drawBlinker();
printLog(F("ItemInput::enterEditMode"), value);
return true;
};
bool back(Context& context) {
Expand All @@ -191,6 +192,7 @@ class ItemInput : public MenuItem {
if (callback != NULL) {
callback(value);
}
printLog(F("ItemInput::exitEditMode"), value);
return true;
};
bool left(Context& context) {
Expand All @@ -208,7 +210,7 @@ class ItemInput : public MenuItem {
}
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() - 1));
// Log
printCmd(F("LEFT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
printLog(F("ItemInput::left"), value);
return true;
};
bool right(Context& context) {
Expand All @@ -227,7 +229,7 @@ class ItemInput : public MenuItem {
}
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() + 1));
// Log
printCmd(F("RIGHT"), value[display->getBlinkerPosition() - (strlen(text) + 2)]);
printLog(F("ItemInput::right"), value);
return true;
};
/**
Expand All @@ -246,7 +248,7 @@ class ItemInput : public MenuItem {
return true;
}
remove(value, cursor - 1, 1);
printCmd(F("BACKSPACE"), value);
printLog(F("ItemInput::backspace"), value);
cursor--;
if (cursor < view) {
view--;
Expand Down Expand Up @@ -288,7 +290,7 @@ class ItemInput : public MenuItem {
MenuItem::draw(display);
display->resetBlinker(constrainBlinkerPosition(display, display->getBlinkerPosition() + 1));
// Log
printCmd(F("TYPE-CHAR"), character);
printLog(F("ItemInput::typeChar"), character);
return true;
}
/**
Expand All @@ -304,7 +306,7 @@ class ItemInput : public MenuItem {
//
value = (char*)"";
// Log
printCmd(F("CLEAR"), value);
printLog(F("ItemInput::clear"), value);
//
// update blinker position
//
Expand Down
3 changes: 2 additions & 1 deletion src/ItemInputCharset.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class ItemInputCharset : public ItemInput {
concat(value, charset[charsetPosition], buf);
value = buf;
}
printCmd(F("CHARSET"), value);
stopCharsetEditMode(display);
ItemInput::right(context);
return true;
Expand Down Expand Up @@ -129,6 +128,7 @@ class ItemInputCharset : public ItemInput {
}
charsetPosition = (charsetPosition + 1) % charsetSize;
display->drawChar(charset[charsetPosition]);
printLog(F("ItemInputCharset::down"), charset[charsetPosition]);
return true;
}

Expand All @@ -142,6 +142,7 @@ class ItemInputCharset : public ItemInput {
}
charsetPosition = constrain(charsetPosition - 1, 0, charsetSize);
display->drawChar(charset[charsetPosition]);
printLog(F("ItemInputCharset::up"), charset[charsetPosition]);
return true;
}
};
Expand Down
35 changes: 23 additions & 12 deletions src/ItemList.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
*
* # ItemList
*
* This item type indicates that the current item is a **list**.
* This item type represents a **list**.
*
* ### Functionality
* - `left()`: cycles down the list
* - `right()`: cycles up the list. This function can be used alone with a
* single button. Once the menu reaches the end of the list, it automatically
* starts back from the beginning.
* - `enter()`: invokes the command *(callback)* bound to this item.
* - `down()`: cycles down the list.
* - `up()`: cycles up the list. This function can be used with a single button.
* Once the menu reaches the end of the list, it automatically
* starts back from the beginning.
* - `enter()`: invokes the command (callback) bound to this item.
*
* ### Usage
* This item can be used for other primitive data types; you just need to pass
* it as a string, then parse the result to the desired datatype.
* them as strings, then parse the result to the desired datatype.
*
* ### Parameters
* @param key: key of the items
* @param items: array of items to display
* @param itemCount: number of items in `items`
* @param key: key of the item.
* @param items: array of items to display.
* @param itemCount: number of items in `items`.
* @param callback: reference to a callback function that will be invoked
* when the user selects an item from the list.
*/
Expand Down Expand Up @@ -85,6 +85,15 @@ class ItemList : public MenuItem {
*/
String* getItems() { return items; }

/**
* @brief Returns the value of the currently selected item.
*
* @return The value of the currently selected item.
*/
char* getValue() {
return (char*)items[itemIndex].c_str();
}

protected:
void draw(DisplayInterface* display, uint8_t row) override {
uint8_t maxCols = display->getMaxCols();
Expand All @@ -110,6 +119,7 @@ class ItemList : public MenuItem {
return false;
}
display->setEditModeEnabled(true);
printLog(F("ItemList::enterEditMode"), getValue());
return true;
};

Expand All @@ -122,6 +132,7 @@ class ItemList : public MenuItem {
if (callback != NULL) {
callback(itemIndex);
}
printLog(F("ItemList::exitEditMode"), getValue());
return true;
};

Expand All @@ -133,9 +144,9 @@ class ItemList : public MenuItem {
uint8_t previousIndex = itemIndex;
itemIndex = constrain(itemIndex - 1, 0, (uint16_t)(itemCount)-1);
if (previousIndex != itemIndex) {
printCmd(F("LEFT"), items[itemIndex].c_str());
MenuItem::draw(display);
}
printLog(F("ItemList::down"), getValue());
return true;
};

Expand All @@ -147,9 +158,9 @@ class ItemList : public MenuItem {
uint8_t previousIndex = itemIndex;
itemIndex = constrain((itemIndex + 1) % itemCount, 0, (uint16_t)(itemCount)-1);
if (previousIndex != itemIndex) {
printCmd(F("RIGHT"), items[itemIndex].c_str());
MenuItem::draw(display);
}
printLog(F("ItemList::up"), getValue());
return true;
};
};
Expand Down
6 changes: 4 additions & 2 deletions src/ItemProgress.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ItemProgress : public MenuItem {
return;
}
progress += stepLength;
printLog(F("ItemProgress::increment"), getValue());
}

/**
Expand All @@ -64,6 +65,7 @@ class ItemProgress : public MenuItem {
return;
}
progress -= stepLength;
printLog(F("ItemProgress::decrement"), getValue());
}

/**
Expand Down Expand Up @@ -127,6 +129,7 @@ class ItemProgress : public MenuItem {
return false;
}
display->setEditModeEnabled(true);
printLog(F("ItemProgress::enterEditMode"), getValue());
return true;
};

Expand All @@ -139,6 +142,7 @@ class ItemProgress : public MenuItem {
if (callback != NULL) {
callback(progress);
}
printLog(F("ItemProgress::exitEditMode"), getValue());
return true;
};

Expand All @@ -150,7 +154,6 @@ class ItemProgress : public MenuItem {
uint16_t oldProgress = progress;
decrement();
if (progress != oldProgress) {
printCmd(F("LEFT"), getValue());
MenuItem::draw(display);
}
return true;
Expand All @@ -164,7 +167,6 @@ class ItemProgress : public MenuItem {
uint16_t oldProgress = progress;
increment();
if (progress != oldProgress) {
printCmd(F("RIGHT"), getValue());
MenuItem::draw(display);
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/ItemSubMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ItemSubMenu : public MenuItem {
* Open next screen.
*/
bool enter(Context& context) {
printCmd(F("Opening screen..."));
printLog(F("ItemSubMenu::enter"), text);
screen->setParent(context.menu->getScreen());
context.menu->setScreen(screen);
return true;
Expand Down
1 change: 1 addition & 0 deletions src/ItemToggle.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ItemToggle : public MenuItem {
enabled = !enabled;
if (callback != NULL) {
callback(enabled);
printLog(F("ItemToggle::toggle"), enabled ? textOn : textOff);
}
MenuItem::draw(context.display);
return true;
Expand Down
8 changes: 8 additions & 0 deletions src/LcdMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class LcdMenu {
* @param screen the new screen to display
*/
void setScreen(MenuScreen* screen);
/**
* @brief Process the input character.
* @param c the input character
* @return `true` if the input was processed successfully
*/
bool process(const unsigned char c);
/**
* @brief Reset current screen to initial state.
Expand Down Expand Up @@ -107,5 +112,8 @@ class LcdMenu {
* @return `MenuItem` item at `position`
*/
MenuItem* getItemAt(uint8_t position);
/**
* @brief Refresh the current screen.
*/
void refresh();
};
1 change: 1 addition & 0 deletions src/MenuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ bool MenuScreen::back(MenuItem::Context context) {
if (parent != NULL) {
context.menu->setScreen(parent);
}
printLog(F("MenuScreen::back"));
return true;
}
8 changes: 7 additions & 1 deletion src/MenuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class MenuScreen {
*/
bool up(MenuItem::Context context) {
if (cursor == 0) {
printLog(F("MenuScreen:up"), cursor);
return false;
}
DisplayInterface* display = context.display;
Expand All @@ -172,6 +173,7 @@ class MenuScreen {
} else {
display->moveCursor(cursor - view);
}
printLog(F("MenuScreen:up"), cursor);
return true;
}
/**
Expand All @@ -180,6 +182,7 @@ class MenuScreen {
*/
bool down(MenuItem::Context context) {
if (cursor == itemsCount() - 1) {
printLog(F("MenuScreen:down"), cursor);
return false;
}
DisplayInterface* display = context.display;
Expand All @@ -190,6 +193,7 @@ class MenuScreen {
} else {
display->moveCursor(cursor - view);
}
printLog(F("MenuScreen:down"), cursor);
return true;
}
/**
Expand All @@ -198,7 +202,9 @@ class MenuScreen {
* Navigates up once.
*/
bool back(MenuItem::Context context);

/**
* @brief Reset the screen to initial state.
*/
void reset(DisplayInterface* display) {
cursor = 0;
view = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/display/LiquidCrystalAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ class LiquidCrystalAdapter : public DisplayInterface {
lcd->setCursor(blinkerPosition, cursorRow);
lcd->print(c);
lcd->setCursor(blinkerPosition, cursorRow); // Move back
// Log
printCmd(F("DRAW-CHAR"), c);
return true;
}

void updateTimer() {
if (millis() != startTime + DISPLAY_TIMEOUT) {
return;
}
printCmd(F("TIMEOUT"));
printLog(F("TIMEOUT"));
lcd->noDisplay();
}

Expand Down
4 changes: 1 addition & 3 deletions src/display/LiquidCrystal_I2CAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ class LiquidCrystal_I2CAdapter : public DisplayInterface {
lcd->setCursor(blinkerPosition, cursorRow);
lcd->print(c);
lcd->setCursor(blinkerPosition, cursorRow); // Move back
// Log
printCmd(F("DRAW-CHAR"), c);
return true;
}

void updateTimer() {
if (millis() != startTime + DISPLAY_TIMEOUT) {
return;
}
printCmd(F("TIMEOUT"));
printLog(F("LiquidCrystal_I2CAdapter::timeout"));
lcd->noDisplay();
lcd->noBacklight();
}
Expand Down
Loading

0 comments on commit 438a9c1

Please sign in to comment.