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
} }
} }
} }
@ -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) {
@ -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

@ -84,6 +84,9 @@ bool onSetEffect(const HomieRange& range, const String& value){
else if (effect == "randomfade") { else if (effect == "randomfade") {
strip.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 { else {
strip.None(); strip.None();
} }