Bind
C++ UI toolkit for Arduino
 
Loading...
Searching...
No Matches
BindSwitch.hpp
1#ifndef __BINDSWITCH_HPP
2#define __BINDSWITCH_HPP
3#include "BindView.hpp"
4#include "BindUtils.hpp"
32class BindSwitch : public BindView
33{
34public:
40 BindSwitch(const char *cstr)
41 {
42 this->tag = tagIndex++;
43 setLabel(cstr);
44 this->changeCallback = nullptr;
45 }
46
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
137private:
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 */
Represents a toggle switch UI element in the Bind framework.
Definition BindSwitch.hpp:33
bool value
The current state (ON/OFF) of the toggle switch.
Definition BindSwitch.hpp:63
int16_t y
Y-coordinate position of the toggle switch.
Definition BindSwitch.hpp:61
BindSwitch()
Default constructor to create a BindSwitch with a default label ("Switch").
Definition BindSwitch.hpp:50
uint16_t getBytes(uint8_t *out) override
Generates and returns the byte data representing the toggle switch configuration.
Definition BindSwitch.hpp:117
uint8_t cmdId
Command identifier. See the notes for possible cmdId values.
Definition BindSwitch.hpp:62
void setLabel(const char *cstr)
Sets the label text for the toggle switch.
Definition BindSwitch.hpp:72
void setCallback(void(*callback)(bool))
Set the Callback function for the toggle switch.
Definition BindSwitch.hpp:94
int32_t textColor
Text color for the switch label.
Definition BindSwitch.hpp:65
int16_t fontSize
Font size for the switch label.
Definition BindSwitch.hpp:64
int16_t x
X-coordinate position of the toggle switch.
Definition BindSwitch.hpp:60
BindSwitch(const char *cstr)
Constructor to create a BindSwitch with a custom label.
Definition BindSwitch.hpp:40
Definition BindView.hpp:22