文本 Arduino C Python C++ Java C# Bash Go kotlin JavaScript HTML/XML CSS SQL #include "Adafruit_NeoPixel.h"
enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE };
enum direction { FORWARD, REVERSE };
class NeoPatterns : public Adafruit_NeoPixel
{
public:
pattern ActivePattern;
direction Direction;
unsigned long Interval;
unsigned long lastUpdate;
uint32_t Color1, Color2;
uint16_t TotalSteps;
uint16_t Index;
void (*OnComplete)();
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
:Adafruit_NeoPixel(pixels, pin, type)
{
OnComplete = callback;
}
void Update()
{
if ((millis() - lastUpdate) > Interval)
{
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 ;
default :
break ;
}
}
}
void Increment()
{
if (Direction == FORWARD)
{
Index++;
if (Index >= TotalSteps)
{
Index = 0 ;
if (OnComplete != NULL )
{
OnComplete();
}
}
}
else
{
--Index;
if (Index <= 0 )
{
Index = TotalSteps-1 ;
if (OnComplete != NULL )
{
OnComplete();
}
}
}
}
void Reverse()
{
if (Direction == FORWARD)
{
Direction = REVERSE;
Index = TotalSteps-1 ;
}
else
{
Direction = FORWARD;
Index = 0 ;
}
}
void RainbowCycle(uint8_t interval, direction dir = FORWARD)
{
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255 ;
Index = 0 ;
Direction = dir;
}
void RainbowCycleUpdate()
{
for (int i=0 ; i< numPixels(); i++)
{
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255 ));
}
show();
Increment();
}
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
{
ActivePattern = THEATER_CHASE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color1;
Color2 = color2;
Index = 0 ;
Direction = dir;
}
void TheaterChaseUpdate()
{
for (int i=0 ; i< numPixels(); i++)
{
if ((i + Index) % 3 == 0 )
{
setPixelColor(i, Color1);
}
else
{
setPixelColor(i, Color2);
}
}
show();
Increment();
}
void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
{
ActivePattern = COLOR_WIPE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color;
Index = 0 ;
Direction = dir;
}
void ColorWipeUpdate()
{
setPixelColor(Index, Color1);
show();
Increment();
}
void Scanner(uint32_t color1, uint8_t interval)
{
ActivePattern = SCANNER;
Interval = interval;
TotalSteps = (numPixels() - 1 ) * 2 ;
Color1 = color1;
Index = 0 ;
}
void ScannerUpdate()
{
for (int i = 0 ; i < numPixels(); i++)
{
if (i == Index)
{
setPixelColor(i, Color1);
}
else if (i == TotalSteps - Index)
{
setPixelColor(i, Color1);
}
else
{
setPixelColor(i, DimColor(getPixelColor(i)));
}
}
show();
Increment();
}
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
{
ActivePattern = FADE;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Color2 = color2;
Index = 0 ;
Direction = dir;
}
void FadeUpdate()
{
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();
}
uint32_t DimColor(uint32_t color)
{
uint32_t dimColor = Color(Red(color) >> 1 , Green(color) >> 1 , Blue(color) >> 1 );
return dimColor;
}
void ColorSet(uint32_t color)
{
for (int i = 0 ; i < numPixels(); i++)
{
setPixelColor(i, color);
}
show();
}
uint8_t Red(uint32_t color)
{
return (color >> 16 ) & 0xFF ;
}
uint8_t Green(uint32_t color)
{
return (color >> 8 ) & 0xFF ;
}
uint8_t Blue(uint32_t color)
{
return color & 0xFF ;
}
uint32_t 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 );
}
}
};
void JewelsComplete() ;
NeoPatterns Jewels(14, 1, NEO_GRBW + NEO_KHZ800, &JewelsComplete) ;
const int BRIGHTNESS = 50 ;
void setup()
{
Serial.begin(115200 );
pinMode(2 , INPUT);
pinMode(0 , INPUT);
Jewels.setBrightness(BRIGHTNESS);
Jewels.begin();
Jewels.TheaterChase(Jewels.Color(255 ,50 ,0 ), Jewels.Color(0 ,0 ,0 ,50 ), 100 );
}
void loop()
{
Jewels.Update();
if (digitalRead(2 ) == HIGH)
{
Jewels.Color1 = Jewels.Color(255 , 50 , 0 );
Jewels.ActivePattern = FADE;
Jewels.TotalSteps = 100 ;
Jewels.Interval = 1 ;
}
else if (digitalRead(0 ) == HIGH)
{
Jewels.Color1 = Jewels.Color(255 , 0 , 0 );
Jewels.ActivePattern = SCANNER;
Jewels.TotalSteps = Jewels.numPixels();
Jewels.Interval = 100 ;
}
else
{
Jewels.Color1 = Jewels.Color(255 , 50 , 0 );
Jewels.ActivePattern = THEATER_CHASE;
Jewels.TotalSteps = Jewels.numPixels();
Jewels.Interval = 100 ;
}
}
void JewelsComplete()
{
Jewels.Reverse();
}