diff --git a/examples/Other Examples/PixelTester/PixelTester.ino b/examples/Other Examples/PixelTester/PixelTester.ino new file mode 100644 index 0000000..98a3278 --- /dev/null +++ b/examples/Other Examples/PixelTester/PixelTester.ino @@ -0,0 +1,131 @@ +/********************************************************************************* + * MIT License + * + * Copyright (c) 2024 Gregg E. Berman + * + * https://github.com/HomeSpan/HomeSpan + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + ********************************************************************************/ + +/////////////////////// PIXEL TESTER ////////////////////////// + +// This sketch is designed to help identify the proper settings to use for a NeoPixel, NeoPixel Strip, +// or any device containing one or more single-wire addressable RGB or RGBW LEDs (the "Pixel Device"). + +// Before compiling, set PIXEL_PIN to the ESP32 pin that is connected to your Pixel Device, and set NPIXELS to +// the numnber of Pixels in the Pixel Device. Note that for some strips a single chip controls more than one LED, +// in which case NPIXELS should be set to the number of controlling chips, NOT the number of LEDs. + +// To start, the second argument of the Pixel constructor for the testPixel object below should remain +// set to PixelType::RGBW + +// When run, the sketch will repeatedly cycle colors by setting ALL pixels in the device first to RED, then GREEN, +// followed by BLUE, and then finally WHITE. After a short pause, the cycle repeats. + +// For each color the brightness will increase from 0 through MAX_BRIGHTNESS, and then back to 0. You can change +// MAX_BRIGHTNESS to something lower than 255 if you want to limit how bright the pixels get. + +// For Pixel Devices with more than one pixel, diagnostics are as follows: +// +// * If all 4 colors repeatedly flash in the order expected, this means the base setting of PixelType::RGBW is correct! +// +// * If instead of each pixel being set to the same color, the pixels in the strip each light up with a different color +// (or no color at all), this means you have an RGB LED, not an RGBW LED. Change the second parameter of the constructor +// to PixelType::RGB and re-run the sketch. +// +// * If all of the pixels are being set to the same color, but the sequence is NOT in the order RED, GREEN, BLUE, change +// the second parameter of the constructor so that the order of the PixelType colors match the sequence of the colors +// that appear on the Pixel Device. For example, if your RGBW Pixel Device flashes GREEN, RED, BLUE, and than WHITE, use +// PixelType::GRBW. + +// For Pixel Devices with only a single pixel, diagnostics are as follows: + +// * If all 4 colors repeatedly flash in the order expected, this means the base setting of PixelType::RGBW is correct! +// +// * If the pixel does not light at all when set to WHITE this means you have an RGB LED, not an RGBW LED. Change the +// second parameter of the constructor to PixelType::RGB and re-run the sketch. +// +// * If all of the pixels are being set to the same color, but the sequence is NOT in the order RED, GREEN, BLUE, change +// the second parameter of the constructor so that the order of the PixelType colors match the sequence of the colors +// that appear on the Pixel Device. For example, if your RGB Pixel Device flashes GREEN, RED, and then BLUE, use +// PixelType::GRB. + +////////////////////////////////////// + +#include "HomeSpan.h" + +////////////////////////////////////// + +#define MAX_BRIGHTNESS 255 // maximum brightness when flashing RGBW [0-255] + +#define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only" +#define NPIXELS 8 // set to number of pixels in strip + +Pixel testPixel(PIXEL_PIN, PixelType::RGBW); // change the second argument until device operates with correct colors + +////////////////////////////////////// + +void setup() { + + Serial.begin(115200); + delay(1000); + + Serial.printf("\n\nPixel Test on pin %d with %d pixels\n\n",PIXEL_PIN,NPIXELS); +} + +////////////////////////////////////// + +void flashColor(boolean r, boolean g, boolean b, boolean w){ + + for(int i=0;i=0;i--){ + testPixel.set(Pixel::RGB(i*r,i*g,i*b,i*w),NPIXELS); + delay(4); + } +} + +////////////////////////////////////// + +void loop(){ + + Serial.printf("Red..."); + flashColor(1,0,0,0); + + Serial.printf("Green..."); + flashColor(0,1,0,0); + + Serial.printf("Blue..."); + flashColor(0,0,1,0); + + if(testPixel.isRGBW()){ + Serial.printf("White..."); + flashColor(0,0,0,1); + } + + Serial.printf("Pausing.\n"); + delay(1000); +} + +////////////////////////////////////// diff --git a/src/src/extras/Pixel.h b/src/src/extras/Pixel.h index 921c928..1cd37a1 100644 --- a/src/src/extras/Pixel.h +++ b/src/src/extras/Pixel.h @@ -41,7 +41,6 @@ typedef const uint8_t pixelType_t[]; namespace PixelType { -// const uint8_t RGB[4]={31,23,15,0}; pixelType_t RGB={31,23,15,0}; pixelType_t RBG={31,15,23,0}; pixelType_t BRG={23,15,31,0}; @@ -163,15 +162,15 @@ class Pixel : public Blinkable { public: - Pixel(int pin, pixelType_t pixelType=PixelType::GRB); // creates addressable single-wire LED of pixelType connected to pin (such as the SK68 or WS28) - Pixel(int pin, boolean isRGBW):Pixel(pin,isRGBW?PixelType::GRBW:PixelType::GRB){}; // old-style constructor included for backwards compatibility - 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 + Pixel(int pin, pixelType_t pixelType=PixelType::GRB); // creates addressable single-wire LED of pixelType connected to pin (such as the SK68 or WS28) + 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 static Color RGB(uint8_t r, uint8_t g, uint8_t b, uint8_t w=0){return(Color().RGB(r,g,b,w));} // an alternative method for returning an RGB Color static Color HSV(float h, float s, float v, double w=0){return(Color().HSV(h,s,v,w));} // an alternative method for returning an HSV Color int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1 + boolean isRGBW(){return(bytesPerPixel==4);} // returns true if RGBW LED, else false if RGB LED void setTiming(float high0, float low0, float high1, float low1, uint32_t lowReset); // changes default timings for bit pulse - note parameters are in MICROSECONDS operator bool(){ // override boolean operator to return true/false if creation succeeded/failed @@ -181,6 +180,10 @@ class Pixel : public Blinkable { void on() {set(onColor);} void off() {set(RGB(0,0,0,0));} Pixel *setOnColor(Color c){onColor=c;return(this);} + + [[deprecated("Please use Pixel(int pin, pixelType_t pixelType) constructor instead.")]] + Pixel(int pin, boolean isRGBW):Pixel(pin,isRGBW?PixelType::GRBW:PixelType::GRB){}; + }; //////////////////////////////////////////// diff --git a/src/src/extras/extras.ino b/src/src/extras/extras.ino index 14d5006..637bf1e 100644 --- a/src/src/extras/extras.ino +++ b/src/src/extras/extras.ino @@ -27,18 +27,34 @@ #include "Pixel.h" -#define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only" -#define NPIXELS 8 // set to number of pixels in strand +#define MAX_BRIGHTNESS 255 // maximum brightness when flashing RGB [0-255] -Pixel testPixel(PIXEL_PIN); +#define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only" +#define NPIXELS 8 // set to number of pixels in strand + +Pixel testPixel(PIXEL_PIN, PixelType::RGBW); void setup() { Serial.begin(115200); delay(1000); - Serial.printf("\n\nPixel Test on pin %d with %d pixels\n\n",PIXEL_PIN,NPIXELS); + Serial.printf("\n\nPixel Test on pin %d with %d pixels\n\n",PIXEL_PIN,NPIXELS); +} + +////////////////////////////////////// + +void flashColor(boolean r, boolean g, boolean b, boolean w){ + for(int i=0;i=0;i--){ + testPixel.set(Pixel::RGB(i*r,i*g,i*b,i*w),NPIXELS); + delay(4); + } } ////////////////////////////////////// @@ -46,46 +62,17 @@ void setup() { void loop(){ Serial.printf("Red..."); - for(int i=0;i<255;i++){ - testPixel.set(Pixel::RGB(i,0,0,0),NPIXELS); - delay(10); - } - for(int i=255;i>=0;i--){ - testPixel.set(Pixel::RGB(i,0,0,0),NPIXELS); - delay(10); - } - delay(2000); + flashColor(1,0,0,0); Serial.printf("Green..."); - for(int i=0;i<255;i++){ - testPixel.set(Pixel::RGB(0,i,0,0),NPIXELS); - delay(10); - } - for(int i=255;i>=0;i--){ - testPixel.set(Pixel::RGB(0,i,0,0),NPIXELS); - delay(10); - } - delay(2000); - - Serial.printf("Blue..."); - for(int i=0;i<255;i++){ - testPixel.set(Pixel::RGB(0,0,i,0),NPIXELS); - delay(10); - } - for(int i=255;i>=0;i--){ - testPixel.set(Pixel::RGB(0,0,i,0),NPIXELS); - delay(10); - } - delay(2000); + flashColor(0,1,0,0); - Serial.printf("White...\n"); - for(int i=0;i<255;i++){ - testPixel.set(Pixel::RGB(0,0,0,i),NPIXELS); - delay(10); - } - for(int i=255;i>=0;i--){ - testPixel.set(Pixel::RGB(0,0,0,i),NPIXELS); - delay(10); - } - delay(2000); + Serial.printf("Blue..."); + flashColor(0,0,1,0); + + Serial.printf("White..."); + flashColor(0,0,0,1); + + Serial.printf("Pausing.\n"); + delay(1000); }