use constrain function

This commit is contained in:
interfisch 2020-12-16 23:38:40 +01:00
parent b859b30ce4
commit c05133d8f8
4 changed files with 105 additions and 18 deletions

View File

@ -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
}

View File

@ -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
}
}

74
src/fx_shootingstar.cpp Normal file
View File

@ -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;
}

25
src/fx_shootingstar.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef FX_SHOOTINGSTAR_H
#define FX_SHOOTINGSTAR_H
#include <Adafruit_NeoPixel.h>
#include <math.h>
#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