forked from jasoncoon/esp8266-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDream-LED.ino
1654 lines (1329 loc) · 56 KB
/
Dream-LED.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
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ESP8266 FastLED WebServer: https://github.com/hyotynen/Dream-LED
(C) Kimmo Hyötynen
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.
See <http://www.gnu.org/licenses/>.
*/
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#include <FastLED.h>
FASTLED_USING_NAMESPACE
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <FS.h>
#include <EEPROM.h>
#include "GradientPalettes.h"
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
#include "Field.h"
ESP8266WebServer webServer(80);
ESP8266HTTPUpdateServer httpUpdateServer;
#define DATA_PIN 2
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 72
#define MILLI_AMPS 3000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA). Separate PSU needed
#define FRAMES_PER_SECOND 500 // here you can control the speed. With the Access Point / Web Server the animations run a bit slower.
const bool apMode = false;
#include "Secrets.h" // this file is intentionally not included in the sketch, so nobody accidentally commits their secret information.
//char* ssid = "SSID";
//char* password = "PASSWD";
CRGB leds[NUM_LEDS];
const uint8_t brightnessCount = 5;
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
uint8_t brightnessIndex = 0;
uint8_t speed = 30; // 1...255
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( CRGB::Black);
uint8_t currentPatternIndex = 0; // Index number of which pattern is current
uint8_t currentPaletteIndex = 0; // Index number of which pattern is current
uint8_t glitter = 0;
uint8_t strobe = 0;
bool strobePulse = false;
uint8_t microphone = 0;
uint8_t autoplay = 0;
uint8_t autocolor = 0;
uint8_t autoplayDuration = 10;
unsigned long autoPlayTimeout = 0;
uint8_t autocolorDuration = 10;
unsigned long autocolorTimeout = 0;
uint8_t gHue = 0; // rotating "base color" used by the patterns
CRGB solidColor = CRGB::Blue;
// scale the brightness of all pixels down
void dimAll(byte value)
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(value);
}
}
// Sound handling and most of the sound reactive effects By: Andrew Tuline
#define MIC_PIN A0 // Analog port for microphone
uint8_t squelch = 5; // Anything below this is background noise, so we'll make it '0'.
int sample; // Current sample.
float sampleAvg = 0; // Smoothed Average.
float micLev = 0; // Used to convert returned value to have '0' as minimum.
uint8_t maxVol = 11; // Reasonable value for constant volume for 'peak detector', as it won't always trigger.
bool samplePeak = 0; // Boolean flag for peak. Responding routine must reset this flag.
int sampleAgc, multAgc;
uint8_t targetAgc = 60; // This is our setPoint at 20% of max for the adjusted output.
typedef void (*Pattern)();
typedef Pattern PatternList[];
typedef struct {
Pattern pattern;
String name;
} PatternAndName;
typedef PatternAndName PatternAndNameList[];
// List of patterns to cycle through. Each is defined as a separate function below.
PatternAndNameList patterns = {
{ soundBlocks, "Blocks" },
{ bpm, "BPM" },
{ breathing, "Breathing" },
{ rainbowSolid, "Changing solid" },
{ colorWaves, "Color waves" },
{ confetti, "Confetti" },
{ fire, "Fire" },
{ rainbow, "Gradient" },
{ heartBeat, "Heart beat" },
{ juggle, "Juggle" },
{ rain, "Rain" },
{ runningLights, "Running lights" },
{ shootingStar, "Shooting star" },
{ sinelon, "Sinelon" },
{ discostrobe, "Strobe" },
{ theaterChase, "Theater" },
{ soundGradient, "(Sound) Brightness" },
{ confettiSound, "(Sound) Confetti" },
{ firewide, "(Sound) Fire" },
{ jugglep, "(Sound) Juggle" },
{ rainbowpeak, "(Sound) Lightning" },
{ matrix, "(Sound) Matrix" },
{ fillnoise, "(Sound) Noise" },
{ noisewide, "(Sound) Noise 2" },
{ sinephase, "(Sound) Phase" },
{ pixelblend, "(Sound) Pixels" },
{ plasma, "(Sound) Plasma" },
{ plasma2, "(Sound) Plasma 2" },
{ ripple, "(Sound) Ripple" },
{ onesine, "(Sound) Sine wave" },
{ myvumeter, "(Sound) VU meter" },
{ showSolidColor, "Solid Color" } // This should be last
};
const uint8_t patternCount = ARRAY_SIZE(patterns);
typedef struct {
CRGBPalette16 palette;
String name;
} PaletteAndName;
typedef PaletteAndName PaletteAndNameList[];
const CRGBPalette16 palettes[] = {
GMT_gray_gp,
Blues_06_gp,
ib_jul01_gp,
christmas_candy_gp,
CloudColors_p,
GMT_drywet_gp,
elevation_gp,
es_emerald_dragon_08_gp,
ForestColors_p,
Fuschia_7_gp,
gr65_hult_gp,
HeatColors_p,
LavaColors_p,
OceanColors_p,
PartyColors_p,
bhw4_029_gp,
es_pinksplash_08_gp,
rgi_15_gp,
RainbowColors_p,
RainbowStripeColors_p,
ramp_gp,
rainbowsherbet_gp,
subtle_gp,
bhw1_07_gp,
ib15_gp,
Sunset_Real_gp,
bhw1_26_gp,
bhw1_06_gp,
es_vintage_57_gp
};
const uint8_t paletteCount = ARRAY_SIZE(palettes);
const String paletteNames[paletteCount] = {
"Black & white",
"Blues",
"Christmas",
"Christmas candy",
"Cloud",
"Dry & wet",
"Elevation",
"Emerald",
"Forest",
"Fuschia",
"Grove",
"Heat",
"Lava",
"Ocean",
"Party",
"Pastel",
"Pink splash",
"Purple",
"Rainbow",
"Rainbow stripes",
"Ramp",
"Sherbet",
"Subtle",
"Sun",
"Sunrise",
"Sunset",
"Tricolor",
"Turquoise",
"Vintage"
};
#include "Fields.h"
void setup() {
WiFi.setSleepMode(WIFI_NONE_SLEEP);
Serial.begin(115200);
delay(100);
Serial.setDebugOutput(true);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // for WS2812 (Neopixel)
FastLED.setDither(false);
FastLED.setCorrection(TypicalLEDStrip);
FastLED.setBrightness(brightness);
FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
EEPROM.begin(512);
loadSettings();
FastLED.setBrightness(brightness);
Serial.println();
Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
Serial.print( F("Boot Vers: ") ); Serial.println(system_get_boot_version());
Serial.print( F("CPU: ") ); Serial.println(system_get_cpu_freq());
Serial.print( F("SDK: ") ); Serial.println(system_get_sdk_version());
Serial.print( F("Chip ID: ") ); Serial.println(system_get_chip_id());
Serial.print( F("Flash ID: ") ); Serial.println(spi_flash_get_id());
Serial.print( F("Flash Size: ") ); Serial.println(ESP.getFlashChipRealSize());
Serial.print( F("Vcc: ") ); Serial.println(ESP.getVcc());
Serial.println();
SPIFFS.begin();
{
Serial.println("SPIFFS contents:");
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), String(fileSize).c_str());
}
Serial.printf("\n");
}
if (apMode)
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
Serial.printf("Connect to Wi-Fi access point: %s\n", AP_NameChar);
Serial.println("and open http://192.168.4.1 in your browser");
}
else
{
WiFi.mode(WIFI_STA);
Serial.printf("Connecting to %s\n", ssid);
if (String(WiFi.SSID()) != String(ssid)) {
WiFi.begin(ssid, password);
}
}
httpUpdateServer.setup(&webServer);
webServer.on("/all", HTTP_GET, []() {
String json = getFieldsJson(fields, fieldCount);
webServer.send(200, "text/json", json);
});
webServer.on("/fieldValue", HTTP_GET, []() {
String name = webServer.arg("name");
String value = getFieldValue(name, fields, fieldCount);
webServer.send(200, "text/json", value);
});
webServer.on("/fieldValue", HTTP_POST, []() {
String name = webServer.arg("name");
String value = webServer.arg("value");
String newValue = setFieldValue(name, value, fields, fieldCount);
webServer.send(200, "text/json", newValue);
});
webServer.on("/power", HTTP_POST, []() {
String value = webServer.arg("value");
setPower(value.toInt());
sendInt(power);
});
webServer.on("/speed", HTTP_POST, []() {
String value = webServer.arg("value");
speed = value.toInt();
broadcastInt("speed", speed);
sendInt(speed);
});
webServer.on("/solidColor", HTTP_POST, []() {
String r = webServer.arg("r");
String g = webServer.arg("g");
String b = webServer.arg("b");
setSolidColor(r.toInt(), g.toInt(), b.toInt());
sendString(String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b));
});
webServer.on("/pattern", HTTP_POST, []() {
String value = webServer.arg("value");
setPattern(value.toInt());
sendInt(currentPatternIndex);
});
webServer.on("/patternName", HTTP_POST, []() {
String value = webServer.arg("value");
setPatternName(value);
sendInt(currentPatternIndex);
});
webServer.on("/palette", HTTP_POST, []() {
String value = webServer.arg("value");
setPalette(value.toInt());
sendInt(currentPaletteIndex);
});
webServer.on("/paletteName", HTTP_POST, []() {
String value = webServer.arg("value");
setPaletteName(value);
sendInt(currentPaletteIndex);
});
webServer.on("/brightness", HTTP_POST, []() {
String value = webServer.arg("value");
setBrightness(value.toInt());
sendInt(brightness);
});
webServer.on("/glitter", HTTP_POST, []() {
String value = webServer.arg("value");
setGlitter(value.toInt());
sendInt(glitter);
});
webServer.on("/strobe", HTTP_POST, []() {
String value = webServer.arg("value");
setStrobe(value.toInt());
sendInt(strobe);
});
webServer.on("/microphone", HTTP_POST, []() {
String value = webServer.arg("value");
setMicrophone(value.toInt());
sendInt(microphone);
});
webServer.on("/autoplay", HTTP_POST, []() {
String value = webServer.arg("value");
setAutoplay(value.toInt());
sendInt(autoplay);
});
webServer.on("/autoplayDuration", HTTP_POST, []() {
String value = webServer.arg("value");
setAutoplayDuration(value.toInt());
sendInt(autoplayDuration);
});
webServer.on("/autocolor", HTTP_POST, []() {
String value = webServer.arg("value");
setAutocolor(value.toInt());
sendInt(autocolor);
});
webServer.on("/autocolorDuration", HTTP_POST, []() {
String value = webServer.arg("value");
setAutocolorDuration(value.toInt());
sendInt(autocolorDuration);
});
webServer.serveStatic("/", SPIFFS, "/", "max-age=86400");
webServer.begin();
Serial.println("HTTP web server started");
autoPlayTimeout = millis() + (autoplayDuration * 1000);
}
void sendInt(uint8_t value)
{
sendString(String(value));
}
void sendString(String value)
{
webServer.send(200, "text/plain", value);
}
void broadcastInt(String name, uint8_t value)
{
String json = "{\"name\":\"" + name + "\",\"value\":" + String(value) + "}";
// webSocketsServer.broadcastTXT(json);
}
void broadcastString(String name, String value)
{
String json = "{\"name\":\"" + name + "\",\"value\":\"" + String(value) + "\"}";
}
void loop() {
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(65535));
webServer.handleClient();
if (power == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
return;
}
static bool hasConnected = false;
EVERY_N_SECONDS(1) {
if (WiFi.status() != WL_CONNECTED) {
// Serial.printf("Connecting to %s\n", ssid);
hasConnected = false;
}
else if (!hasConnected) {
hasConnected = true;
Serial.print("Connected! Open http://");
Serial.print(WiFi.localIP());
Serial.println(" in your browser");
}
}
EVERY_N_MILLISECONDS(40) {
// Blend the current palette to the next
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 30); // 1...48, 1=slowest
gHue++; // slowly cycle the "base color" through the rainbow
}
if (autoplay && (millis() > autoPlayTimeout)) {
if (microphone) setPattern(random(16, patternCount-1)); // pick random sound reactive pattern
else setPattern(random(patternCount-1)); // pick random, but not solid color
autoPlayTimeout = millis() + (autoplayDuration * 1000);
}
if (autocolor && (millis() > autocolorTimeout)) {
setPalette(random(paletteCount)); // pick random
autocolorTimeout = millis() + (autocolorDuration * 1000);
}
// Call the current pattern function once, updating the 'leds' array
patterns[currentPatternIndex].pattern();
if (glitter) {
addGlitter(80);
}
EVERY_N_MILLISECONDS(100) {
if (strobe) {
if (strobePulse) {
FastLED.setBrightness(brightness);
strobePulse = false;
}
else {
FastLED.setBrightness(0);
strobePulse = true;
}
}
}
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
void getSample() {
int16_t micIn; // Current sample starts with negative values and large values, which is why it's 16 bit signed.
static long peakTime;
if (microphone) micIn = analogRead(MIC_PIN); // Poor man's analog Read.
else micIn = beatsin8( 240, random(60), random(60)+50) + beatsin8( 480, random(70), random(70)+50) - beatsin8(1000,0,250) ; // Faking the audio signal used when microphone is turned off
micLev = ((micLev * 31) + micIn) / 32; // Smooth it out over the last 32 samples for automatic centering.
micIn -= micLev; // Let's center it to 0 now.
micIn = abs(micIn); // And get the absolute value of each sample.
sample = (micIn <= squelch) ? 0 : (sample + micIn) / 2; // Using a ternary operator, the resultant sample is either 0 or it's a bit smoothed out with the last sample.
sampleAvg = ((sampleAvg * 31) + sample) / 32; // Smooth it out over the last 32 samples.
if (sample > (sampleAvg+maxVol) && millis() > (peakTime + 50)) { // Poor man's beat detection by seeing if sample > Average + some value.
samplePeak = 1; // Then we got a peak, else we don't. Display routines need to reset the samplepeak value in case they miss the trigger.
peakTime=millis();
}
}
void agcAvg() { // A simple averaging multiplier to automatically adjust sound sensitivity.
multAgc = (sampleAvg < 1) ? targetAgc : targetAgc / sampleAvg; // Make the multiplier so that sampleAvg * multiplier = setpoint
sampleAgc = sample * multAgc;
if (sampleAgc > 255) sampleAgc = 255;
}
void loadSettings()
{
brightness = EEPROM.read(0);
currentPatternIndex = EEPROM.read(1);
if (currentPatternIndex < 0)
currentPatternIndex = 0;
else if (currentPatternIndex >= patternCount)
currentPatternIndex = patternCount - 1;
byte r = EEPROM.read(2);
byte g = EEPROM.read(3);
byte b = EEPROM.read(4);
if (r == 0 && g == 0 && b == 0)
{
}
else
{
solidColor = CRGB(r, g, b);
}
power = EEPROM.read(5);
autoplay = EEPROM.read(6);
autoplayDuration = EEPROM.read(7);
currentPaletteIndex = EEPROM.read(8);
if (currentPaletteIndex < 0)
currentPaletteIndex = 0;
else if (currentPaletteIndex >= paletteCount)
currentPaletteIndex = paletteCount - 1;
gCurrentPalette = palettes[currentPaletteIndex];
gTargetPalette = palettes[currentPaletteIndex];
glitter = EEPROM.read(9);
autocolor = EEPROM.read(10);
autocolorDuration = EEPROM.read(11);
strobe = EEPROM.read(12);
microphone = EEPROM.read(13);
}
void setPower(uint8_t value)
{
power = value == 0 ? 0 : 1;
EEPROM.write(5, power);
EEPROM.commit();
broadcastInt("power", power);
}
void setGlitter(uint8_t value)
{
glitter = value == 0 ? 0 : 1;
EEPROM.write(9, glitter);
EEPROM.commit();
broadcastInt("glitter", glitter);
}
void setStrobe(uint8_t value)
{
strobe = value == 0 ? 0 : 1;
if (strobe == 0) FastLED.setBrightness(brightness);
EEPROM.write(12, strobe);
EEPROM.commit();
broadcastInt("strobe", strobe);
}
void setMicrophone(uint8_t value)
{
microphone = value == 0 ? 0 : 1;
EEPROM.write(13, microphone);
EEPROM.commit();
broadcastInt("microphone", microphone);
}
void setAutoplay(uint8_t value)
{
autoplay = value == 0 ? 0 : 1;
EEPROM.write(6, autoplay);
EEPROM.commit();
broadcastInt("autoplay", autoplay);
}
void setAutoplayDuration(uint8_t value)
{
autoplayDuration = value;
EEPROM.write(7, autoplayDuration);
EEPROM.commit();
autoPlayTimeout = millis() + (autoplayDuration * 1000);
broadcastInt("autoplayDuration", autoplayDuration);
}
void setAutocolor(uint8_t value)
{
autocolor = value == 0 ? 0 : 1;
EEPROM.write(10, autocolor);
EEPROM.commit();
broadcastInt("autocolor", autocolor);
}
void setAutocolorDuration(uint8_t value)
{
autocolorDuration = value;
EEPROM.write(11, autocolorDuration);
EEPROM.commit();
autocolorTimeout = millis() + (autocolorDuration * 1000);
broadcastInt("autocolorDuration", autocolorDuration);
}
void setSolidColor(CRGB color)
{
setSolidColor(color.r, color.g, color.b);
}
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
{
solidColor = CRGB(r, g, b);
EEPROM.write(2, r);
EEPROM.write(3, g);
EEPROM.write(4, b);
EEPROM.commit();
setPattern(patternCount - 1); // Must match the place in the list of patterns
broadcastString("color", String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b));
}
// increase or decrease the current pattern number, and wrap around at the ends
void adjustPattern(bool up)
{
if (up)
currentPatternIndex++;
else
currentPatternIndex--;
// wrap around at the ends
if (currentPatternIndex < 0)
currentPatternIndex = patternCount - 1;
if (currentPatternIndex >= patternCount)
currentPatternIndex = 0;
if (autoplay == 0) {
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
broadcastInt("pattern", currentPatternIndex);
}
void setPattern(uint8_t value)
{
if (value >= patternCount)
value = patternCount - 1;
currentPatternIndex = value;
FastLED.setBrightness(brightness); // Reset brightness back if some pattern has adjusted it
if (autoplay == 0) {
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
broadcastInt("pattern", currentPatternIndex);
}
void setPatternName(String name)
{
for (uint8_t i = 0; i < patternCount; i++) {
if (patterns[i].name == name) {
setPattern(i);
break;
}
}
}
void setPalette(uint8_t value)
{
if (value >= paletteCount)
value = paletteCount - 1;
currentPaletteIndex = value;
gTargetPalette = palettes[currentPaletteIndex];
if (autocolor == 0) {
EEPROM.write(8, currentPaletteIndex);
EEPROM.commit();
}
broadcastInt("palette", currentPaletteIndex);
}
void setPaletteName(String name)
{
for (uint8_t i = 0; i < paletteCount; i++) {
if (paletteNames[i] == name) {
setPalette(i);
break;
}
}
}
void adjustBrightness(bool up)
{
if (up && brightnessIndex < brightnessCount - 1)
brightnessIndex++;
else if (!up && brightnessIndex > 0)
brightnessIndex--;
brightness = brightnessMap[brightnessIndex];
FastLED.setBrightness(brightness);
EEPROM.write(0, brightness);
EEPROM.commit();
broadcastInt("brightness", brightness);
}
void setBrightness(uint8_t value)
{
if (value > 255)
value = 255;
else if (value < 0) value = 0;
brightness = value;
FastLED.setBrightness(brightness);
EEPROM.write(0, brightness);
EEPROM.commit();
broadcastInt("brightness", brightness);
}
void addGlitter( uint8_t chanceOfGlitter)
{
if ( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void waveit() { // Shifting pixels from the center to the left and right.
for (int i = NUM_LEDS - 1; i > NUM_LEDS/2; i--) { // Move to the right.
leds[i] = leds[i - 1];
}
for (int i = 0; i < NUM_LEDS/2; i++) { // Move to the left.
leds[i] = leds[i + 1];
}
}
//
// PATTERNS
//
void showSolidColor()
{
fill_solid(leds, NUM_LEDS, solidColor);
}
// Patterns modified from FastLED example DemoReel100: https://github.com/FastLED/FastLED/blob/master/examples/DemoReel100/DemoReel100.ino
void rainbow()
{
gHue = (int) gHue + speed / 20;
fill_palette( leds, NUM_LEDS, gHue, 6, gCurrentPalette, 255, LINEARBLEND);
}
void rainbowSolid()
{
gHue = (int) gHue + speed / 20;
CRGB color = ColorFromPalette(gCurrentPalette, gHue, 255);
fill_solid(leds, NUM_LEDS, color);
}
void confetti()
{
gHue = (int) gHue + speed / 20;
fadeToBlackBy( leds, NUM_LEDS, speed / 4);
int pos = random16(NUM_LEDS);
leds[pos] += ColorFromPalette(gCurrentPalette, gHue + random8(64));
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(speed, 0, NUM_LEDS);
static int prevpos = 0;
CRGB color = ColorFromPalette(gCurrentPalette, gHue, 255);
if ( pos < prevpos ) {
fill_solid( leds + pos, (prevpos - pos) + 1, color);
} else {
fill_solid( leds + prevpos, (pos - prevpos) + 1, color);
}
prevpos = pos;
}
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t beat = beatsin8( speed, 64, 255);
CRGBPalette16 palette = gCurrentPalette;
for ( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
}
}
void juggle()
{
static uint8_t numdots = 4; // Number of dots in use.
static uint8_t faderate = 2; // How long should the trails be. Very low value = longer trails.
static uint8_t basebeat = (int) speed / 2.7 + 0.5; // Higher = faster movement.
static uint8_t lastSecond = 99; // Static variable, means it's only defined once. This is our 'debounce' variable.
uint8_t secondHand = (millis() / 1000) % 30; // IMPORTANT!!! Change '30' to a different value to change duration of the loop.
if (lastSecond != secondHand) { // Debounce to make sure we're not repeating an assignment.
lastSecond = secondHand;
switch (secondHand) {
case 0: numdots = 1; basebeat = (int) speed / 3 + 2; faderate = 2; break; // You can change values here, one at a time , or altogether.
case 10: numdots = 4; basebeat = (int) speed / 2.6 + 3; faderate = 8; break;
case 20: numdots = 8; basebeat = (int) speed / 3.2 + 4; faderate = 8; break; // Only gets called once, and not continuously for the next several seconds. Therefore, no rainbows.
case 30: break;
}
}
// Several colored dots, weaving in and out of sync with each other
fadeToBlackBy(leds, NUM_LEDS, faderate);
for ( int i = 0; i < numdots; i++) {
//beat16 is a FastLED 3.1 function
leds[beatsin16(basebeat + i + numdots, 0, NUM_LEDS)] += ColorFromPalette(gCurrentPalette, gHue, 255);
}
}
// based on FastLED example Fire2012WithPalette: https://github.com/FastLED/FastLED/blob/master/examples/Fire2012WithPalette/Fire2012WithPalette.ino
void fire()
{
CRGBPalette16 palette = gCurrentPalette;
uint8_t cooling = 50;
uint8_t sparking = speed;
bool up = true;
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(256));
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];
byte colorindex;
// Step 1. Cool down every cell a little
for ( uint16_t i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((cooling * 10) / NUM_LEDS) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for ( uint16_t k = NUM_LEDS - 1; k >= 2; 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] = qadd8( heat[y], random8(160, 255) );
}
// Step 4. Map from heat cells to LED colors
for ( uint16_t j = 0; j < NUM_LEDS; j++) {
// Scale the heat value from 0-255 down to 0-240
// for best results with color palettes.
colorindex = scale8(heat[j], 190);
CRGB color = ColorFromPalette(palette, colorindex);
if (up) {
leds[j] = color;
}
else {
leds[(NUM_LEDS - 1) - j] = color;
}
}
}
// ColorWavesWithPalettes by Mark Kriegsman: https://gist.github.com/kriegsman/8281905786e8b2632aeb
// This function draws color waves with an ever-changing,
// widely-varying set of parameters, using a color palette.
void colorWaves()
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( speed*64, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(speed*32, 300, 1500);
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;
uint16_t h16_128 = hue16 >> 7;
if ( h16_128 & 0x100) {
hue8 = 255 - (h16_128 >> 1);
} else {
hue8 = h16_128 >> 1;
}
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);
uint8_t index = hue8;
index = scale8( index, 240);
CRGB newcolor = ColorFromPalette( gCurrentPalette, index, bri8);
uint16_t pixelnumber = i;
pixelnumber = (NUM_LEDS - 1) - pixelnumber;
nblend( leds[pixelnumber], newcolor, 128);
}
}
void discostrobe()
{
// First, we black out all the LEDs
fill_solid( leds, NUM_LEDS, CRGB::Black);
delayToSyncFrameRate( speed);
// To achive the strobe effect, we actually only draw lit pixels
// every Nth frame (e.g. every 4th frame).
// sStrobePhase is a counter that runs from zero to kStrobeCycleLength-1,
// and then resets to zero.
const uint8_t kStrobeCycleLength = 4; // light every Nth frame
static uint8_t sStrobePhase = 0;
sStrobePhase = sStrobePhase + 1;
if( sStrobePhase >= kStrobeCycleLength ) {