From f10f5cffcd98242f3c01325f0a1807fcac727e23 Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 6 Jan 2022 21:15:03 -0600 Subject: [PATCH] Normalized and optimized Pixel methods created color_t typedef and use as basis for all RGB and HSV methods --- src/extras/Pixel.cpp | 41 ++++++++++++++++++++++++++++++++++------- src/extras/Pixel.h | 14 ++++++++++---- src/extras/extras.ino | 29 ++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/src/extras/Pixel.cpp b/src/extras/Pixel.cpp index 40639e1..cc3f37a 100644 --- a/src/extras/Pixel.cpp +++ b/src/extras/Pixel.cpp @@ -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;iphase(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;iphase(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)); +} + +/////////////////// diff --git a/src/extras/Pixel.h b/src/extras/Pixel.h index af08dd1..ae9d9ad 100644 --- a/src/extras/Pixel.h +++ b/src/extras/Pixel.h @@ -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,15 +17,19 @@ 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] - int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -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); diff --git a/src/extras/extras.ino b/src/extras/extras.ino index e134f05..49d5ce5 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -2,7 +2,7 @@ #include "Pixel.h" -Pixel px(23); +Pixel px(21); void setup() { @@ -13,18 +13,29 @@ void setup() { Serial.println("\n\nHomeSpan Pixel Example\n\n"); 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); - delay(1000); - px.setHSV(120,1.0,1.0,3); - delay(1000); - px.setHSV(240,1.0,1.0,3); - delay(1000); - } +// 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); + x[0]=px.getColorHSV(0,0.7,0.2); + x[1]=px.getColorHSV(0,1,0.2); + px.setColor(x,2); + delay(1000); } // end of loop()