Loading...
Searching...
No Matches
BindSwitch.hpp
1#ifndef __BINDSWITCH_HPP
2#define __BINDSWITCH_HPP
3#include "BindView.hpp"
4#include "BindUtils.hpp"
32 class BindSwitch : public BindView
33{
34 public:
40 BindSwitch(const char *cstr)
41 {
42 this->tag = tagIndex++;
43 setLabel(cstr);
44 this->changeCallback = nullptr;
45 }
46
50 BindSwitch()
51 {
52 this->tag = tagIndex++;
53 // add a default label for the switch + "SW-[tag]" so like "Switch_1"
54 char label[20];
55 sprintf(label, "SW-%d", this->tag);
56 setLabel(label);
57 this->changeCallback = nullptr;
58 }
59
60 int16_t x;
61 int16_t y;
62 uint8_t cmdId = 0;
63 bool value;
64 int16_t fontSize;
65 int32_t textColor;
66
72 void setLabel(const char *cstr)
73 {
74 str = cstr;
75 }
76
77 //backwards compatibility
78 void setlabel(const char *cstr)
79 {
80 setLabel(cstr);
81 }
82
94 void setCallback(void (*callback)(bool))
95 {
96 this->changeCallback = callback;
97 }
98
99 void invokeCallback(bool val)
100 {
101 this->value = val;
102 if (this->changeCallback != nullptr)
103 {
104 this->changeCallback(val);
105 }
106 }
107
117 uint16_t getBytes(uint8_t *out) override
118 {
119 offset = 0;
120 strLength = strlen(str);
121 if (strLength > MAX_STRING_LENGTH_TERMINAL)
122 {
123 strLength = MAX_STRING_LENGTH_TERMINAL;
124 }
125 copyAndOffset(out, &offset, &objID, sizeof(objID));
126 copyAndOffset(out, &offset, &x, 2);
127 copyAndOffset(out, &offset, &y, 2);
128 copyAndOffset(out, &offset, &tag, 2);
129 copyAndOffset(out, &offset, &cmdId, sizeof(cmdId));
130 copyAndOffset(out, &offset, &value, sizeof(value));
131 copyAndOffset(out, &offset, &fontSize, sizeof(fontSize));
132 copyAndOffset(out, &offset, &textColor, sizeof(textColor));
133 copyAndOffset(out, &offset, str, strLength);
134 return offset;
135 }
136
137 private:
138 uint8_t objID = BIND_ID_TOGGLE_SWITCH;
139 uint16_t offset = 0;
140 int strLength = 0;
141 const char *str;
142 void (*changeCallback)(bool) = nullptr;
143 static int16_t tagIndex;
144};
145
146#endif /* __BINDSWITCH_HPP */
BindSwitch
Represents a toggle switch UI element in the Bind framework.
Definition BindSwitch.hpp:33
BindSwitch::value
bool value
The current state (ON/OFF) of the toggle switch.
Definition BindSwitch.hpp:63
BindSwitch::y
int16_t y
Y-coordinate position of the toggle switch.
Definition BindSwitch.hpp:61
BindSwitch::BindSwitch
BindSwitch()
Default constructor to create a BindSwitch with a default label ("Switch").
Definition BindSwitch.hpp:50
BindSwitch::getBytes
uint16_t getBytes(uint8_t *out) override
Generates and returns the byte data representing the toggle switch configuration.
Definition BindSwitch.hpp:117
BindSwitch::cmdId
uint8_t cmdId
Command identifier. See the notes for possible cmdId values.
Definition BindSwitch.hpp:62
BindSwitch::setLabel
void setLabel(const char *cstr)
Sets the label text for the toggle switch.
Definition BindSwitch.hpp:72
BindSwitch::setCallback
void setCallback(void(*callback)(bool))
Set the Callback function for the toggle switch.
Definition BindSwitch.hpp:94
BindSwitch::textColor
int32_t textColor
Text color for the switch label.
Definition BindSwitch.hpp:65
BindSwitch::fontSize
int16_t fontSize
Font size for the switch label.
Definition BindSwitch.hpp:64
BindSwitch::x
int16_t x
X-coordinate position of the toggle switch.
Definition BindSwitch.hpp:60
BindSwitch::BindSwitch
BindSwitch(const char *cstr)
Constructor to create a BindSwitch with a custom label.
Definition BindSwitch.hpp:40
BindView
Definition BindView.hpp:22