Added logic for RGB vs. RGBW
Tested with original 8-pixel RGB and 60-pixel RGBW. Works as expected.
This commit is contained in:
parent
877f47a64d
commit
d2bbd4f56c
|
|
@ -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
|
rf=new RFControl(pin,false,false); // set clock to 1/80 usec, no default driver
|
||||||
if(!*rf)
|
if(!*rf)
|
||||||
return;
|
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)
|
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
|
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)
|
if(!*rf || nPixels==0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uint32_t data=getColorRGB(r,g,b);
|
uint32_t data=getColorRGB(r,g,b,w);
|
||||||
setColors(&data,nPixels,false);
|
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;
|
float r,g,b;
|
||||||
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&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.nPixels=nPixels;
|
||||||
status.data=data;
|
status.data=data;
|
||||||
status.iMem=0;
|
status.iMem=0;
|
||||||
status.iBit=24;
|
status.iBit=32;
|
||||||
status.started=true;
|
status.started=true;
|
||||||
status.px=this;
|
status.px=this;
|
||||||
status.multiColor=multiColor;
|
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){
|
uint32_t Pixel::getColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w){
|
||||||
return(g<<16 | r<<8 | b);
|
// 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;
|
float r,g,b;
|
||||||
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&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++)
|
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];
|
RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem++].val=status.px->pattern[(*status.data>>(--status.iBit))&1];
|
||||||
|
|
||||||
if(status.iBit==0){
|
if(status.iBit==status.px->pType){
|
||||||
status.iBit=24;
|
status.iBit=32;
|
||||||
status.data+=status.multiColor;
|
status.data+=status.multiColor;
|
||||||
status.nPixels--;
|
status.nPixels--;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,41 +10,48 @@
|
||||||
|
|
||||||
class Pixel {
|
class Pixel {
|
||||||
|
|
||||||
struct pixel_status_t {
|
public:
|
||||||
int nPixels;
|
enum pixel_type_t {
|
||||||
const uint32_t *data;
|
RGB=8,
|
||||||
int iBit;
|
RGBW=0
|
||||||
int iMem;
|
};
|
||||||
boolean started;
|
|
||||||
Pixel *px;
|
|
||||||
boolean multiColor;
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
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
|
RFControl *rf; // Pixel utilizes RFControl
|
||||||
uint32_t pattern[2]; // storage for zero-bit and one-bit pulses
|
uint32_t pattern[2]; // storage for zero-bit and one-bit pulses
|
||||||
uint32_t resetTime; // minimum time (in usec) between pulse trains
|
uint32_t resetTime; // minimum time (in usec) between pulse trains
|
||||||
uint32_t txEndMask; // mask for end-of-transmission interrupt
|
uint32_t txEndMask; // mask for end-of-transmission interrupt
|
||||||
uint32_t txThrMask; // mask for threshold 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
|
const int memSize=sizeof(RMTMEM.chan[0].data32)/4; // determine size (in pulses) of one channel
|
||||||
|
|
||||||
static void loadData(void *arg); // interrupt handler
|
static void loadData(void *arg); // interrupt handler
|
||||||
volatile static pixel_status_t status; // storage for volatile information modified in interupt handler
|
volatile static pixel_status_t status; // storage for volatile information modified in interupt handler
|
||||||
|
|
||||||
public:
|
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
|
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 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 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, uint32_t nPixels=1); // sets color of nPixels to HSV values where h=[0,360], s=[0,100], v=[0,100]
|
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
|
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 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); // return pixel Color from HSV 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
|
operator bool(){ // override boolean operator to return true/false if creation succeeded/failed
|
||||||
return(*rf);
|
return(*rf);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ struct Effect1 {
|
||||||
|
|
||||||
Pixel *px;
|
Pixel *px;
|
||||||
int H=0;
|
int H=0;
|
||||||
uint32_t x[8];
|
|
||||||
uint32_t alarmTime=0;
|
uint32_t alarmTime=0;
|
||||||
uint32_t speed;
|
uint32_t speed;
|
||||||
|
|
||||||
|
|
@ -19,10 +18,7 @@ struct Effect1 {
|
||||||
if(millis()<alarmTime)
|
if(millis()<alarmTime)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for(int i=0;i<8;i++)
|
px->setHSV(H,100,100,0,60);
|
||||||
x[i]=px->getColorHSV(H,i*3+79,i*2+5);
|
|
||||||
|
|
||||||
px->setColors(x,8);
|
|
||||||
H=(H+1)%360;
|
H=(H+1)%360;
|
||||||
|
|
||||||
alarmTime=millis()+speed;
|
alarmTime=millis()+speed;
|
||||||
|
|
@ -35,7 +31,7 @@ struct Effect2 {
|
||||||
int phase=0;
|
int phase=0;
|
||||||
int dir=1;
|
int dir=1;
|
||||||
int H=0;
|
int H=0;
|
||||||
uint32_t x[8];
|
uint32_t x[60];
|
||||||
uint32_t alarmTime=0;
|
uint32_t alarmTime=0;
|
||||||
uint32_t speed;
|
uint32_t speed;
|
||||||
|
|
||||||
|
|
@ -48,23 +44,23 @@ struct Effect2 {
|
||||||
if(millis()<alarmTime)
|
if(millis()<alarmTime)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for(int i=0;i<8;i++){
|
for(int i=0;i<60;i++){
|
||||||
if(i==phase)
|
if(i==phase)
|
||||||
x[i]=px->getColorHSV(H,100,10);
|
x[i]=px->getColorHSV(H,100,100);
|
||||||
else if(i==7-phase)
|
else if(i==59-phase)
|
||||||
x[i]=px->getColorHSV(H+180,100,10);
|
x[i]=px->getColorHSV(H+180,100,100);
|
||||||
else
|
else
|
||||||
x[i]=0;
|
x[i]=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
px->setColors(x,8);
|
px->setColors(x,60);
|
||||||
phase=(phase+dir)%8;
|
phase=(phase+dir)%60;
|
||||||
|
|
||||||
if(phase==0){
|
if(phase==0){
|
||||||
dir=1;
|
dir=1;
|
||||||
H=(H+10)%360;
|
H=(H+10)%360;
|
||||||
}
|
}
|
||||||
else if(phase==7){
|
else if(phase==59){
|
||||||
dir=-1;
|
dir=-1;
|
||||||
H=(H+10)%360;
|
H=(H+10)%360;
|
||||||
}
|
}
|
||||||
|
|
@ -74,23 +70,27 @@ struct Effect2 {
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(CONFIG_IDF_TARGET_ESP32C3)
|
#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)
|
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
|
||||||
#define PIN 1
|
|
||||||
#else
|
#define PIXEL_PIN_1 18
|
||||||
#define PIN 21
|
#define PIXEL_PIN_2 7
|
||||||
|
|
||||||
|
#elif defined(CONFIG_IDF_TARGET_ESP32)
|
||||||
|
|
||||||
|
#define PIXEL_PIN_1 23
|
||||||
|
#define PIXEL_PIN_2 21
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Pixel px2(2);
|
Pixel px1(PIXEL_PIN_1,Pixel::RGBW);
|
||||||
Pixel px(PIN);
|
Pixel px2(PIXEL_PIN_2,Pixel::RGBW);
|
||||||
Pixel px3(3);
|
|
||||||
Pixel px4(4);
|
|
||||||
Pixel px5(5);
|
|
||||||
Pixel px6(6);
|
|
||||||
Pixel px7(7);
|
|
||||||
|
|
||||||
Effect1 effect1(&px,5);
|
Effect1 effect1(&px1,20);
|
||||||
Effect2 effect2(&px,100);
|
Effect2 effect2(&px2,20);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
|
|
@ -103,5 +103,6 @@ void setup() {
|
||||||
} // end of setup()
|
} // end of setup()
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
|
effect1.update();
|
||||||
effect2.update();
|
effect2.update();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue