From d5b27d6e1462700650a28772c4044aabd486146b Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 6 Jan 2022 18:09:22 -0600 Subject: [PATCH] Added ability to set same color on multiple pixels Added 4th, optional argument, nPixels, to setRGB() and setHSV(). --- src/extras/Pixel.cpp | 15 ++++++++------- src/extras/Pixel.h | 6 +++--- src/extras/extras.ino | 41 +++++++++++------------------------------ 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/extras/Pixel.cpp b/src/extras/Pixel.cpp index 8feb825..40639e1 100644 --- a/src/extras/Pixel.cpp +++ b/src/extras/Pixel.cpp @@ -12,30 +12,31 @@ Pixel::Pixel(int pin, float high0, float low0, float high1, float low1, float lo LR=lowReset*80; rf=new RFControl(pin,false); // set clock to 1/80 usec - setRGB(0,0,0); } /////////////////// -void Pixel::setRGB(uint8_t r, uint8_t g, uint8_t b){ +void Pixel::setRGB(uint8_t r, uint8_t g, uint8_t b, int nPixels){ if(!*rf) return; rf->clear(); - loadColor(g); - loadColor(r); - loadColor(b); + for(int i=0;iphase(LR,0); // end-marker delay/reset rf->start(); } /////////////////// -void Pixel::setHSV(float h, float s, float v){ +void Pixel::setHSV(float h, float s, float v, int nPixels){ float r,g,b; LedPin::HSVtoRGB(h,s,v,&r,&g,&b); - setRGB(r*255,g*255,b*255); + setRGB(r*255,g*255,b*255,nPixels); } /////////////////// diff --git a/src/extras/Pixel.h b/src/extras/Pixel.h index bfcc5b8..af08dd1 100644 --- a/src/extras/Pixel.h +++ b/src/extras/Pixel.h @@ -21,9 +21,9 @@ class Pixel { 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); // sets color to rgb values (0-255) - void setHSV(float h, float s, float v); // sets color 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] + int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1 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 a7b1d1a..e134f05 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -2,6 +2,8 @@ #include "Pixel.h" +Pixel px(23); + void setup() { Serial.begin(115200); // start the Serial interface @@ -10,40 +12,19 @@ void setup() { Serial.println("\n\nHomeSpan Pixel Example\n\n"); - Pixel px(8); - Pixel test7(7); - Pixel test6(6); - Serial.printf("PX on Pin=%d check: %s\n",px.getPin(),px?"OKAY":"BAD"); - Serial.printf("Test 7 on Pin=%d check: %s\n",test7.getPin(),test7?"OKAY":"BAD"); - Serial.printf("Test 6 on Pin=%d check: %s\n",test6.getPin(),test6?"OKAY":"BAD"); - -for(int i=0;i<5;i++){ - px.setHSV(60,0.9,0.5); - delay(1000); - px.setHSV(120,0.9,0.5); - delay(1000); - px.setHSV(240,0.9,0.5); - delay(1000); -} - - - -// while(1){ -// for(int i=0;i<50;i++){ -// px.setRGB(i,0,0); -// delay(2); -// } -// for(int i=50;i>=0;i--){ -// px.setRGB(i,0,0); -// delay(2); -// } -// } - - Serial.println("Done!"); } // 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); + } + } // end of loop()