implement scanner effect
This commit is contained in:
parent
ac6015cf65
commit
f6b8fa4ff4
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include "wagon.h"
|
||||
#include "fx_scanner.h"
|
||||
|
||||
#define PIN D2
|
||||
#define NUMPIXELS 600
|
||||
|
@ -39,6 +40,8 @@ int selectedpixel=-1; //-1 = none
|
|||
|
||||
uint8_t wagoncount=0;
|
||||
|
||||
FX_Scanner effect;
|
||||
|
||||
|
||||
//define config
|
||||
//#define RESPAWNWAGON
|
||||
|
@ -374,6 +377,9 @@ void checkSerial(){
|
|||
}else{
|
||||
Serial.println("Error: Value too high!");
|
||||
}
|
||||
}else if (serialstring.equals("fx_scanner")){
|
||||
Serial.println("Effect Scanner");
|
||||
effect=FX_Scanner(NUMPIXELS,&strip,height,255,-200,strip.Color(100,0,0));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -411,12 +417,18 @@ void loop_achterbahn(){
|
|||
strip.setPixelColor(i,c);
|
||||
}
|
||||
|
||||
//Wagons
|
||||
for (std::vector<Wagon>::iterator it = wagon_arr.begin(); it != wagon_arr.end(); ++it) //all wagons
|
||||
{
|
||||
Wagon & w = *it;
|
||||
w.updateGraphics();
|
||||
}
|
||||
|
||||
//Effects
|
||||
if (effect.active()){
|
||||
effect.updateGraphics();
|
||||
}
|
||||
|
||||
strip.show();
|
||||
}
|
||||
|
||||
|
@ -445,6 +457,11 @@ void loop_achterbahn(){
|
|||
wagoncount++;
|
||||
}
|
||||
}
|
||||
|
||||
//Effects
|
||||
if (effect.active()){
|
||||
effect.updateRoutine(ROUTINEUPDATETIME);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -134,6 +134,11 @@ void setup() {
|
|||
cp5.addButton("Run")
|
||||
.setPosition(10, 240)
|
||||
.setSize(100, 30);
|
||||
|
||||
|
||||
cp5.addButton("FX_Scanner")
|
||||
.setPosition(200, 200)
|
||||
.setSize(100, 20);
|
||||
|
||||
|
||||
myPort = new Serial(this, "/dev/ttyUSB0", 115200);
|
||||
|
@ -176,6 +181,12 @@ void SpawnRandom() {
|
|||
myPort.write(writeserial);
|
||||
}
|
||||
|
||||
|
||||
void FX_Scanner() {
|
||||
String writeserial="fx_scanner\n";
|
||||
myPort.write(writeserial);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void draw() {
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
#include "fx_scanner.h"
|
||||
|
||||
FX_Scanner::FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor)
|
||||
{
|
||||
_numpixels=numpixels;
|
||||
_pos = startpos;
|
||||
_strip=strip;
|
||||
_height=height;
|
||||
_vel=scannervel;
|
||||
_scannercolor=scannercolor;
|
||||
}
|
||||
|
||||
FX_Scanner::FX_Scanner()
|
||||
{
|
||||
_numpixels=0;
|
||||
_pos = -1; //for active false
|
||||
_strip=0;
|
||||
_height=0;
|
||||
_vel=0;
|
||||
_scannercolor=0;
|
||||
}
|
||||
|
||||
void FX_Scanner::updateRoutine(float updatedelayms)
|
||||
{
|
||||
_pos+=_vel*updatedelayms/1000.0;
|
||||
}
|
||||
|
||||
void FX_Scanner::updateGraphics()
|
||||
{
|
||||
#define FEATHERDISTANCE 3 //in both directions
|
||||
for(int i=0;i<_numpixels;i++){
|
||||
float heightdistfromlaser=abs(_height[i]-_pos);
|
||||
if (heightdistfromlaser<=FEATHERDISTANCE) {
|
||||
|
||||
uint8_t _r = _scannercolor >> 16;
|
||||
uint8_t _g = _scannercolor >> 8;
|
||||
uint8_t _b = _scannercolor;
|
||||
|
||||
float distmult=1.0-(heightdistfromlaser/FEATHERDISTANCE);
|
||||
_r*=distmult;
|
||||
_g*=distmult;
|
||||
_b*=distmult;
|
||||
|
||||
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_Scanner::active()
|
||||
{
|
||||
if (_pos>256 || _pos<0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t FX_Scanner::Wheel(byte WheelPos,float brightness) {
|
||||
WheelPos = 255 - WheelPos;
|
||||
if(WheelPos < 85) {
|
||||
return _strip->Color(255 - WheelPos * 3*brightness, 0, WheelPos * 3*brightness);
|
||||
}
|
||||
if(WheelPos < 170) {
|
||||
WheelPos -= 85;
|
||||
return _strip->Color(0, WheelPos * 3*brightness, 255 - WheelPos * 3*brightness);
|
||||
}
|
||||
WheelPos -= 170;
|
||||
return _strip->Color(WheelPos * 3*brightness, 255 - WheelPos * 3*brightness, 0);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef FX_SCANNER_H
|
||||
#define FX_SCANNER_H
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#include <math.h>
|
||||
|
||||
class FX_Scanner
|
||||
{
|
||||
public:
|
||||
FX_Scanner(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,int startpos,float scannervel,uint32_t scannercolor);
|
||||
FX_Scanner();
|
||||
void updateRoutine(float updatedelayms);
|
||||
void updateGraphics();
|
||||
uint32_t Wheel(byte WheelPos,float brightness);
|
||||
bool active();
|
||||
private:
|
||||
int _numpixels;
|
||||
Adafruit_NeoPixel *_strip;
|
||||
float _pos;
|
||||
float _vel;
|
||||
uint8_t *_height;
|
||||
uint32_t _scannercolor;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue