Skip to content

Commit 15352a5

Browse files
committed
Added color, palette and speed fields.
1 parent c0ab3d8 commit 15352a5

File tree

5 files changed

+191
-6
lines changed

5 files changed

+191
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ Control addressable LEDs with an ESP32 via a web browser over Wi-Fi.
1414
* [x] pattern
1515
* [x] autoplay
1616
* [x] autoplay duration
17+
* [x] speed
18+
* [x] palette
1719

1820
### Currently Lacking:
1921
* [ ] Setting storage in EEPROM
2022
* [ ] More patterns
2123
* [ ] More parameters
22-
* [ ] speed
2324
* [ ] solid color
2425
* [ ] fire cooling/sparking
2526
* [ ] twinkle speed/density

data/js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function postValue(name, value) {
407407

408408
var body = { name: name, value: value };
409409

410-
$.post(urlBase + "fieldValue?" + name + "=" + value, body, function(data) {
410+
$.post(urlBase + "fieldValue?name=" + name + "&value=" + value, body, function(data) {
411411
if (data.name != null) {
412412
$("#status").html("Set " + name + ": " + data.name);
413413
} else {
@@ -428,7 +428,7 @@ function postColor(name, value) {
428428

429429
var body = { name: name, r: value.r, g: value.g, b: value.b };
430430

431-
$.post(urlBase + name + "?r=" + value.r + "&g=" + value.g + "&b=" + value.b, body, function(data) {
431+
$.post(urlBase + "fieldValue?name=" + name + "&value=" + "&r=" + value.r + "&g=" + value.g + "&b=" + value.b, body, function(data) {
432432
$("#status").html("Set " + name + ": " + data);
433433
})
434434
.fail(function(textStatus, errorThrown) { $("#status").html("Fail: " + textStatus + " " + errorThrown); });

esp32-fastled-webserver.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ uint8_t brightness = 64;
4949

5050
uint8_t speed = 30;
5151

52+
CRGB solidColor = CRGB::Blue;
53+
5254
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
5355

5456
#define DATA_PIN 12

fields.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,23 @@ String getPattern() {
4848
return String(currentPatternIndex);
4949
}
5050

51+
void setPattern(uint8_t value)
52+
{
53+
if (value >= patternCount)
54+
value = patternCount - 1;
55+
56+
currentPatternIndex = value;
57+
58+
// if (autoplay == 0) {
59+
// EEPROM.write(1, currentPatternIndex);
60+
// EEPROM.commit();
61+
// }
62+
63+
// broadcastInt("pattern", currentPatternIndex);
64+
}
65+
5166
String setPattern(String value) {
52-
currentPatternIndex = value.toInt();
53-
if (currentPatternIndex < 0) currentPatternIndex = 0;
54-
else if (currentPatternIndex >= patternCount) currentPatternIndex = patternCount - 1;
67+
setPattern(value.toInt());
5568
return String(currentPatternIndex);
5669
}
5770

@@ -122,6 +135,37 @@ String setAutoplayDuration(String value) {
122135
return String(autoplayDuration);
123136
}
124137

138+
String getSolidColor() {
139+
return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b);
140+
}
141+
142+
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
143+
{
144+
solidColor = CRGB(r, g, b);
145+
146+
// EEPROM.write(2, r);
147+
// EEPROM.write(3, g);
148+
// EEPROM.write(4, b);
149+
// EEPROM.commit();
150+
151+
setPattern(patternCount - 1);
152+
153+
// broadcastString("color", String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b));
154+
}
155+
156+
void setSolidColor(CRGB color)
157+
{
158+
setSolidColor(color.r, color.g, color.b);
159+
}
160+
161+
String setSolidColor(String value) {
162+
String r = webServer.arg("r");
163+
String g = webServer.arg("g");
164+
String b = webServer.arg("b");
165+
setSolidColor(r.toInt(), g.toInt(), b.toInt());
166+
return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b);
167+
}
168+
125169
FieldList fields = {
126170
{ "power", "Power", BooleanFieldType, 0, 1, getPower, NULL, setPower },
127171
{ "brightness", "Brightness", NumberFieldType, 1, 255, getBrightness, NULL, setBrightness },
@@ -131,6 +175,8 @@ FieldList fields = {
131175
{ "autoplaySection", "Autoplay", SectionFieldType },
132176
{ "autoplay", "Autoplay", BooleanFieldType, 0, 1, getAutoplay, NULL, setAutoplay },
133177
{ "autoplayDuration", "Autoplay Duration", NumberFieldType, 0, 255, getAutoplayDuration, NULL, setAutoplayDuration },
178+
{ "solidColorSection", "Solid Color", SectionFieldType },
179+
{ "solidColor", "Color", ColorFieldType, 0, 255, getSolidColor, NULL, setSolidColor },
134180
};
135181

136182
uint8_t fieldCount = ARRAY_SIZE(fields);

patterns.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
ESP32 FastLED WebServer: https://github.com/jasoncoon/esp32-fastled-webserver
3+
Copyright (C) 2017 Jason Coon
4+
5+
Built upon the amazing FastLED work of Daniel Garcia and Mark Kriegsman:
6+
https://github.com/FastLED/FastLED
7+
8+
ESP32 support provided by the hard work of Sam Guyer:
9+
https://github.com/samguyer/FastLED
10+
11+
This program is free software: you can redistribute it and/or modify
12+
it under the terms of the GNU General Public License as published by
13+
the Free Software Foundation, either version 3 of the License, or
14+
(at your option) any later version.
15+
16+
This program is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
GNU General Public License for more details.
20+
21+
You should have received a copy of the GNU General Public License
22+
along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
const CRGBPalette16 palettes[] = {
25+
RainbowColors_p,
26+
RainbowStripeColors_p,
27+
CloudColors_p,
28+
LavaColors_p,
29+
OceanColors_p,
30+
ForestColors_p,
31+
PartyColors_p,
32+
HeatColors_p
33+
};
34+
35+
const uint8_t paletteCount = ARRAY_SIZE(palettes);
36+
37+
const String paletteNames[paletteCount] = {
38+
"Rainbow",
39+
"Rainbow Stripe",
40+
"Cloud",
41+
"Lava",
42+
"Ocean",
43+
"Forest",
44+
"Party",
45+
"Heat",
46+
};
47+
48+
void rainbow()
49+
{
50+
// FastLED's built-in rainbow generator
51+
fill_rainbow( leds, NUM_LEDS, gHue, speed);
52+
}
53+
54+
void addGlitter( fract8 chanceOfGlitter)
55+
{
56+
if ( random8() < chanceOfGlitter) {
57+
leds[ random16(NUM_LEDS) ] += CRGB::White;
58+
}
59+
}
60+
61+
void rainbowWithGlitter()
62+
{
63+
// built-in FastLED rainbow, plus some random sparkly glitter
64+
rainbow();
65+
addGlitter(80);
66+
}
67+
68+
void confetti()
69+
{
70+
// random colored speckles that blink in and fade smoothly
71+
fadeToBlackBy( leds, NUM_LEDS, 10);
72+
int pos = random16(NUM_LEDS);
73+
leds[pos] += CHSV( gHue + random8(64), 200, 255);
74+
}
75+
76+
void sinelon()
77+
{
78+
// a colored dot sweeping back and forth, with fading trails
79+
fadeToBlackBy( leds, NUM_LEDS, 20);
80+
int pos = beatsin16(speed, 0, NUM_LEDS - 1);
81+
static int prevpos = 0;
82+
CRGB color = ColorFromPalette(palettes[currentPaletteIndex], gHue, 255);
83+
if ( pos < prevpos ) {
84+
fill_solid( leds + pos, (prevpos - pos) + 1, color);
85+
} else {
86+
fill_solid( leds + prevpos, (pos - prevpos) + 1, color);
87+
}
88+
prevpos = pos;
89+
}
90+
91+
void bpm()
92+
{
93+
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
94+
uint8_t beat = beatsin8( speed, 64, 255);
95+
CRGBPalette16 palette = palettes[currentPaletteIndex];
96+
for ( int i = 0; i < NUM_LEDS; i++) {
97+
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
98+
}
99+
}
100+
101+
void juggle() {
102+
// eight colored dots, weaving in and out of sync with each other
103+
fadeToBlackBy( leds, NUM_LEDS, 20);
104+
byte dothue = 0;
105+
for ( int i = 0; i < 8; i++) {
106+
leds[beatsin16( i + speed, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
107+
dothue += 32;
108+
}
109+
}
110+
111+
void showSolidColor()
112+
{
113+
fill_solid(leds, NUM_LEDS, solidColor);
114+
}
115+
116+
typedef void (*Pattern)();
117+
typedef Pattern PatternList[];
118+
typedef struct {
119+
Pattern pattern;
120+
String name;
121+
} PatternAndName;
122+
typedef PatternAndName PatternAndNameList[];
123+
124+
PatternAndNameList patterns = {
125+
{ rainbow, "rainbow" },
126+
{ rainbowWithGlitter, "rainbowWithGlitter" },
127+
{ confetti, "confetti" },
128+
{ sinelon, "sinelon" },
129+
{ juggle, "juggle" },
130+
{ bpm, "bpm" },
131+
132+
{ showSolidColor, "Solid Color" },
133+
};
134+
135+
const uint8_t patternCount = ARRAY_SIZE(patterns);
136+

0 commit comments

Comments
 (0)