esp32-sk6812/include/Fader.h

76 lines
2.5 KiB
C
Raw Normal View History

#pragma once
#include <vector>
class Fader
{
public:
struct Color {
int16_t r, g, b, w;
Color(int16_t ir = 0, int16_t ig = 0, int16_t ib = 0, int16_t iw = 0) : r(ir), g(ig), b(ib), w(iw) {}
void operator += (const Color &color)
{
this->r += color.r;
this->g += color.g;
this->b += color.b;
this->w += color.w;
}
void normalize(void)
{
if(r > 255) { r = 255; } else if (r < 0) { r = 0; };
if(g > 255) { g = 255; } else if (g < 0) { g = 0; };
if(b > 255) { b = 255; } else if (b < 0) { b = 0; };
if(w > 255) { w = 255; } else if (w < 0) { w = 0; };
}
};
Fader(std::size_t nstrips, std::size_t nmodules_per_strip, uint8_t fadestep = 1, uint32_t flip_strips_mask = 0x00);
void set_color(uint32_t strip, uint32_t module, const Color &color);
void fade_color(uint32_t strip, uint32_t module, const Color &color);
void add_color(uint32_t strip, uint32_t module, const Color &color);
void set_color(const Color &color); // for all LEDs
void fade_color(const Color &color); // for all LEDs
void add_color(const Color &color); // for all LEDs
void set_fadestep(uint8_t newFadestep);
void update(void);
/*!
* \returns Whether any color value has changed since the last call to getColorValues().
*/
bool something_changed(void) const {return m_somethingChanged; };
const std::vector<Color>& get_color_values(void) {
m_somethingChanged = false;
return m_curColor;
}
std::size_t modules_per_strip(void) { return m_modulesPerStrip; }
std::size_t strips(void) { return m_strips; }
private:
/*!
* Fade the colour value in cur towards target.
*
* \param cur The colour value to update.
* \param target The target value that should be reached.
* \returns Whether the value was actually changed.
*/
bool update_fade(int16_t *cur, const int16_t *target);
inline uint32_t make_module_idx(uint32_t strip, uint32_t module);
std::size_t m_strips;
std::size_t m_modulesPerStrip;
uint8_t m_fadestep;
uint32_t m_flipStripsMask;
bool m_somethingChanged;
std::vector<Color> m_curColor;
std::vector<Color> m_targetColor;
};