Normalized and optimized Pixel methods

created color_t typedef and use as basis for all RGB and HSV methods
This commit is contained in:
Gregg 2022-01-06 21:15:03 -06:00
parent d5b27d6e14
commit f10f5cffcd
3 changed files with 64 additions and 20 deletions

View File

@ -22,11 +22,22 @@ void Pixel::setRGB(uint8_t r, uint8_t g, uint8_t b, int nPixels){
return;
rf->clear();
for(int i=0;i<nPixels;i++){
loadColor(g);
loadColor(r);
loadColor(b);
for(int i=0;i<nPixels;i++)
loadColor(getColorRGB(r,g,b));
rf->phase(LR,0); // end-marker delay/reset
rf->start();
}
///////////////////
void Pixel::setColor(color_t *color, int nPixels){
if(!*rf)
return;
rf->clear();
for(int i=0;i<nPixels;i++)
loadColor(color[i]);
rf->phase(LR,0); // end-marker delay/reset
rf->start();
}
@ -41,12 +52,28 @@ void Pixel::setHSV(float h, float s, float v, int nPixels){
///////////////////
void Pixel::loadColor(uint8_t c){
void Pixel::loadColor(color_t c){
for(int i=7;i>=0;i--){
for(int i=23;i>=0;i--){
if((c>>i)&1)
rf->add(H1,L1); // 1-bit
else
rf->add(H0,L0); // 0-bit
}
}
///////////////////
color_t Pixel::getColorRGB(uint8_t r, uint8_t g, uint8_t b){
return(g<<16 | r<<8 | b);
}
///////////////////
color_t Pixel::getColorHSV(float h, float s, float v){
float r,g,b;
LedPin::HSVtoRGB(h,s,v,&r,&g,&b);
return(getColorRGB(r*255,g*255,b*255));
}
///////////////////

View File

@ -8,6 +8,8 @@
#include "RFControl.h"
#include "PwmPin.h"
typedef uint32_t color_t;
class Pixel {
private:
uint32_t H0, L0; // High and Low times for a zero-pulse (in units of 1/80 microseconds)
@ -15,16 +17,20 @@ class Pixel {
uint32_t LR; // Low time for a reset/end-of-data (in units of 1/80 microseconds)
RFControl *rf;
void loadColor(uint8_t c);
void loadColor(color_t c); // creates bit pattern for RGB color (encoded in low 24-bits)
public:
Pixel(int pin, float high0, float low0, float high1, float low1, float lowReset); // creates addressable single-wire RGB LED on pin (such as the SK68 or WS28); parameters are in MICROSECONDS!
Pixel(int pin) : Pixel(pin, 0.32, 0.88, 0.64, 0.56, 80.0) {}; // default parameters for SK68XXMINI-HS LEDs, though will likely work with many other variations as well
void setRGB(uint8_t r, uint8_t g, uint8_t b, int nPixels=1); // sets color of nPixels to rgb values (0-255)
void setHSV(float h, float s, float v, int nPixels=1); // sets color of nPixels to hsv values where h=[0,360], s=[0,1], v=[0,1]
void setRGB(uint8_t r, uint8_t g, uint8_t b, int nPixels=1); // sets color of nPixels to RGB values (0-255)
void setHSV(float h, float s, float v, int nPixels=1); // sets color of nPixels to HSV values where h=[0,360], s=[0,1], v=[0,1]
void setColor(color_t *color, int nPixels); // sets color of nPixels from array of Colors
int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1
static color_t getColorRGB(uint8_t r, uint8_t g, uint8_t b); // return pixel Color from RGB values
static color_t getColorHSV(float h, float s, float v); // return pixel Color from HSV values
operator bool(){ // override boolean operator to return true/false if creation succeeded/failed
return(*rf);
}

View File

@ -2,7 +2,7 @@
#include "Pixel.h"
Pixel px(23);
Pixel px(21);
void setup() {
@ -14,17 +14,28 @@ void setup() {
Serial.printf("PX on Pin=%d check: %s\n",px.getPin(),px?"OKAY":"BAD");
px.setRGB(0,0,0,8);
} // end of setup()
void loop(){
for(int i=0;i<5;i++){
px.setHSV(0,1.0,1.0,3);
// px.setHSV(0,1.0,0.2,8);
// delay(1000);
// px.setHSV(120,1.0,0.2,4);
// delay(1000);
// px.setHSV(240,1.0,0.2,2);
// delay(1000);
color_t x[2];
x[0]=Pixel::getColorHSV(0,1,0.2);
x[1]=px.getColorHSV(0,0.7,0.2);
px.setColor(x,2);
delay(1000);
px.setHSV(120,1.0,1.0,3);
x[0]=px.getColorHSV(0,0.7,0.2);
x[1]=px.getColorHSV(0,1,0.2);
px.setColor(x,2);
delay(1000);
px.setHSV(240,1.0,1.0,3);
delay(1000);
}
} // end of loop()