Refactoring: scanner, randomscanner (color-changing scanner), larsonspiral, none working

This commit is contained in:
starcalc 2017-03-01 00:02:17 +01:00
parent 8fd25937d5
commit 99e5762296
3 changed files with 496 additions and 289 deletions

View File

@ -1,293 +1,363 @@
#include "NeoPatterns.h" #include "NeoPatterns.h"
NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()) : NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()) :
Adafruit_NeoPixel(pixels, pin, type) Adafruit_NeoPixel(pixels, pin, type)
{ {
OnComplete = callback; OnComplete = callback;
} }
void NeoPatterns::Update(){ void NeoPatterns::Update() {
if((millis() - lastUpdate) > Interval) // time to update if ((millis() - lastUpdate) > Interval) // time to update
{ {
lastUpdate = millis(); lastUpdate = millis();
switch(ActivePattern) switch (ActivePattern)
{ {
case RAINBOW_CYCLE: case RAINBOW_CYCLE:
RainbowCycleUpdate(); RainbowCycleUpdate();
break; break;
case THEATER_CHASE: case THEATER_CHASE:
TheaterChaseUpdate(); TheaterChaseUpdate();
break; break;
case COLOR_WIPE: case COLOR_WIPE:
ColorWipeUpdate(); ColorWipeUpdate();
break; break;
case SCANNER: case SCANNER:
ScannerUpdate(); ScannerUpdate();
break; break;
case FADE: case FADE:
FadeUpdate(); FadeUpdate();
break; break;
case RANDOM_FADE: case RANDOM_FADE:
RandomFadeUpdate(); RandomFadeUpdate();
break; break;
case NONE: case NONE:
break; break;
default: default:
break; break;
} }
} }
} }
void NeoPatterns::Increment() void NeoPatterns::Increment()
{ {
if (Direction == FORWARD) if (Direction == FORWARD)
{ {
Index++; Index++;
if (Index >= TotalSteps) if (Index >= TotalSteps)
{ {
Index = 0; Index = 0;
if (OnComplete != NULL) if (OnComplete != NULL)
{ {
OnComplete(); // call the comlpetion callback OnComplete(); // call the comlpetion callback
} }
} }
} }
else // Direction == REVERSE else // Direction == REVERSE
{ {
--Index; --Index;
if (Index <= 0) if (Index <= 0)
{ {
Index = TotalSteps-1; Index = TotalSteps - 1;
if (OnComplete != NULL) if (OnComplete != NULL)
{ {
OnComplete(); // call the comlpetion callback OnComplete(); // call the comlpetion callback
} }
} }
} }
} }
void NeoPatterns::Reverse(){ void NeoPatterns::Reverse() {
if (Direction == FORWARD) if (Direction == FORWARD)
{ {
Direction = REVERSE; Direction = REVERSE;
Index = TotalSteps-1; Index = TotalSteps - 1;
} }
else else
{ {
Direction = FORWARD; Direction = FORWARD;
Index = 0; Index = 0;
} }
} }
void NeoPatterns::None(){ void NeoPatterns::None() {
if(ActivePattern != NONE) { if (ActivePattern != NONE) {
clear(); clear();
show(); show();
} }
ActivePattern = NONE; ActivePattern = NONE;
} }
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir){ /****************** Effects ******************/
ActivePattern = RAINBOW_CYCLE;
Interval = interval; void NeoPatterns::RainbowCycle(uint8_t interval, direction dir) {
TotalSteps = 255; ActivePattern = RAINBOW_CYCLE;
Index = 0; Interval = interval;
Direction = dir; TotalSteps = 255;
Index = 0;
Direction = dir;
} }
void NeoPatterns::RainbowCycleUpdate() void NeoPatterns::RainbowCycleUpdate()
{ {
for(int i=0; i< numPixels(); i++) for (int i = 0; i < numPixels(); i++)
{ {
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255)); setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
} }
show(); show();
Increment(); Increment();
} }
void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir){ void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir) {
ActivePattern = THEATER_CHASE; ActivePattern = THEATER_CHASE;
Interval = interval; Interval = interval;
TotalSteps = numPixels(); TotalSteps = numPixels();
Color1 = color1; Color1 = color1;
Color2 = color2; Color2 = color2;
Index = 0; Index = 0;
Direction = dir; Direction = dir;
} }
void NeoPatterns::TheaterChaseUpdate(){ void NeoPatterns::TheaterChaseUpdate() {
for(int i=0; i< numPixels(); i++) for (int i = 0; i < numPixels(); i++)
{ {
if ((i + Index) % 3 == 0) if ((i + Index) % 3 == 0)
{ {
setPixelColor(i, Color1); setPixelColor(i, Color1);
} }
else else
{ {
setPixelColor(i, Color2); setPixelColor(i, Color2);
} }
} }
show(); show();
Increment(); Increment();
} }
void NeoPatterns::ColorWipe(uint32_t color, uint8_t interval, direction dir) void NeoPatterns::ColorWipe(uint32_t color, uint8_t interval, direction dir)
{ {
ActivePattern = COLOR_WIPE; ActivePattern = COLOR_WIPE;
Interval = interval; Interval = interval;
TotalSteps = numPixels(); TotalSteps = numPixels();
Color1 = color; Color1 = color;
Index = 0; Index = 0;
Direction = dir; Direction = dir;
} }
// Update the Color Wipe Pattern // Update the Color Wipe Pattern
void NeoPatterns::ColorWipeUpdate() void NeoPatterns::ColorWipeUpdate()
{ {
setPixelColor(Index, Color1); setPixelColor(Index, Color1);
show(); show();
Increment(); Increment();
} }
// Initialize for a SCANNNER // Initialize for a SCANNNER
void NeoPatterns::Scanner(uint32_t color1, uint8_t interval, bool colorful) void NeoPatterns::Scanner(uint32_t color1, uint8_t interval, bool colorful, bool spiral)
{ {
ActivePattern = SCANNER; ActivePattern = SCANNER;
Interval = interval; Interval = interval;
TotalSteps = (numPixels() - 1) * 2; TotalSteps = (numPixels() - 1) * 2;
Color1 = color1; Color1 = color1;
Index = 0; Index = 0;
wPos = 0; wPos = 0;
this->colorful = colorful; this->colorful = colorful;
this->spiral = spiral;
} }
// Update the Scanner Pattern // Update the Scanner Pattern
void NeoPatterns::ScannerUpdate() void NeoPatterns::ScannerUpdate()
{ {
if(colorful) { if (colorful) {
Color1 = Wheel(wPos); Color1 = Wheel(wPos);
if(wPos >= 255) { if (wPos >= 255) {
wPos =0; wPos = 0;
} }
else { else {
wPos++; wPos++;
} }
} }
for (int i = 0; i < numPixels(); i++) for (int i = 0; i < numPixels(); i++)
{ {
if (i == Index) // Scan Pixel to the right int finalpos;
{ if (spiral) {
setPixelColor(i, Color1); finalpos = numToSpiralPos(i);
} }
else if (i == TotalSteps - Index) // Scan Pixel to the left else
{ {
setPixelColor(i, Color1); finalpos=i;
} }
else // Fading tail if (i == Index) // Scan Pixel to the right
{ {
setPixelColor(i, DimColor(getPixelColor(i))); setPixelColor(finalpos, Color1);
} }
} else if (i == TotalSteps - Index) // Scan Pixel to the left
show(); {
Increment(); setPixelColor(finalpos, Color1);
}
else // Fading tail
{
setPixelColor(finalpos, DimColor(getPixelColor(finalpos)));
}
}
show();
Increment();
} }
void NeoPatterns::Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir) void NeoPatterns::Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir)
{ {
ActivePattern = FADE; ActivePattern = FADE;
Interval = interval; Interval = interval;
TotalSteps = steps; TotalSteps = steps;
Color1 = color1; Color1 = color1;
Color2 = color2; Color2 = color2;
Index = 0; Index = 0;
Direction = dir; Direction = dir;
} }
// Update the Fade Pattern // Update the Fade Pattern
void NeoPatterns::FadeUpdate() void NeoPatterns::FadeUpdate()
{ {
// Calculate linear interpolation between Color1 and Color2 // Calculate linear interpolation between Color1 and Color2
// Optimise order of operations to minimize truncation error // Optimise order of operations to minimize truncation error
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps; uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps; uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps; uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
ColorSet(Color(red, green, blue)); ColorSet(Color(red, green, blue));
show(); show();
Increment(); Increment();
} }
void NeoPatterns::RandomFade(uint8_t interval ){ void NeoPatterns::RandomFade(uint8_t interval ) {
ActivePattern = RANDOM_FADE; ActivePattern = RANDOM_FADE;
Interval = interval; Interval = interval;
TotalSteps = 255; TotalSteps = 255;
Index = 0; Index = 0;
} }
void NeoPatterns::RandomFadeUpdate(){ void NeoPatterns::RandomFadeUpdate() {
ColorSet(Wheel(Index)); ColorSet(Wheel(Index));
Increment(); Increment();
} }
void NeoPatterns::SetColor1(uint32_t color){ /****************** Helper functions ******************/
Color1 = color;
void NeoPatterns::SetColor1(uint32_t color) {
Color1 = color;
} }
void NeoPatterns::SetColor2(uint32_t color){ void NeoPatterns::SetColor2(uint32_t color) {
Color2 = color; Color2 = color;
} }
// Calculate 50% dimmed version of a color (used by ScannerUpdate) // Calculate 50% dimmed version of a color (used by ScannerUpdate)
uint32_t NeoPatterns::DimColor(uint32_t color) uint32_t NeoPatterns::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 = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1); uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
return dimColor; return dimColor;
} }
// Set all pixels to a color (synchronously) // Set all pixels to a color (synchronously)
void NeoPatterns::ColorSet(uint32_t color) void NeoPatterns::ColorSet(uint32_t color)
{ {
for (int i = 0; i < numPixels(); i++) for (int i = 0; i < numPixels(); i++)
{ {
setPixelColor(i, color); setPixelColor(i, color);
} }
show(); show();
} }
// Returns the Red component of a 32-bit color // Returns the Red component of a 32-bit color
uint8_t NeoPatterns::Red(uint32_t color) uint8_t NeoPatterns::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 NeoPatterns::Green(uint32_t color) uint8_t NeoPatterns::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 NeoPatterns::Blue(uint32_t color) uint8_t NeoPatterns::Blue(uint32_t color)
{ {
return color & 0xFF; return color & 0xFF;
} }
// Input a value 0 to 255 to get a color value. // Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r. // The colors are a transition r - g - b - back to r.
uint32_t NeoPatterns::Wheel(byte WheelPos) uint32_t NeoPatterns::Wheel(byte WheelPos)
{ {
WheelPos = 255 - WheelPos; WheelPos = 255 - WheelPos;
if(WheelPos < 85) if (WheelPos < 85)
{ {
return Color(255 - WheelPos * 3, 0, WheelPos * 3); return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} }
else if(WheelPos < 170) else if (WheelPos < 170)
{ {
WheelPos -= 85; WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3); return Color(0, WheelPos * 3, 255 - WheelPos * 3);
} }
else else
{ {
WheelPos -= 170; WheelPos -= 170;
return Color(WheelPos * 3, 255 - WheelPos * 3, 0); return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} }
} }
// Convert x y pixel position to matrix position
uint8_t NeoPatterns::xyToPos(int x, int y) {
if (y % 2 == 0) {
return (y * 8 + x);
} else {
return (y * 8 + (7 - x));
}
}
// Convert pixel number to actual 8x8 matrix position in a spiral
uint8_t NeoPatterns::numToSpiralPos(int num) {
int edge = (int)sqrt(numPixels());
int findx = edge-1; // 7
int findy = 0;
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
if (num < edge) {
return num; // trivial
}
for (int i = edge; i <= num; i++)
{
count++;
if (count == stepsize) {
count = 0;
// Change direction
dir++;
stepnumber++;
if (stepnumber == 2) {
stepsize -= 1;
stepnumber = 0;
}
if (dir == 4) {
dir = 0;
}
}
switch (dir) {
case 0:
findx++;
break;
case 1:
findy++;
break;
case 2:
findx--;
break;
case 3:
findy--;
break;
}
}
return xyToPos(findx, findy);
}

View File

@ -7,53 +7,56 @@ enum direction { FORWARD, REVERSE };
class NeoPatterns : public Adafruit_NeoPixel class NeoPatterns : public Adafruit_NeoPixel
{ {
public: public:
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()); NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)());
void Update(); void Update();
void Reverse(); void Reverse();
void None(); void None();
void RainbowCycle(uint8_t interval, direction dir = FORWARD); void RainbowCycle(uint8_t interval, direction dir = FORWARD);
void RainbowCycleUpdate(); void RainbowCycleUpdate();
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD); void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD);
void TheaterChaseUpdate(); void TheaterChaseUpdate();
void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD); void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD);
void ColorWipeUpdate(); void ColorWipeUpdate();
void Scanner(uint32_t color1, uint8_t interval = 40,bool colorful = false); void Scanner(uint32_t color1, uint8_t interval = 40, bool colorful = false, bool spiral = false);
void ScannerUpdate(); void ScannerUpdate();
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD); void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD);
void FadeUpdate(); void FadeUpdate();
void RandomFade(uint8_t interval = 100); void RandomFade(uint8_t interval = 100);
void RandomFadeUpdate(); void RandomFadeUpdate();
void SetColor1(uint32_t color); void SetColor1(uint32_t color);
void SetColor2(uint32_t color); void SetColor2(uint32_t color);
//Utilities //Utilities
void ColorSet(uint32_t color); void ColorSet(uint32_t color);
uint8_t Red(uint32_t color); uint8_t Red(uint32_t color);
uint8_t Green(uint32_t color); uint8_t Green(uint32_t color);
uint8_t Blue(uint32_t color); uint8_t Blue(uint32_t color);
uint32_t Wheel(byte WheelPos); uint32_t Wheel(byte WheelPos);
uint8_t numToSpiralPos(int num);
uint8_t xyToPos(int x, int y);
private: private:
// Member Variables: // Member Variables:
pattern ActivePattern; // which pattern is running pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern direction Direction; // direction to run the pattern
unsigned long Interval; // milliseconds between updates unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position unsigned long lastUpdate; // last update of position
uint32_t Color1, Color2; // What colors are in use uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern uint16_t Index; // current step within the pattern
byte wPos; byte wPos;
bool colorful; bool colorful;
bool spiral;
uint32_t DimColor(uint32_t color); uint32_t DimColor(uint32_t color);
void Increment(); void Increment();
void (*OnComplete)(); // Callback on completion of pattern void (*OnComplete)(); // Callback on completion of pattern
}; };

View File

@ -10,11 +10,177 @@
#define PIN D1 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2) #define PIN D1 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2)
#define NUMPIXELS 64 #define NUMPIXELS 64
#define FPS 15 void StripComplete() {
return;
}
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete);
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 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 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 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);
}
void loopHandler() {
strip.Update();
}
void setup() {
Serial.begin(115200);
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);
strip.begin();
strip.clear();
strip.setBrightness(64);
strip.show();
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();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
}
// Diese Effekte müssen nach dem Umbau wieder vorhanden sein:
/*
case EFFECT_SMOOTH:
led_movingPoint();
led_smooth();
break;
case EFFECT_SPIRAL:
led_spiral();
break;
case EFFECT_RANDOMFADE:
led_randomfade();
break;
case EFFECT_CHASE:
led_chase();
break;
case EFFECT_RADAR:
led_radar();
break;
case EFFECT_LARSON:
led_larson();
break;
*/
/************ Old stuff ************/
/*
#define FPS 15
uint8_t effect = 0; uint8_t effect = 0;
#define EFFECT_NONE 0
#define EFFECT_SMOOTH 1
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;
@ -22,13 +188,14 @@ 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_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
@ -38,14 +205,6 @@ 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
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
HomieNode homieNode("pixel", "commands");
uint8_t pixelR[NUMPIXELS]; uint8_t pixelR[NUMPIXELS];
uint8_t pixelG[NUMPIXELS]; uint8_t pixelG[NUMPIXELS];
uint8_t pixelB[NUMPIXELS]; uint8_t pixelB[NUMPIXELS];
@ -57,6 +216,7 @@ 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);
@ -72,16 +232,17 @@ int numToPos(int num) { //convert pixel number to actual 8x8 matrix position
} }
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 findx = 7; int edge = (int)sqrt(NUMPIXELS);
int findx = edge-1; // 7
int findy = 0; int findy = 0;
int stepsize = 7; // 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
if (num < 8) { if (num < edge) {
return num; // trivial return num; // trivial
} }
for (int i = 8; i <= num; i++) for (int i = edge; i <= num; i++)
{ {
count++; count++;
if (count == stepsize) { if (count == stepsize) {
@ -177,24 +338,6 @@ uint8_t getAverage(uint8_t array[NUMPIXELS], uint8_t i, int x, int y)
sum += array[i + 1]; sum += array[i + 1];
count++; count++;
} }
/*
if (i>=(8+1)){ //up left
sum+=array[i-8-1];
count++;
}
if (i<(64-8-1)){ //down left
sum+=array[i+8-1];
count++;
}
if (i>=(8-1)){ //up right
sum+=array[i-8+1];
count++;
}
if (i<(64-8+1)){ //down right
sum+=array[i+8+1];
count++;
}*/
return sum / count; return sum / count;
} }
@ -414,7 +557,7 @@ void led_spiral()
wheelPos++; wheelPos++;
int qp = Index % every; int qp = Index % every;
Index++; Index++;
if (Index >= strip.numPixels()-1) { if (Index >= strip.numPixels() - 1) {
Index = 0; Index = 0;
} }
int q = Index % every; int q = Index % every;
@ -673,7 +816,7 @@ void setup() {
Serial.println("\nEnd"); Serial.println("\nEnd");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
strip.setPixelColor(numToPos((progress / (total / strip.numPixels()))), strip.Color(255, 255, 255)); strip.setPixelColor(numToPos((progress / (total / strip.numPixels()))), strip.Color(100, 0, 0));
strip.show(); strip.show();
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}); });
@ -687,11 +830,6 @@ void setup() {
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
// led_fill(strip.Color(0, 0, 0));
// Initialer Effekt
//effect = EFFECT_CHASE;
//effect = EFFECT_SPIRAL;
effect = EFFECT_LARSON; effect = EFFECT_LARSON;
Serial << "Setup finished" << endl; Serial << "Setup finished" << endl;
@ -736,8 +874,4 @@ void loop() {
} }
*/