Embedded HSV and RGB methods inside Color object

Makes coding with Pixel() and Dot() easier
This commit is contained in:
Gregg 2022-02-12 15:31:55 -06:00
parent effe8e5965
commit eee0eb6954
3 changed files with 52 additions and 54 deletions

View File

@ -70,25 +70,6 @@ void Pixel::set(Color *c, int nPixels, boolean multiColor){
///////////////////
Pixel::Color Pixel::RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t white){
Color x;
x.red=red;
x.green=green;
x.blue=blue;
x.white=white;
return(x);
}
///////////////////
Pixel::Color Pixel::HSV(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(RGB(r*255,g*255,b*255,w*2.555));
}
///////////////////
void IRAM_ATTR Pixel::loadData(void *arg){
if(RMT.int_st.val & status.px->txEndMask){
@ -188,24 +169,4 @@ void Dot::set(Color *c, int nPixels, boolean multiColor){
}
}
///////////////////
Dot::Color Dot::RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t drive){
Color x;
x.red=red;
x.green=green;
x.blue=blue;
x.drive=drive;
x.flags=7;
return(x);
}
///////////////////
Dot::Color Dot::HSV(float h, float s, float v, double level){
float r,g,b;
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b);
return(RGB(r*255,g*255,b*255,level*0.315));
}
////////////////////////////////////////////

View File

@ -26,6 +26,24 @@ class Pixel {
uint32_t val;
};
Color RGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0){ // returns Color based on provided RGB(W) values where r/g/b/w=[0-255]
this->red=r;
this->green=g;
this->blue=b;
this->white=w;
return(*this);
}
Color HSV(float h, float s, float v, double w=0){ // returns Color based on provided HSV(W) values where h=[0,360] and s/v/w=[0,100]
float r,g,b;
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b);
this->red=r*255;
this->green=g*255;
this->blue=b*255;
this->white=w*2.555;
return(*this);
}
bool operator==(const Color& color){
return(val==color.val);
}
@ -68,7 +86,7 @@ class Pixel {
return(*this);
}
};
}; // Color
private:
struct pixel_status_t {
@ -95,8 +113,6 @@ class Pixel {
public:
Pixel(int pin, boolean isRGBW=false); // creates addressable single-wire RGB (false) or RGBW (true) LED connected to pin (such as the SK68 or WS28)
static Color RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t white=0); // returns Color based on provided RGB(W) values where r/g/b/w=[0-255]
static Color HSV(float h, float s, float v, double w=0); // returns Color based on provided HSV(W) values where h=[0,360] and s/v/w=[0,100]
void set(Color *c, int nPixels, boolean multiColor=true); // sets colors of nPixels based on array of Colors c; setting multiColor to false repeats Color in c[0] for all nPixels
void set(Color c, int nPixels=1){set(&c,nPixels,false);} // sets color of nPixels to be equal to specific Color c
@ -127,6 +143,26 @@ class Dot {
uint32_t val;
};
Color RGB(uint8_t r, uint8_t g, uint8_t b, uint8_t driveLevel=31){ // returns Color based on provided RGB values where r/g/b=[0-255] and current-limiting drive level=[0,31]
this->red=r;
this->green=g;
this->blue=b;
this->drive=driveLevel;
this->flags=7;
return(*this);
}
Color HSV(float h, float s, float v, double drivePercent=100){ // returns Color based on provided HSV values where h=[0,360], s/v=[0,100], and current-limiting drive percent=[0,100]
float r,g,b;
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b);
this->red=r*255;
this->green=g*255;
this->blue=b*255;
this->drive=drivePercent*0.315;
this->flags=7;
return(*this);
}
bool operator==(const Color& color){
return(val==color.val);
}
@ -177,8 +213,6 @@ class Dot {
public:
Dot(uint8_t dataPin, uint8_t clockPin); // creates addressable two-wire RGB LED connected to dataPin and clockPin (such as the DotStar SK9822 or APA102)
static Color RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t drive=31); // returns Color based on provided RGB values where r/g/b=[0-255] and current-limiting drive=[0,31]
static Color HSV(float h, float s, float v, double level=100); // returns Color based on provided HSV values where h=[0,360], s/v=[0,100], and current-limiting drive=[0,100]
void set(Color *c, int nPixels, boolean multiColor=true); // sets colors of nPixels based on array of Colors c; setting multiColor to false repeats Color in c[0] for all nPixels
void set(Color c, int nPixels=1){set(&c,nPixels,false);} // sets color of nPixels to be equal to specific Color c
};

View File

@ -9,6 +9,7 @@ struct Effect1 {
uint32_t alarmTime=0;
uint32_t speed;
uint8_t nPixels;
Pixel::Color c;
Effect1(Pixel *px, uint32_t speed, uint8_t nPixels){
this->px=px;
@ -20,7 +21,7 @@ struct Effect1 {
if(millis()<alarmTime)
return;
px->set(px->HSV(H,100,10),nPixels);
px->set(c.HSV(H,100,10),nPixels);
H=(H+1)%360;
alarmTime=millis()+speed;
@ -50,11 +51,11 @@ struct Effect2 {
for(int i=0;i<nPixels;i++){
if(i==phase)
x[i]=px->HSV(H,100,100);
x[i].HSV(H,100,100);
else if(i==nPixels-1-phase)
x[i]=px->HSV(H+180,100,100);
x[i].HSV(H+180,100,100);
else
x[i]=Pixel::HSV(0,0,0);
x[i].RGB(0,0,0);
}
px->set(x,nPixels);
@ -80,6 +81,7 @@ struct Effect3 {
uint32_t alarmTime=0;
uint32_t speed;
uint8_t nPixels;
Dot::Color c;
Effect3(Dot *dot, uint32_t speed, uint8_t nPixels){
this->dot=dot;
@ -91,7 +93,7 @@ struct Effect3 {
if(millis()<alarmTime)
return;
dot->set(Dot::HSV(H,100,100),nPixels);
dot->set(c.HSV(H,100,100),nPixels);
H=(H+1)%360;
alarmTime=millis()+speed;
@ -106,14 +108,15 @@ struct Effect3 {
#elif defined(CONFIG_IDF_TARGET_ESP32)
Pixel px1(23); // NeoPixel RGB
Pixel px1(23,true); // NeoPixel RGB
Pixel px2(21,true); // NeoPixel RGBW
Dot dot(32,5); // DotStar
#endif
Effect1 effect1(&px1,20,8);
Effect2 effect2(&px2,20,60);
//Effect1 effect1(&px1,20,60);
//Effect2 effect2(&px2,20,60);
Effect2 effect2(&px1,20,60);
Effect3 effect3(&dot,20,30);
void setup() {
@ -127,7 +130,7 @@ void setup() {
} // end of setup()
void loop(){
effect1.update();
// effect1.update();
effect2.update();
effect3.update();
}