Skip to content

Commit c0ab3d8

Browse files
committed
Added palette and speed fields.
1 parent 6a4c804 commit c0ab3d8

File tree

2 files changed

+54
-100
lines changed

2 files changed

+54
-100
lines changed

esp32-fastled-webserver.ino

Lines changed: 18 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#include <FS.h>
2929
#include <SPIFFS.h>
3030

31+
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001008)
32+
#warning "Requires FastLED 3.1.8 or later; check github for latest code."
33+
#endif
34+
3135
WebServer webServer(80);
3236

3337
const int led = 5;
@@ -37,52 +41,15 @@ uint8_t autoplayDuration = 10;
3741
unsigned long autoPlayTimeout = 0;
3842

3943
uint8_t currentPatternIndex = 0; // Index number of which pattern is current
44+
uint8_t currentPaletteIndex = 0;
4045
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
4146

42-
const uint8_t brightnessCount = 5;
43-
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
44-
uint8_t brightnessIndex = 0;
4547
uint8_t power = 1;
46-
uint8_t brightness = brightnessMap[brightnessIndex];
47-
48-
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
48+
uint8_t brightness = 64;
4949

50-
typedef void (*Pattern)();
51-
typedef Pattern PatternList[];
52-
typedef struct {
53-
Pattern pattern;
54-
String name;
55-
} PatternAndName;
56-
typedef PatternAndName PatternAndNameList[];
57-
58-
void rainbow();
59-
void rainbowWithGlitter();
60-
void confetti();
61-
void sinelon();
62-
void juggle();
63-
void bpm();
64-
65-
PatternAndNameList patterns = {
66-
{ rainbow, "rainbow" },
67-
{ rainbowWithGlitter, "rainbowWithGlitter" },
68-
{ confetti, "confetti" },
69-
{ sinelon, "sinelon" },
70-
{ juggle, "juggle" },
71-
{ bpm, "bpm" },
72-
};
73-
74-
const uint8_t patternCount = ARRAY_SIZE(patterns);
50+
uint8_t speed = 30;
7551

76-
#include "field.h"
77-
#include "fields.h"
78-
79-
#include "secrets.h"
80-
#include "wifi.h"
81-
#include "web.h"
82-
83-
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001008)
84-
#warning "Requires FastLED 3.1.8 or later; check github for latest code."
85-
#endif
52+
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
8653

8754
#define DATA_PIN 12
8855
//#define CLK_PIN 4
@@ -91,12 +58,20 @@ const uint8_t patternCount = ARRAY_SIZE(patterns);
9158
#define NUM_LEDS 14
9259
CRGB leds[NUM_LEDS];
9360

94-
#define BRIGHTNESS 60
9561
#define FRAMES_PER_SECOND 120
9662

9763
// -- The core to run FastLED.show()
9864
#define FASTLED_SHOW_CORE 0
9965

66+
#include "patterns.h"
67+
68+
#include "field.h"
69+
#include "fields.h"
70+
71+
#include "secrets.h"
72+
#include "wifi.h"
73+
#include "web.h"
74+
10075
// wifi ssid and password should be added to a file in the sketch named secrets.h
10176
// the secrets.h file should be added to the .gitignore file and never committed or
10277
// pushed to public source control (GitHub).
@@ -196,7 +171,7 @@ void setup() {
196171
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
197172

198173
// set master brightness control
199-
FastLED.setBrightness(BRIGHTNESS);
174+
FastLED.setBrightness(brightness);
200175

201176
int core = xPortGetCoreID();
202177
Serial.print("Main code running on core ");
@@ -244,60 +219,5 @@ void nextPattern()
244219
currentPatternIndex = (currentPatternIndex + 1) % ARRAY_SIZE( patterns);
245220
}
246221

247-
void rainbow()
248-
{
249-
// FastLED's built-in rainbow generator
250-
fill_rainbow( leds, NUM_LEDS, gHue, 7);
251-
}
252-
253-
void rainbowWithGlitter()
254-
{
255-
// built-in FastLED rainbow, plus some random sparkly glitter
256-
rainbow();
257-
addGlitter(80);
258-
}
259-
260-
void addGlitter( fract8 chanceOfGlitter)
261-
{
262-
if ( random8() < chanceOfGlitter) {
263-
leds[ random16(NUM_LEDS) ] += CRGB::White;
264-
}
265-
}
266222

267-
void confetti()
268-
{
269-
// random colored speckles that blink in and fade smoothly
270-
fadeToBlackBy( leds, NUM_LEDS, 10);
271-
int pos = random16(NUM_LEDS);
272-
leds[pos] += CHSV( gHue + random8(64), 200, 255);
273-
}
274-
275-
void sinelon()
276-
{
277-
// a colored dot sweeping back and forth, with fading trails
278-
fadeToBlackBy( leds, NUM_LEDS, 20);
279-
int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
280-
leds[pos] += CHSV( gHue, 255, 192);
281-
}
282-
283-
void bpm()
284-
{
285-
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
286-
uint8_t BeatsPerMinute = 62;
287-
CRGBPalette16 palette = PartyColors_p;
288-
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
289-
for ( int i = 0; i < NUM_LEDS; i++) { //9948
290-
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
291-
}
292-
}
293-
294-
void juggle() {
295-
// eight colored dots, weaving in and out of sync with each other
296-
fadeToBlackBy( leds, NUM_LEDS, 20);
297-
byte dothue = 0;
298-
for ( int i = 0; i < 8; i++) {
299-
leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
300-
dothue += 32;
301-
}
302-
}
303223

fields.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ String getBrightness() {
3838

3939
String setBrightness(String value) {
4040
brightness = value.toInt();
41-
if (brightness < 0) brightness = 0;
42-
else if (brightness > 255) brightness = 255;
41+
// if (brightness < 0) brightness = 0;
42+
// else if (brightness > 255) brightness = 255;
4343
FastLED.setBrightness(brightness);
4444
return String(brightness);
4545
}
@@ -67,6 +67,38 @@ String getPatterns() {
6767
return json;
6868
}
6969

70+
String getPalette() {
71+
return String(currentPaletteIndex);
72+
}
73+
74+
String setPalette(String value) {
75+
currentPaletteIndex = value.toInt();
76+
if (currentPaletteIndex < 0) currentPaletteIndex = 0;
77+
else if (currentPaletteIndex >= paletteCount) currentPaletteIndex = paletteCount - 1;
78+
return String(currentPaletteIndex);
79+
}
80+
81+
String getPalettes() {
82+
String json = "";
83+
84+
for (uint8_t i = 0; i < paletteCount; i++) {
85+
json += "\"" + paletteNames[i] + "\"";
86+
if (i < paletteCount - 1)
87+
json += ",";
88+
}
89+
90+
return json;
91+
}
92+
93+
String getSpeed() {
94+
return String(speed);
95+
}
96+
97+
String setSpeed(String value) {
98+
speed = value.toInt();
99+
return String(speed);
100+
}
101+
70102
String getAutoplay() {
71103
return String(autoplay);
72104
}
@@ -94,6 +126,8 @@ FieldList fields = {
94126
{ "power", "Power", BooleanFieldType, 0, 1, getPower, NULL, setPower },
95127
{ "brightness", "Brightness", NumberFieldType, 1, 255, getBrightness, NULL, setBrightness },
96128
{ "pattern", "Pattern", SelectFieldType, 0, patternCount, getPattern, getPatterns, setPattern },
129+
{ "palette", "Palette", SelectFieldType, 0, paletteCount, getPalette, getPalettes, setPalette },
130+
{ "speed", "Speed", NumberFieldType, 1, 255, getSpeed, NULL, setSpeed },
97131
{ "autoplaySection", "Autoplay", SectionFieldType },
98132
{ "autoplay", "Autoplay", BooleanFieldType, 0, 1, getAutoplay, NULL, setAutoplay },
99133
{ "autoplayDuration", "Autoplay Duration", NumberFieldType, 0, 255, getAutoplayDuration, NULL, setAutoplayDuration },

0 commit comments

Comments
 (0)