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

@ -87,6 +87,8 @@ void NeoPatterns::None(){
ActivePattern = NONE;
}
/****************** Effects ******************/
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir) {
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
@ -149,7 +151,7 @@ void NeoPatterns::ColorWipeUpdate()
}
// 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;
Interval = interval;
@ -158,6 +160,7 @@ void NeoPatterns::Scanner(uint32_t color1, uint8_t interval, bool colorful)
Index = 0;
wPos = 0;
this->colorful = colorful;
this->spiral = spiral;
}
// Update the Scanner Pattern
@ -174,17 +177,25 @@ void NeoPatterns::ScannerUpdate()
}
for (int i = 0; i < numPixels(); i++)
{
int finalpos;
if (spiral) {
finalpos = numToSpiralPos(i);
}
else
{
finalpos=i;
}
if (i == Index) // Scan Pixel to the right
{
setPixelColor(i, Color1);
setPixelColor(finalpos, Color1);
}
else if (i == TotalSteps - Index) // Scan Pixel to the left
{
setPixelColor(i, Color1);
setPixelColor(finalpos, Color1);
}
else // Fading tail
{
setPixelColor(i, DimColor(getPixelColor(i)));
setPixelColor(finalpos, DimColor(getPixelColor(finalpos)));
}
}
show();
@ -228,6 +239,8 @@ void NeoPatterns::RandomFadeUpdate(){
Increment();
}
/****************** Helper functions ******************/
void NeoPatterns::SetColor1(uint32_t color) {
Color1 = color;
}
@ -272,7 +285,7 @@ uint8_t NeoPatterns::Blue(uint32_t color)
}
// 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)
{
WheelPos = 255 - WheelPos;
@ -291,3 +304,60 @@ uint32_t NeoPatterns::Wheel(byte WheelPos)
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

@ -20,7 +20,7 @@ void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction
void TheaterChaseUpdate();
void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD);
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 Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD);
void FadeUpdate();
@ -35,6 +35,8 @@ uint8_t Red(uint32_t color);
uint8_t Green(uint32_t color);
uint8_t Blue(uint32_t color);
uint32_t Wheel(byte WheelPos);
uint8_t numToSpiralPos(int num);
uint8_t xyToPos(int x, int y);
private:
@ -51,6 +53,7 @@ uint16_t Index; // current step within the pattern
byte wPos;
bool colorful;
bool spiral;
uint32_t DimColor(uint32_t color);
void Increment();

View File

@ -10,11 +10,177 @@
#define PIN D1 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2)
#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;
#define EFFECT_NONE 0
#define EFFECT_SMOOTH 1
uint8_t movingPoint_x = 3;
uint8_t movingPoint_y = 3;
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 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
@ -38,14 +205,6 @@ 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
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
HomieNode homieNode("pixel", "commands");
uint8_t pixelR[NUMPIXELS];
uint8_t pixelG[NUMPIXELS];
uint8_t pixelB[NUMPIXELS];
@ -57,6 +216,7 @@ uint8_t pixelB_buffer[NUMPIXELS];
long lastMillis = 0;
long fpsdelay = 1000 / FPS;
int xyToPos(int x, int y) { //convert x y pixel position to matrix position
if (y % 2 == 0) {
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 findx = 7;
int edge = (int)sqrt(NUMPIXELS);
int findx = edge-1; // 7
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 count = -1;
int dir = 1; // direction: 0 = incX, 1=incY, 2=decX, 3=decY
if (num < 8) {
if (num < edge) {
return num; // trivial
}
for (int i = 8; i <= num; i++)
for (int i = edge; i <= num; i++)
{
count++;
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];
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;
}
@ -673,7 +816,7 @@ void setup() {
Serial.println("\nEnd");
});
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();
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
@ -687,11 +830,6 @@ void setup() {
});
ArduinoOTA.begin();
// led_fill(strip.Color(0, 0, 0));
// Initialer Effekt
//effect = EFFECT_CHASE;
//effect = EFFECT_SPIRAL;
effect = EFFECT_LARSON;
Serial << "Setup finished" << endl;
@ -736,8 +874,4 @@ void loop() {
}
*/