Files
buzzer/user/Fader.h

69 lines
1.0 KiB
C++
Executable File

#ifndef FADER_H
#define FADER_H
#include "ESP8266lib/ext/led/WS2812B.h"
/** fade between two colors */
class FadeBetween {
Color c1;
Color c2;
int pos;
int speed;
int dir;
public:
/** ctor */
FadeBetween() {
restart();
}
void setColor1(Color c1) {
this->c1 = c1;
}
void setColor2(Color c2) {
this->c2 = c2;
}
void restart() {
pos = 0;
speed = 64;
dir = 0;
}
void update() {
if (dir == 0) {
++pos;
if (pos == speed) {dir = 1;}
} else {
--pos;
if (pos == 0) {dir = 0;}
}
}
Color getCurrent() const {
const int fac0 = pos;
const int fac1 = speed - fac0;
Color c;
c.r = clamp( (c1.r * fac0 + c2.r * fac1) / speed );
c.g = clamp( (c1.g * fac0 + c2.g * fac1) / speed );
c.b = clamp( (c1.b * fac0 + c2.b * fac1) / speed );
return c;
}
private:
static inline uint8_t clamp(int val) {
if (val > 255) {return 255;}
if (val < 0) {return 0;}
return val;
}
};
#endif // FADER_H