forked from russp81/LEDLAMP_FASTLEDs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
colormodes.h
464 lines (390 loc) · 15.1 KB
/
colormodes.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
// Copyright (c) 2016 @jake-b, @russp81, @toblum
// Griswold LED Lighting Controller
// Griswold is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Griswold is a fork of the LEDLAMP project at
// https://github.com/russp81/LEDLAMP_FASTLEDs
// The LEDLAMP project is a fork of the McLighting Project at
// https://github.com/toblum/McLighting
// ***************************************************************************
// Color modes
// ***************************************************************************
//#include "definitions.h"
char* listStatusJSON();
extern WebSocketsServer webSocket;
// These functions originally displayed the color using a call to FastLed.show()
// This has been refactored out, theser functions now simply render into the
// leds[] array. The FastLed.show() call happens in the main loop now.
// Furthermore, the 'add glitter' option also refactored out to the main loop.
void addGlitter(fract8 chanceOfGlitter) {
if (random8() < chanceOfGlitter) {
leds[random16(NUM_LEDS)] +=
CRGB(settings.glitter_color.red, settings.glitter_color.green,
settings.glitter_color.blue);
}
}
void rainbow() {
// FastLED's built-in rainbow generator
fill_rainbow(leds, NUM_LEDS, gHue, 7);
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(500/settings.fps)));
}
void confetti() {
// random colored speckles that blink in and fade smoothly
fadeToBlackBy(leds, NUM_LEDS, settings.ftb_speed);
for (int x=0; x<settings.confetti_dens; x++) {
int pos = random16(NUM_LEDS);
leds[pos] += CHSV(gHue + random8(64), 200, settings.effect_brightness);
}
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(1000/settings.fps)));
}
void sinelon() {
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy(leds, NUM_LEDS, settings.ftb_speed);
int pos = beatsin16(13, 0, NUM_LEDS);
leds[pos] += CHSV(gHue, 255, settings.effect_brightness);
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(1000/settings.fps)));
}
void bpm() {
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8(BeatsPerMinute, 64, settings.effect_brightness);
for (int i = 0; i < NUM_LEDS; i++) { // 9948
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
}
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(1000/settings.fps)));
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy(leds, NUM_LEDS, settings.ftb_speed);
int dothue = 0;
for (int i = 0; i < 8; i++) {
leds[beatsin16(i + 7, 0, NUM_LEDS)] |=
CHSV(dothue, 200, settings.effect_brightness);
dothue += 32;
}
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(1000/settings.fps)));
}
//******************************************************************************************
// PALETTE ANIMATION FUNCTIONS
//******************************************************************************************
int wipeInProgress = 0;
void FillLEDsFromPaletteColors(CRGBPalette16 palette, uint8_t paletteStartIndex, uint16_t endingLEDIndex=0xFFFF) {
uint8_t colorIndex = paletteStartIndex;
for (int i = 0; i < NUM_LEDS; i++) {
if (i > endingLEDIndex) return; //stop condition
// leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16),
// brightness);
leds[i] = ColorFromPalette(palette, colorIndex,
settings.effect_brightness);
if (anim_direction == FORWARD) {
colorIndex += 3;
}
if (anim_direction == BACK) {
colorIndex -= 3;
}
}
}
void ChangePalettePeriodically(bool forceNow) {
if (forceNow || millis() - paletteMillis > (settings.show_length * 1000)) {
paletteMillis = millis();
targetPaletteIndex = random(0, paletteCount);
currentPalette = targetPalette;
anim_direction = (DIRECTION)!anim_direction; // DIRECTION enum allows flipping by boolean not.
loadPaletteFromFile(targetPaletteIndex, &targetPalette);
DBG_OUTPUT_PORT.printf("New pallet index: %d\n", targetPaletteIndex);
if (settings.glitter_wipe_on) {
DBG_OUTPUT_PORT.println("Begin glitter wipe");
wipeInProgress = true;
}
}
}
void colorWipe() {
static CRGB prevColor = CHSV(gHue, 255, settings.effect_brightness);
static CRGB currentColor = CHSV(gHue+60, 255, settings.effect_brightness);
// Wrap around if necessary
if (wipePos >= NUM_LEDS) {
wipePos = 0;
prevColor = currentColor;
gHue += 60;
currentColor = CHSV(gHue, 255, settings.effect_brightness);
}
// Render the first half of the wipe
for (int x=0; x<wipePos; x++) {
leds[x] = currentColor;
}
// Render the second half
for (int x=wipePos; x<NUM_LEDS; x++) {
leds[x] = prevColor;
}
//Render the glitter at the intersection
if (settings.glitter_wipe_on) {
for (int x=0; x < 3; x++) {
int speckle = wipePos + random(-SPARKLE_SPREAD,SPARKLE_SPREAD);
if (speckle >= 0 && speckle < NUM_LEDS) {
leds[speckle] += CRGB(settings.glitter_color.red, settings.glitter_color.green,
settings.glitter_color.blue);
}
}
}
// Advance for next frame
wipePos+=WIPE_SPEED;
}
void palette_anims() {
currentBlending = LINEARBLEND;
if (settings.palette_ndx == -1) ChangePalettePeriodically(false);
if (!settings.glitter_wipe_on) {
uint8_t maxChanges = int(float(settings.fps / 2));
nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
// Update the current palette if necessary-- and send to any connected clients.
if (currentPaletteIndex != targetPaletteIndex) {
currentPaletteIndex = targetPaletteIndex;
// Send current palette name to the UI.
String name = getPaletteNameWithIndex(currentPaletteIndex);
webSocket.broadcastTXT("p"+name);
}
}
static uint8_t startIndex = 0;
/* motion speed */
startIndex = startIndex + 3;
FillLEDsFromPaletteColors(currentPalette,startIndex);
if (settings.glitter_wipe_on && wipeInProgress) {
if (wipePos >= NUM_LEDS) {
DBG_OUTPUT_PORT.println("End glitter wipe");
wipeInProgress = false;
wipePos = 0;
currentPalette = targetPalette;
currentPaletteIndex = targetPaletteIndex;
// Send current palette name to the UI.
String name = getPaletteNameWithIndex(currentPaletteIndex);
webSocket.broadcastTXT("p"+name);
FillLEDsFromPaletteColors(targetPalette,startIndex);
} else {
FillLEDsFromPaletteColors(targetPalette,startIndex, wipePos);
for (int x=0; x < 3; x++) {
int speckle = wipePos + random(-SPARKLE_SPREAD,SPARKLE_SPREAD);
if (speckle >= 0 && speckle < NUM_LEDS) {
leds[speckle] += CRGB(settings.glitter_color.red, settings.glitter_color.green,
settings.glitter_color.blue);
}
}
wipePos+=WIPE_SPEED;
}
}
}
//*****************LED
//RIPPLE*****************************************************
void one_color_allHSV(int ahue,
int abright) { // SET ALL LEDS TO ONE COLOR (HSV)
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(ahue, 255, abright);
}
}
int wrap(int step) {
if (step < 0) return NUM_LEDS + step;
if (step > NUM_LEDS - 1) return step - NUM_LEDS;
return step;
}
void ripple() {
if (currentBg == nextBg) {
nextBg = random(256);
} else if (nextBg > currentBg) {
currentBg++;
} else {
currentBg--;
}
for (uint16_t l = 0; l < NUM_LEDS; l++) {
leds[l] = CHSV(currentBg, 255,
settings.effect_brightness); // strip.setPixelColor(l,
// Wheel(currentBg, 0.1));
}
if (step == -1) {
center = random(NUM_LEDS);
color = random(256);
step = 0;
}
if (step == 0) {
leds[center] = CHSV(
color, 255, settings.effect_brightness); // strip.setPixelColor(center,
// Wheel(color, 1));
step++;
} else {
if (step < maxSteps) {
//Serial.println(pow(fadeRate, step));
leds[wrap(center + step)] =
CHSV(color, 255,
pow(fadeRate, step) * 255); // strip.setPixelColor(wrap(center
// + step), Wheel(color,
// pow(fadeRate, step)));
leds[wrap(center - step)] =
CHSV(color, 255,
pow(fadeRate, step) * 255); // strip.setPixelColor(wrap(center
// - step), Wheel(color,
// pow(fadeRate, step)));
if (step > 3) {
leds[wrap(center + step - 3)] =
CHSV(color, 255, pow(fadeRate, step - 2) *
255); // strip.setPixelColor(wrap(center +
// step - 3), Wheel(color,
// pow(fadeRate, step - 2)));
leds[wrap(center - step + 3)] =
CHSV(color, 255, pow(fadeRate, step - 2) *
255); // strip.setPixelColor(wrap(center -
// step + 3), Wheel(color,
// pow(fadeRate, step - 2)));
}
step++;
} else {
step = -1;
}
}
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// frame has been created, now show it
// FastLED.show();
// insert a delay to keep the framerate modest
// FastLED.delay(int(float(1000/settings.fps)));
}
//***************************END LED
//RIPPLE*****************************************************
void comet() {
fadeToBlackBy(leds, NUM_LEDS, settings.ftb_speed);
lead_dot = beatsin16(int(float(settings.fps / 3)), 0, NUM_LEDS);
leds[lead_dot] = CHSV(dothue, 200, 255);
dothue += 8;
// if (settings.glitter_on == true){addGlitter(settings.glitter_density);}
// FastLED.show();
}
// Theatre-style crawling lights.
void theaterChase() {
static int8_t frame = 0;
// turn off the previous frame's led
for (int i = 0; i < NUM_LEDS; i = i + 3) {
if (i + frame < NUM_LEDS) {
leds[i + frame] = CRGB(0, 0, 0); // turn every third pixel off
}
}
// advance the frame
frame++;
if (frame > 2) frame = 0;
// turn on the current frame's leds
for (int i = 0; i < NUM_LEDS; i = i + 3) {
if (i + frame < NUM_LEDS) {
leds[i + frame] =
CRGB(settings.main_color.red, settings.main_color.green,
settings.main_color.blue); // turn every third pixel on
}
}
}
//***********TV
int dipInterval = 10;
int darkTime = 250;
unsigned long currentDipTime;
unsigned long dipStartTime;
unsigned long currentMillis;
int ledState = LOW;
long previousMillis = 0;
int ledBrightness[NUM_LEDS];
uint16_t ledHue[NUM_LEDS];
int led = 5;
int interval = 2000;
int twitch = 50;
int dipCount = 0;
int analogLevel = 100;
boolean timeToDip = false;
CRGB hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright) {
// Source: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/
uint8_t temp[5], n = (index >> 8) % 3;
temp[0] = temp[3] = (uint8_t)(( (sat ^ 255) * bright) / 255);
temp[1] = temp[4] = (uint8_t)((((( (index & 255) * sat) / 255) + (sat ^ 255)) * bright) / 255);
temp[2] = (uint8_t)(((((((index & 255) ^ 255) * sat) / 255) + (sat ^ 255)) * bright) / 255);
return CRGB(temp[n + 2], temp[n + 1], temp[n]);
}
void _tvUpdateLed (int led, int brightness) {
ledBrightness[led] = brightness;
for (int i=0; i<NUM_LEDS; i++) {
uint16_t index = (i%3 == 0) ? 400 : random(0,767);
ledHue[led] = index;
}
}
// See: http://forum.mysensors.org/topic/85/phoneytv-for-vera-is-here/13
void tv() {
if (timeToDip == false) {
currentMillis = millis();
if (currentMillis-previousMillis > interval) {
previousMillis = currentMillis;
interval = random(750,4001);//Adjusts the interval for more/less frequent random light changes
twitch = random(40,100);// Twitch provides motion effect but can be a bit much if too high
dipCount++;
}
if (currentMillis-previousMillis<twitch) {
led=random(0, NUM_LEDS-1);
analogLevel=random(50,255);// set the range of the 3 pwm leds
ledState = ledState == LOW ? HIGH: LOW; // if the LED is off turn it on and vice-versa:
_tvUpdateLed(led, (ledState) ? 255 : 0);
if (dipCount > dipInterval) {
//DBG_OUTPUT_PORT.println("dip");
timeToDip = true;
dipCount = 0;
dipStartTime = millis();
darkTime = random(50,150);
dipInterval = random(5,250);// cycles of flicker
}
}
} else {
//DBG_OUTPUT_PORT.println("Dip Time");
currentDipTime = millis();
if (currentDipTime - dipStartTime < darkTime) {
for (int i=3; i<NUM_LEDS; i++) {
_tvUpdateLed(i, 0);
}
} else {
timeToDip = false;
}
}
// Render the thing, with a little flicker
uint8_t flicker = 255;
int sat = 200;
EVERY_N_MILLISECONDS(150) {
flicker = random(220,255);
sat = random(180, 220);
}
for (int i=0; i<NUM_LEDS; i++) {
uint16_t index = (i%3 == 0) ? 400 : random(0,767);
//leds[i] = ((index >> 8) % 3, 200, ledBrightness[i]);
leds[i] = hsb2rgbAN1(ledHue[i], sat, ledBrightness[i]).nscale8_video(flicker);
}
}
//*******************************ARRAY OF SHOW ANIMATIONS FOR MIXED SHOW
//MODE***********************
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = {rainbow, confetti, sinelon, juggle,
bpm, palette_anims, ripple, comet};
//**************************************************************************************************