From bde51bc675634f227599c6b3025d2cc0c949189b Mon Sep 17 00:00:00 2001 From: Jason Coon Date: Wed, 19 Jul 2017 17:20:51 -0500 Subject: [PATCH] Added more sunrise patterns. --- esp8266-fastled-webserver.ino | 15 ++- sunrise.h | 192 +++++++++++++++++++++++++++++++--- 2 files changed, 189 insertions(+), 18 deletions(-) diff --git a/esp8266-fastled-webserver.ino b/esp8266-fastled-webserver.ino index 01bd435..51dd136 100644 --- a/esp8266-fastled-webserver.ino +++ b/esp8266-fastled-webserver.ino @@ -67,7 +67,7 @@ ESP8266HTTPUpdateServer httpUpdateServer; #define MatrixHeight 8 #define NUM_LEDS MatrixWidth * MatrixHeight -#define MILLI_AMPS 2000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA) +#define MILLI_AMPS 500 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA) #define FRAMES_PER_SECOND 120 // here you can control the speed. With the Access Point / Web Server the animations run a bit slower. CRGB leds[NUM_LEDS]; @@ -109,6 +109,8 @@ CRGBPalette16 IceColors_p = CRGBPalette16(CRGB::Black, CRGB::Blue, CRGB::Aqua, C uint8_t currentPatternIndex = 0; // Index number of which pattern is current uint8_t autoplay = 0; +bool resetPattern = true; + uint8_t autoplayDuration = 10; unsigned long autoPlayTimeout = 0; @@ -165,7 +167,12 @@ typedef PatternAndName PatternAndNameList[]; // List of patterns to cycle through. Each is defined as a separate function below. PatternAndNameList patterns = { - { sunrise, "Sunrise" }, + { sunriseStatic, "Sunrise Static" }, + { sunriseFlicker, "Sunrise Flicker" }, + { sunriseWavesVertical, "Sunrise Waves Vertical" }, + { sunriseWavesHorizontal, "Sunrise Waves Horizontal" }, + { sunriseWavesDiagonal, "Sunrise Waves Diagonal" }, + { sunriseWavesRotating, "Sunrise Waves Rotating" }, { pride, "Pride" }, { colorWaves, "Color Waves" }, @@ -540,6 +547,8 @@ void loop() { FastLED.show(); + resetPattern = false; + // insert a delay to keep the framerate modest // FastLED.delay(1000 / FRAMES_PER_SECOND); } @@ -908,6 +917,8 @@ void setPattern(uint8_t value) } broadcastInt("pattern", currentPatternIndex); + + resetPattern = true; } void setPatternName(String name) diff --git a/sunrise.h b/sunrise.h index 13b20bf..9705f1d 100644 --- a/sunrise.h +++ b/sunrise.h @@ -1,32 +1,192 @@ -void sunrise() { - dimAll(240); +const uint16_t sunriseSeconds = 60; // how long should the "sun" take to rise from completely dark to completely lit +const uint16_t sunriseMillis = (sunriseSeconds * 1000); +const uint16_t sunriseInterval = sunriseMillis / 240; // when using palettes, the usable range is 0-240 before it starts wrapping from the last color to the first - CRGBPalette16 palette = HeatColors_p; +uint8_t sunriseIncrement = 4; // how much to change brightness for each level of the matrix - const uint8_t centerX = MatrixWidth / 2; +uint8_t sunriseLevel; - static uint8_t currentLevel = 0; +const CRGBPalette16 sunrisePalette = HeatColors_p; +const uint8_t centerX = MatrixWidth / 2; - static uint8_t inc = 4; - - EVERY_N_MILLIS(250) { - if (currentLevel < 240) { - currentLevel++; - Serial.print("Current level: "); Serial.println(currentLevel); +void updateSunrise() { + EVERY_N_MILLIS(sunriseInterval) { + if (sunriseLevel < 240) { + sunriseLevel++; + Serial.print("Current level: "); Serial.println(sunriseLevel); } - else if (inc > 0) { - inc--; + else if (sunriseIncrement > 0) { + sunriseIncrement--; } } + if (resetPattern) { + sunriseLevel = 0; + sunriseIncrement = 4; + FastLED.clear(); + } +} + +void sunriseStatic() { + updateSunrise(); + for (uint8_t x = 0; x < MatrixWidth; x++) { - int16_t d = currentLevel - inc; + int16_t d = sunriseLevel - sunriseIncrement; for (uint8_t y = 0; y < MatrixHeight; y++) { if (d >= 0) { - leds[XY(x, y)] += ColorFromPalette(palette, random(0, d), random8(d, 255)); + CRGB newcolor = ColorFromPalette(sunrisePalette, random(0, d), random8(d, 255)); + uint16_t pixelnumber = XY(x, y); + nblend(leds[pixelnumber], newcolor, 64); } - d -= inc; + d -= sunriseIncrement; + } + } +} + +void sunriseFlicker() { + dimAll(240); + + updateSunrise(); + + for (uint8_t x = 0; x < MatrixWidth; x++) { + int16_t d = sunriseLevel - sunriseIncrement; + + for (uint8_t y = 0; y < MatrixHeight; y++) { + if (d >= 0) { + CRGB newcolor = ColorFromPalette(sunrisePalette, random(0, d), random8(d, 255)); + uint16_t pixelnumber = XY(x, y); + nblend(leds[pixelnumber], newcolor, 64); + } + d -= sunriseIncrement; + } + } +} + +void sunriseWavesDiagonal() { + dimAll(240); + + updateSunrise(); + + uint8_t t = beat8(60); + + for (uint8_t x = 0; x < MatrixWidth; x++) { + for (uint8_t y = 0; y < MatrixHeight; y++) { + uint8_t cx = sin8(x); + uint8_t cy = cos8(y); + uint8_t bri8 = sin8(8 * (cx + cy) + t); + + CRGB newcolor = ColorFromPalette(sunrisePalette, sunriseLevel, bri8); + + uint16_t pixelnumber = XY(x, y); + pixelnumber = (NUM_LEDS - 1) - pixelnumber; + + nblend(leds[pixelnumber], newcolor, 64); + } + } +} + +void sunriseWavesVertical() { + updateSunrise(); + + static uint16_t sPseudotime = 0; + static uint16_t sLastMillis = 0; + + 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 ms = millis(); + uint16_t deltams = ms - sLastMillis ; + sLastMillis = ms; + sPseudotime += deltams * msmultiplier; + uint16_t brightnesstheta16 = sPseudotime; + + for (uint8_t y = 0; y < MatrixHeight; y++) { + 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); + + for (uint8_t x = 0; x < MatrixWidth; x++) { + CRGB newcolor = ColorFromPalette(sunrisePalette, sunriseLevel, bri8); + + uint16_t pixelnumber = XY(x, (MatrixHeight - 1) - y); + pixelnumber = (NUM_LEDS - 1) - pixelnumber; + + nblend(leds[pixelnumber], newcolor, 64); + } + } +} + +void sunriseWavesHorizontal() { + updateSunrise(); + + static uint16_t sPseudotime = 0; + static uint16_t sLastMillis = 0; + + 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 ms = millis(); + uint16_t deltams = ms - sLastMillis ; + sLastMillis = ms; + sPseudotime += deltams * msmultiplier; + uint16_t brightnesstheta16 = sPseudotime; + + for (uint8_t x = 0; x < MatrixWidth; x++) { + 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); + + for (uint8_t y = 0; y < MatrixHeight; y++) { + CRGB newcolor = ColorFromPalette(sunrisePalette, sunriseLevel, bri8); + + uint16_t pixelnumber = XY(x, y); + pixelnumber = (NUM_LEDS - 1) - pixelnumber; + + nblend(leds[pixelnumber], newcolor, 64); + } + } +} + +void sunriseWavesRotating() { + updateSunrise(); + + static uint16_t sPseudotime = 0; + static uint16_t sLastMillis = 0; + + 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 ms = millis(); + uint16_t deltams = ms - sLastMillis ; + sLastMillis = ms; + sPseudotime += deltams * msmultiplier; + uint16_t brightnesstheta16 = sPseudotime; + + for (uint8_t x = 0; x < MatrixWidth; x++) { + for (uint8_t y = 0; y < MatrixHeight; y++) { + 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 = ColorFromPalette(sunrisePalette, sunriseLevel, bri8); + + uint16_t pixelnumber = XY(x, y); + pixelnumber = (NUM_LEDS - 1) - pixelnumber; + + nblend(leds[pixelnumber], newcolor, 64); } } }