-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAI-Thinker_RGBW_Bulb.cpp
320 lines (269 loc) · 8.96 KB
/
AI-Thinker_RGBW_Bulb.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "AI-Thinker_RGBW_Bulb.h"
volatile uint8_t effect = EFFECT_NOT_DEFINED;
///////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR, INIT() AND LOOP()
///////////////////////////////////////////////////////////////////////////
AIRGBWBulb::AIRGBWBulb(void) {
// creates a new instance of the my9291 driver
m_my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND_DEFAULT);
#if defined(GAMMA_CORRECTION)
m_isGammaCorrectionEnabled = true;
#endif
}
void AIRGBWBulb::init(void) {
// sets initial color to white (255, 255, 255)
//setColor(255, 255, 255);
// sets initial brightness to 100% (255)
//setBrightness(255);
// sets initial color temperature to 500 (Home Assistant max value)
//setColorTemperature(COLOR_TEMP_HA_MIN_IN_MIRED); // COLOR_TEMP_HA_MIN_IN_MIRED: white point
// sets initial white to 100% (255)
setWhite(255);
// sets initial state to false
m_my9291->setState(true);
}
void AIRGBWBulb::loop(void) {
// TODO: handles effects
static unsigned long last = millis();
switch (effect) {
case EFFECT_NOT_DEFINED:
break;
case EFFECT_RAMBOW:
static unsigned char count = 0;
if (millis() - last > EFFECT_RAINBOW_DELAY) {
last = millis();
rainbowEffect(count++);
}
break;
case EFFECT_BLINK:
if (millis() - last > EFFECT_BLINK_DELAY) {
last = millis();
setState(!getState());
}
break;
}
}
///////////////////////////////////////////////////////////////////////////
// STATE
///////////////////////////////////////////////////////////////////////////
bool AIRGBWBulb::getState(void) {
return m_my9291->getState();
}
bool AIRGBWBulb::setState(bool p_state) {
// checks if the given state is different from the actual state
if (p_state != m_my9291->getState())
m_my9291->setState(p_state);
else
return false;
return m_my9291->getState() == p_state;
}
///////////////////////////////////////////////////////////////////////////
// BRIGHTNESS
///////////////////////////////////////////////////////////////////////////
uint8_t AIRGBWBulb::getBrightness(void) {
return m_brightness;
}
bool AIRGBWBulb::setBrightness(uint8_t p_brightness) {
// checks if the value is smaller, bigger or equal to the actual brightness value
if (p_brightness < 0 || p_brightness > 255 || p_brightness == m_brightness)
return false;
// saves the new brightness value
m_brightness = p_brightness;
if (m_color.white != 0)
//m_color.white = p_brightness;
return setWhite(p_brightness);
return setColor();
}
///////////////////////////////////////////////////////////////////////////
// RGB COLOR
///////////////////////////////////////////////////////////////////////////
Color AIRGBWBulb::getColor(void) {
return m_color;
}
bool AIRGBWBulb::setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue) {
if ((p_red < 0 || p_red > 255) || (p_green < 0 || p_green > 255) || (p_blue < 0 || p_blue > 255))
return false;
// saves the new values
m_color.red = p_red;
m_color.green = p_green;
m_color.blue = p_blue;
// switches off the white leds
m_color.white = 0;
return setColor();
}
bool AIRGBWBulb::setColor() {
// maps the RGB values with the actual brightness
uint8_t red = (m_isGammaCorrectionEnabled) ? pgm_read_byte(&gamma8[m_color.red]) : m_color.red;
uint8_t green = (m_isGammaCorrectionEnabled) ? pgm_read_byte(&gamma8[m_color.green]) : m_color.green;
uint8_t blue = (m_isGammaCorrectionEnabled) ? pgm_read_byte(&gamma8[m_color.blue]) : m_color.blue;
red = map(red, 0, 255, 0, m_brightness);
green = map(green, 0, 255, 0, m_brightness);
blue = map(blue, 0, 255, 0, m_brightness);
// sets the new color
m_my9291->setColor((my9291_color_t) {
red,
green,
blue,
0
});
// checks if the values have been successfully updated
my9291_color_t my9291Color = m_my9291->getColor();
if (my9291Color.red != red || my9291Color.green != green || my9291Color.blue != blue)
return false;
return true;
}
///////////////////////////////////////////////////////////////////////////
// WHITE COLOR
///////////////////////////////////////////////////////////////////////////
bool AIRGBWBulb::setWhite(uint8_t p_white) {
// checks if the value is smaller, bigger or equal to the actual white value
if (p_white < 0 || p_white > 255 || p_white == m_color.white)
return false;
// saves the new white value
m_color.white = p_white;
m_brightness = p_white;
// switch off the RGB leds
m_color.red = 0;
m_color.green = 0;
m_color.blue = 0;
// adjusts the white value
m_my9291->setColor((my9291_color_t) {
0,
0,
0,
m_color.white
});
// checks if the value has been successfully updated
my9291_color_t my9291Color = m_my9291->getColor();
if (my9291Color.white != m_color.white)
return false;
return true;
}
///////////////////////////////////////////////////////////////////////////
// GETTER/SETTER COLOR TEMPERATURE
///////////////////////////////////////////////////////////////////////////
uint16_t AIRGBWBulb::getColorTemperature(void) {
return m_colorTemperature;
}
bool AIRGBWBulb::setColorTemperature(uint16_t p_colorTemperature) {
// checks if the value is equal to the actual color temperature
if (p_colorTemperature < COLOR_TEMP_HA_MIN_IN_MIRED || p_colorTemperature == m_colorTemperature || p_colorTemperature > COLOR_TEMP_HA_MAX_IN_MIRED)
return false;
// switches off the white leds
m_color.white = 0;
// saves the new colour temperature
m_colorTemperature = p_colorTemperature;
// https://fr.wikipedia.org/wiki/Mired
// http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
// M = 1000000 / T <> T [kelvin] = 1000000 / M [mired]
// int tmpKelvin = 1000000 / m_colorTemperature;
//
// if (tmpKelvin < 1000) {
// tmpKelvin = 1000;
// } else if (tmpKelvin > 40000) {
// tmpKelvin = 40000;
// }
int tmpKelvin = map(p_colorTemperature, COLOR_TEMP_HA_MIN_IN_MIRED, COLOR_TEMP_HA_MAX_IN_MIRED, COLOR_TEMP_MAX_IN_KELVIN, COLOR_TEMP_MIN_IN_KELVIN);
tmpKelvin = tmpKelvin / 100;
// computes red
if (tmpKelvin <= 66) {
m_color.red = 255;
} else {
float red = tmpKelvin - 60;
red = 329.698727446 * pow(red, -0.1332047592);
if (red < 0) {
m_color.red = 0;
} else if (red > 255) {
m_color.red = 255;
} else {
m_color.red = red;
}
}
// computes green
if (tmpKelvin <= 66) {
float green = tmpKelvin;
green = 99.4708025861 * log(green) - 161.1195681661;
if (green < 0) {
m_color.green = 0;
} else if (green > 255) {
m_color.green = 255;
} else {
m_color.green = green;
}
} else {
float green = tmpKelvin - 60;
green = 288.1221695283 * pow(green, -0.0755148492);
if (green < 0) {
m_color.green = 0;
} else if (green > 255) {
m_color.green = 255;
} else {
m_color.green = green;
}
}
// computes blue
if (tmpKelvin <= 66) {
m_color.blue = 255;
} else {
if (tmpKelvin <= 19) {
m_color.blue = 0;
} else {
float blue = tmpKelvin - 10;
blue = 138.5177312231 * log(blue) - 305.0447927307;
if (blue < 0) {
m_color.blue = 0;
} else if (blue > 255) {
m_color.blue = 255;
} else {
m_color.blue = blue;
}
}
}
return setColor();
}
///////////////////////////////////////////////////////////////////////////
// EFFECTS
///////////////////////////////////////////////////////////////////////////
bool AIRGBWBulb::setEffect(const char* p_effect) {
if (strcmp(p_effect, EFFECT_NOT_DEFINED_NAME) == 0) {
effect = EFFECT_NOT_DEFINED;
return true;
} else if (strcmp(p_effect, EFFECT_RAINBOW_NAME) == 0) {
effect = EFFECT_RAMBOW;
return true;
} else if (strcmp(p_effect, EFFECT_BLINK_NAME) == 0) {
effect = EFFECT_BLINK;
return true;
}
return false;
}
// https://github.com/xoseperez/my9291/blob/master/examples/esp8285/esp8285_rainbow.cpp
void AIRGBWBulb::rainbowEffect(uint8_t p_index) {
if (p_index < 85) {
setColor(p_index * 3, 255 - p_index * 3, 0);
} else if (p_index < 170) {
p_index -= 85;
setColor(255 - p_index * 3, 0, p_index * 3);
} else {
p_index -= 170;
setColor(0, p_index * 3, 255 - p_index * 3);
}
}
///////////////////////////////////////////////////////////////////////////
// MQTT DISCOVERY
///////////////////////////////////////////////////////////////////////////
bool AIRGBWBulb::isDiscovered(void) {
return m_isDiscovered;
}
void AIRGBWBulb::isDiscovered(bool p_isDiscovered) {
m_isDiscovered = p_isDiscovered;
}
///////////////////////////////////////////////////////////////////////////
// GAMMA CORRECTION
///////////////////////////////////////////////////////////////////////////
bool AIRGBWBulb::isGammaCorrectionEnabled(void) {
return m_isGammaCorrectionEnabled;
}
void AIRGBWBulb::isGammaCorrectionEnabled(bool p_isGammaCorrectionEnabled) {
m_isGammaCorrectionEnabled = p_isGammaCorrectionEnabled;
}