Added palette selector

* Added palette selector and supporting code.
* Switched several patterns to use the selected palette: Confetti,
Sinelon, BPM, Juggle
* Limit EEPROM writes in autoplay mode.
This commit is contained in:
Jason Coon 2017-02-18 13:48:33 -06:00
parent d8149e2fd3
commit f04a089fa4
2 changed files with 109 additions and 18 deletions

View File

@ -57,6 +57,22 @@ String getPatterns() {
return json; return json;
} }
String getPalette() {
return String(currentPaletteIndex);
}
String getPalettes() {
String json = "";
for (uint8_t i = 0; i < paletteCount; i++) {
json += "\"" + paletteNames[i] + "\"";
if (i < paletteCount - 1)
json += ",";
}
return json;
}
String getAutoplay() { String getAutoplay() {
return String(autoplay); return String(autoplay);
} }
@ -93,6 +109,7 @@ FieldList fields = {
{ "power", "Power", BooleanFieldType, 0, 1, getPower }, { "power", "Power", BooleanFieldType, 0, 1, getPower },
{ "brightness", "Brightness", NumberFieldType, 1, 255, getBrightness }, { "brightness", "Brightness", NumberFieldType, 1, 255, getBrightness },
{ "pattern", "Pattern", SelectFieldType, 0, patternCount, getPattern, getPatterns }, { "pattern", "Pattern", SelectFieldType, 0, patternCount, getPattern, getPatterns },
{ "palette", "Palette", SelectFieldType, 0, paletteCount, getPalette, getPalettes },
{ "speed", "Speed", NumberFieldType, 1, 255, getSpeed }, { "speed", "Speed", NumberFieldType, 1, 255, getSpeed },
{ "autoplay", "Autoplay", SectionFieldType }, { "autoplay", "Autoplay", SectionFieldType },
{ "autoplay", "Autoplay", BooleanFieldType, 0, 1, getAutoplay }, { "autoplay", "Autoplay", BooleanFieldType, 0, 1, getAutoplay },

View File

@ -59,9 +59,9 @@ ESP8266HTTPUpdateServer httpUpdateServer;
#include "FSBrowser.h" #include "FSBrowser.h"
#define DATA_PIN D7 #define DATA_PIN D8
#define LED_TYPE WS2811 #define LED_TYPE WS2811
#define COLOR_ORDER RGB #define COLOR_ORDER GRB
#define NUM_LEDS 24 #define NUM_LEDS 24
#define MILLI_AMPS 2000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA) #define MILLI_AMPS 2000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA)
@ -69,8 +69,6 @@ ESP8266HTTPUpdateServer httpUpdateServer;
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
uint8_t patternIndex = 0;
const uint8_t brightnessCount = 5; const uint8_t brightnessCount = 5;
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 }; uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
uint8_t brightnessIndex = 0; uint8_t brightnessIndex = 0;
@ -111,6 +109,8 @@ uint8_t autoplay = 0;
uint8_t autoplayDuration = 10; uint8_t autoplayDuration = 10;
unsigned long autoPlayTimeout = 0; unsigned long autoPlayTimeout = 0;
uint8_t currentPaletteIndex = 0;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns uint8_t gHue = 0; // rotating "base color" used by many of the patterns
CRGB solidColor = CRGB::Blue; CRGB solidColor = CRGB::Blue;
@ -177,6 +177,36 @@ PatternAndNameList patterns = {
const uint8_t patternCount = ARRAY_SIZE(patterns); const uint8_t patternCount = ARRAY_SIZE(patterns);
typedef struct {
CRGBPalette16 palette;
String name;
} PaletteAndName;
typedef PaletteAndName PaletteAndNameList[];
const CRGBPalette16 palettes[] = {
RainbowColors_p,
RainbowStripeColors_p,
CloudColors_p,
LavaColors_p,
OceanColors_p,
ForestColors_p,
PartyColors_p,
HeatColors_p
};
const uint8_t paletteCount = ARRAY_SIZE(palettes);
const String paletteNames[paletteCount] = {
"Rainbow",
"Rainbow Stripe",
"Cloud",
"Lava",
"Ocean",
"Forest",
"Party",
"Heat",
};
#include "Fields.h" #include "Fields.h"
void setup() { void setup() {
@ -359,6 +389,18 @@ void setup() {
sendInt(currentPatternIndex); 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, []() { webServer.on("/brightness", HTTP_POST, []() {
String value = webServer.arg("value"); String value = webServer.arg("value");
setBrightness(value.toInt()); setBrightness(value.toInt());
@ -451,15 +493,11 @@ void loop() {
EVERY_N_SECONDS( secondsPerPalette ) { EVERY_N_SECONDS( secondsPerPalette ) {
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount); gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ]; gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
// paletteIndex = addmod8( paletteIndex, 1, paletteCount);
// targetPalette = palettes[paletteIndex];
} }
EVERY_N_MILLISECONDS(40) { EVERY_N_MILLISECONDS(40) {
// slowly blend the current palette to the next // slowly blend the current palette to the next
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 8); nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 8);
// nblendPaletteTowardPalette(currentPalette, targetPalette, 16);
gHue++; // slowly cycle the "base color" through the rainbow gHue++; // slowly cycle the "base color" through the rainbow
} }
@ -747,6 +785,12 @@ void loadSettings()
autoplay = EEPROM.read(6); autoplay = EEPROM.read(6);
autoplayDuration = EEPROM.read(7); autoplayDuration = EEPROM.read(7);
currentPaletteIndex = EEPROM.read(1);
if (currentPaletteIndex < 0)
currentPaletteIndex = 0;
else if (currentPaletteIndex >= paletteCount)
currentPaletteIndex = paletteCount - 1;
} }
void setPower(uint8_t value) void setPower(uint8_t value)
@ -814,8 +858,10 @@ void adjustPattern(bool up)
if (currentPatternIndex >= patternCount) if (currentPatternIndex >= patternCount)
currentPatternIndex = 0; currentPatternIndex = 0;
EEPROM.write(1, currentPatternIndex); if (autoplay == 0) {
EEPROM.commit(); EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
broadcastInt("pattern", currentPatternIndex); broadcastInt("pattern", currentPatternIndex);
} }
@ -827,8 +873,10 @@ void setPattern(uint8_t value)
currentPatternIndex = value; currentPatternIndex = value;
EEPROM.write(1, currentPatternIndex); if (autoplay == 0) {
EEPROM.commit(); EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
broadcastInt("pattern", currentPatternIndex); broadcastInt("pattern", currentPatternIndex);
} }
@ -843,6 +891,29 @@ void setPatternName(String name)
} }
} }
void setPalette(uint8_t value)
{
if (value >= paletteCount)
value = paletteCount - 1;
currentPaletteIndex = value;
EEPROM.write(1, 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) void adjustBrightness(bool up)
{ {
if (up && brightnessIndex < brightnessCount - 1) if (up && brightnessIndex < brightnessCount - 1)
@ -922,7 +993,8 @@ void confetti()
// random colored speckles that blink in and fade smoothly // random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 10); fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS); int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255); // leds[pos] += CHSV( gHue + random8(64), 200, 255);
leds[pos] += ColorFromPalette(palettes[currentPaletteIndex], gHue + random8(64));
} }
void sinelon() void sinelon()
@ -931,10 +1003,11 @@ void sinelon()
fadeToBlackBy( leds, NUM_LEDS, 20); fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(speed, 0, NUM_LEDS); int pos = beatsin16(speed, 0, NUM_LEDS);
static int prevpos = 0; static int prevpos = 0;
CRGB color = ColorFromPalette(palettes[currentPaletteIndex], gHue, 255);
if( pos < prevpos ) { if( pos < prevpos ) {
fill_solid( leds+pos, (prevpos-pos)+1, CHSV(gHue,220,255)); fill_solid( leds+pos, (prevpos-pos)+1, color);
} else { } else {
fill_solid( leds+prevpos, (pos-prevpos)+1, CHSV( gHue,220,255)); fill_solid( leds+prevpos, (pos-prevpos)+1, color);
} }
prevpos = pos; prevpos = pos;
} }
@ -943,8 +1016,9 @@ void bpm()
{ {
// colored stripes pulsing at a defined Beats-Per-Minute (BPM) // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t beat = beatsin8( speed, 64, 255); uint8_t beat = beatsin8( speed, 64, 255);
CRGBPalette16 palette = palettes[currentPaletteIndex];
for ( int i = 0; i < NUM_LEDS; i++) { for ( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(gCurrentPalette, gHue + (i * 2), beat - gHue + (i * 10)); leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
} }
} }
@ -988,9 +1062,9 @@ void fire()
} }
void water() void water()
{ {
heatMap(IceColors_p, false); heatMap(IceColors_p, false);
} }
// Pride2015 by Mark Kriegsman: https://gist.github.com/kriegsman/964de772d64c502760e5 // Pride2015 by Mark Kriegsman: https://gist.github.com/kriegsman/964de772d64c502760e5
// This function draws rainbows with an ever-changing, // This function draws rainbows with an ever-changing,