Refactor: Effect smooth now working.

This commit is contained in:
starcalc 2017-03-01 16:21:34 +01:00
parent 99e5762296
commit 408b9d3671
3 changed files with 389 additions and 232 deletions

View File

@ -4,6 +4,14 @@ NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*call
Adafruit_NeoPixel(pixels, pin, type) Adafruit_NeoPixel(pixels, pin, type)
{ {
OnComplete = callback; 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() { void NeoPatterns::Update() {
@ -30,6 +38,9 @@ void NeoPatterns::Update() {
case RANDOM_FADE: case RANDOM_FADE:
RandomFadeUpdate(); RandomFadeUpdate();
break; break;
case SMOOTH:
SmoothUpdate();
break;
case NONE: case NONE:
break; break;
default: default:
@ -48,7 +59,7 @@ void NeoPatterns::Increment()
Index = 0; Index = 0;
if (OnComplete != NULL) if (OnComplete != NULL)
{ {
OnComplete(); // call the comlpetion callback OnComplete(); // call the completion callback
} }
} }
} }
@ -60,7 +71,7 @@ void NeoPatterns::Increment()
Index = TotalSteps - 1; Index = TotalSteps - 1;
if (OnComplete != NULL) if (OnComplete != NULL)
{ {
OnComplete(); // call the comlpetion callback OnComplete(); // call the completion callback
} }
} }
} }
@ -180,10 +191,10 @@ void NeoPatterns::ScannerUpdate()
int finalpos; int finalpos;
if (spiral) { if (spiral) {
finalpos = numToSpiralPos(i); finalpos = numToSpiralPos(i);
} }
else else
{ {
finalpos=i; finalpos = i;
} }
if (i == Index) // Scan Pixel to the right if (i == Index) // Scan Pixel to the right
{ {
@ -239,6 +250,114 @@ void NeoPatterns::RandomFadeUpdate() {
Increment(); 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 ******************/ /****************** Helper functions ******************/
void NeoPatterns::SetColor1(uint32_t color) { void NeoPatterns::SetColor1(uint32_t color) {
@ -306,7 +425,7 @@ uint32_t NeoPatterns::Wheel(byte WheelPos)
} }
// Convert x y pixel position to matrix position // 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) { if (y % 2 == 0) {
return (y * 8 + x); return (y * 8 + x);
} else { } else {
@ -315,11 +434,11 @@ uint8_t NeoPatterns::xyToPos(int x, int y) {
} }
// Convert pixel number to actual 8x8 matrix position in a spiral // 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 edge = (int)sqrt(numPixels());
int findx = edge-1; // 7 int findx = edge - 1; // 7
int findy = 0; 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 stepnumber = 0; // each "step" should be used twice
int count = -1; int count = -1;
int dir = 1; // direction: 0 = incX, 1=incY, 2=decX, 3=decY 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); 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;
}

View File

@ -1,7 +1,7 @@
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
// Pattern types supported: // 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: // Patern directions supported:
enum direction { FORWARD, REVERSE }; enum direction { FORWARD, REVERSE };
@ -26,6 +26,8 @@ class NeoPatterns : public Adafruit_NeoPixel
void FadeUpdate(); void FadeUpdate();
void RandomFade(uint8_t interval = 100); void RandomFade(uint8_t interval = 100);
void RandomFadeUpdate(); void RandomFadeUpdate();
void Smooth(uint8_t wheelSpeed, uint8_t smoothing, uint8_t strength, uint8_t interval);
void SmoothUpdate();
void SetColor1(uint32_t color); void SetColor1(uint32_t color);
void SetColor2(uint32_t color); void SetColor2(uint32_t color);
@ -37,7 +39,7 @@ class NeoPatterns : public Adafruit_NeoPixel
uint32_t Wheel(byte WheelPos); uint32_t Wheel(byte WheelPos);
uint8_t numToSpiralPos(int num); uint8_t numToSpiralPos(int num);
uint8_t xyToPos(int x, int y); uint8_t xyToPos(int x, int y);
uint8_t getAverage(uint8_t array[], uint8_t i, int x, int y);
private: private:
// Member Variables: // Member Variables:
@ -54,6 +56,18 @@ class NeoPatterns : public Adafruit_NeoPixel
byte wPos; byte wPos;
bool colorful; bool colorful;
bool spiral; 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); uint32_t DimColor(uint32_t color);
void Increment(); void Increment();

View File

@ -19,137 +19,140 @@ NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComp
HomieNode homieNode("pixel", "commands"); HomieNode homieNode("pixel", "commands");
bool onSetColor(const HomieRange& range, const String& value){ bool onSetColor(const HomieRange& range, const String& value) {
if (!range.isRange || range.index < 0 || range.index > 1) { if (!range.isRange || range.index < 0 || range.index > 1) {
return false; return false;
} }
switch(range.index) { switch (range.index) {
case 0: case 0:
strip.SetColor1(value.toInt()); strip.SetColor1(value.toInt());
break; break;
case 1: case 1:
strip.SetColor2(value.toInt()); strip.SetColor2(value.toInt());
break; break;
} }
homieNode.setProperty("color_" + String(range.index)).send(value); homieNode.setProperty("color_" + String(range.index)).send(value);
} }
bool onSetPixel(const HomieRange& range, const String& value){ bool onSetPixel(const HomieRange& range, const String& value) {
if(!range.isRange) { if (!range.isRange) {
strip.None(); strip.None();
strip.ColorSet(value.toInt()); strip.ColorSet(value.toInt());
homieNode.setProperty("pixel").send(value); homieNode.setProperty("pixel").send(value);
return true; return true;
} }
if (range.index < 0 || range.index > strip.numPixels()-1) { if (range.index < 0 || range.index > strip.numPixels() - 1) {
return false; return false;
} }
strip.None(); strip.None();
strip.setPixelColor(range.index, value.toInt()); strip.setPixelColor(range.index, value.toInt());
strip.show(); strip.show();
homieNode.setProperty("pixel_" + String(range.index)).send(value); homieNode.setProperty("pixel_" + String(range.index)).send(value);
} }
bool onSetBrightness(const HomieRange& range, const String& value){ bool onSetBrightness(const HomieRange& range, const String& value) {
long brightness= value.toInt(); long brightness = value.toInt();
if (brightness < 0 || brightness > 255) { if (brightness < 0 || brightness > 255) {
return false; return false;
} }
strip.setBrightness(brightness); strip.setBrightness(brightness);
strip.show(); strip.show();
homieNode.setProperty("brightness").send(value); homieNode.setProperty("brightness").send(value);
} }
bool onSetEffect(const HomieRange& range, const String& value){ bool onSetEffect(const HomieRange& range, const String& value) {
String effect = value; String effect = value;
effect.toLowerCase(); effect.toLowerCase();
if(effect == "scanner") { if (effect == "scanner") {
strip.Scanner(strip.Color(255, 0, 0)); strip.Scanner(strip.Color(255, 0, 0));
} }
else if(effect == "randomscanner") { else if (effect == "randomscanner") {
strip.Scanner(strip.Color(255, 0, 0), 40, true); strip.Scanner(strip.Color(255, 0, 0), 40, true);
} }
else if(effect == "larsonspiral") { else if (effect == "larsonspiral") {
strip.Scanner(strip.Color(255, 0, 0), 40, true, true); strip.Scanner(strip.Color(255, 0, 0), 40, true, true);
} }
else if(effect == "rainbowcycle") { else if (effect == "rainbowcycle") {
strip.RainbowCycle(50); strip.RainbowCycle(50);
} }
else if(effect == "theaterchase") { else if (effect == "theaterchase") {
strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0,0,255), 100); strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 100);
} }
else if(effect == "fade") { else if (effect == "fade") {
strip.Fade(strip.Color(255, 0, 0), strip.Color(0,0,255), 200, 100); strip.Fade(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 200, 100);
} }
else if(effect == "randomfade") { else if (effect == "randomfade") {
strip.RandomFade(); strip.RandomFade();
} }
else { else if (effect == "smooth") { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
strip.None(); strip.Smooth(16, 80, 50, 40);
} }
homieNode.setProperty("effect").send(value); else {
strip.None();
}
homieNode.setProperty("effect").send(value);
} }
bool onSetClear(const HomieRange& range, const String& value){ bool onSetClear(const HomieRange& range, const String& value) {
strip.None(); strip.None();
strip.clear(); strip.clear();
strip.show(); strip.show();
homieNode.setProperty("clear").send(value); homieNode.setProperty("clear").send(value);
} }
bool onSetLength(const HomieRange& range, const String& value){ bool onSetLength(const HomieRange& range, const String& value) {
strip.None(); strip.None();
strip.clear(); strip.clear();
strip.show(); strip.show();
int newLength = value.toInt(); int newLength = value.toInt();
if(newLength > 0) { if (newLength > 0) {
strip.updateLength(newLength); strip.updateLength(newLength);
} }
homieNode.setProperty("length").send(value); homieNode.setProperty("length").send(value);
} }
void loopHandler() { void loopHandler() {
strip.Update(); strip.Update();
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Homie_setFirmware("pixelprojektor", "1.0.0"); Homie_setFirmware("pixelprojektor", "1.0.0");
Homie.setLoopFunction(loopHandler); Homie.setLoopFunction(loopHandler);
homieNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel); homieNode.advertiseRange("pixel", 0, NUMPIXELS - 1).settable(onSetPixel);
homieNode.advertiseRange("color", 0, 1).settable(onSetColor); homieNode.advertiseRange("color", 0, 1).settable(onSetColor);
homieNode.advertise("brightness").settable(onSetBrightness); homieNode.advertise("brightness").settable(onSetBrightness);
homieNode.advertise("effect").settable(onSetEffect); homieNode.advertise("effect").settable(onSetEffect);
homieNode.advertise("clear").settable(onSetClear); homieNode.advertise("clear").settable(onSetClear);
homieNode.advertise("length").settable(onSetLength); homieNode.advertise("length").settable(onSetLength);
strip.begin(); strip.begin();
strip.clear(); strip.clear();
strip.setBrightness(64); strip.setBrightness(64);
strip.show(); strip.show();
Homie.setup(); Homie.setup();
ArduinoOTA.setHostname("pixelprojektor"); ArduinoOTA.setHostname("pixelprojektor");
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
strip.clear(); strip.clear();
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
strip.clear(); strip.clear();
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0)); strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0));
strip.show(); strip.show();
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
} }
void loop() { void loop() {
Homie.loop(); Homie.loop();
ArduinoOTA.handle(); ArduinoOTA.handle();
} }
// Diese Effekte müssen nach dem Umbau wieder vorhanden sein: // Diese Effekte müssen nach dem Umbau wieder vorhanden sein:
@ -179,59 +182,59 @@ void loop() {
/************ Old stuff ************/ /************ Old stuff ************/
/* /*
#define FPS 15 #define FPS 15
uint8_t effect = 0; uint8_t effect = 0;
uint8_t movingPoint_x = 3; uint8_t movingPoint_x = 3;
uint8_t movingPoint_y = 3; uint8_t movingPoint_y = 3;
uint8_t wheelPos = 0; uint8_t wheelPos = 0;
uint8_t wheelPosSlow = 0; //for slower wheelPos increment than 1 uint8_t wheelPosSlow = 0; //for slower wheelPos increment than 1
int wheelSpeed = 16; //16=+1/frame int wheelSpeed = 16; //16=+1/frame
int smoothing = 80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing. int smoothing = 80; //0 to 100. 100=no change (ultrasmooth), 0=no smoothing.
int strength = 50; //how much pixels to apply color to int strength = 50; //how much pixels to apply color to
#define EFFECT_NONE 0 #define EFFECT_NONE 0
#define EFFECT_SMOOTH 1 #define EFFECT_SMOOTH 1
#define EFFECT_SPIRAL 2 #define EFFECT_SPIRAL 2
#define EFFECT_RANDOMFADE 3 #define EFFECT_RANDOMFADE 3
#define EFFECT_CHASE 4 #define EFFECT_CHASE 4
#define EFFECT_RADAR 5 #define EFFECT_RADAR 5
#define EFFECT_LARSON 6 #define EFFECT_LARSON 6
int fadespeedmax = 5; //1 to 255 int fadespeedmax = 5; //1 to 255
int iconCountStart = 0; //for percentage calculation int iconCountStart = 0; //for percentage calculation
int iconCountdown = 0; //0=off int iconCountdown = 0; //0=off
uint8_t iconchar = 0; //last displayed char uint8_t iconchar = 0; //last displayed char
int32_t iconcolor = 0; //last icon color int32_t iconcolor = 0; //last icon color
uint16_t Index; // current step within the pattern uint16_t Index; // current step within the pattern
// int Index = 0; // Step for Effect (e.g. chase) // int Index = 0; // Step for Effect (e.g. chase)
// int state = 0; // Direction for Larson Scanner (spiral) // int state = 0; // Direction for Larson Scanner (spiral)
direction Direction; // direction to run the pattern direction Direction; // direction to run the pattern
uint8_t pixelR[NUMPIXELS]; uint8_t pixelR[NUMPIXELS];
uint8_t pixelG[NUMPIXELS]; uint8_t pixelG[NUMPIXELS];
uint8_t pixelB[NUMPIXELS]; uint8_t pixelB[NUMPIXELS];
//write to buffer, flip with showBuffer() //write to buffer, flip with showBuffer()
uint8_t pixelR_buffer[NUMPIXELS]; uint8_t pixelR_buffer[NUMPIXELS];
uint8_t pixelG_buffer[NUMPIXELS]; uint8_t pixelG_buffer[NUMPIXELS];
uint8_t pixelB_buffer[NUMPIXELS]; uint8_t pixelB_buffer[NUMPIXELS];
long lastMillis = 0; long lastMillis = 0;
long fpsdelay = 1000 / FPS; 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) { if (y % 2 == 0) {
return (y * 8 + x); return (y * 8 + x);
} else { } else {
return (y * 8 + (7 - x)); 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 x = num % 8;
int y = num / 8; int y = num / 8;
return xyToPos(x, y); 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 edge = (int)sqrt(NUMPIXELS);
int findx = edge-1; // 7 int findx = edge-1; // 7
int findy = 0; int findy = 0;
@ -274,9 +277,9 @@ int numToSpiralPos(int num) { // convert pixel number to actual 8x8 matrix posit
} }
} }
return xyToPos(findx, findy); return xyToPos(findx, findy);
} }
uint32_t wheel(byte WheelPos) { uint32_t wheel(byte WheelPos) {
WheelPos = 255 - WheelPos; WheelPos = 255 - WheelPos;
if (WheelPos < 85) { if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
@ -287,28 +290,28 @@ uint32_t wheel(byte WheelPos) {
} }
WheelPos -= 170; WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 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++) { for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c); strip.setPixelColor(i, c);
} }
strip.show(); strip.show();
} }
void led_random() void led_random()
{ {
for (int i = 0; i < strip.numPixels(); i++) { for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, wheel(random(0, 255))); strip.setPixelColor(i, wheel(random(0, 255)));
} }
strip.show(); strip.show();
} }
void showBuffer() void showBuffer()
{ {
for (int i = 0; i < strip.numPixels(); i++) { for (int i = 0; i < strip.numPixels(); i++) {
pixelR[i] = pixelR_buffer[i]; pixelR[i] = pixelR_buffer[i];
pixelG[i] = pixelG_buffer[i]; pixelG[i] = pixelG_buffer[i];
@ -316,10 +319,10 @@ void showBuffer()
strip.setPixelColor(i, pixelR[i], pixelG[i], pixelB[i]); strip.setPixelColor(i, pixelR[i], pixelG[i], pixelB[i]);
} }
strip.show(); 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; uint16_t sum = 0;
uint8_t count = 0; uint8_t count = 0;
if (i >= 8) { //up if (i >= 8) { //up
@ -339,11 +342,11 @@ uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y)
count++; count++;
} }
return sum / count; return sum / count;
} }
void led_smooth() void led_smooth()
{ {
for (int i = 0; i < strip.numPixels(); i++) { for (int i = 0; i < strip.numPixels(); i++) {
//uint8_t avgbrightness=pixelR_buffer[i]/3+pixelG_buffer[i]/3+pixelB_buffer[i]/3; //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); 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(); showBuffer();
} }
void led_movingPoint() void led_movingPoint()
{ {
uint32_t c = wheel(wheelPos); uint32_t c = wheel(wheelPos);
wheelPosSlow += wheelSpeed; wheelPosSlow += wheelSpeed;
wheelPos = (wheelPos + (wheelPosSlow / 10) ) % 255; wheelPos = (wheelPos + (wheelPosSlow / 10) ) % 255;
@ -433,18 +436,18 @@ void led_movingPoint()
movingPoint_x = startx; movingPoint_x = startx;
movingPoint_y = starty; movingPoint_y = starty;
} }
void bufferClear() void bufferClear()
{ {
for (int i = 0; i < strip.numPixels(); i++) { for (int i = 0; i < strip.numPixels(); i++) {
pixelR_buffer[i] = 0; pixelR_buffer[i] = 0;
pixelG_buffer[i] = 0; pixelG_buffer[i] = 0;
pixelB_buffer[i] = 0; pixelB_buffer[i] = 0;
} }
} }
void led_chase() void led_chase()
{ {
Index += 1; Index += 1;
if (Index > 255) { if (Index > 255) {
Index = 1; Index = 1;
@ -462,44 +465,44 @@ void led_chase()
strip.setPixelColor(i + q, 0); //turn every third pixel off strip.setPixelColor(i + q, 0); //turn every third pixel off
} }
} }
} }
void led_radar() void led_radar()
{ {
// "Sweep" in cirles... // "Sweep" in cirles...
// line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle))); // line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
} }
// Calculate 50% dimmed version of a color (used by led_larson()) // Calculate 50% dimmed version of a color (used by led_larson())
uint32_t DimColor(uint32_t color) uint32_t DimColor(uint32_t color)
{ {
// Shift R, G and B components one bit to the right // 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); uint32_t Dimcolor = strip.Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
return Dimcolor; return Dimcolor;
} }
// Returns the Red component of a 32-bit color // Returns the Red component of a 32-bit color
uint8_t Red(uint32_t color) uint8_t Red(uint32_t color)
{ {
return (color >> 16) & 0xFF; return (color >> 16) & 0xFF;
} }
// Returns the Green component of a 32-bit color // Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color) uint8_t Green(uint32_t color)
{ {
return (color >> 8) & 0xFF; return (color >> 8) & 0xFF;
} }
// Returns the Blue component of a 32-bit color // Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color) uint8_t Blue(uint32_t color)
{ {
return color & 0xFF; return color & 0xFF;
} }
void Increment() void Increment()
{ {
if (Direction == FORWARD) if (Direction == FORWARD)
{ {
Index++; Index++;
@ -516,11 +519,11 @@ void Increment()
Index = (strip.numPixels() - 1) * 2 - 1; Index = (strip.numPixels() - 1) * 2 - 1;
} }
} }
} }
void led_larson() void led_larson()
{ {
int EyeSize = 5; int EyeSize = 5;
uint32_t rgb[3] = {0}; uint32_t rgb[3] = {0};
wheelPos++; wheelPos++;
@ -548,11 +551,11 @@ void led_larson()
strip.show(); strip.show();
Increment(); Increment();
} }
void led_spiral() void led_spiral()
{ {
int every = 4; int every = 4;
wheelPos++; wheelPos++;
int qp = Index % every; int qp = Index % every;
@ -569,19 +572,19 @@ void led_spiral()
} }
strip.show(); strip.show();
} }
void led_randomfade() void led_randomfade()
{ {
for (int i = 0; i < strip.numPixels(); i++) { 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 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.setPixelColor(i, wheel(pixelR_buffer[i]));
} }
strip.show(); 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++) { for (int i = 0; i < strip.numPixels(); i++) {
uint64_t mask = 1LL << (uint64_t)i; uint64_t mask = 1LL << (uint64_t)i;
@ -598,20 +601,20 @@ void led_icon(uint8_t fontchar, uint32_t iconcolor)
} }
strip.show(); strip.show();
} }
void set_randomBuffer() void set_randomBuffer()
{ {
for (int i = 0; i < strip.numPixels(); i++) { for (int i = 0; i < strip.numPixels(); i++) {
uint32_t c = wheel(random(0, 256)); uint32_t c = wheel(random(0, 256));
pixelR_buffer[i] = (uint8_t)(c >> 16); pixelR_buffer[i] = (uint8_t)(c >> 16);
pixelG_buffer[i] = (uint8_t)(c >> 8); pixelG_buffer[i] = (uint8_t)(c >> 8);
pixelB_buffer[i] = (uint8_t)c; pixelB_buffer[i] = (uint8_t)c;
} }
} }
uint32_t parseColor(String value) { uint32_t parseColor(String value) {
if (value.charAt(0) == '#') { //solid fill if (value.charAt(0) == '#') { //solid fill
String color = value.substring(1); String color = value.substring(1);
int number = (int) strtol( &color[0], NULL, 16); int number = (int) strtol( &color[0], NULL, 16);
@ -629,10 +632,10 @@ uint32_t parseColor(String value) {
} }
return 0; return 0;
} }
bool effectHandler(const HomieRange& range, const String& value) { bool effectHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "-> " << value << endl; Homie.getLogger() << "-> " << value << endl;
int sep = value.indexOf("|"); int sep = value.indexOf("|");
@ -721,9 +724,9 @@ bool effectHandler(const HomieRange& range, const String& value) {
strip.show(); strip.show();
} }
return true; return true;
} }
bool pixelsHandler(const HomieRange& range, const String& value) { bool pixelsHandler(const HomieRange& range, const String& value) {
String remaining = value; String remaining = value;
int i = 0; int i = 0;
@ -748,9 +751,9 @@ bool pixelsHandler(const HomieRange& range, const String& value) {
strip.show(); strip.show();
return true; return true;
} }
bool iconHandler(const HomieRange& range, const String& value) { bool iconHandler(const HomieRange& range, const String& value) {
String _iconname = value; String _iconname = value;
iconcolor = strip.Color(255, 255, 255); //default color iconcolor = strip.Color(255, 255, 255); //default color
if (value[0] == '#') { //color given if (value[0] == '#') { //color given
@ -769,9 +772,9 @@ bool iconHandler(const HomieRange& range, const String& value) {
strip.show(); strip.show();
return true; return true;
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial << endl << endl; Serial << endl << endl;
@ -833,9 +836,9 @@ void setup() {
effect = EFFECT_LARSON; effect = EFFECT_LARSON;
Serial << "Setup finished" << endl; Serial << "Setup finished" << endl;
} }
void loop() { void loop() {
Homie.loop(); Homie.loop();
ArduinoOTA.handle(); ArduinoOTA.handle();
@ -872,6 +875,6 @@ void loop() {
lastMillis = currentMillis; lastMillis = currentMillis;
} }
} }
*/ */