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

@ -6,11 +6,11 @@ NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*call
OnComplete = callback;
}
void NeoPatterns::Update(){
if((millis() - lastUpdate) > Interval) // time to update
void NeoPatterns::Update() {
if ((millis() - lastUpdate) > Interval) // time to update
{
lastUpdate = millis();
switch(ActivePattern)
switch (ActivePattern)
{
case RAINBOW_CYCLE:
RainbowCycleUpdate();
@ -57,7 +57,7 @@ void NeoPatterns::Increment()
--Index;
if (Index <= 0)
{
Index = TotalSteps-1;
Index = TotalSteps - 1;
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
@ -66,11 +66,11 @@ void NeoPatterns::Increment()
}
}
void NeoPatterns::Reverse(){
void NeoPatterns::Reverse() {
if (Direction == FORWARD)
{
Direction = REVERSE;
Index = TotalSteps-1;
Index = TotalSteps - 1;
}
else
{
@ -79,15 +79,17 @@ void NeoPatterns::Reverse(){
}
}
void NeoPatterns::None(){
if(ActivePattern != NONE) {
void NeoPatterns::None() {
if (ActivePattern != NONE) {
clear();
show();
}
ActivePattern = NONE;
}
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir){
/****************** Effects ******************/
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir) {
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255;
@ -97,7 +99,7 @@ void NeoPatterns::RainbowCycle(uint8_t interval, direction dir){
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));
}
@ -105,7 +107,7 @@ void NeoPatterns::RainbowCycleUpdate()
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;
Interval = interval;
TotalSteps = numPixels();
@ -114,8 +116,8 @@ void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interva
Index = 0;
Direction = dir;
}
void NeoPatterns::TheaterChaseUpdate(){
for(int i=0; i< numPixels(); i++)
void NeoPatterns::TheaterChaseUpdate() {
for (int i = 0; i < numPixels(); i++)
{
if ((i + Index) % 3 == 0)
{
@ -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,15 +160,16 @@ 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
void NeoPatterns::ScannerUpdate()
{
if(colorful) {
if (colorful) {
Color1 = Wheel(wPos);
if(wPos >= 255) {
wPos =0;
if (wPos >= 255) {
wPos = 0;
}
else {
wPos++;
@ -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();
@ -217,21 +228,23 @@ void NeoPatterns::FadeUpdate()
Increment();
}
void NeoPatterns::RandomFade(uint8_t interval ){
void NeoPatterns::RandomFade(uint8_t interval ) {
ActivePattern = RANDOM_FADE;
Interval = interval;
TotalSteps = 255;
Index = 0;
}
void NeoPatterns::RandomFadeUpdate(){
void NeoPatterns::RandomFadeUpdate() {
ColorSet(Wheel(Index));
Increment();
}
void NeoPatterns::SetColor1(uint32_t color){
/****************** Helper functions ******************/
void NeoPatterns::SetColor1(uint32_t color) {
Color1 = color;
}
void NeoPatterns::SetColor2(uint32_t color){
void NeoPatterns::SetColor2(uint32_t color) {
Color2 = color;
}
@ -272,15 +285,15 @@ 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;
if(WheelPos < 85)
if (WheelPos < 85)
{
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else if(WheelPos < 170)
else if (WheelPos < 170)
{
WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
@ -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

@ -7,53 +7,56 @@ enum direction { FORWARD, REVERSE };
class NeoPatterns : public Adafruit_NeoPixel
{
public:
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)());
public:
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)());
void Update();
void Update();
void Reverse();
void None();
void RainbowCycle(uint8_t interval, direction dir = FORWARD);
void RainbowCycleUpdate();
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD);
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 ScannerUpdate();
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD);
void FadeUpdate();
void RandomFade(uint8_t interval = 100);
void RandomFadeUpdate();
void Reverse();
void None();
void RainbowCycle(uint8_t interval, direction dir = FORWARD);
void RainbowCycleUpdate();
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD);
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, bool spiral = false);
void ScannerUpdate();
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD);
void FadeUpdate();
void RandomFade(uint8_t interval = 100);
void RandomFadeUpdate();
void SetColor1(uint32_t color);
void SetColor2(uint32_t color);
//Utilities
void ColorSet(uint32_t color);
uint8_t Red(uint32_t color);
uint8_t Green(uint32_t color);
uint8_t Blue(uint32_t color);
uint32_t Wheel(byte WheelPos);
void SetColor1(uint32_t color);
void SetColor2(uint32_t color);
//Utilities
void ColorSet(uint32_t color);
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:
private:
// Member Variables:
pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern
// Member Variables:
pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern
unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position
unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position
uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern
uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern
byte wPos;
bool colorful;
byte wPos;
bool colorful;
bool spiral;
uint32_t DimColor(uint32_t color);
void Increment();
void (*OnComplete)(); // Callback on completion of pattern
uint32_t DimColor(uint32_t color);
void Increment();
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 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;
}
@ -414,7 +557,7 @@ void led_spiral()
wheelPos++;
int qp = Index % every;
Index++;
if (Index >= strip.numPixels()-1) {
if (Index >= strip.numPixels() - 1) {
Index = 0;
}
int q = Index % every;
@ -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() {
}
*/