From 408b9d3671b749bd4a3530c071914ef235b06798 Mon Sep 17 00:00:00 2001 From: starcalc Date: Wed, 1 Mar 2017 16:21:34 +0100 Subject: [PATCH] Refactor: Effect smooth now working. --- pixelprojektor/NeoPatterns.cpp | 158 ++++++++++- pixelprojektor/NeoPatterns.h | 18 +- pixelprojektor/pixelprojektor.ino | 445 +++++++++++++++--------------- 3 files changed, 389 insertions(+), 232 deletions(-) diff --git a/pixelprojektor/NeoPatterns.cpp b/pixelprojektor/NeoPatterns.cpp index 8b834fe..4a66080 100644 --- a/pixelprojektor/NeoPatterns.cpp +++ b/pixelprojektor/NeoPatterns.cpp @@ -4,6 +4,14 @@ NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*call Adafruit_NeoPixel(pixels, pin, type) { OnComplete = callback; + // TODO: Arrays hier initialisieren mit konkreten Werten? Und in der NeoPatterns.h nur der Platzhalter? + //Allocate a zero initialized block of memory big enough to hold "pixels" uint8_t. + pixelR = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); + pixelG = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); + pixelB = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); + pixelR_buffer = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); + pixelG_buffer = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); + pixelB_buffer = ( uint8_t* ) calloc( pixels, sizeof( uint8_t ) ); } void NeoPatterns::Update() { @@ -30,6 +38,9 @@ void NeoPatterns::Update() { case RANDOM_FADE: RandomFadeUpdate(); break; + case SMOOTH: + SmoothUpdate(); + break; case NONE: break; default: @@ -48,7 +59,7 @@ void NeoPatterns::Increment() Index = 0; if (OnComplete != NULL) { - OnComplete(); // call the comlpetion callback + OnComplete(); // call the completion callback } } } @@ -60,7 +71,7 @@ void NeoPatterns::Increment() Index = TotalSteps - 1; if (OnComplete != NULL) { - OnComplete(); // call the comlpetion callback + OnComplete(); // call the completion callback } } } @@ -180,10 +191,10 @@ void NeoPatterns::ScannerUpdate() int finalpos; if (spiral) { finalpos = numToSpiralPos(i); - } + } else { - finalpos=i; + finalpos = i; } if (i == Index) // Scan Pixel to the right { @@ -239,6 +250,114 @@ void NeoPatterns::RandomFadeUpdate() { Increment(); } +void NeoPatterns::Smooth(uint8_t wheelSpeed, uint8_t smoothing, uint8_t strength, uint8_t interval) { + ActivePattern = SMOOTH; + Interval = interval; + TotalSteps = 1000; // Beim Smooth nicht sinnvoll? + Index = 0; + WheelSpeed = wheelSpeed; + Smoothing = smoothing; + Strength = strength; + movingPoint_x = 3; + movingPoint_y = 3; + // Clear buffer (from previous or different effects) + for (int i = 0; i < numPixels(); i++) { + pixelR_buffer[i] = 0; + pixelG_buffer[i] = 0; + pixelB_buffer[i] = 0; + } +} + +void NeoPatterns::SmoothUpdate() { + uint32_t c = Wheel(wPos); + wPosSlow += WheelSpeed; + wPos = (wPos + (wPosSlow / 10) ) % 255; + wPosSlow = wPosSlow % 16; + + uint8_t r = (uint8_t)(c >> 16); + uint8_t g = (uint8_t)(c >> 8); + uint8_t b = (uint8_t)c; + + movingPoint_x = movingPoint_x + 8 + random(-random(0, 1 + 1), random(0, 1 + 1) + 1); + movingPoint_y = movingPoint_y + 8 + random(-random(0, 1 + 1), random(0, 1 + 1) + 1); + if (movingPoint_x < 8) { + movingPoint_x = 8 - movingPoint_x; + } else if (movingPoint_x >= 16) { + movingPoint_x = 22 - movingPoint_x; + } else { + movingPoint_x -= 8; + } + + if (movingPoint_y < 8) { + movingPoint_y = 8 - movingPoint_y; + } else if (movingPoint_y >= 16) { + movingPoint_y = 22 - movingPoint_y; + } else { + movingPoint_y -= 8; + } + uint8_t startx = movingPoint_x; + uint8_t starty = movingPoint_y; + + for (int i = 0; i < Strength; i++) { + + movingPoint_x = startx + 8 + random(-random(0, 2 + 1), random(0, 2 + 1) + 1); + movingPoint_y = starty + 8 + random(-random(0, 2 + 1), random(0, 2 + 1) + 1); + + if (movingPoint_x < 8) { + movingPoint_x = 8 - movingPoint_x; + } else if (movingPoint_x >= 16) { + movingPoint_x = 22 - movingPoint_x; + } else { + movingPoint_x -= 8; + } + + if (movingPoint_y < 8) { + movingPoint_y = 8 - movingPoint_y; + } else if (movingPoint_y >= 16) { + movingPoint_y = 22 - movingPoint_y; + } else { + movingPoint_y -= 8; + } + + if (pixelR[xyToPos(movingPoint_x, movingPoint_y)] < r) { + pixelR[xyToPos(movingPoint_x, movingPoint_y)]++; + } else if (pixelR[xyToPos(movingPoint_x, movingPoint_y)] > r) { + pixelR[xyToPos(movingPoint_x, movingPoint_y)]--; + } + if (pixelG[xyToPos(movingPoint_x, movingPoint_y)] < g) { + pixelG[xyToPos(movingPoint_x, movingPoint_y)]++; + } else if (pixelG[xyToPos(movingPoint_x, movingPoint_y)] > g) { + pixelG[xyToPos(movingPoint_x, movingPoint_y)]--; + } + if (pixelB[xyToPos(movingPoint_x, movingPoint_y)] < b) { + pixelB[xyToPos(movingPoint_x, movingPoint_y)]++; + } else if (pixelB[xyToPos(movingPoint_x, movingPoint_y)] > b) { + pixelB[xyToPos(movingPoint_x, movingPoint_y)]--; + } + } + + movingPoint_x = startx; + movingPoint_y = starty; + + for (int i = 0; i < numPixels(); i++) { + pixelR_buffer[i] = (Smoothing / 100.0) * pixelR[i] + (1.0 - (Smoothing / 100.0)) * getAverage(pixelR, i, 0, 0); + pixelG_buffer[i] = (Smoothing / 100.0) * pixelG[i] + (1.0 - (Smoothing / 100.0)) * getAverage(pixelG, i, 0, 0); + pixelB_buffer[i] = (Smoothing / 100.0) * pixelB[i] + (1.0 - (Smoothing / 100.0)) * getAverage(pixelB, i, 0, 0); + + } + + for (int i = 0; i < numPixels(); i++) { + pixelR[i] = pixelR_buffer[i]; + pixelG[i] = pixelG_buffer[i]; + pixelB[i] = pixelB_buffer[i]; + setPixelColor(i, pixelR[i], pixelG[i], pixelB[i]); + } + + show(); + Increment(); + +} + /****************** Helper functions ******************/ void NeoPatterns::SetColor1(uint32_t color) { @@ -306,7 +425,7 @@ uint32_t NeoPatterns::Wheel(byte WheelPos) } // Convert x y pixel position to matrix position -uint8_t NeoPatterns::xyToPos(int x, int y) { +uint8_t NeoPatterns::xyToPos(int x, int y) { if (y % 2 == 0) { return (y * 8 + x); } else { @@ -315,11 +434,11 @@ uint8_t NeoPatterns::xyToPos(int x, int y) { } // Convert pixel number to actual 8x8 matrix position in a spiral -uint8_t NeoPatterns::numToSpiralPos(int num) { +uint8_t NeoPatterns::numToSpiralPos(int num) { int edge = (int)sqrt(numPixels()); - int findx = edge-1; // 7 + int findx = edge - 1; // 7 int findy = 0; - int stepsize = edge-1; // initial value (0..7) + int stepsize = edge - 1; // initial value (0..7) int stepnumber = 0; // each "step" should be used twice int count = -1; int dir = 1; // direction: 0 = incX, 1=incY, 2=decX, 3=decY @@ -360,4 +479,25 @@ uint8_t NeoPatterns::numToSpiralPos(int num) { return xyToPos(findx, findy); } - +uint8_t NeoPatterns::getAverage(uint8_t array[], uint8_t i, int x, int y) +{ + uint16_t sum = 0; + uint8_t count = 0; + if (i >= 8) { //up + sum += array[i - 8]; + count++; + } + if (i < (64 - 8)) { //down + sum += array[i + 8]; + count++; + } + if (i >= 1) { //left + sum += array[i - 1]; + count++; + } + if (i < (64 - 1)) { //right + sum += array[i + 1]; + count++; + } + return sum / count; +} diff --git a/pixelprojektor/NeoPatterns.h b/pixelprojektor/NeoPatterns.h index 032f217..a546cc4 100644 --- a/pixelprojektor/NeoPatterns.h +++ b/pixelprojektor/NeoPatterns.h @@ -1,7 +1,7 @@ #include // Pattern types supported: -enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE, RANDOM_FADE }; +enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE, RANDOM_FADE, SMOOTH }; // Patern directions supported: enum direction { FORWARD, REVERSE }; @@ -26,6 +26,8 @@ class NeoPatterns : public Adafruit_NeoPixel void FadeUpdate(); void RandomFade(uint8_t interval = 100); void RandomFadeUpdate(); + void Smooth(uint8_t wheelSpeed, uint8_t smoothing, uint8_t strength, uint8_t interval); + void SmoothUpdate(); void SetColor1(uint32_t color); void SetColor2(uint32_t color); @@ -37,7 +39,7 @@ class NeoPatterns : public Adafruit_NeoPixel uint32_t Wheel(byte WheelPos); uint8_t numToSpiralPos(int num); uint8_t xyToPos(int x, int y); - + uint8_t getAverage(uint8_t array[], uint8_t i, int x, int y); private: // Member Variables: @@ -54,6 +56,18 @@ class NeoPatterns : public Adafruit_NeoPixel byte wPos; bool colorful; bool spiral; + uint8_t wPosSlow; + uint8_t WheelSpeed; + uint8_t Smoothing; + uint8_t Strength; + uint8_t movingPoint_x; + uint8_t movingPoint_y; + uint8_t *pixelR; + uint8_t *pixelG; + uint8_t *pixelB; + uint8_t *pixelR_buffer; + uint8_t *pixelG_buffer; + uint8_t *pixelB_buffer; uint32_t DimColor(uint32_t color); void Increment(); diff --git a/pixelprojektor/pixelprojektor.ino b/pixelprojektor/pixelprojektor.ino index db365b8..79d8c92 100644 --- a/pixelprojektor/pixelprojektor.ino +++ b/pixelprojektor/pixelprojektor.ino @@ -19,137 +19,140 @@ NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComp HomieNode homieNode("pixel", "commands"); -bool onSetColor(const HomieRange& range, const String& value){ - if (!range.isRange || range.index < 0 || range.index > 1) { - return false; - } - switch(range.index) { - case 0: - strip.SetColor1(value.toInt()); - break; - case 1: - strip.SetColor2(value.toInt()); - break; - } - homieNode.setProperty("color_" + String(range.index)).send(value); +bool onSetColor(const HomieRange& range, const String& value) { + if (!range.isRange || range.index < 0 || range.index > 1) { + return false; + } + switch (range.index) { + case 0: + strip.SetColor1(value.toInt()); + break; + case 1: + strip.SetColor2(value.toInt()); + break; + } + homieNode.setProperty("color_" + String(range.index)).send(value); } -bool onSetPixel(const HomieRange& range, const String& value){ - if(!range.isRange) { - strip.None(); - strip.ColorSet(value.toInt()); - homieNode.setProperty("pixel").send(value); - return true; - } - if (range.index < 0 || range.index > strip.numPixels()-1) { - return false; - } - strip.None(); - strip.setPixelColor(range.index, value.toInt()); - strip.show(); - homieNode.setProperty("pixel_" + String(range.index)).send(value); +bool onSetPixel(const HomieRange& range, const String& value) { + if (!range.isRange) { + strip.None(); + strip.ColorSet(value.toInt()); + homieNode.setProperty("pixel").send(value); + return true; + } + if (range.index < 0 || range.index > strip.numPixels() - 1) { + return false; + } + strip.None(); + strip.setPixelColor(range.index, value.toInt()); + strip.show(); + homieNode.setProperty("pixel_" + String(range.index)).send(value); } -bool onSetBrightness(const HomieRange& range, const String& value){ - long brightness= value.toInt(); - if (brightness < 0 || brightness > 255) { - return false; - } - strip.setBrightness(brightness); - strip.show(); - homieNode.setProperty("brightness").send(value); +bool onSetBrightness(const HomieRange& range, const String& value) { + long brightness = value.toInt(); + if (brightness < 0 || brightness > 255) { + return false; + } + strip.setBrightness(brightness); + strip.show(); + homieNode.setProperty("brightness").send(value); } -bool onSetEffect(const HomieRange& range, const String& value){ - String effect = value; - effect.toLowerCase(); - if(effect == "scanner") { - strip.Scanner(strip.Color(255, 0, 0)); - } - else if(effect == "randomscanner") { - strip.Scanner(strip.Color(255, 0, 0), 40, true); - } - else if(effect == "larsonspiral") { - strip.Scanner(strip.Color(255, 0, 0), 40, true, true); - } - else if(effect == "rainbowcycle") { - strip.RainbowCycle(50); - } - else if(effect == "theaterchase") { - strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0,0,255), 100); - } - else if(effect == "fade") { - strip.Fade(strip.Color(255, 0, 0), strip.Color(0,0,255), 200, 100); - } - else if(effect == "randomfade") { - strip.RandomFade(); - } - else { - strip.None(); - } - homieNode.setProperty("effect").send(value); +bool onSetEffect(const HomieRange& range, const String& value) { + String effect = value; + effect.toLowerCase(); + if (effect == "scanner") { + strip.Scanner(strip.Color(255, 0, 0)); + } + else if (effect == "randomscanner") { + strip.Scanner(strip.Color(255, 0, 0), 40, true); + } + else if (effect == "larsonspiral") { + strip.Scanner(strip.Color(255, 0, 0), 40, true, true); + } + else if (effect == "rainbowcycle") { + strip.RainbowCycle(50); + } + else if (effect == "theaterchase") { + strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 100); + } + else if (effect == "fade") { + strip.Fade(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 200, 100); + } + else if (effect == "randomfade") { + strip.RandomFade(); + } + else if (effect == "smooth") { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255 + strip.Smooth(16, 80, 50, 40); + } + else { + strip.None(); + } + homieNode.setProperty("effect").send(value); } -bool onSetClear(const HomieRange& range, const String& value){ - strip.None(); - strip.clear(); - strip.show(); - homieNode.setProperty("clear").send(value); +bool onSetClear(const HomieRange& range, const String& value) { + strip.None(); + strip.clear(); + strip.show(); + homieNode.setProperty("clear").send(value); } -bool onSetLength(const HomieRange& range, const String& value){ - strip.None(); - strip.clear(); - strip.show(); - int newLength = value.toInt(); - if(newLength > 0) { - strip.updateLength(newLength); - } - homieNode.setProperty("length").send(value); +bool onSetLength(const HomieRange& range, const String& value) { + strip.None(); + strip.clear(); + strip.show(); + int newLength = value.toInt(); + if (newLength > 0) { + strip.updateLength(newLength); + } + homieNode.setProperty("length").send(value); } void loopHandler() { - strip.Update(); + strip.Update(); } void setup() { - Serial.begin(115200); + Serial.begin(115200); - Homie_setFirmware("pixelprojektor", "1.0.0"); - Homie.setLoopFunction(loopHandler); + Homie_setFirmware("pixelprojektor", "1.0.0"); + Homie.setLoopFunction(loopHandler); - homieNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel); - homieNode.advertiseRange("color", 0, 1).settable(onSetColor); - homieNode.advertise("brightness").settable(onSetBrightness); - homieNode.advertise("effect").settable(onSetEffect); - homieNode.advertise("clear").settable(onSetClear); - homieNode.advertise("length").settable(onSetLength); + homieNode.advertiseRange("pixel", 0, NUMPIXELS - 1).settable(onSetPixel); + homieNode.advertiseRange("color", 0, 1).settable(onSetColor); + homieNode.advertise("brightness").settable(onSetBrightness); + homieNode.advertise("effect").settable(onSetEffect); + homieNode.advertise("clear").settable(onSetClear); + homieNode.advertise("length").settable(onSetLength); - strip.begin(); - strip.clear(); - strip.setBrightness(64); - strip.show(); + strip.begin(); + strip.clear(); + strip.setBrightness(64); + strip.show(); - Homie.setup(); + Homie.setup(); - ArduinoOTA.setHostname("pixelprojektor"); - ArduinoOTA.onStart([]() { - strip.clear(); - }); - ArduinoOTA.onEnd([]() { - strip.clear(); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0)); - strip.show(); - }); - ArduinoOTA.begin(); + ArduinoOTA.setHostname("pixelprojektor"); + ArduinoOTA.onStart([]() { + strip.clear(); + }); + ArduinoOTA.onEnd([]() { + strip.clear(); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0)); + strip.show(); + }); + ArduinoOTA.begin(); } void loop() { - Homie.loop(); - ArduinoOTA.handle(); + Homie.loop(); + ArduinoOTA.handle(); } // Diese Effekte müssen nach dem Umbau wieder vorhanden sein: @@ -179,59 +182,59 @@ void loop() { /************ Old stuff ************/ /* -#define FPS 15 -uint8_t effect = 0; -uint8_t movingPoint_x = 3; -uint8_t movingPoint_y = 3; -uint8_t wheelPos = 0; -uint8_t wheelPosSlow = 0; //for slower wheelPos increment than 1 -int wheelSpeed = 16; //16=+1/frame -int smoothing = 80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing. -int strength = 50; //how much pixels to apply color to -#define EFFECT_NONE 0 -#define EFFECT_SMOOTH 1 -#define EFFECT_SPIRAL 2 -#define EFFECT_RANDOMFADE 3 -#define EFFECT_CHASE 4 -#define EFFECT_RADAR 5 -#define EFFECT_LARSON 6 -int fadespeedmax = 5; //1 to 255 -int iconCountStart = 0; //for percentage calculation -int iconCountdown = 0; //0=off -uint8_t iconchar = 0; //last displayed char -int32_t iconcolor = 0; //last icon color + #define FPS 15 + uint8_t effect = 0; + uint8_t movingPoint_x = 3; + uint8_t movingPoint_y = 3; + uint8_t wheelPos = 0; + uint8_t wheelPosSlow = 0; //for slower wheelPos increment than 1 + int wheelSpeed = 16; //16=+1/frame + int smoothing = 80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing. + int strength = 50; //how much pixels to apply color to + #define EFFECT_NONE 0 + #define EFFECT_SMOOTH 1 + #define EFFECT_SPIRAL 2 + #define EFFECT_RANDOMFADE 3 + #define EFFECT_CHASE 4 + #define EFFECT_RADAR 5 + #define EFFECT_LARSON 6 + int fadespeedmax = 5; //1 to 255 + int iconCountStart = 0; //for percentage calculation + int iconCountdown = 0; //0=off + uint8_t iconchar = 0; //last displayed char + int32_t iconcolor = 0; //last icon color -uint16_t Index; // current step within the pattern -// int Index = 0; // Step for Effect (e.g. chase) -// int state = 0; // Direction for Larson Scanner (spiral) -direction Direction; // direction to run the pattern -uint8_t pixelR[NUMPIXELS]; -uint8_t pixelG[NUMPIXELS]; -uint8_t pixelB[NUMPIXELS]; -//write to buffer, flip with showBuffer() -uint8_t pixelR_buffer[NUMPIXELS]; -uint8_t pixelG_buffer[NUMPIXELS]; -uint8_t pixelB_buffer[NUMPIXELS]; + uint16_t Index; // current step within the pattern + // int Index = 0; // Step for Effect (e.g. chase) + // int state = 0; // Direction for Larson Scanner (spiral) + direction Direction; // direction to run the pattern + uint8_t pixelR[NUMPIXELS]; + uint8_t pixelG[NUMPIXELS]; + uint8_t pixelB[NUMPIXELS]; + //write to buffer, flip with showBuffer() + uint8_t pixelR_buffer[NUMPIXELS]; + uint8_t pixelG_buffer[NUMPIXELS]; + uint8_t pixelB_buffer[NUMPIXELS]; -long lastMillis = 0; -long fpsdelay = 1000 / FPS; + long lastMillis = 0; + long fpsdelay = 1000 / FPS; -int xyToPos(int x, int y) { //convert x y pixel position to matrix position + int xyToPos(int x, int y) { //convert x y pixel position to matrix position if (y % 2 == 0) { return (y * 8 + x); } else { return (y * 8 + (7 - x)); } -} + } -int numToPos(int num) { //convert pixel number to actual 8x8 matrix position + int numToPos(int num) { //convert pixel number to actual 8x8 matrix position int x = num % 8; int y = num / 8; return xyToPos(x, y); -} + } -int numToSpiralPos(int num) { // convert pixel number to actual 8x8 matrix position in a spiral + int numToSpiralPos(int num) { // convert pixel number to actual 8x8 matrix position in a spiral int edge = (int)sqrt(NUMPIXELS); int findx = edge-1; // 7 int findy = 0; @@ -274,9 +277,9 @@ int numToSpiralPos(int num) { // convert pixel number to actual 8x8 matrix posit } } return xyToPos(findx, findy); -} + } -uint32_t wheel(byte WheelPos) { + uint32_t wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); @@ -287,28 +290,28 @@ uint32_t wheel(byte WheelPos) { } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); -} + } -void led_fill(uint32_t c) -{ + void led_fill(uint32_t c) + { for (int i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); } strip.show(); -} + } -void led_random() -{ + void led_random() + { for (int i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, wheel(random(0, 255))); } strip.show(); -} + } -void showBuffer() -{ + void showBuffer() + { for (int i = 0; i < strip.numPixels(); i++) { pixelR[i] = pixelR_buffer[i]; pixelG[i] = pixelG_buffer[i]; @@ -316,10 +319,10 @@ void showBuffer() strip.setPixelColor(i, pixelR[i], pixelG[i], pixelB[i]); } strip.show(); -} + } -uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y) -{ + uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y) + { uint16_t sum = 0; uint8_t count = 0; if (i >= 8) { //up @@ -339,11 +342,11 @@ uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y) count++; } return sum / count; -} + } -void led_smooth() -{ + void led_smooth() + { for (int i = 0; i < strip.numPixels(); i++) { //uint8_t avgbrightness=pixelR_buffer[i]/3+pixelG_buffer[i]/3+pixelB_buffer[i]/3; pixelR_buffer[i] = (smoothing / 100.0) * pixelR[i] + (1.0 - (smoothing / 100.0)) * getAverage(pixelR, i, 0, 0); @@ -352,10 +355,10 @@ void led_smooth() } showBuffer(); -} + } -void led_movingPoint() -{ + void led_movingPoint() + { uint32_t c = wheel(wheelPos); wheelPosSlow += wheelSpeed; wheelPos = (wheelPos + (wheelPosSlow / 10) ) % 255; @@ -433,18 +436,18 @@ void led_movingPoint() movingPoint_x = startx; movingPoint_y = starty; -} -void bufferClear() -{ + } + void bufferClear() + { for (int i = 0; i < strip.numPixels(); i++) { pixelR_buffer[i] = 0; pixelG_buffer[i] = 0; pixelB_buffer[i] = 0; } -} + } -void led_chase() -{ + void led_chase() + { Index += 1; if (Index > 255) { Index = 1; @@ -462,44 +465,44 @@ void led_chase() strip.setPixelColor(i + q, 0); //turn every third pixel off } } -} + } -void led_radar() -{ + void led_radar() + { // "Sweep" in cirles... // line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle))); -} + } -// Calculate 50% dimmed version of a color (used by led_larson()) -uint32_t DimColor(uint32_t color) -{ + // Calculate 50% dimmed version of a color (used by led_larson()) + uint32_t DimColor(uint32_t color) + { // Shift R, G and B components one bit to the right uint32_t Dimcolor = strip.Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1); return Dimcolor; -} + } -// Returns the Red component of a 32-bit color -uint8_t Red(uint32_t color) -{ + // Returns the Red component of a 32-bit color + uint8_t Red(uint32_t color) + { return (color >> 16) & 0xFF; -} + } -// Returns the Green component of a 32-bit color -uint8_t Green(uint32_t color) -{ + // Returns the Green component of a 32-bit color + uint8_t Green(uint32_t color) + { return (color >> 8) & 0xFF; -} + } -// Returns the Blue component of a 32-bit color -uint8_t Blue(uint32_t color) -{ + // Returns the Blue component of a 32-bit color + uint8_t Blue(uint32_t color) + { return color & 0xFF; -} + } -void Increment() -{ + void Increment() + { if (Direction == FORWARD) { Index++; @@ -516,11 +519,11 @@ void Increment() Index = (strip.numPixels() - 1) * 2 - 1; } } -} + } -void led_larson() -{ + void led_larson() + { int EyeSize = 5; uint32_t rgb[3] = {0}; wheelPos++; @@ -548,11 +551,11 @@ void led_larson() strip.show(); Increment(); -} + } -void led_spiral() -{ + void led_spiral() + { int every = 4; wheelPos++; int qp = Index % every; @@ -569,19 +572,19 @@ void led_spiral() } strip.show(); -} + } -void led_randomfade() -{ + void led_randomfade() + { for (int i = 0; i < strip.numPixels(); i++) { pixelR_buffer[i] += random(0, random(0, fadespeedmax + 1) + 1); //use buffer red channel for color wheel strip.setPixelColor(i, wheel(pixelR_buffer[i])); } strip.show(); -} + } -void led_icon(uint8_t fontchar, uint32_t iconcolor) -{ + void led_icon(uint8_t fontchar, uint32_t iconcolor) + { for (int i = 0; i < strip.numPixels(); i++) { uint64_t mask = 1LL << (uint64_t)i; @@ -598,20 +601,20 @@ void led_icon(uint8_t fontchar, uint32_t iconcolor) } strip.show(); -} + } -void set_randomBuffer() -{ + void set_randomBuffer() + { for (int i = 0; i < strip.numPixels(); i++) { uint32_t c = wheel(random(0, 256)); pixelR_buffer[i] = (uint8_t)(c >> 16); pixelG_buffer[i] = (uint8_t)(c >> 8); pixelB_buffer[i] = (uint8_t)c; } -} + } -uint32_t parseColor(String value) { + uint32_t parseColor(String value) { if (value.charAt(0) == '#') { //solid fill String color = value.substring(1); int number = (int) strtol( &color[0], NULL, 16); @@ -629,10 +632,10 @@ uint32_t parseColor(String value) { } return 0; -} + } -bool effectHandler(const HomieRange& range, const String& value) { + bool effectHandler(const HomieRange& range, const String& value) { Homie.getLogger() << "-> " << value << endl; int sep = value.indexOf("|"); @@ -721,9 +724,9 @@ bool effectHandler(const HomieRange& range, const String& value) { strip.show(); } return true; -} + } -bool pixelsHandler(const HomieRange& range, const String& value) { + bool pixelsHandler(const HomieRange& range, const String& value) { String remaining = value; int i = 0; @@ -748,9 +751,9 @@ bool pixelsHandler(const HomieRange& range, const String& value) { strip.show(); return true; -} + } -bool iconHandler(const HomieRange& range, const String& value) { + bool iconHandler(const HomieRange& range, const String& value) { String _iconname = value; iconcolor = strip.Color(255, 255, 255); //default color if (value[0] == '#') { //color given @@ -769,9 +772,9 @@ bool iconHandler(const HomieRange& range, const String& value) { strip.show(); return true; -} + } -void setup() { + void setup() { Serial.begin(115200); Serial << endl << endl; @@ -833,9 +836,9 @@ void setup() { effect = EFFECT_LARSON; Serial << "Setup finished" << endl; -} + } -void loop() { + void loop() { Homie.loop(); ArduinoOTA.handle(); @@ -872,6 +875,6 @@ void loop() { lastMillis = currentMillis; } -} + } */