From c05133d8f83b3d1e57b856a08e20ea4ca4d59118 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 16 Dec 2020 23:38:40 +0100 Subject: [PATCH] use constrain function --- src/fx_flash.cpp | 12 ++----- src/fx_scanner.cpp | 12 ++----- src/fx_shootingstar.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ src/fx_shootingstar.h | 25 ++++++++++++++ 4 files changed, 105 insertions(+), 18 deletions(-) create mode 100644 src/fx_shootingstar.cpp create mode 100644 src/fx_shootingstar.h diff --git a/src/fx_flash.cpp b/src/fx_flash.cpp index cfb26f2..41ccb46 100644 --- a/src/fx_flash.cpp +++ b/src/fx_flash.cpp @@ -55,15 +55,9 @@ void FX_Flash::updateGraphics() 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; - } + _tmpr=constrain(_tmpr,0,255); + _tmpg=constrain(_tmpg,0,255); + _tmpb=constrain(_tmpb,0,255); _strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel } diff --git a/src/fx_scanner.cpp b/src/fx_scanner.cpp index 4bb6da0..aadacbb 100644 --- a/src/fx_scanner.cpp +++ b/src/fx_scanner.cpp @@ -49,15 +49,9 @@ void FX_Scanner::updateGraphics() 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; - } + _tmpr=constrain(_tmpr,0,255); + _tmpg=constrain(_tmpg,0,255); + _tmpb=constrain(_tmpb,0,255); _strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel } } diff --git a/src/fx_shootingstar.cpp b/src/fx_shootingstar.cpp new file mode 100644 index 0000000..52377df --- /dev/null +++ b/src/fx_shootingstar.cpp @@ -0,0 +1,74 @@ +#include "fx_shootingstar.h" +#include "effect.h" + +FX_ShootingStar::FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height) +{ + _numpixels=numpixels; + _strip=strip; + _starttime=millis(); + _brightness=0; + _flashtime=random(200,3000); + _height=height; +} + +FX_ShootingStar::FX_ShootingStar() +{ +} + +void FX_ShootingStar::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_ShootingStar::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 = 1 >> 16; + uint8_t _g = 1 >> 8; + uint8_t _b = 1; + + _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; + _tmpr=constrain(_tmpr,0,255); + _tmpg=constrain(_tmpr,0,255); + _tmpb=constrain(_tmpr,0,255); + + _strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel + } + + } +} + +bool FX_ShootingStar::active() +{ + if (millis()-_starttime>_flashtime){ + return false; + } + return true; +} + diff --git a/src/fx_shootingstar.h b/src/fx_shootingstar.h new file mode 100644 index 0000000..92ed299 --- /dev/null +++ b/src/fx_shootingstar.h @@ -0,0 +1,25 @@ +#ifndef FX_SHOOTINGSTAR_H +#define FX_SHOOTINGSTAR_H +#include +#include +#include "effect.h" + +class FX_ShootingStar : public Effect +{ + public: + FX_ShootingStar(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height); + FX_ShootingStar(); + void updateRoutine(float updatedelayms); + void updateGraphics(); + uint32_t Wheel(byte WheelPos,float brightness); + bool active(); + private: + int _numpixels; + Adafruit_NeoPixel *_strip; + long _starttime; + uint8_t _brightness; + long _flashtime; + uint8_t *_height; +}; + +#endif