-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathItemValue.h
61 lines (54 loc) · 1.65 KB
/
ItemValue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#ifndef ARDUINO_ARCH_ESP32
#ifndef ARDUINO_ARCH_ESP8266
#include "utils/printf.h"
#endif
#endif
#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);
}