Files
buzzer/user/RainbowBeat.h
2018-02-03 16:55:37 +01:00

73 lines
1.4 KiB
C++
Executable File

#ifndef RAINBOWBEAT_H
#define RAINBOWBEAT_H
#include "ESP8266lib/ext/led/WS2812B.h"
/**
* show rainbow colors using HUE
* use only 25% percent brightness
* when there is a beat, switch to 100% brightness
* and fade back to 25% again
*/
class RainbowBeat {
int p1 = 255;
int p2 = 63;
int fadeOut_ticks = 0;
int ticksLeft = 0;
uint8_t hue = 0;
public:
/** ctor */
RainbowBeat() {
;
}
/** configure the amounts for idle and beat */
void setAmounts(int p1, int p2) {
this->p1 = p1;
this->p2 = p2;
}
/** show a bight beat for x ticks */
void flash(const int duration_ticks) {
this->fadeOut_ticks = duration_ticks;
this->ticksLeft = duration_ticks;
}
/** show a beat by shifting the hue color */
void shift(const int val) {
hue += val;
}
/** shift to the given absolute hue */
void shiftTo(const int valAbs) {
hue = valAbs;
}
/** set hue back to 0 */
void restart() {
this->hue = 0;
}
/** called from the main */
void update() {
if (ticksLeft > 0) {--ticksLeft;} // fade-out
hue += 1;
}
/** called from the main */
Color getCurrent() const {
const int percent = (ticksLeft * 100 / fadeOut_ticks);
const int val = ((p1 * percent) + (p2 * (100-percent))) / 100;
Color c; c.setHSV(hue, 255, val);
return c;
}
};
#endif // RAINBOWBEAT_H