-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFastLED_RGBW.h
583 lines (533 loc) · 18.2 KB
/
FastLED_RGBW.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* FastLED_RGBW
*
* Hack to enable SK6812 RGBW strips to work with FastLED.
*
* Original code by Jim Bumgardner (http://krazydad.com).
* Modified by David Madison (http://partsnotincluded.com).
*
*/
#ifndef FastLED_RGBW_h
#define FastLED_RGBW_h
#include <FastLED.h>
/// scale four one byte values by a fith one, which is treated as
/// the numerator of a fraction whose demominator is 256
/// In other words, it computes r,g,b,w * (scale / 256)
///
/// THIS FUNCTION ALWAYS MODIFIES ITS ARGUMENTS IN PLACE
LIB8STATIC void nscale8x4( uint8_t& r, uint8_t& g, uint8_t& b, uint8_t& w, fract8 scale)
{
uint16_t scale_fixed = scale + 1;
r = (((uint16_t)r) * scale_fixed) >> 8;
g = (((uint16_t)g) * scale_fixed) >> 8;
b = (((uint16_t)b) * scale_fixed) >> 8;
w = (((uint16_t)w) * scale_fixed) >> 8;
}
/// scale four one byte values by a fith one, which is treated as
/// the numerator of a fraction whose demominator is 256
/// In other words, it computes r,g,b,w * (scale / 256), ensuring
/// that non-zero values passed in remain non zero, no matter how low the scale
/// argument.
///
/// THIS FUNCTION ALWAYS MODIFIES ITS ARGUMENTS IN PLACE
LIB8STATIC void nscale8x4_video( uint8_t& r, uint8_t& g, uint8_t& b, uint8_t& w, fract8 scale)
{
#if SCALE8_C == 1
uint8_t nonzeroscale = (scale != 0) ? 1 : 0;
r = (r == 0) ? 0 : (((int)r * (int)(scale) ) >> 8) + nonzeroscale;
g = (g == 0) ? 0 : (((int)g * (int)(scale) ) >> 8) + nonzeroscale;
b = (b == 0) ? 0 : (((int)b * (int)(scale) ) >> 8) + nonzeroscale;
w = (w == 0) ? 0 : (((int)w * (int)(scale) ) >> 8) + nonzeroscale;
#elif SCALE8_AVRASM == 1
nscale8_video_LEAVING_R1_DIRTY( r, scale);
nscale8_video_LEAVING_R1_DIRTY( g, scale);
nscale8_video_LEAVING_R1_DIRTY( b, scale);
nscale8_video_LEAVING_R1_DIRTY( w, scale);
cleanup_R1();
#else
#error "No implementation for nscale8x3 available."
#endif
}
struct CRGBW {
union {
struct {
union {
uint8_t g;
uint8_t green;
};
union {
uint8_t r;
uint8_t red;
};
union {
uint8_t b;
uint8_t blue;
};
union {
uint8_t w;
uint8_t white;
};
};
uint8_t raw[4];
};
CRGBW(){}
/// Allow a CRBGW variable to be assigned an CRGB value
inline CRGBW& operator= (const CRGB& rhs) __attribute__((always_inline)){
//Serial.println("Allow a CRBGW variable to be assigned an CRGB value");
r = rhs.r;
g = rhs.g;
b = rhs.b;
w = 0;
return *this;
}
/// allow assignment from HSV color
inline CRGBW& operator= (const CHSV& rhs) __attribute__((always_inline))
{
CRGB sub;
hsv2rgb_rainbow( rhs, sub);
this->r = sub.r;
this->g = sub.g;
this->b = sub.b;
this->w = 0;
// Serial.println("allow assignment from HSV color");
}
/// Array access operator to index into the crgbw object
/// i.e [0] = red, [1] = green etc . .
inline uint8_t& operator[] (uint8_t x) __attribute__((always_inline))
{
return raw[x];
}
/// Array access operator to index into the crgb object
/// i.e [0] = red, [1] = green etc . .
inline const uint8_t& operator[] (uint8_t x) const __attribute__((always_inline))
{
return raw[x];
}
/// allow construction from R, G, B, W
/// This is for when you declare a variable as CRGBW and want to initialise it with individual values for r,g,b and w
inline CRGBW( uint8_t ir, uint8_t ig, uint8_t ib, uint8_t iw) __attribute__((always_inline))
: r(ir), g(ig), b(ib), w(iw)
{
//Serial.println("This is for when you declare a variable as CRGBW and want to initialise it with 4 individual bytes for r,g,b and w");
}
/// allow construction from 32-bit 0xRRGGBBWW color code
/// This is for when you declare a variable as CRGBW and want to initialise it with a 32bit value (4 x 8 bits)
inline CRGBW( uint32_t colorcode) __attribute__((always_inline))
: r((colorcode >> 24) & 0xFF), g((colorcode >> 16) & 0xFF), b((colorcode >> 8) & 0xFF), w((colorcode >> 0) & 0xFF)
{
//Serial.println("This is for when you declare a variable as CRGBW and want to initialise it with a 32bit value ie 0xRRGGBBWW");
}
/// allow copy construction
inline CRGBW(const CRGBW& rhs) __attribute__((always_inline)) = default;
/// allow construction from HSV color
inline CRGBW(const CHSV& rhs) __attribute__((always_inline))
{ CRGB sub(0,0,0);
hsv2rgb_rainbow( rhs, sub );
this->r = sub.r;
this->g = sub.g;
this->b = sub.b;
this->w = 0;
//Serial.println("allow construction from HSV color");
}
/// allow assignment from one RGBW struct to another
inline CRGBW& operator= (const CRGBW& rhs) __attribute__((always_inline)) = default;
/// allow assignment from 32-bit 0xRRGGBBWW color code
inline CRGBW& operator= (const uint32_t colorcode) __attribute__((always_inline))
{
r = (colorcode >> 24) & 0xFF;
g = (colorcode >> 16) & 0xFF;
b = (colorcode >> 8) & 0xFF;
w = (colorcode >> 0) & 0xFF;
return *this;
}
/// add one RGBW to another, saturating at 0xFF for each channel
inline CRGBW& operator+= (const CRGBW& rhs )
{
//Serial.println("add RGB to RGBW, saturating at 0xFF for each channel");
r = qadd8( r, rhs.r);
g = qadd8( g, rhs.g);
b = qadd8( b, rhs.b);
w = qadd8( w, rhs.w);
return *this;
}
/// add RGB to RGBW, saturating at 0xFF for each channel
inline CRGBW& operator+= (const CRGB& rhs )
{
r = qadd8( r, rhs.r);
g = qadd8( g, rhs.g);
b = qadd8( b, rhs.b);
return *this;
}
/// add a contstant to each channel, saturating at 0xFF
/// this is NOT an operator+= overload because the compiler
/// can't usefully decide when it's being passed a 32-bit
/// constant (e.g. CRGB::Red) and an 8-bit one (CRGB::Blue)
inline CRGBW& addToRGB (uint8_t d )
{
Serial.println("add a contstant to each channel, saturating at 0xFF");
r = qadd8( r, d);
g = qadd8( g, d);
b = qadd8( b, d);
w = qadd8( w, d);
return *this;
}
/// "or" operator brings each channel up to the higher of the two values
/// "or" CRGBW with CRGB
inline CRGBW& operator|= (const CRGB& rhs )
{
if( rhs.r > r) r = rhs.r;
if( rhs.g > g) g = rhs.g;
if( rhs.b > b) b = rhs.b;
return *this;
}
/// "or" CRGBW with CRGBW
inline CRGBW& operator|= (const CRGBW& rhs )
{
if( rhs.r > r) r = rhs.r;
if( rhs.g > g) g = rhs.g;
if( rhs.b > b) b = rhs.b;
if( rhs.w > w) w = rhs.w;
return *this;
}
/// Get the average of the R, G, and B values
inline uint8_t getAverageLight( ) const {
#if FASTLED_SCALE8_FIXED == 1
const uint8_t eightyfive = 85;
#else
const uint8_t eightyfive = 86;
#endif
uint8_t avg = scale8_LEAVING_R1_DIRTY( r, eightyfive) + \
scale8_LEAVING_R1_DIRTY( g, eightyfive) + \
scale8_LEAVING_R1_DIRTY( b, eightyfive);
cleanup_R1();
return avg;
}
/// scale down a RGBW to N 256ths of it's current brightness, using
/// 'plain math' dimming rules, which means that if the low light levels
/// may dim all the way to 100% black.
inline CRGBW& nscale8 (uint8_t scaledown )
{
//Serial.println("scale down a RGBW to N 256ths of it's current brightness");
nscale8x4( r, g, b, w, scaledown);
return *this;
}
/// fadeToBlackBy is a synonym for nscale8( ..., 255-fadefactor)
inline CRGBW& fadeToBlackBy (uint8_t fadefactor )
{
nscale8x4( r, g, b, w, 255 - fadefactor);
return *this;
}
/// scale down a RGB to N 256ths of it's current brightness, using
/// 'video' dimming rules, which means that unless the scale factor is ZERO
/// each channel is guaranteed NOT to dim down to zero. If it's already
/// nonzero, it'll stay nonzero, even if that means the hue shifts a little
/// at low brightness levels.
inline CRGBW& nscale8_video (uint8_t scaledown )
{
nscale8x4_video( r, g, b, w, scaledown);
return *this;
}
/// %= is a synonym for nscale8_video. Think of it is scaling down
/// by "a percentage"
inline CRGBW& operator%= (uint8_t scaledown )
{
nscale8x4_video( r, g, b, w, scaledown);
return *this;
}
/// fadeLightBy is a synonym for nscale8_video( ..., 255-fadefactor)
inline CRGBW& fadeLightBy (uint8_t fadefactor )
{
nscale8x4_video( r, g, b, w, 255 - fadefactor);
return *this;
}
/// Predefined RGBW colors
typedef enum {
AliceBlue=0xF0F8FF00,
Amethyst=0x9966CC00,
AntiqueWhite=0xFAEBD700,
Aqua=0x00FFFF00,
Aquamarine=0x7FFFD400,
Azure=0xF0FFFF00,
Beige=0xF5F5DC00,
Bisque=0xFFE4C400,
Black=0x00000000,
BlanchedAlmond=0xFFEBCD00,
Blue=0x0000FF00,
BlueViolet=0x8A2BE200,
Brown=0xA52A2A00,
BurlyWood=0xDEB88700,
CadetBlue=0x5F9EA000,
Chartreuse=0x7FFF0000,
Chocolate=0xD2691E00,
Coral=0xFF7F5000,
CornflowerBlue=0x6495ED00,
Cornsilk=0xFFF8DC00,
Crimson=0xDC143C00,
Cyan=0x00FFFF00,
DarkBlue=0x00008B00,
DarkCyan=0x008B8B00,
DarkGoldenrod=0xB8860B00,
DarkGray=0xA9A9A900,
DarkGrey=0xA9A9A900,
DarkGreen=0x00640000,
DarkKhaki=0xBDB76B00,
DarkMagenta=0x8B008B00,
DarkOliveGreen=0x556B2F00,
DarkOrange=0xFF8C0000,
DarkOrchid=0x9932CC00,
DarkRed=0x8B000000,
DarkSalmon=0xE9967A00,
DarkSeaGreen=0x8FBC8F00,
DarkSlateBlue=0x483D8B00,
DarkSlateGray=0x2F4F4F00,
DarkSlateGrey=0x2F4F4F00,
DarkTurquoise=0x00CED100,
DarkViolet=0x9400D300,
DeepPink=0xFF149300,
DeepSkyBlue=0x00BFFF00,
DimGray=0x69696900,
DimGrey=0x69696900,
DodgerBlue=0x1E90FF00,
FireBrick=0xB2222200,
FloralWhite=0xFFFAF000,
ForestGreen=0x228B2200,
Fuchsia=0xFF00FF00,
Gainsboro=0xDCDCDC00,
GhostWhite=0xF8F8FF00,
Gold=0xFFD70000,
Goldenrod=0xDAA52000,
Gray=0x80808000,
Grey=0x80808000,
Green=0x00800000,
GreenYellow=0xADFF2F00,
Honeydew=0xF0FFF000,
HotPink=0xFF69B400,
IndianRed=0xCD5C5C00,
Indigo=0x4B008200,
Ivory=0xFFFFF000,
Khaki=0xF0E68C00,
Lavender=0xE6E6FA00,
LavenderBlush=0xFFF0F500,
LawnGreen=0x7CFC0000,
LemonChiffon=0xFFFACD00,
LightBlue=0xADD8E600,
LightCoral=0xF0808000,
LightCyan=0xE0FFFF00,
LightGoldenrodYellow=0xFAFAD200,
LightGreen=0x90EE9000,
LightGrey=0xD3D3D300,
LightPink=0xFFB6C100,
LightSalmon=0xFFA07A00,
LightSeaGreen=0x20B2AA00,
LightSkyBlue=0x87CEFA00,
LightSlateGray=0x77889900,
LightSlateGrey=0x77889900,
LightSteelBlue=0xB0C4DE00,
LightYellow=0xFFFFE000,
Lime=0x00FF0000,
LimeGreen=0x32CD3200,
Linen=0xFAF0E600,
Magenta=0xFF00FF00,
Maroon=0x80000000,
MediumAquamarine=0x66CDAA00,
MediumBlue=0x0000CD00,
MediumOrchid=0xBA55D300,
MediumPurple=0x9370DB00,
MediumSeaGreen=0x3CB37100,
MediumSlateBlue=0x7B68EE00,
MediumSpringGreen=0x00FA9A00,
MediumTurquoise=0x48D1CC00,
MediumVioletRed=0xC7158500,
MidnightBlue=0x19197000,
MintCream=0xF5FFFA00,
MistyRose=0xFFE4E100,
Moccasin=0xFFE4B500,
NavajoWhite=0xFFDEAD00,
Navy=0x00008000,
OldLace=0xFDF5E600,
Olive=0x80800000,
OliveDrab=0x6B8E2300,
Orange=0xFFA50000,
OrangeRed=0xFF450000,
Orchid=0xDA70D600,
PaleGoldenrod=0xEEE8AA00,
PaleGreen=0x98FB9800,
PaleTurquoise=0xAFEEEE00,
PaleVioletRed=0xDB709300,
PapayaWhip=0xFFEFD500,
PeachPuff=0xFFDAB900,
Peru=0xCD853F00,
Pink=0xFFC0CB00,
Plaid=0xCC553300,
Plum=0xDDA0DD00,
PowderBlue=0xB0E0E600,
Purple=0x80008000,
Red=0xFF000000,
RosyBrown=0xBC8F8F00,
RoyalBlue=0x4169E100,
SaddleBrown=0x8B451300,
Salmon=0xFA807200,
SandyBrown=0xF4A46000,
SeaGreen=0x2E8B5700,
Seashell=0xFFF5EE00,
Sienna=0xA0522D00,
Silver=0xC0C0C000,
SkyBlue=0x87CEEB00,
SlateBlue=0x6A5ACD00,
SlateGray=0x70809000,
SlateGrey=0x70809000,
Snow=0xFFFAFA00,
SpringGreen=0x00FF7F00,
SteelBlue=0x4682B400,
Tan=0xD2B48C00,
Teal=0x00808000,
Thistle=0xD8BFD800,
Tomato=0xFF634700,
Turquoise=0x40E0D000,
Violet=0xEE82EE00,
Wheat=0xF5DEB300,
White=0xFFFFFF00,
WhiteSmoke=0xF5F5F500,
Yellow=0xFFFF0000,
YellowGreen=0x9ACD3200,
// LED RGB color that roughly approximates
// the color of incandescent fairy lights,
// assuming that you're using FastLED
// color correction on your LEDs (recommended).
FairyLight=0xFFE42D00,
// If you are using no color correction, use this
FairyLightNCC=0xFF9D2A00
} HTMLColorCode;
};
inline uint16_t getRGBWsize(uint16_t nleds){
uint16_t nbytes = nleds * 4;
if(nbytes % 3 > 0) return nbytes / 3 + 1;
else return nbytes / 3;
}
void fill_solid( struct CRGBW * leds, int numToFill,
const struct CRGB& color)
{
for( int i = 0; i < numToFill; ++i) {
leds[i] = color;
}
}
void nscale8( CRGBW* leds, uint16_t num_leds, uint8_t scale)
{
for( uint16_t i = 0; i < num_leds; ++i) {
leds[i].nscale8( scale);
}
}
void fadeToBlackBy( CRGBW* leds, uint16_t num_leds, uint8_t fadeBy)
{
nscale8( leds, num_leds, 255 - fadeBy);
}
// Forward declaration of the function "XY" which must be provided by
// the application for use in two-dimensional filter functions.
uint16_t XY( uint8_t, uint8_t);// __attribute__ ((weak));
// blur1d: one-dimensional blur filter. Spreads light to 2 line neighbors.
// blur2d: two-dimensional blur filter. Spreads light to 8 XY neighbors.
//
// 0 = no spread at all
// 64 = moderate spreading
// 172 = maximum smooth, even spreading
//
// 173..255 = wider spreading, but increasing flicker
//
// Total light is NOT entirely conserved, so many repeated
// calls to 'blur' will also result in the light fading,
// eventually all the way to black; this is by design so that
// it can be used to (slowly) clear the LEDs to black.
void blur1d( CRGBW* leds, uint16_t numLeds, fract8 blur_amount)
{
uint8_t keep = 255 - blur_amount;
uint8_t seep = blur_amount >> 1;
CRGBW carryover = CRGBW::Black;
for( uint16_t i = 0; i < numLeds; ++i) {
CRGBW cur = leds[i];
CRGBW part = cur;
part.nscale8( seep);
cur.nscale8( keep);
cur += carryover;
if( i) leds[i-1] += part;
leds[i] = cur;
carryover = part;
}
}
// blurRows: perform a blur1d on every row of a rectangular matrix
void blurRows( CRGBW* leds, uint8_t width, uint8_t height, fract8 blur_amount)
{
for( uint8_t row = 0; row < height; ++row) {
CRGBW* rowbase = leds + (row * width);
blur1d( rowbase, width, blur_amount);
}
}
// blurColumns: perform a blur1d on each column of a rectangular matrix
void blurColumns(CRGBW* leds, uint8_t width, uint8_t height, fract8 blur_amount)
{
// blur columns
uint8_t keep = 255 - blur_amount;
uint8_t seep = blur_amount >> 1;
for( uint8_t col = 0; col < width; ++col) {
CRGBW carryover = CRGBW::Black;
for( uint8_t i = 0; i < height; ++i) {
CRGBW cur = leds[XY(col,i)];
CRGBW part = cur;
part.nscale8( seep);
cur.nscale8( keep);
cur += carryover;
if( i) leds[XY(col,i-1)] += part;
leds[XY(col,i)] = cur;
carryover = part;
}
}
}
void blur2d( CRGBW* leds, uint8_t width, uint8_t height, fract8 blur_amount)
{
blurRows(leds, width, height, blur_amount);
blurColumns(leds, width, height, blur_amount);
}
void fill_solid_CRGBW( CRGBW* leds, int numToFill,
const struct CRGBW& CRGBWColor)
{
for( int i = 0; i < numToFill; ++i) {
leds[i] = CRGBWColor;
}
}
void testCRGBW(){
char buffer[100];
CRGB col(101,102,103);
CRGBW colw (201,202,203,204);
sprintf(buffer, "rgb colours for col are r:%u, g:%u, b:%u", col.r, col.g, col.b);
Serial.println(buffer);
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw.r, colw.g, colw.b, colw.w);
Serial.println(buffer);
col = CHSV(241, 100,100);
colw = CHSV(241, 100,100);
sprintf(buffer, "rgb colours for col are r:%u, g:%u, b:%u", col.r, col.g, col.b);
Serial.println(buffer);
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw.r, colw.g, colw.b, colw.w);
Serial.println(buffer);
col = CRGB(100,101,102);
colw = col;
sprintf(buffer, "rgb colours for col are r:%u, g:%u, b:%u", col.r, col.g, col.b);
Serial.println(buffer);
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw.r, colw.g, colw.b, colw.w);
Serial.println(buffer);
sprintf(buffer, "rgbw colours for colw are 0:%u, 1:%u, 2:%u, 3:%u", colw[0], colw[1], colw[2], colw[3]);
Serial.println(buffer);
CRGBW colw1 (0xFFFEFDFC);
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw1.r, colw1.g, colw1.b, colw1.w);
Serial.println(buffer);
CRGBW colw2 (CHSV(241, 100,100));
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw2.r, colw2.g, colw2.b, colw2.w);
Serial.println(buffer);
colw1 = CRGBW(50,51,52,53);
//This line will fail: colw = colw1 + colw2;
//For addition you must use the += operator i.e.
colw1 += colw1;
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw1.r, colw1.g, colw1.b, colw1.w);
Serial.println(buffer);
colw1 += col; // also allows you to add CRGB types
sprintf(buffer, "rgbw colours for colw are r:%u, g:%u, b:%u, w:%u", colw1.r, colw1.g, colw1.b, colw1.w);
Serial.println(buffer);
}
#endif