From f6b8fa4ff4ef850b98adf9fece266633addb7da5 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 11 Jul 2018 00:08:14 +0200 Subject: [PATCH] implement scanner effect --- achterbahn.ino | 17 ++++ .../achterbahnconfig/achterbahnconfig.pde | 11 +++ fx_scanner.cpp | 84 +++++++++++++++++++ fx_scanner.h | 26 ++++++ 4 files changed, 138 insertions(+) create mode 100644 fx_scanner.cpp create mode 100644 fx_scanner.h diff --git a/achterbahn.ino b/achterbahn.ino index b82dd67..54e8c77 100644 --- a/achterbahn.ino +++ b/achterbahn.ino @@ -7,6 +7,7 @@ #include #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::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); + } } diff --git a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde index 85ff540..cfc2d71 100644 --- a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde +++ b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde @@ -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() { diff --git a/fx_scanner.cpp b/fx_scanner.cpp new file mode 100644 index 0000000..3ee14d6 --- /dev/null +++ b/fx_scanner.cpp @@ -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); +} diff --git a/fx_scanner.h b/fx_scanner.h new file mode 100644 index 0000000..bdf6aa7 --- /dev/null +++ b/fx_scanner.h @@ -0,0 +1,26 @@ +#ifndef FX_SCANNER_H +#define FX_SCANNER_H +#include +#include + +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 + +