diff --git a/src/extras/Pixel.cpp b/src/extras/Pixel.cpp index 8264c20..882ba72 100644 --- a/src/extras/Pixel.cpp +++ b/src/extras/Pixel.cpp @@ -3,12 +3,14 @@ /////////////////// -Pixel::Pixel(int pin){ +Pixel::Pixel(int pin, pixel_type_t pType){ rf=new RFControl(pin,false,false); // set clock to 1/80 usec, no default driver if(!*rf) return; - + + this->pType=pType; + setTiming(0.32, 0.88, 0.64, 0.56, 80.0); // set default timing parameters (suitable for most SK68 and WS28 RGB pixels) rmt_isr_register(loadData,NULL,0,NULL); // set custom interrupt handler @@ -35,21 +37,21 @@ void Pixel::setTiming(float high0, float low0, float high1, float low1, uint32_t /////////////////// -void Pixel::setRGB(uint8_t r, uint8_t g, uint8_t b, uint32_t nPixels){ +void Pixel::setRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w, uint32_t nPixels){ if(!*rf || nPixels==0) return; - uint32_t data=getColorRGB(r,g,b); + uint32_t data=getColorRGB(r,g,b,w); setColors(&data,nPixels,false); } /////////////////// -void Pixel::setHSV(float h, float s, float v, uint32_t nPixels){ +void Pixel::setHSV(float h, float s, float v, double w, uint32_t nPixels){ float r,g,b; LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); - setRGB(r*255,g*255,b*255,nPixels); + setRGB(r*255,g*255,b*255,w*2.555,nPixels); } /////////////////// @@ -62,7 +64,7 @@ void Pixel::setColors(const uint32_t *data, uint32_t nPixels, boolean multiColor status.nPixels=nPixels; status.data=data; status.iMem=0; - status.iBit=24; + status.iBit=32; status.started=true; status.px=this; status.multiColor=multiColor; @@ -78,16 +80,17 @@ void Pixel::setColors(const uint32_t *data, uint32_t nPixels, boolean multiColor /////////////////// -uint32_t Pixel::getColorRGB(uint8_t r, uint8_t g, uint8_t b){ - return(g<<16 | r<<8 | b); +uint32_t Pixel::getColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w){ +// return(g<<16 | r<<8 | b); + return(g<<24 | r<<16 | b<<8 | w); } /////////////////// -uint32_t Pixel::getColorHSV(float h, float s, float v){ +uint32_t Pixel::getColorHSV(float h, float s, float v, double w){ float r,g,b; LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); - return(getColorRGB(r*255,g*255,b*255)); + return(getColorRGB(r*255,g*255,b*255,w*2.555)); } /////////////////// @@ -110,8 +113,8 @@ void IRAM_ATTR Pixel::loadData(void *arg){ for(int i=0;i<8;i++) RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem++].val=status.px->pattern[(*status.data>>(--status.iBit))&1]; - if(status.iBit==0){ - status.iBit=24; + if(status.iBit==status.px->pType){ + status.iBit=32; status.data+=status.multiColor; status.nPixels--; } diff --git a/src/extras/Pixel.h b/src/extras/Pixel.h index b2a05b4..bd46bf0 100644 --- a/src/extras/Pixel.h +++ b/src/extras/Pixel.h @@ -10,41 +10,48 @@ class Pixel { - struct pixel_status_t { - int nPixels; - const uint32_t *data; - int iBit; - int iMem; - boolean started; - Pixel *px; - boolean multiColor; - }; - + public: + enum pixel_type_t { + RGB=8, + RGBW=0 + }; + private: + struct pixel_status_t { + int nPixels; + const uint32_t *data; + int iBit; + int iMem; + boolean started; + Pixel *px; + boolean multiColor; + }; + RFControl *rf; // Pixel utilizes RFControl uint32_t pattern[2]; // storage for zero-bit and one-bit pulses uint32_t resetTime; // minimum time (in usec) between pulse trains uint32_t txEndMask; // mask for end-of-transmission interrupt uint32_t txThrMask; // mask for threshold interrupt + pixel_type_t pType; // type of Pixel (RGB or RGBW) const int memSize=sizeof(RMTMEM.chan[0].data32)/4; // determine size (in pulses) of one channel static void loadData(void *arg); // interrupt handler volatile static pixel_status_t status; // storage for volatile information modified in interupt handler - public: - Pixel(int pin); // creates addressable single-wire RGB LED on pin (such as the SK68 or WS28), with OPTIONAL reserve of memory for nPixels + public: + Pixel(int pin, pixel_type_t pType=RGB); // creates addressable single-wire RGB or RGBW LED on pin (such as the SK68 or WS28) void setTiming(float high0, float low0, float high1, float low1, uint32_t lowReset); // changes default timings for bit pulse - note parameters are in MICROSECONDS - void setRGB(uint8_t r, uint8_t g, uint8_t b, uint32_t nPixels=1); // sets color of nPixels to RGB values (0-255) - void setHSV(float h, float s, float v, uint32_t nPixels=1); // sets color of nPixels to HSV values where h=[0,360], s=[0,100], v=[0,100] - void setColors(const uint32_t *data, uint32_t nPixels, bool multiColor=true); // sets colors of nPixels from array of colors stored in data + void setRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0, uint32_t nPixels=1); // sets color of nPixels to RGB(W) values (0-255) + void setHSV(float h, float s, float v, double w=0, uint32_t nPixels=1); // sets color of nPixels to HSV(W) values where h=[0,360], s/v/w=[0,100] + void setColors(const uint32_t *data, uint32_t nPixels, bool multiColor=true); // sets colors of nPixels from array of colors stored in data - int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1 + int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1 - static uint32_t getColorRGB(uint8_t r, uint8_t g, uint8_t b); // return pixel Color from RGB values - static uint32_t getColorHSV(float h, float s, float v); // return pixel Color from HSV values + static uint32_t getColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0); // return pixel Color from RGB(W) values + static uint32_t getColorHSV(float h, float s, float v, double w=0); // return pixel Color from HSV(W) values operator bool(){ // override boolean operator to return true/false if creation succeeded/failed return(*rf); diff --git a/src/extras/extras.ino b/src/extras/extras.ino index e248795..1f94862 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -6,7 +6,6 @@ struct Effect1 { Pixel *px; int H=0; - uint32_t x[8]; uint32_t alarmTime=0; uint32_t speed; @@ -19,10 +18,7 @@ struct Effect1 { if(millis()getColorHSV(H,i*3+79,i*2+5); - - px->setColors(x,8); + px->setHSV(H,100,100,0,60); H=(H+1)%360; alarmTime=millis()+speed; @@ -35,7 +31,7 @@ struct Effect2 { int phase=0; int dir=1; int H=0; - uint32_t x[8]; + uint32_t x[60]; uint32_t alarmTime=0; uint32_t speed; @@ -48,23 +44,23 @@ struct Effect2 { if(millis()getColorHSV(H,100,10); - else if(i==7-phase) - x[i]=px->getColorHSV(H+180,100,10); + x[i]=px->getColorHSV(H,100,100); + else if(i==59-phase) + x[i]=px->getColorHSV(H+180,100,100); else x[i]=0; } - px->setColors(x,8); - phase=(phase+dir)%8; + px->setColors(x,60); + phase=(phase+dir)%60; if(phase==0){ dir=1; H=(H+10)%360; } - else if(phase==7){ + else if(phase==59){ dir=-1; H=(H+10)%360; } @@ -74,23 +70,27 @@ struct Effect2 { }; #if defined(CONFIG_IDF_TARGET_ESP32C3) - #define PIN 1 + + #define PIXEL_PIN_1 8 + #define PIXEL_PIN_2 1 + #elif defined(CONFIG_IDF_TARGET_ESP32S2) - #define PIN 1 -#else - #define PIN 21 + + #define PIXEL_PIN_1 18 + #define PIXEL_PIN_2 7 + +#elif defined(CONFIG_IDF_TARGET_ESP32) + + #define PIXEL_PIN_1 23 + #define PIXEL_PIN_2 21 + #endif -Pixel px2(2); -Pixel px(PIN); -Pixel px3(3); -Pixel px4(4); -Pixel px5(5); -Pixel px6(6); -Pixel px7(7); +Pixel px1(PIXEL_PIN_1,Pixel::RGBW); +Pixel px2(PIXEL_PIN_2,Pixel::RGBW); -Effect1 effect1(&px,5); -Effect2 effect2(&px,100); +Effect1 effect1(&px1,20); +Effect2 effect2(&px2,20); void setup() { @@ -103,5 +103,6 @@ void setup() { } // end of setup() void loop(){ + effect1.update(); effect2.update(); }