Refactoring: scanner, randomscanner (color-changing scanner), larsonspiral, none working
This commit is contained in:
parent
8fd25937d5
commit
99e5762296
|
@ -1,293 +1,363 @@
|
|||
#include "NeoPatterns.h"
|
||||
|
||||
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(){
|
||||
if((millis() - lastUpdate) > Interval) // time to update
|
||||
{
|
||||
lastUpdate = millis();
|
||||
switch(ActivePattern)
|
||||
{
|
||||
case RAINBOW_CYCLE:
|
||||
RainbowCycleUpdate();
|
||||
break;
|
||||
case THEATER_CHASE:
|
||||
TheaterChaseUpdate();
|
||||
break;
|
||||
case COLOR_WIPE:
|
||||
ColorWipeUpdate();
|
||||
break;
|
||||
case SCANNER:
|
||||
ScannerUpdate();
|
||||
break;
|
||||
case FADE:
|
||||
FadeUpdate();
|
||||
break;
|
||||
case RANDOM_FADE:
|
||||
RandomFadeUpdate();
|
||||
break;
|
||||
case NONE:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void NeoPatterns::Update() {
|
||||
if ((millis() - lastUpdate) > Interval) // time to update
|
||||
{
|
||||
lastUpdate = millis();
|
||||
switch (ActivePattern)
|
||||
{
|
||||
case RAINBOW_CYCLE:
|
||||
RainbowCycleUpdate();
|
||||
break;
|
||||
case THEATER_CHASE:
|
||||
TheaterChaseUpdate();
|
||||
break;
|
||||
case COLOR_WIPE:
|
||||
ColorWipeUpdate();
|
||||
break;
|
||||
case SCANNER:
|
||||
ScannerUpdate();
|
||||
break;
|
||||
case FADE:
|
||||
FadeUpdate();
|
||||
break;
|
||||
case RANDOM_FADE:
|
||||
RandomFadeUpdate();
|
||||
break;
|
||||
case NONE:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NeoPatterns::Increment()
|
||||
{
|
||||
if (Direction == FORWARD)
|
||||
{
|
||||
Index++;
|
||||
if (Index >= TotalSteps)
|
||||
{
|
||||
Index = 0;
|
||||
if (OnComplete != NULL)
|
||||
{
|
||||
OnComplete(); // call the comlpetion callback
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Direction == REVERSE
|
||||
{
|
||||
--Index;
|
||||
if (Index <= 0)
|
||||
{
|
||||
Index = TotalSteps-1;
|
||||
if (OnComplete != NULL)
|
||||
{
|
||||
OnComplete(); // call the comlpetion callback
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Direction == FORWARD)
|
||||
{
|
||||
Index++;
|
||||
if (Index >= TotalSteps)
|
||||
{
|
||||
Index = 0;
|
||||
if (OnComplete != NULL)
|
||||
{
|
||||
OnComplete(); // call the comlpetion callback
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Direction == REVERSE
|
||||
{
|
||||
--Index;
|
||||
if (Index <= 0)
|
||||
{
|
||||
Index = TotalSteps - 1;
|
||||
if (OnComplete != NULL)
|
||||
{
|
||||
OnComplete(); // call the comlpetion callback
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NeoPatterns::Reverse(){
|
||||
if (Direction == FORWARD)
|
||||
{
|
||||
Direction = REVERSE;
|
||||
Index = TotalSteps-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Direction = FORWARD;
|
||||
Index = 0;
|
||||
}
|
||||
void NeoPatterns::Reverse() {
|
||||
if (Direction == FORWARD)
|
||||
{
|
||||
Direction = REVERSE;
|
||||
Index = TotalSteps - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Direction = FORWARD;
|
||||
Index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void NeoPatterns::None(){
|
||||
if(ActivePattern != NONE) {
|
||||
clear();
|
||||
show();
|
||||
}
|
||||
ActivePattern = NONE;
|
||||
void NeoPatterns::None() {
|
||||
if (ActivePattern != NONE) {
|
||||
clear();
|
||||
show();
|
||||
}
|
||||
ActivePattern = NONE;
|
||||
}
|
||||
|
||||
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir){
|
||||
ActivePattern = RAINBOW_CYCLE;
|
||||
Interval = interval;
|
||||
TotalSteps = 255;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
/****************** Effects ******************/
|
||||
|
||||
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir) {
|
||||
ActivePattern = RAINBOW_CYCLE;
|
||||
Interval = interval;
|
||||
TotalSteps = 255;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
}
|
||||
|
||||
void NeoPatterns::RainbowCycleUpdate()
|
||||
{
|
||||
for(int i=0; i< numPixels(); i++)
|
||||
{
|
||||
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
|
||||
}
|
||||
show();
|
||||
Increment();
|
||||
for (int i = 0; i < numPixels(); i++)
|
||||
{
|
||||
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
|
||||
}
|
||||
show();
|
||||
Increment();
|
||||
}
|
||||
|
||||
void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir){
|
||||
ActivePattern = THEATER_CHASE;
|
||||
Interval = interval;
|
||||
TotalSteps = numPixels();
|
||||
Color1 = color1;
|
||||
Color2 = color2;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir) {
|
||||
ActivePattern = THEATER_CHASE;
|
||||
Interval = interval;
|
||||
TotalSteps = numPixels();
|
||||
Color1 = color1;
|
||||
Color2 = color2;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
}
|
||||
void NeoPatterns::TheaterChaseUpdate(){
|
||||
for(int i=0; i< numPixels(); i++)
|
||||
{
|
||||
if ((i + Index) % 3 == 0)
|
||||
{
|
||||
setPixelColor(i, Color1);
|
||||
}
|
||||
else
|
||||
{
|
||||
setPixelColor(i, Color2);
|
||||
}
|
||||
}
|
||||
show();
|
||||
Increment();
|
||||
void NeoPatterns::TheaterChaseUpdate() {
|
||||
for (int i = 0; i < numPixels(); i++)
|
||||
{
|
||||
if ((i + Index) % 3 == 0)
|
||||
{
|
||||
setPixelColor(i, Color1);
|
||||
}
|
||||
else
|
||||
{
|
||||
setPixelColor(i, Color2);
|
||||
}
|
||||
}
|
||||
show();
|
||||
Increment();
|
||||
}
|
||||
|
||||
void NeoPatterns::ColorWipe(uint32_t color, uint8_t interval, direction dir)
|
||||
{
|
||||
ActivePattern = COLOR_WIPE;
|
||||
Interval = interval;
|
||||
TotalSteps = numPixels();
|
||||
Color1 = color;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
ActivePattern = COLOR_WIPE;
|
||||
Interval = interval;
|
||||
TotalSteps = numPixels();
|
||||
Color1 = color;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
}
|
||||
|
||||
// Update the Color Wipe Pattern
|
||||
void NeoPatterns::ColorWipeUpdate()
|
||||
{
|
||||
setPixelColor(Index, Color1);
|
||||
show();
|
||||
Increment();
|
||||
setPixelColor(Index, Color1);
|
||||
show();
|
||||
Increment();
|
||||
}
|
||||
|
||||
// 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;
|
||||
TotalSteps = (numPixels() - 1) * 2;
|
||||
Color1 = color1;
|
||||
Index = 0;
|
||||
wPos = 0;
|
||||
this->colorful = colorful;
|
||||
ActivePattern = SCANNER;
|
||||
Interval = interval;
|
||||
TotalSteps = (numPixels() - 1) * 2;
|
||||
Color1 = color1;
|
||||
Index = 0;
|
||||
wPos = 0;
|
||||
this->colorful = colorful;
|
||||
this->spiral = spiral;
|
||||
}
|
||||
|
||||
// Update the Scanner Pattern
|
||||
void NeoPatterns::ScannerUpdate()
|
||||
{
|
||||
if(colorful) {
|
||||
Color1 = Wheel(wPos);
|
||||
if(wPos >= 255) {
|
||||
wPos =0;
|
||||
}
|
||||
else {
|
||||
wPos++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < numPixels(); i++)
|
||||
{
|
||||
if (i == Index) // Scan Pixel to the right
|
||||
{
|
||||
setPixelColor(i, Color1);
|
||||
}
|
||||
else if (i == TotalSteps - Index) // Scan Pixel to the left
|
||||
{
|
||||
setPixelColor(i, Color1);
|
||||
}
|
||||
else // Fading tail
|
||||
{
|
||||
setPixelColor(i, DimColor(getPixelColor(i)));
|
||||
}
|
||||
}
|
||||
show();
|
||||
Increment();
|
||||
if (colorful) {
|
||||
Color1 = Wheel(wPos);
|
||||
if (wPos >= 255) {
|
||||
wPos = 0;
|
||||
}
|
||||
else {
|
||||
wPos++;
|
||||
}
|
||||
}
|
||||
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(finalpos, Color1);
|
||||
}
|
||||
else if (i == TotalSteps - Index) // Scan Pixel to the left
|
||||
{
|
||||
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)
|
||||
{
|
||||
ActivePattern = FADE;
|
||||
Interval = interval;
|
||||
TotalSteps = steps;
|
||||
Color1 = color1;
|
||||
Color2 = color2;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
ActivePattern = FADE;
|
||||
Interval = interval;
|
||||
TotalSteps = steps;
|
||||
Color1 = color1;
|
||||
Color2 = color2;
|
||||
Index = 0;
|
||||
Direction = dir;
|
||||
}
|
||||
|
||||
// Update the Fade Pattern
|
||||
void NeoPatterns::FadeUpdate()
|
||||
{
|
||||
// Calculate linear interpolation between Color1 and Color2
|
||||
// Optimise order of operations to minimize truncation error
|
||||
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(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;
|
||||
// Calculate linear interpolation between Color1 and Color2
|
||||
// Optimise order of operations to minimize truncation error
|
||||
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(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;
|
||||
|
||||
ColorSet(Color(red, green, blue));
|
||||
show();
|
||||
Increment();
|
||||
ColorSet(Color(red, green, blue));
|
||||
show();
|
||||
Increment();
|
||||
}
|
||||
|
||||
void NeoPatterns::RandomFade(uint8_t interval ){
|
||||
ActivePattern = RANDOM_FADE;
|
||||
Interval = interval;
|
||||
TotalSteps = 255;
|
||||
Index = 0;
|
||||
void NeoPatterns::RandomFade(uint8_t interval ) {
|
||||
ActivePattern = RANDOM_FADE;
|
||||
Interval = interval;
|
||||
TotalSteps = 255;
|
||||
Index = 0;
|
||||
}
|
||||
void NeoPatterns::RandomFadeUpdate(){
|
||||
ColorSet(Wheel(Index));
|
||||
Increment();
|
||||
void NeoPatterns::RandomFadeUpdate() {
|
||||
ColorSet(Wheel(Index));
|
||||
Increment();
|
||||
}
|
||||
|
||||
void NeoPatterns::SetColor1(uint32_t color){
|
||||
Color1 = color;
|
||||
/****************** Helper functions ******************/
|
||||
|
||||
void NeoPatterns::SetColor1(uint32_t color) {
|
||||
Color1 = color;
|
||||
}
|
||||
void NeoPatterns::SetColor2(uint32_t color){
|
||||
Color2 = color;
|
||||
void NeoPatterns::SetColor2(uint32_t color) {
|
||||
Color2 = color;
|
||||
}
|
||||
|
||||
// Calculate 50% dimmed version of a color (used by ScannerUpdate)
|
||||
uint32_t NeoPatterns::DimColor(uint32_t color)
|
||||
{
|
||||
// Shift R, G and B components one bit to the right
|
||||
uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
|
||||
return dimColor;
|
||||
// Shift R, G and B components one bit to the right
|
||||
uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
|
||||
return dimColor;
|
||||
}
|
||||
|
||||
// Set all pixels to a color (synchronously)
|
||||
void NeoPatterns::ColorSet(uint32_t color)
|
||||
{
|
||||
for (int i = 0; i < numPixels(); i++)
|
||||
{
|
||||
setPixelColor(i, color);
|
||||
}
|
||||
show();
|
||||
for (int i = 0; i < numPixels(); i++)
|
||||
{
|
||||
setPixelColor(i, color);
|
||||
}
|
||||
show();
|
||||
}
|
||||
|
||||
// Returns the Red component of a 32-bit 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
|
||||
uint8_t NeoPatterns::Green(uint32_t color)
|
||||
{
|
||||
return (color >> 8) & 0xFF;
|
||||
return (color >> 8) & 0xFF;
|
||||
}
|
||||
|
||||
// Returns the Blue component of a 32-bit 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.
|
||||
// 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)
|
||||
{
|
||||
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
|
||||
}
|
||||
else if(WheelPos < 170)
|
||||
{
|
||||
WheelPos -= 85;
|
||||
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
WheelPos -= 170;
|
||||
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
|
||||
}
|
||||
WheelPos = 255 - WheelPos;
|
||||
if (WheelPos < 85)
|
||||
{
|
||||
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
|
||||
}
|
||||
else if (WheelPos < 170)
|
||||
{
|
||||
WheelPos -= 85;
|
||||
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
WheelPos -= 170;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
};
|
||||
|
|
|
@ -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() {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue