-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ItemValue class for dynamic value display
- Introduced a new template class to create menu items that display values. - Added polling functionality to update the menu at regular intervals. - Updated LcdMenu and MenuScreen classes to support polling. - Defined ITEM_VALUE macro for easy item creation with formatting options.
- Loading branch information
Showing
7 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include "BaseItemZeroWidget.h" | ||
|
||
/** | ||
* @class ItemValue | ||
* @brief A menu item that displays a value. | ||
* | ||
* This class extends the BaseItemZeroWidget class and provides a menu item | ||
* that displays a value. The value is provided as a reference during construction | ||
* and is displayed using the provided format string. | ||
*/ | ||
template <typename T> | ||
class ItemValue : public BaseItemZeroWidget { | ||
private: | ||
T& value; | ||
const char* format; | ||
|
||
public: | ||
ItemValue(const char* text, T& value, const char* format) : BaseItemZeroWidget(text), value(value), format(format) { | ||
this->polling = true; | ||
} | ||
|
||
protected: | ||
void handleCommit(LcdMenu* menu) override {} | ||
|
||
void draw(MenuRenderer* renderer) override { | ||
char buffer[ITEM_DRAW_BUFFER_SIZE]; | ||
snprintf(buffer, ITEM_DRAW_BUFFER_SIZE, format, value); | ||
renderer->drawItem(text, buffer); | ||
} | ||
}; | ||
|
||
/** | ||
* @brief Create a new item that displays a value. | ||
* @note If you want to display a value that changes over time, use this item and | ||
* call `LcdMenu::poll` in the loop function. | ||
* | ||
* @tparam T the type of the value to display | ||
* @param text the text to display for the item | ||
* @param value the value to display | ||
* @param format the format string to use when displaying the value | ||
* @return MenuItem* the created item | ||
* | ||
* @example | ||
* int temp = 25; | ||
* auto item = ITEM_VALUE("Temperature", temp, "%d°C"); | ||
*/ | ||
template <typename T> | ||
inline MenuItem* ITEM_VALUE( | ||
const char* text, | ||
T& value, | ||
const char* format = "%s") { | ||
return new ItemValue<T>(text, value, format); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,6 @@ | |
#endif | ||
#endif | ||
|
||
#define ITEM_DRAW_BUFFER_SIZE 25 | ||
|
||
class LcdMenu; | ||
|
||
/** | ||
|