-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastLED_Effects.ino
More file actions
executable file
·884 lines (782 loc) · 27.4 KB
/
Copy pathFastLED_Effects.ino
File metadata and controls
executable file
·884 lines (782 loc) · 27.4 KB
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
#include <Arduino.h>
#define Serial_BAUDRATE 74880 // Arduino: 9600, ESP32: z.B. 115200
// Comment #define and uncomment #undef to exclude DHT11 Sensor if you just want the LEDs
//#define ENABLE_DHT11 1
#undef ENABLE_DHT11
// WiFi connectivity and webserver
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
ESP8266WebServer server(80);
DNSServer dnsserver;
const char* my_ssid = "";
const char* my_pass = "";
const char * esp_name = "ESP LED";
int rssi = -1;
int apmode = 0;
int ssid_len = 0;
int ssid_ind =-1;
int pw_len = 0;
int pw_ind = -1;
// LED
#define FASTLED_ESP8266_RAW_PIN_ORDER
//To get rid of all the warnings, add
//d1_mini_lite.build.extra_flags=-Wno-register -Wno-misleading-indentation
//to c:\Users\xxxxx\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\
#pragma GCC diagnostic ignored "-Wregister"
#include <FastLED.h>
#define DATA_PIN 16 //D6
#define NUM_LEDS 300 //264 for SZ
CRGBArray<NUM_LEDS> leds;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
//Avoid getting to dark (or black) as this is perceived as a flicker
#define FADE_MIN 32
int timer=0;
// Setting that should survive power cycle
#include <EEPROM.h>
// Change this if the structure of "settings" is changing to avoid incorrect assignments
#define MAGIC_WORD 11269
struct settings {
uint16_t magic=MAGIC_WORD;
uint8_t mode = 0; // Effect mode
uint8_t speed = 10; // Speed setting for some modes
uint8_t fade = 50;
uint8_t hue = 20;
uint8_t col_r = 255;
uint8_t col_g = 0;
uint8_t col_b = 0;
uint8_t rgbmode = 1;
uint8_t endless = 1;
uint8_t reverse = 0;
uint8_t brightness = 255;
uint8_t ssid_len = 0;
uint8_t pw_len = 0;
char ssid[64]="ssid";
char pw[64]="pw";
};
settings mysets;
#ifdef ENABLE_DHT11
// Temperature and Humidity sensor
#include "DHTesp.h"
DHTesp dht;
int dhtPin = 5; //D1
float temperature = -1;
float humidity = -1;
#endif
#define BUTTON_PIN 2
// To test the page it can be renamed to .html
// uses the C++ raw string literals
#include "webpage.h"
// Called when system is powered up
void setup() {
Serial.begin(Serial_BAUDRATE);
Serial.println("Starting ESP");
// Try to read WiFi connection parameters from EEPROM
EEPROM.begin(160);
uint16_t magic;
EEPROM.get(0,magic);
//Check magic word to make sure settings are valid
if (magic==MAGIC_WORD) {
Serial.println("EEPROM valid, reading settings");
EEPROM.get(0,mysets);
}
Serial.flush();
delay(50);
Serial.print("SSID: ");
Serial.println(mysets.ssid);
Serial.print("PASS: ");
Serial.println(mysets.pw);
// While developping prioritize hardcoded setting
if (strlen(my_ssid)>0 and strlen(my_pass)>0) {
WiFi.begin(my_ssid,my_pass);
}
WiFi.begin(mysets.ssid,mysets.pw);
if (WiFi.waitForConnectResult(10000) != WL_CONNECTED) {
Serial.println("Error connecting Wifi!");
// IPAddress local_IP(192,168,33,1);
IPAddress local_IP(4,3,2,1); //Samsung devices don't accept 192.168.x.x adresses
IPAddress subnet(255,255,255,0);
WiFi.mode(WIFI_AP);
WiFi.softAP(esp_name);
delay(100);
WiFi.softAPConfig(local_IP, local_IP, subnet);
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
dnsserver.setErrorReplyCode(DNSReplyCode::NoError);
dnsserver.setTTL(3600);
dnsserver.start(53, "*", local_IP);
apmode=1;
} else {
Serial.println("Successful WiFi connect with stored values");
Serial.flush();
apmode=0;
}
WiFi.setAutoReconnect(true);
WiFi.setHostname(esp_name);
#ifdef ENABLE_DHT11
dht.setup(dhtPin, DHTesp::DHT11);
#endif
pinMode(BUTTON_PIN,INPUT);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
leds.fadeToBlackBy(5);
setBrightness(mysets.brightness);
random16_add_entropy( random());
sinus_init();
Serial.print("WiFi connected to ");
Serial.print(WiFi.localIP());
Serial.print(" with ");
Serial.println(WiFi.RSSI());
if (apmode) {
server.on("/", []() {
server.send(200, "text/html", login_page);
});
// Captive AP
server.on("/generate_204", []() { server.send(200, "text/html", login_page); }); // android captive portal redirect
server.on("/redirect", []() { server.send(200, "text/html", login_page); }); // microsoft redirect
server.on("/hotspot-detect.html", []() { server.send(200, "text/html", login_page); }); // apple call home
server.on("/canonical.html", []() { server.send(200, "text/html", login_page); }); // firefox captive portal call home
server.on("/success.txt", []() { server.send(200, "text/html", login_page); }); // firefox captive portal call home
server.on("/ncsi.txt", []() { server.send(200, "text/html", login_page); }); // windows call home
server.on("/login", []() {
Serial.print("SSID:");
Serial.println(server.arg("ssid"));
ssid_len=urlDecode(server.arg("ssid").c_str(),mysets.ssid,64);
Serial.println(mysets.ssid);
Serial.print("Password:");
Serial.println(server.arg("pw"));
urlDecode(server.arg("pw").c_str(),mysets.pw,64);
pw_len=Serial.println(mysets.pw);
Serial.println(mysets.pw);
});
} else {
server.on("/", []() {
server.send(200, "text/html", html_page);
});
server.on("/command", []() { //Define the handling function for the javascript path
int newmode=server.arg("id").toInt();
if (newmode==100) {
EEPROM.put(0,mysets);
EEPROM.commit();
} else if (newmode==101) {
timer=millis()/1000+60*60; // fixed timer 1h for now
} else {
initMode(newmode);
}
server.send(200,"text/html","ok");
});
server.on("/settings", []() {
String reply="{\"speed\":\""+String(mysets.speed)+"\",\"fade\":\""+String(mysets.fade)+"\",";
reply+="\"brightness\":\""+String(mysets.brightness)+"\",";
reply+="\"hue\":\""+String(mysets.hue)+"\",";
reply+="\"effect\":\""+String(mysets.mode)+"\",";
if (timer>0) {
reply+="\"timer\":\"(Timer: "+String((timer-millis()/1000)/60)+" min"+")\",";
} else {
reply+="\"timer\":\" \",";
}
#ifdef ENABLE_DHT11
reply+= "\"temperature\":\""+String(temperature)+"\",\"humidity\":\""+String(humidity)+"\",";
#endif
char color[8];
sprintf(color,"#%02x%02x%02x",mysets.col_r,mysets.col_g,mysets.col_b);
reply+= "\"color\":\""+String(color)+"\",";
reply+="\"col_r\":\""+String(mysets.col_r)+"\",\"col_g\":\""+String(mysets.col_g)+"\",\"col_b\":\""+String(mysets.col_b)+"\",";
reply+="\"rgb\":\""+String(mysets.rgbmode)+"\",\"endless\":\""+String(mysets.endless)+"\",\"reverse\":\""+String(mysets.reverse)+"\"}";
Serial.println(reply);
server.send(200,"text/html",reply);
});
server.on("/slider", []() { //Define the handling function for the javascript path
mysets.speed=set_arg(mysets.speed,server.arg("speed"));
mysets.fade=set_arg(mysets.fade,server.arg("fade"));
mysets.hue=set_arg(mysets.hue,server.arg("hue"));
mysets.col_r=set_arg(mysets.col_r,server.arg("r"));
mysets.col_g=set_arg(mysets.col_g,server.arg("g"));
mysets.col_b=set_arg(mysets.col_b,server.arg("b"));
mysets.rgbmode=set_arg(mysets.rgbmode,server.arg("rgb"));
mysets.endless=set_arg(mysets.endless,server.arg("endless"));
mysets.reverse=set_arg(mysets.reverse,server.arg("reverse"));
String my_brightness=server.arg("brightness");
if (my_brightness.length()>0) {
mysets.brightness=my_brightness.toInt();
setBrightness(mysets.brightness);
}
Serial.println("Color:"+server.arg("color"));
if (server.arg("color").length()>0) {
Serial.println("Color:"+server.arg("color"));
sscanf(server.arg("color").c_str(),"#%02hhX%02hhX%02hhX",&mysets.col_r,&mysets.col_g,&mysets.col_b);
}
server.send(200,"text/html","ok");
});
server.on("/reset", []() {
// next reboot will require all settings including WiFi to be set again
mysets.magic=0;
EEPROM.put(0,mysets);
EEPROM.commit();
});
}
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.println("Executing main");
Serial.flush();
}
void initMode(int newmode) {
mysets.mode=newmode;
//Reset brightness as some effects change it
setBrightness(mysets.brightness);
// Set reasonable defaults
switch (mysets.mode) {
case 0: timer=0; leds.fadeToBlackBy(5); break; //OFF
case 1: mysets.speed=255; mysets.fade=50;
mysets.rgbmode=0; mysets.col_r=255; mysets.col_g=0; mysets.col_b=0;
mysets.endless=0; break; // KITT
case 2: mysets.speed=180; mysets.fade=50; mysets.rgbmode=1; break; // Multi
case 3: mysets.speed=255; mysets.fade=50; mysets.rgbmode=1; mysets.endless=1; break; // Mirror
case 4: mysets.speed=255; mysets.hue=32; break; // Pride
case 5: mysets.speed=50; break; // Constant
case 6: mysets.speed=50; break; // ConstantFade
case 7: sinus_init(); mysets.speed=100; mysets.fade=25; break; // Sinelon
case 8: mysets.speed=140; mysets.fade=80; break; // Fire
case 9: mysets.speed=230; mysets.fade=100; mysets.rgbmode=1; break; // Multi
case 10: mysets.speed=50; mysets.hue=32; break; // Pacifica
case 11: mysets.speed=50; mysets.hue=32; break; // Rainbow
case 12: mysets.speed=50; mysets.hue=32; break; // Rainbow with Glitter
case 13: mysets.fade=20; mysets.speed=120; break; // Confetti
case 14: mysets.fade=8; mysets.speed=255; break; // Confetti fast
case 15: mysets.speed=90; mysets.fade=30; break; // BPM
case 16: sinus_init(); mysets.speed=50; mysets.fade=30; break; // Juggle
}
}
// Sets the brightness exponentially which rather looks like what you expect
void setBrightness(int val) {
FastLED.setBrightness(val*val/256);
}
// Helper function to only modify an argument if it was actually set
int set_arg(int val,String arg) {
if (arg.length()>0) {
if (arg == "true") {
return 1;
}
if (arg == "false") {
return 0;
}
return arg.toInt();
}
return val;
}
//Revert javascript function encodeURIComponent(pw) (%xx -> character)
int urlDecode(const char *src,char *dest,int len) {
Serial.println(src);
char ch[3];
int d=0;
int i, ii;
for (i=0; i<strlen(src); i++) {
if (src[i]=='%') {
ch[0]=src[i+1];
ch[1]=src[i+2];
ch[2]=0;
sscanf(ch, "%x", &ii);
dest[d++]=static_cast<char>(ii);
i=i+2;
} else {
dest[d++]=src[i];
}
}
dest[d]=0;
return d;
}
/* ------------------------------------------------------------ */
void handleNotFound() {
String message = "Page Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
message += "Headers:";
message += server.headers();
message += "\n";
for (uint8_t i = 0; i < server.headers(); i++) {
message += " " + server.headerName(i) + ":" + server.header(i) + "\n";
}
Serial.println(message);
server.send(404, "text/plain", message);
}
int button=1;
// Executed repeatedly after setup has finished
void loop() {
if (apmode) {
getCredentials();
dnsserver.processNextRequest();
gHue+=13;
CRGB col;
// To signal APMode flash on/off with rotating colors
if ((gHue%2)>0) {
col=CHSV( gHue, 255, 255);
} else {
col=CRGB(0,0,0);
}
Constant(col);
}
server.handleClient();
if (!apmode) {
#ifdef ENABLE_DHT11
handle_dht11();
#endif
handle_led();
}
int bt=digitalRead(BUTTON_PIN);
if (button!=bt) {
Serial.print("Button:");
Serial.println(bt);
button=bt;
if (button==1) {
mysets.mode++;
if (mysets.mode>16) {
mysets.mode=0;
}
initMode(mysets.mode);
Serial.print("New mode:");
Serial.println(mysets.mode);
//Hack: Pressing Button exits apmode
apmode=0;
}
}
}
// Scan serial for SSID/PW is stateful mode to allow AP Input in parallel
void getCredentials() {
if (ssid_len == 0) {
if (ssid_ind<0) {
Serial.println();
Serial.print("Enter SSID:");
ssid_ind=0;
} else {
int len = Serial.readBytes(&mysets.ssid[ssid_ind], 64-ssid_ind);
ssid_ind+=len;
if (mysets.ssid[ssid_ind-1]==10) {
mysets.ssid[ssid_ind-1]=0;
ssid_len=ssid_ind-1;
Serial.println(mysets.ssid);
Serial.flush();
}
}
}
if (pw_len == 0 && ssid_len>0) {
if (pw_ind<0) {
Serial.println();
Serial.print("Enter Password:");
pw_ind=0;
} else {
int len = Serial.readBytes(&mysets.pw[pw_ind], 64-pw_ind);
pw_ind+=len;
if (mysets.pw[pw_ind-1]==10) {
mysets.pw[pw_ind-1]=0;
pw_len=pw_ind-1;
Serial.println(mysets.pw);
Serial.flush();
}
}
}
if (pw_len>0 && ssid_len>0) {
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.println("Connecting to "+String(mysets.ssid)+"/"+String(mysets.pw));
Serial.flush();
WiFi.begin(mysets.ssid,mysets.pw);
if (WiFi.waitForConnectResult(10000) != WL_CONNECTED) {
Serial.println("Error connecting Wifi!");
} else {
Serial.println("Successfully registered new WiFi connection");
}
EEPROM.put(0,mysets);
EEPROM.commit();
ESP.restart();
}
}
int sec=0;
int mil=0;
int led_index=0;
#ifdef ENABLE_DHT11
// Update Temperature and Humidity
void handle_dht11() {
int cur=millis()/1000;
if (sec<cur) {
TempAndHumidity newValues = dht.getTempAndHumidity();
if (newValues.temperature != temperature) {
temperature=newValues.temperature;
Serial.println("T:" + String(newValues.temperature));
}
if (newValues.humidity != humidity) {
humidity=newValues.humidity;
Serial.println("H:" + String(newValues.humidity));
}
// Only read temperatures once every 30 seconds, regardless how long the LED effects take
sec=sec+30;
}
}
#endif
// Handle all the LED related stuff
void handle_led() {
CRGB col;
if (mysets.rgbmode==0) {
col=CRGB(mysets.col_r,mysets.col_g,mysets.col_b);
} else {
col=CHSV( gHue, 255, 255);
}
if (timer>0 && millis()/1000>timer) {
mysets.mode=0; timer=0;
}
switch (mysets.mode) {
case 0: leds.fadeToBlackBy(25); FastLED.delay(50); FastLED.show(); break;
case 1: if (mysets.endless==0) {
KITT(col);
} else {
KITT2(col,mysets.reverse==0?1:-1);
}
break;
case 2: Multi(col,mysets.reverse==0?1:-1); break;
case 3: Mirror(col); break;
case 4: pride(); break;
case 5: Constant(col); break;
case 6: ConstantFade(col); break;
case 7: sinelon(col); break;
case 8: Fire(0,-50);Fire(50,50);Fire(100,-50);Fire(150,50);Fire(200,-50);
if (NUM_LEDS>299) { Fire(250,50);} break;
case 9: Multi(col,mysets.reverse==0?1:-1); break; // with faster settings
case 10: pacifica_loop(); break;
case 11: rainbow(); break;
case 12: rainbowWithGlitter(); break;
case 13: confetti(); break;
case 14: confetti(); break; // with faster settings
case 15: bpm(); break;
case 16: juggle(); break;
}
if (mil<millis()) {
mil=millis()+mysets.hue;
gHue++; // slowly cycle the "base color" through the rainbow
led_index++;
if (led_index>NUM_LEDS) { led_index=0;}
}
}
int pos=0;
int dir=1;
// Some self developed effects
// Two streams meeting in the middle - either reversing back or repeating
void Mirror(CRGB col){
static uint8_t hue;
pos+=dir;
if (pos>NUM_LEDS/2) {
if (mysets.endless==0) {
pos=0;
} else {
dir=-1;
}
}
if (pos<1) {
dir=1;
}
leds.fadeToBlackBy(mysets.fade);
leds[pos] = col;
leds(NUM_LEDS/2,NUM_LEDS-1) = leds(NUM_LEDS/2 - 1 ,0);
FastLED.delay(256-mysets.speed);
}
// Multiple streams that get more when fade is set faster (so the LED strip is still pretty filled)
void Multi(CRGB col,int dir) {
pos+=dir;
if (pos>=NUM_LEDS) {
pos=0;
}
if (pos<0) {
pos=NUM_LEDS-1;
}
int step=NUM_LEDS/(mysets.fade/4+1);
leds.fadeToBlackBy(mysets.fade);
for (int i=0;i<NUM_LEDS;i+=step) {
leds[abs(pos+i)%(NUM_LEDS-1)]= col;
}
FastLED.delay(256-mysets.speed);
}
// Classical Knight Rider effect, but with different modes, directions, speed and colors
void KITT(CRGB col) {
pos=pos+dir;
if (pos>=NUM_LEDS-1) {
dir=-1;
}
if (pos<=0) {
dir=1;
}
leds.fadeToBlackBy(mysets.fade);
leds[pos]= col;
FastLED.delay(256-mysets.speed);
}
void KITT2(CRGB col, int dir) {
pos+=dir;
if (pos>=NUM_LEDS) {
pos=0;
}
if (pos<=-1) {
pos=NUM_LEDS-1;
}
leds.fadeToBlackBy(mysets.fade);
leds[pos]= col;
FastLED.delay(256-mysets.speed);
}
void Constant(CRGB col) {
for (int i=0;i<NUM_LEDS;i++) {
leds[i]=col;
}
FastLED.delay(mysets.speed);
}
void ConstantFade(CRGB col) {
Constant(col);
int fade_max=(mysets.brightness<FADE_MIN?FADE_MIN:mysets.brightness);
int t=(millis()*mysets.speed/2000)%(fade_max*2);
if (t>fade_max) {t=fade_max*2-t;}
if (t<FADE_MIN) {t=FADE_MIN;}
setBrightness(t);
}
// Inspired by FastLED Sinelon example, but rewritten to fill gaps for smoother output
void sinelon(CRGB col) {
fadeToBlackBy( leds, NUM_LEDS, mysets.fade);
//Divide by 50 to reduce to reasonable range
sinus(mysets.speed/10+1,0,col);
FastLED.delay(20);
// Settings Beat and Speed independently does not make much sense
// Speed could be fixed to 20 and beat from 1-255 - so speed setting could still be used
}
// remember positions for sinus-wave
int ppos[8];
int cpos[8];
// init start position
void sinus_init() {
for( int i = 0; i < 8; i++) {
ppos[i]=beatsin16( i+7, 0, NUM_LEDS );
cpos[i]=beatsin16( i+7, 0, NUM_LEDS );
}
}
// Support function for Sinus-Wave that fills the missed out LEDs - used by juggle + sinelon
void sinus(int beat, int index, CRGB col) {
ppos[index]=cpos[index];
cpos[index] = beatsin16(beat, 0, NUM_LEDS );
if (ppos[index]<cpos[index]) {
for (int i=ppos[index];i<cpos[index];i++) {
leds[i] |= col;
}
}
if (ppos[index]>=cpos[index]) {
for (int i=cpos[index];i<ppos[index];i++) {
leds[i] |= col;
}
}
}
/**** Below are effects copied from
**** http://fastled.io/docs/examples.html
**** some have been slightly modified to work in this framework
****/
void rainbow() {
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
FastLED.delay((256-mysets.speed)/3+1);
}
void rainbowWithGlitter() {
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
for (int i=0;i<NUM_LEDS/64;i++) { // Longer strip, more glitter
addGlitter(80);
}
//FastLED.show();
FastLED.show(); // No delay here as rainbow() already does it
}
void addGlitter( fract8 chanceOfGlitter) {
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void confetti() {
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, mysets.fade);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
FastLED.delay((256-mysets.speed)/3);
}
void bpm() {
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = mysets.speed/3;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for( int i = 0; i < NUM_LEDS; i++) { //9948
leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
}
FastLED.delay(mysets.speed/3);
}
// Based on https://github.com/atuline/FastLED-Demos/blob/master/juggle_pal/juggle_pal.ino
// Originally by: Mark Kriegsman
// Modified By: Andrew Tuline
// Modified to use an enhanced sinus function that fills the gaps between the dots of the wave resulting in a nicer flow
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, mysets.fade);
uint8_t dothue = 0;
for( int i = 0; i < 8; i++) {
sinus(i+7,i,CHSV( dothue, 255, 192));
dothue += 32;
}
//FastLED.show();
FastLED.delay(mysets.speed/5);
}
// From FastLED examples
// https://github.com/FastLED/FastLED/blob/master/examples/Pride2015/Pride2015.ino
void pride() {
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 1, 3000);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
CRGB newcolor = CHSV( hue8, sat8, bri8);
uint16_t pixelnumber = i;
pixelnumber = (NUM_LEDS-1) - pixelnumber;
nblend( leds[pixelnumber], newcolor, 64);
}
FastLED.delay(mysets.speed/5);
}
// Pacifica effect, simulating waves, all in blue
// December 2019, Mark Kriegsman and Mary Corey March.
// https://dmadison.github.io/FastLED/docs/_pacifica_8ino-example.html
CRGBPalette16 pacifica_palette_1 =
{ 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x14554B, 0x28AA50 };
CRGBPalette16 pacifica_palette_2 =
{ 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x0C5F52, 0x19BE5F };
CRGBPalette16 pacifica_palette_3 =
{ 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33,
0x000E39, 0x001040, 0x001450, 0x001860, 0x001C70, 0x002080, 0x1040BF, 0x2060FF };
void pacifica_loop()
{
// Increment the four "color index start" counters, one for each wave layer.
// Each is incremented at a different speed, and the speeds vary over time.
static uint16_t sCIStart1, sCIStart2, sCIStart3, sCIStart4;
static uint32_t sLastms = 0;
uint32_t ms = GET_MILLIS();
uint32_t deltams = ms - sLastms;
sLastms = ms;
uint16_t speedfactor1 = beatsin16(3, 179, 269);
uint16_t speedfactor2 = beatsin16(4, 179, 269);
uint32_t deltams1 = (deltams * speedfactor1) / 256;
uint32_t deltams2 = (deltams * speedfactor2) / 256;
uint32_t deltams21 = (deltams1 + deltams2) / 2;
sCIStart1 += (deltams1 * beatsin88(1011,10,13));
sCIStart2 -= (deltams21 * beatsin88(777,8,11));
sCIStart3 -= (deltams1 * beatsin88(501,5,7));
sCIStart4 -= (deltams2 * beatsin88(257,4,6));
// Clear out the LED array to a dim background blue-green
fill_solid( leds, NUM_LEDS, CRGB( 2, 6, 10));
// Render each of four layers, with different scales and speeds, that vary over time
pacifica_one_layer( pacifica_palette_1, sCIStart1, beatsin16( 3, 11 * 256, 14 * 256), beatsin8( 10, 70, 130), 0-beat16( 301) );
pacifica_one_layer( pacifica_palette_2, sCIStart2, beatsin16( 4, 6 * 256, 9 * 256), beatsin8( 17, 40, 80), beat16( 401) );
pacifica_one_layer( pacifica_palette_3, sCIStart3, 6 * 256, beatsin8( 9, 10,38), 0-beat16(503));
pacifica_one_layer( pacifica_palette_3, sCIStart4, 5 * 256, beatsin8( 8, 10,28), beat16(601));
// Add brighter 'whitecaps' where the waves lines up more
pacifica_add_whitecaps();
// Deepen the blues and greens a bit
pacifica_deepen_colors();
FastLED.delay((256-mysets.speed)/5);
}
// Add one layer of waves into the led array
void pacifica_one_layer( CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff)
{
uint16_t ci = cistart;
uint16_t waveangle = ioff;
uint16_t wavescale_half = (wavescale / 2) + 20;
for( uint16_t i = 0; i < NUM_LEDS; i++) {
waveangle += 250;
uint16_t s16 = sin16( waveangle ) + 32768;
uint16_t cs = scale16( s16 , wavescale_half ) + wavescale_half;
ci += cs;
uint16_t sindex16 = sin16( ci) + 32768;
uint8_t sindex8 = scale16( sindex16, 240);
CRGB c = ColorFromPalette( p, sindex8, bri, LINEARBLEND);
leds[i] += c;
}
}
// Add extra 'white' to areas where the four layers of light have lined up brightly
void pacifica_add_whitecaps()
{
uint8_t basethreshold = beatsin8( 9, 55, 65);
uint8_t wave = beat8( 7 );
for( uint16_t i = 0; i < NUM_LEDS; i++) {
uint8_t threshold = scale8( sin8( wave), 20) + basethreshold;
wave += 7;
uint8_t l = leds[i].getAverageLight();
if( l > threshold) {
uint8_t overage = l - threshold;
uint8_t overage2 = qadd8( overage, overage);
leds[i] += CRGB( overage, overage2, qadd8( overage2, overage2));
}
}
}
// Deepen the blues and greens
void pacifica_deepen_colors()
{
for( uint16_t i = 0; i < NUM_LEDS; i++) {
leds[i].blue = scale8( leds[i].blue, 145);
leds[i].green= scale8( leds[i].green, 200);
leds[i] |= CRGB( 2, 5, 7);
}
}
// Based on Fire2012 by Mark Kriegsman, July 2012
// https://dmadison.github.io/FastLED/docs/_fire2012_8ino-example.html
// Modified slightly to support multiple mirroring fires for long LED strips
#define SPARKING 120
void Fire(int start,int size_s)
{
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];
int size=abs(size_s);
// Step 1. Cool down every cell a little
for( int i = start; i < start+size; i++) {
heat[i] = qsub8( heat[i], random8(0, ((mysets.fade * 10) / size) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= size+start; k > start+2; k--) { //int k= size+start - 3; k > start; k--)
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[y+start] = qadd8( heat[y+start], random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < size; j++) {
if (size_s>0) {
leds[j+start] = HeatColor( heat[j+start]);
} else {
//Copy in reverse to create to mirroring fires when size is negative
leds[start+size-j] = HeatColor( heat[j+start]);
}
}
FastLED.delay((256-mysets.speed)/20+1);
}