-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathItemToggle.h
112 lines (98 loc) · 3.12 KB
/
ItemToggle.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef ItemToggle_H
#define ItemToggle_H
#include "LcdMenu.h"
#include "MenuItem.h"
#include <utils/utils.h>
/**
* @brief Item that allows user to toggle between ON/OFF states.
*
* ```
* ┌────────────────────────────┐
* │ > T E X T : O F F │
* └────────────────────────────┘
* ```
*
* Additionally to `text` this item has ON/OFF `enabled` state.
*/
class ItemToggle : public MenuItem {
private:
bool enabled = false;
const char* textOn = NULL;
const char* textOff = NULL;
fptrBool callback = NULL;
public:
/**
* @brief Create an ItemToggle object with default values as `ON` and `OFF`.
*
* @param key key of the item
* @param callback reference to callback function
*/
ItemToggle(const char* key, fptrBool callback)
: ItemToggle(key, false, callback) {}
/**
* @brief Construct a new Item Toggle object with an initial state
* and default ON/OFF texts.
*
* @param text
* @param enabled
* @param callback
*/
ItemToggle(const char* text, boolean enabled, fptrBool callback)
: ItemToggle(text, "ON", "OFF", callback) {
this->enabled = enabled;
}
/**
* @brief Construct a new Item Toggle object.
* @param text the text of the item
* @param textOn display text when ON
* @param textOff display text when OFF
* @param callback reference to callback function
*/
ItemToggle(const char* text, const char* textOn, const char* textOff, fptrBool callback)
: MenuItem(text),
textOn(textOn),
textOff(textOff),
callback(callback) {}
/**
* @brief Get the integer callback function of this item.
* @return the integer callback function
*/
fptrBool getCallbackInt() { return callback; }
/**
* @brief Get the current state of this toggle item.
* @return the current state
*/
boolean isOn() { return enabled; }
/**
* @brief Set the current state of this toggle item.
* @note You need to call `LcdMenu::refresh` after this method to see the changes.
* @param isOn the new state
*/
void setIsOn(boolean isOn) { this->enabled = isOn; }
const char* getTextOn() { return this->textOn; }
const char* getTextOff() { return this->textOff; }
void draw(MenuRenderer* renderer) override {
renderer->drawItem(text, enabled ? textOn : textOff);
};
protected:
bool process(LcdMenu* menu, const unsigned char command) override {
MenuRenderer* display = menu->getRenderer();
switch (command) {
case ENTER:
toggle(display);
return true;
default:
return false;
}
};
void toggle(MenuRenderer* renderer) {
enabled = !enabled;
LOG(F("ItemToggle::toggle"), enabled ? textOn : textOff);
draw(renderer);
if (callback != NULL) {
callback(enabled);
}
}
};
#define ITEM_TOGGLE(...) (new ItemToggle(__VA_ARGS__))
#endif