57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
#include "LedControl.h"
|
|
|
|
class LedAnimation
|
|
{
|
|
public:
|
|
/// Sets leds in the strip and returns number of milliseconds to wait until it is called again
|
|
virtual int operator()(LedStrip &leds) = 0;
|
|
};
|
|
|
|
class SweepCircularAnimation : public LedAnimation
|
|
{
|
|
public:
|
|
SweepCircularAnimation(const ColorRGB &color, int delayMs = 100, int numLedsHalfWidth = 3, float brightnessFallOff = 0.8)
|
|
: color_(color), delayMs_(delayMs), numLedsHalfWidth_(numLedsHalfWidth), brightnessFallOff_(brightnessFallOff), currentCenter_(0)
|
|
{
|
|
}
|
|
|
|
int operator()(LedStrip &leds) override
|
|
{
|
|
for (int i = 0; i < leds.numLeds(); ++i)
|
|
leds.setColor(i, 0, 0, 0, 0);
|
|
|
|
leds.setColor(currentCenter_, color_);
|
|
|
|
ColorRGB currColor = color_;
|
|
for (int i = 1; i <= numLedsHalfWidth_; ++i)
|
|
{
|
|
currColor.r = uint8_t(float(currColor.r) * brightnessFallOff_);
|
|
currColor.g = uint8_t(float(currColor.g) * brightnessFallOff_);
|
|
currColor.b = uint8_t(float(currColor.b) * brightnessFallOff_);
|
|
leds.setColor(leds.normalizeLedIdx(currentCenter_ - i), currColor);
|
|
leds.setColor(leds.normalizeLedIdx(currentCenter_ + i), currColor);
|
|
}
|
|
currentCenter_ = leds.normalizeLedIdx(currentCenter_ + 1);
|
|
return delayMs_;
|
|
}
|
|
|
|
private:
|
|
// parameters
|
|
ColorRGB color_;
|
|
int delayMs_;
|
|
int numLedsHalfWidth_; // number of leds on to the left and right of center
|
|
float brightnessFallOff_;
|
|
|
|
// state
|
|
int currentCenter_;
|
|
};
|
|
|
|
// strategy:
|
|
// use queue to send animation pointers over
|
|
// task calls animate function and waits for new even in queue with timeout of next due call
|
|
//
|
|
|
|
void setAnimation(LedAnimation *animation)
|
|
{
|
|
}
|