forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxplg_ws2812.ino
400 lines (357 loc) · 12.6 KB
/
xplg_ws2812.ino
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
xplg_ws2812.ino - ws2812 led string support for Sonoff-Tasmota
Copyright (C) 2018 Heiko Krupp and Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_WS2812
/*********************************************************************************************\
* WS2812 RGB / RGBW Leds using NeopixelBus library
\*********************************************************************************************/
#include <NeoPixelBus.h>
#ifdef USE_WS2812_DMA
#if (USE_WS2812_CTYPE == 1)
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> *strip = NULL;
#elif (USE_WS2812_CTYPE == 2)
NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> *strip = NULL;
#elif (USE_WS2812_CTYPE == 3)
NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod> *strip = NULL;
#else // USE_WS2812_CTYPE
NeoPixelBus<NeoRgbFeature, Neo800KbpsMethod> *strip = NULL;
#endif // USE_WS2812_CTYPE
#else // USE_WS2812_DMA
#if (USE_WS2812_CTYPE == 1)
NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> *strip = NULL;
#elif (USE_WS2812_CTYPE == 2)
NeoPixelBus<NeoRgbwFeature, NeoEsp8266BitBang800KbpsMethod> *strip = NULL;
#elif (USE_WS2812_CTYPE == 3)
NeoPixelBus<NeoGrbwFeature, NeoEsp8266BitBang800KbpsMethod> *strip = NULL;
#else // USE_WS2812_CTYPE
NeoPixelBus<NeoRgbFeature, NeoEsp8266BitBang800KbpsMethod> *strip = NULL;
#endif // USE_WS2812_CTYPE
#endif // USE_WS2812_DMA
struct WsColor {
uint8_t red, green, blue;
};
struct ColorScheme {
WsColor* colors;
uint8_t count;
};
WsColor kIncandescent[2] = { 255,140,20, 0,0,0 };
WsColor kRgb[3] = { 255,0,0, 0,255,0, 0,0,255 };
WsColor kChristmas[2] = { 255,0,0, 0,255,0 };
WsColor kHanukkah[2] = { 0,0,255, 255,255,255 };
WsColor kwanzaa[3] = { 255,0,0, 0,0,0, 0,255,0 };
WsColor kRainbow[7] = { 255,0,0, 255,128,0, 255,255,0, 0,255,0, 0,0,255, 128,0,255, 255,0,255 };
WsColor kFire[3] = { 255,0,0, 255,102,0, 255,192,0 };
ColorScheme kSchemes[WS2812_SCHEMES] = {
kIncandescent, 2,
kRgb, 3,
kChristmas, 2,
kHanukkah, 2,
kwanzaa, 3,
kRainbow, 7,
kFire, 3 };
uint8_t kWidth[5] = {
1, // Small
2, // Medium
4, // Large
8, // Largest
255 }; // All
uint8_t kRepeat[5] = {
8, // Small
6, // Medium
4, // Large
2, // Largest
1 }; // All
uint8_t ws_show_next = 1;
/********************************************************************************************/
void Ws2812StripShow()
{
#if (USE_WS2812_CTYPE > 1)
RgbwColor c;
#else
RgbColor c;
#endif
if (Settings.light_correction) {
for (uint16_t i = 0; i < Settings.light_pixels; i++) {
c = strip->GetPixelColor(i);
c.R = ledTable[c.R];
c.G = ledTable[c.G];
c.B = ledTable[c.B];
#if (USE_WS2812_CTYPE > 1)
c.W = ledTable[c.W];
#endif
strip->SetPixelColor(i, c);
}
}
strip->Show();
}
int mod(int a, int b)
{
int ret = a % b;
if (ret < 0) {
ret += b;
}
return ret;
}
#define cmin(a,b) ((a)<(b)?(a):(b))
void Ws2812UpdatePixelColor(int position, struct WsColor hand_color, float offset)
{
#if (USE_WS2812_CTYPE > 1)
RgbwColor color;
#else
RgbColor color;
#endif
uint16_t mod_position = mod(position, (int)Settings.light_pixels);
color = strip->GetPixelColor(mod_position);
float dimmer = 100 / (float)Settings.light_dimmer;
color.R = cmin(color.R + ((hand_color.red / dimmer) * offset), 255);
color.G = cmin(color.G + ((hand_color.green / dimmer) * offset), 255);
color.B = cmin(color.B + ((hand_color.blue / dimmer) * offset), 255);
strip->SetPixelColor(mod_position, color);
}
void Ws2812UpdateHand(int position, uint8_t index)
{
if (Settings.flag.ws_clock_reverse) {
position = Settings.light_pixels -position;
}
WsColor hand_color = { Settings.ws_color[index][WS_RED], Settings.ws_color[index][WS_GREEN], Settings.ws_color[index][WS_BLUE] };
Ws2812UpdatePixelColor(position, hand_color, 1);
uint8_t range = ((Settings.ws_width[index] -1) / 2) +1;
for (uint8_t h = 1; h < range; h++) {
float offset = (float)(range - h) / (float)range;
Ws2812UpdatePixelColor(position -h, hand_color, offset);
Ws2812UpdatePixelColor(position +h, hand_color, offset);
}
}
void Ws2812Clock()
{
strip->ClearTo(0); // Reset strip
int clksize = 60000 / (int)Settings.light_pixels;
Ws2812UpdateHand((RtcTime.second * 1000) / clksize, WS_SECOND);
Ws2812UpdateHand((RtcTime.minute * 1000) / clksize, WS_MINUTE);
Ws2812UpdateHand(((RtcTime.hour % 12) * (5000 / clksize)) + ((RtcTime.minute * 1000) / (12 * clksize)), WS_HOUR);
Ws2812StripShow();
}
void Ws2812GradientColor(uint8_t schemenr, struct WsColor* mColor, uint16_t range, uint16_t gradRange, uint16_t i)
{
/*
* Compute the color of a pixel at position i using a gradient of the color scheme.
* This function is used internally by the gradient function.
*/
ColorScheme scheme = kSchemes[schemenr];
uint16_t curRange = i / range;
uint16_t rangeIndex = i % range;
uint16_t colorIndex = rangeIndex / gradRange;
uint16_t start = colorIndex;
uint16_t end = colorIndex +1;
if (curRange % 2 != 0) {
start = (scheme.count -1) - start;
end = (scheme.count -1) - end;
}
float dimmer = 100 / (float)Settings.light_dimmer;
float fmyRed = (float)map(rangeIndex % gradRange, 0, gradRange, scheme.colors[start].red, scheme.colors[end].red) / dimmer;
float fmyGrn = (float)map(rangeIndex % gradRange, 0, gradRange, scheme.colors[start].green, scheme.colors[end].green) / dimmer;
float fmyBlu = (float)map(rangeIndex % gradRange, 0, gradRange, scheme.colors[start].blue, scheme.colors[end].blue) / dimmer;
mColor->red = (uint8_t)fmyRed;
mColor->green = (uint8_t)fmyGrn;
mColor->blue = (uint8_t)fmyBlu;
}
void Ws2812Gradient(uint8_t schemenr)
{
/*
* This routine courtesy Tony DiCola (Adafruit)
* Display a gradient of colors for the current color scheme.
* Repeat is the number of repetitions of the gradient (pick a multiple of 2 for smooth looping of the gradient).
*/
#if (USE_WS2812_CTYPE > 1)
RgbwColor c;
c.W = 0;
#else
RgbColor c;
#endif
ColorScheme scheme = kSchemes[schemenr];
if (scheme.count < 2) {
return;
}
uint8_t repeat = kRepeat[Settings.light_width]; // number of scheme.count per ledcount
uint16_t range = (uint16_t)ceil((float)Settings.light_pixels / (float)repeat);
uint16_t gradRange = (uint16_t)ceil((float)range / (float)(scheme.count - 1));
uint16_t speed = ((Settings.light_speed * 2) -1) * (STATES / 10);
uint16_t offset = speed > 0 ? strip_timer_counter / speed : 0;
WsColor oldColor, currentColor;
Ws2812GradientColor(schemenr, &oldColor, range, gradRange, offset);
currentColor = oldColor;
for (uint16_t i = 0; i < Settings.light_pixels; i++) {
if (kRepeat[Settings.light_width] > 1) {
Ws2812GradientColor(schemenr, ¤tColor, range, gradRange, i +offset);
}
if (Settings.light_speed > 0) {
// Blend old and current color based on time for smooth movement.
c.R = map(strip_timer_counter % speed, 0, speed, oldColor.red, currentColor.red);
c.G = map(strip_timer_counter % speed, 0, speed, oldColor.green, currentColor.green);
c.B = map(strip_timer_counter % speed, 0, speed, oldColor.blue, currentColor.blue);
}
else {
// No animation, just use the current color.
c.R = currentColor.red;
c.G = currentColor.green;
c.B = currentColor.blue;
}
strip->SetPixelColor(i, c);
oldColor = currentColor;
}
Ws2812StripShow();
}
void Ws2812Bars(uint8_t schemenr)
{
/*
* This routine courtesy Tony DiCola (Adafruit)
* Display solid bars of color for the current color scheme.
* Width is the width of each bar in pixels/lights.
*/
#if (USE_WS2812_CTYPE > 1)
RgbwColor c;
c.W = 0;
#else
RgbColor c;
#endif
uint16_t i;
ColorScheme scheme = kSchemes[schemenr];
uint16_t maxSize = Settings.light_pixels / scheme.count;
if (kWidth[Settings.light_width] > maxSize) {
maxSize = 0;
}
uint16_t speed = ((Settings.light_speed * 2) -1) * (STATES / 10);
uint8_t offset = speed > 0 ? strip_timer_counter / speed : 0;
WsColor mcolor[scheme.count];
memcpy(mcolor, scheme.colors, sizeof(mcolor));
float dimmer = 100 / (float)Settings.light_dimmer;
for (i = 0; i < scheme.count; i++) {
float fmyRed = (float)mcolor[i].red / dimmer;
float fmyGrn = (float)mcolor[i].green / dimmer;
float fmyBlu = (float)mcolor[i].blue / dimmer;
mcolor[i].red = (uint8_t)fmyRed;
mcolor[i].green = (uint8_t)fmyGrn;
mcolor[i].blue = (uint8_t)fmyBlu;
}
uint8_t colorIndex = offset % scheme.count;
for (i = 0; i < Settings.light_pixels; i++) {
if (maxSize) {
colorIndex = ((i + offset) % (scheme.count * kWidth[Settings.light_width])) / kWidth[Settings.light_width];
}
c.R = mcolor[colorIndex].red;
c.G = mcolor[colorIndex].green;
c.B = mcolor[colorIndex].blue;
strip->SetPixelColor(i, c);
}
Ws2812StripShow();
}
/*********************************************************************************************\
* Public
\*********************************************************************************************/
void Ws2812Init()
{
#ifdef USE_WS2812_DMA
#if (USE_WS2812_CTYPE == 1)
strip = new NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod>(WS2812_MAX_LEDS); // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
#elif (USE_WS2812_CTYPE == 2)
strip = new NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod>(WS2812_MAX_LEDS); // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
#elif (USE_WS2812_CTYPE == 3)
strip = new NeoPixelBus<NeoGrbwFeature, Neo800KbpsMethod>(WS2812_MAX_LEDS); // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
#else // USE_WS2812_CTYPE
strip = new NeoPixelBus<NeoRgbFeature, Neo800KbpsMethod>(WS2812_MAX_LEDS); // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
#endif // USE_WS2812_CTYPE
#else // USE_WS2812_DMA
#if (USE_WS2812_CTYPE == 1)
strip = new NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod>(WS2812_MAX_LEDS, pin[GPIO_WS2812]);
#elif (USE_WS2812_CTYPE == 2)
strip = new NeoPixelBus<NeoRgbwFeature, NeoEsp8266BitBang800KbpsMethod>(WS2812_MAX_LEDS, pin[GPIO_WS2812]);
#elif (USE_WS2812_CTYPE == 3)
strip = new NeoPixelBus<NeoGrbwFeature, NeoEsp8266BitBang800KbpsMethod>(WS2812_MAX_LEDS, pin[GPIO_WS2812]);
#else // USE_WS2812_CTYPE
strip = new NeoPixelBus<NeoRgbFeature, NeoEsp8266BitBang800KbpsMethod>(WS2812_MAX_LEDS, pin[GPIO_WS2812]);
#endif // USE_WS2812_CTYPE
#endif // USE_WS2812_DMA
strip->Begin();
Ws2812Clear();
}
void Ws2812Clear()
{
strip->ClearTo(0);
strip->Show();
ws_show_next = 1;
}
void Ws2812SetColor(uint16_t led, uint8_t red, uint8_t green, uint8_t blue, uint8_t white)
{
#if (USE_WS2812_CTYPE > 1)
RgbwColor lcolor;
lcolor.W = white;
#else
RgbColor lcolor;
#endif
lcolor.R = red;
lcolor.G = green;
lcolor.B = blue;
if (led) {
strip->SetPixelColor(led -1, lcolor); // Led 1 is strip Led 0 -> substract offset 1
} else {
// strip->ClearTo(lcolor); // Set WS2812_MAX_LEDS pixels
for (uint16_t i = 0; i < Settings.light_pixels; i++) {
strip->SetPixelColor(i, lcolor);
}
}
strip->Show();
ws_show_next = 1;
}
char* Ws2812GetColor(uint16_t led, char* scolor)
{
uint8_t sl_ledcolor[4];
#if (USE_WS2812_CTYPE > 1)
RgbwColor lcolor = strip->GetPixelColor(led -1);
sl_ledcolor[3] = lcolor.W;
#else
RgbColor lcolor = strip->GetPixelColor(led -1);
#endif
sl_ledcolor[0] = lcolor.R;
sl_ledcolor[1] = lcolor.G;
sl_ledcolor[2] = lcolor.B;
scolor[0] = '\0';
for (byte i = 0; i < light_subtype; i++) {
if (Settings.flag.decimal_text) {
snprintf_P(scolor, 25, PSTR("%s%s%d"), scolor, (i > 0) ? "," : "", sl_ledcolor[i]);
} else {
snprintf_P(scolor, 25, PSTR("%s%02X"), scolor, sl_ledcolor[i]);
}
}
return scolor;
}
void Ws2812ShowScheme(uint8_t scheme)
{
switch (scheme) {
case 0: // Clock
if (((STATES/10)*2 == state) || (ws_show_next)) {
Ws2812Clock();
ws_show_next = 0;
}
break;
default:
if (1 == Settings.light_fade) {
Ws2812Gradient(scheme -1);
} else {
Ws2812Bars(scheme -1);
}
ws_show_next = 1;
break;
}
}
#endif // USE_WS2812