ws2812-achterbahn/src/fx_flash.cpp

81 lines
1.8 KiB
C++

#include "fx_flash.h"
#include "effect.h"
FX_Flash::FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,uint32_t flashcolor)
{
_numpixels=numpixels;
_strip=strip;
_flashcolor=flashcolor;
_starttime=millis();
_brightness=0;
_flashtime=random(200,3000);
_height=height;
}
FX_Flash::FX_Flash()
{
}
void FX_Flash::updateRoutine(float updatedelayms)
{
float negativelikelihood=pow(( (millis()-_starttime)*1.0/_flashtime ),2); // 0=most likely, 1=unlikely
_brightness*=0.2;
if (random(0,negativelikelihood*20)==0){
_brightness=random(100,255);
}
//_brightness=(1.0-pow((255-_brightness)/255.0,4))*255.0;
}
void FX_Flash::updateGraphics()
{
#define STARTFLASHHEIGHT 100
#define BRIGHTFLASHHEIGHT 150
for(int i=0;i<_numpixels;i++){
if (_height[i]>=STARTFLASHHEIGHT){
uint8_t heightbrightness=map(_height[i],STARTFLASHHEIGHT,BRIGHTFLASHHEIGHT,0,255);
uint8_t _r = _flashcolor >> 16;
uint8_t _g = _flashcolor >> 8;
uint8_t _b = _flashcolor;
_r*=_brightness/255.0;
_g*=_brightness/255.0;
_b*=_brightness/255.0;
_r*=heightbrightness/255.0;
_g*=heightbrightness/255.0;
_b*=heightbrightness/255.0;
uint32_t _pxcolor=_strip->getPixelColor(i); //get current color of that pixel
uint8_t _pxr = _pxcolor >> 16;
uint8_t _pxg = _pxcolor >> 8;
uint8_t _pxb = _pxcolor;
uint16_t _tmpr=_pxr+_r; //add colors
uint16_t _tmpg=_pxg+_g;
uint16_t _tmpb=_pxb+_b;
if (_tmpr>255){ //clamp
_tmpr=255;
}
if (_tmpg>255){
_tmpg=255;
}
if (_tmpb>255){
_tmpb=255;
}
_strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel
}
}
}
bool FX_Flash::active()
{
if (millis()-_starttime>_flashtime){
return false;
}
return true;
}