forked from forkineye/ESPixelStick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffectEngine.cpp
485 lines (427 loc) · 14.5 KB
/
EffectEngine.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
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
#include <Arduino.h>
#include <ArduinoJson.h>
#include "WNRF.h"
#include "EffectEngine.h"
extern config_t config;
// List of all the supported effects and their names
const EffectDesc EFFECT_LIST[] = {
// Mirror AllLeds
// name; func; htmlid; Color; Reverse wsTCode
{ "Disabled", nullptr, "t_disabled", 1, 1, 1, 1, "T0" },
{ "Solid", &EffectEngine::effectSolidColor, "t_static", 1, 0, 0, 0, "T1" },
{ "Blink", &EffectEngine::effectBlink, "t_blink", 1, 0, 0, 0, "T2" },
{ "Flash", &EffectEngine::effectFlash, "t_flash", 1, 0, 0, 0, "T3" },
{ "Rainbow", &EffectEngine::effectRainbow, "t_rainbow", 0, 1, 1, 1, "T5" },
{ "Chase", &EffectEngine::effectChase, "t_chase", 1, 1, 1, 0, "T4" },
{ "Fire flicker", &EffectEngine::effectFireFlicker,"t_fireflicker", 1, 0, 0, 0, "T6" },
{ "Lightning", &EffectEngine::effectLightning, "t_lightning", 1, 0, 0, 0, "T7" },
{ "Breathe", &EffectEngine::effectBreathe, "t_breathe", 1, 0, 0, 0, "T8" }
};
// Effect defaults
#define DEFAULT_EFFECT_NAME "Disabled"
#define DEFAULT_EFFECT_COLOR { 183, 0, 255 }
#define DEFAULT_EFFECT_BRIGHTNESS 1.0
#define DEFAULT_EFFECT_REVERSE false
#define DEFAULT_EFFECT_MIRROR false
#define DEFAULT_EFFECT_ALLLEDS false
#define DEFAULT_EFFECT_SPEED 6
EffectEngine::EffectEngine() {
// Initialize with defaults
setFromDefaults();
}
void EffectEngine::setFromDefaults() {
config.effect_name = DEFAULT_EFFECT_NAME;
config.effect_color = DEFAULT_EFFECT_COLOR;
config.effect_brightness = DEFAULT_EFFECT_BRIGHTNESS;
config.effect_reverse = DEFAULT_EFFECT_REVERSE;
config.effect_mirror = DEFAULT_EFFECT_MIRROR;
config.effect_allleds = DEFAULT_EFFECT_ALLLEDS;
config.effect_speed = DEFAULT_EFFECT_SPEED;
setFromConfig();
}
void EffectEngine::setFromConfig() {
// Initialize with defaults
setEffect(config.effect_name);
setColor(config.effect_color);
setBrightness(config.effect_brightness);
setReverse(config.effect_reverse);
setMirror(config.effect_mirror);
setAllLeds(config.effect_allleds);
setSpeed(config.effect_speed);
}
void EffectEngine::setBrightness(float brightness) {
_effectBrightness = brightness;
if (_effectBrightness > 1.0)
_effectBrightness = 1.0;
if (_effectBrightness < 0.0)
_effectBrightness = 0.0;
}
// Yukky maths here. Input speeds from 1..10 get mapped to 17782..100
void EffectEngine::setSpeed(uint16_t speed) {
_effectSpeed = speed;
setDelay( pow (10, (10-speed)/4.0 +2 ) );
}
void EffectEngine::setDelay(uint16_t delay) {
_effectDelay = delay;
if (_effectDelay < MIN_EFFECT_DELAY)
_effectDelay = MIN_EFFECT_DELAY;
}
void EffectEngine::begin(DRIVER* ledDriver, uint16_t ledCount) {
_ledDriver = ledDriver;
_ledCount = ledCount;
_initialized = true;
}
void EffectEngine::run() {
if (_initialized && _activeEffect && _activeEffect->func) {
if (millis() - _effectLastRun >= _effectWait) {
_effectLastRun = millis();
uint16_t wait = (this->*_activeEffect->func)();
_effectWait = max((int)wait, MIN_EFFECT_DELAY);
_effectCounter++;
}
}
}
void EffectEngine::setEffect(const String effectName) {
const uint8_t effectCount = sizeof(EFFECT_LIST) / sizeof(EffectDesc);
for (uint8_t effect = 0; effect < effectCount; effect++) {
if ( effectName.equalsIgnoreCase(EFFECT_LIST[effect].name) ) {
if (_activeEffect != &EFFECT_LIST[effect]) {
_activeEffect = &EFFECT_LIST[effect];
_effectLastRun = millis();
_effectWait = MIN_EFFECT_DELAY;
_effectCounter = 0;
_effectStep = 0;
}
return;
}
}
_activeEffect = nullptr;
clearAll();
}
int EffectEngine::getEffectCount() {
return sizeof(EFFECT_LIST) / sizeof(EffectDesc);
}
// find effect info by its index in the table
const EffectDesc* EffectEngine::getEffectInfo(unsigned a) {
if (a >= sizeof(EFFECT_LIST) / sizeof(EffectDesc))
a = 0;
return &EFFECT_LIST[a];
}
// find effect info by its web services Tcode
const EffectDesc* EffectEngine::getEffectInfo(const String TCode) {
const uint8_t effectCount = sizeof(EFFECT_LIST) / sizeof(EffectDesc);
for (uint8_t effect = 0; effect < effectCount; effect++) {
if ( TCode.equalsIgnoreCase(EFFECT_LIST[effect].wsTCode) ) {
return &EFFECT_LIST[effect];
}
}
return nullptr;
}
bool EffectEngine::isValidEffect(const String effectName) {
const uint8_t effectCount = sizeof(EFFECT_LIST) / sizeof(EffectDesc);
for (uint8_t effect = 0; effect < effectCount; effect++) {
if ( effectName.equalsIgnoreCase(EFFECT_LIST[effect].name) ) {
return true;
}
}
return false;
}
void EffectEngine::setPixel(uint16_t idx, CRGB color) {
_ledDriver->setValue(3 * idx + 0, (uint8_t)(color.r * _effectBrightness) );
_ledDriver->setValue(3 * idx + 1, (uint8_t)(color.g * _effectBrightness) );
_ledDriver->setValue(3 * idx + 2, (uint8_t)(color.b * _effectBrightness) );
}
void EffectEngine::setRange(uint16_t first, uint16_t len, CRGB color) {
for (uint16_t i=first; i < min(uint16_t(first+len), _ledCount); i++) {
setPixel(i, color);
}
}
void EffectEngine::clearRange(uint16_t first, uint16_t len) {
for (uint16_t i=first; i < min(uint16_t(first+len), _ledCount); i++) {
setPixel(i, {0, 0, 0});
}
}
void EffectEngine::setAll(CRGB color) {
setRange(0, _ledCount, color);
}
void EffectEngine::clearAll() {
clearRange(0, _ledCount);
}
CRGB EffectEngine::colorWheel(uint8_t pos) {
pos = 255 - pos;
if (pos < 85) {
return { 255 - pos * 3, 0, pos * 3};
} else if (pos < 170) {
pos -= 85;
return { 0, pos * 3, 255 - pos * 3 };
} else {
pos -= 170;
return { pos * 3, 255 - pos * 3, 0 };
}
}
uint16_t EffectEngine::effectSolidColor() {
for (uint16_t i=0; i < _ledCount; i++) {
setPixel(i, _effectColor);
}
return 32;
}
uint16_t EffectEngine::effectChase() {
// calculate only half the pixels if mirroring
uint16_t lc = _ledCount;
if (_effectMirror) {
lc = lc / 2;
}
// Prevent errors if we come from another effect with more steps
// or switch from the upper half of non-mirror to mirror mode
_effectStep = _effectStep % lc;
for (uint16_t i=0; i < lc; i++) {
if (i != _effectStep) {
if (_effectMirror) {
setPixel(i + lc, {0, 0, 0});
setPixel(lc - 1 - i, {0, 0, 0});
} else {
setPixel(i, {0, 0, 0});
}
}
}
uint16_t pixel = _effectStep;
if (_effectReverse) {
pixel = lc - 1 - pixel;
}
if (_effectMirror) {
setPixel(pixel + lc, _effectColor);
setPixel(lc - 1 - pixel, _effectColor);
} else {
setPixel(pixel, _effectColor);
}
_effectStep = (1+_effectStep) % lc;
return _effectDelay / 32;
}
uint16_t EffectEngine::effectRainbow() {
// calculate only half the pixels if mirroring
uint16_t lc = _ledCount;
if (_effectMirror) {
lc = lc / 2;
}
for (uint16_t i=0; i < lc; i++) {
// CRGB color = colorWheel(((i * 256 / lc) + _effectStep) & 0xFF);
double hue = 0;
if (_effectAllLeds) {
hue = _effectStep*360.0d / 256; // all same colour
} else {
hue = 360.0 * (((i * 256 / lc) + _effectStep) & 0xFF) / 255;
}
double sat = 1.0;
double val = 1.0;
CRGB color = hsv2rgb ( { hue, sat, val } );
uint16_t pixel = i;
if (_effectReverse) {
pixel = lc - 1 - pixel;
}
if (_effectMirror) {
setPixel(pixel + lc, color);
setPixel(lc - 1 - pixel, color);
} else {
setPixel(pixel, color);
}
}
_effectStep = (1+_effectStep) & 0xFF;
return _effectDelay / 256;
}
uint16_t EffectEngine::effectBlink() {
// The Blink effect uses two "time slots": on, off
// Using default delay, a complete sequence takes 2s.
if (_effectStep % 2) {
clearAll();
} else {
setAll(_effectColor);
}
_effectStep = (1+_effectStep) % 2;
return _effectDelay / 1;
}
uint16_t EffectEngine::effectFlash() {
// The Flash effect uses 6 "time slots": on, off, on, off, off, off
// Using default delay, a complete sequence takes 2s.
// Prevent errors if we come from another effect with more steps
_effectStep = _effectStep % 6;
switch (_effectStep) {
case 0:
case 2:
setAll(_effectColor);
break;
default:
clearAll();
}
_effectStep = (1+_effectStep) % 6;
return _effectDelay / 3;
}
uint16_t EffectEngine::effectFireFlicker() {
byte rev_intensity = 6; // more=less intensive, less=more intensive
byte lum = max(_effectColor.r, max(_effectColor.g, _effectColor.b)) / rev_intensity;
for ( int i = 0; i < _ledCount; i++) {
byte flicker = random(lum);
setPixel(i, CRGB { max(_effectColor.r - flicker, 0), max(_effectColor.g - flicker, 0), max(_effectColor.b - flicker, 0) });
}
_effectStep = (1+_effectStep) % _ledCount;
return _effectDelay / 10;
}
uint16_t EffectEngine::effectLightning() {
static byte maxFlashes;
static int timeslot = _effectDelay / 1000; // 1ms
int flashPause = 10; // 10ms
uint16_t ledStart = random(_ledCount);
uint16_t ledLen = random(1, _ledCount - ledStart);
byte intensity; // flash intensity
if (_effectStep % 2) {
// odd steps = clear
clearAll();
if (_effectStep == 1) {
// pause after 1st flash is longer
flashPause = 130;
} else {
flashPause = random(50, 151); // pause between flashes 50-150ms
}
} else {
// even steps = flashes
if (_effectStep == 0) {
// first flash (weaker and longer pause)
maxFlashes = random(3, 8); // 2-6 follow-up flashes
intensity = random(128);
} else {
// follow-up flashes (stronger)
intensity = random(128, 256); // next flashes are stronger
}
CRGB temprgb = { _effectColor.r*intensity/256, _effectColor.g*intensity/256, _effectColor.b*intensity/256 };
setRange(ledStart, ledLen, temprgb );
flashPause = random(4, 21); // flash duration 4-20ms
}
_effectStep++;
if (_effectStep >= maxFlashes * 2) {
_effectStep = 0;
flashPause = random(100, 5001); // between 0.1 and 5s
}
return timeslot * flashPause;
}
uint16_t EffectEngine::effectBreathe() {
/*
* Subtle "breathing" effect, works best with gamma correction on.
*
* The average resting respiratory rate of an adult is 12–18 breaths/minute.
* We use 12 breaths/minute = 5.0s/breath at the default _effectDelay.
* The tidal volume (~0.5l) is much less than the total lung capacity,
* so we vary only between 75% and 100% of the set brightness.
*
* Per default, this is subtle enough to use with a flood, spot, ceiling or
* even bedside light. If you want more variation, use the values given
* below for a 33%/67% variation.
*
* In the calculation, we use some constants to make it faster:
* 0.367879441 is: 1/e
* 0.106364766 is: 0.25/(e-1/e) [25% brightness variation, use 0.140401491 for 33%]
* 0.75 is the offset [75% min brightness, use 0.67 for 67%]
*
* See also https://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
* for a nice explanation of the math.
*/
// sin() is in radians, so 2*PI rad is a full period; compiler should optimize.
float val = (exp(sin(millis()/(_effectDelay*5.0)*2*PI)) - 0.367879441) * 0.106364766 + 0.75;
setAll({_effectColor.r*val, _effectColor.g*val, _effectColor.b*val});
return _effectDelay / 40; // update every 25ms
}
// dCHSV hue 0->360 sat 0->1.0 val 0->1.0
dCHSV EffectEngine::rgb2hsv(CRGB in_int)
{
dCHSV out;
dCRGB in = {in_int.r/255.0d, in_int.g/255.0d, in_int.b/255.0d};
double min, max, delta;
min = in.r < in.g ? in.r : in.g;
min = min < in.b ? min : in.b;
max = in.r > in.g ? in.r : in.g;
max = max > in.b ? max : in.b;
out.v = max; // v
delta = max - min;
if (delta < 0.00001)
{
out.s = 0;
out.h = 0; // undefined, maybe nan?
return out;
}
if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
out.s = (delta / max); // s
} else {
// if max is 0, then r = g = b = 0
// s = 0, v is undefined
out.s = 0.0;
out.h = NAN; // its now undefined
return out;
}
if( in.r >= max ) // > is bogus, just keeps compilor happy
out.h = ( in.g - in.b ) / delta; // between yellow & magenta
else
if( in.g >= max )
out.h = 2.0 + ( in.b - in.r ) / delta; // between cyan & yellow
else
out.h = 4.0 + ( in.r - in.g ) / delta; // between magenta & cyan
out.h *= 60.0; // degrees
if( out.h < 0.0 )
out.h += 360.0;
return out;
}
// dCHSV hue 0->360 sat 0->1.0 val 0->1.0
CRGB EffectEngine::hsv2rgb(dCHSV in)
{
double hh, p, q, t, ff;
long i;
dCRGB out;
CRGB out_int = {0,0,0};
if(in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
out.g = in.v;
out.b = in.v;
out_int = {255*out.r, 255*out.g, 255*out.b};
return out_int;
}
hh = in.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch(i) {
case 0:
out.r = in.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = in.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = in.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = in.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = in.v;
break;
case 5:
default:
out.r = in.v;
out.g = p;
out.b = q;
break;
}
out_int = {255*out.r, 255*out.g, 255*out.b};
return out_int;
}