Updated Pixel Example

This commit is contained in:
Gregg 2022-02-13 11:57:34 -06:00
parent eee0eb6954
commit a4fb99a684
2 changed files with 115 additions and 61 deletions

View File

@ -33,17 +33,20 @@
// IMPORTANT: YOU LIKELY WILL NEED TO CHANGE THE PIN NUMBERS BELOW TO MATCH YOUR SPECIFIC ESP32/S2/C3 BOARD // IMPORTANT: YOU LIKELY WILL NEED TO CHANGE THE PIN NUMBERS BELOW TO MATCH YOUR SPECIFIC ESP32/S2/C3 BOARD
// //
#define NEOPIXEL_RGB_PIN 26
#define NEOPIXEL_RGBW_PIN 32
#define DOTSTAR_DATA_PIN 33
#define DOTSTAR_CLOCK_PIN 27
#define NEOPIXEL_PIN 23 // only one pin needed for NeoPixels #define NEOPIXEL_PIN 23 // only one pin needed for NeoPixels
#define DOTSTAR_DATA_PIN 32 // two pins are needed to DotStars
#define DOTSTAR_CLOCK_PIN 5
#include "HomeSpan.h" #include "HomeSpan.h"
#include "extras/Pixel.h" // include the HomeSpan Pixel class #include "extras/Pixel.h" // include the HomeSpan Pixel class
/////////////////////////////// ///////////////////////////////
struct Pixel_Light : Service::LightBulb { // Addressable single-wire RGB LED Strand (e.g. NeoPixel) struct NeoPixel_RGB : Service::LightBulb { // Addressable single-wire RGB LED Strand (e.g. NeoPixel)
Characteristic::On power{0,true}; Characteristic::On power{0,true};
Characteristic::Hue H{0,true}; Characteristic::Hue H{0,true};
@ -52,10 +55,10 @@ struct Pixel_Light : Service::LightBulb { // Addressable single-wire RGB LE
Pixel *pixel; Pixel *pixel;
uint8_t nPixels; uint8_t nPixels;
Pixel_Light(uint8_t pin, uint8_t nPixels) : Service::LightBulb(){ NeoPixel_RGB(uint8_t pin, uint8_t nPixels) : Service::LightBulb(){
V.setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1% V.setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
pixel=new Pixel(pin); // creates Pixel LED on specified pin using default timing parameters suitable for most SK68xx LEDs pixel=new Pixel(pin); // creates Pixel LED on specified pin
this->nPixels=nPixels; // save number of Pixels in this LED Strand this->nPixels=nPixels; // save number of Pixels in this LED Strand
update(); // manually call update() to set pixel with restored initial values update(); // manually call update() to set pixel with restored initial values
} }
@ -68,7 +71,9 @@ struct Pixel_Light : Service::LightBulb { // Addressable single-wire RGB LE
float s=S.getNewVal<float>(); // range = [0,100] float s=S.getNewVal<float>(); // range = [0,100]
float v=V.getNewVal<float>(); // range = [0,100] float v=V.getNewVal<float>(); // range = [0,100]
pixel->set(Pixel::HSV(h*p, s*p, v*p),nPixels); // sets all nPixels to the same HSV color Pixel::Color color;
pixel->set(color.HSV(h*p, s*p, v*p),nPixels); // sets all nPixels to the same HSV color
return(true); return(true);
} }
@ -76,58 +81,72 @@ struct Pixel_Light : Service::LightBulb { // Addressable single-wire RGB LE
/////////////////////////////// ///////////////////////////////
struct Dot_Light : Service::LightBulb { // Addressable two-wire RGB LED Strand (e.g. DotStar) - Chasing Light Effect struct NeoPixel_RGBW : Service::LightBulb { // Addressable single-wire RGBW LED Strand (e.g. NeoPixel)
Characteristic::On power{0,true};
Characteristic::Brightness V{100,true};
Characteristic::ColorTemperature T{140,true};
Pixel *pixel;
uint8_t nPixels;
NeoPixel_RGBW(uint8_t pin, uint8_t nPixels) : Service::LightBulb(){
V.setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
pixel=new Pixel(pin,true); // creates Pixel RGBW LED (second parameter set to true for RGBW) on specified pin
this->nPixels=nPixels; // save number of Pixels in this LED Strand
update(); // manually call update() to set pixel with restored initial values
}
boolean update() override {
int p=power.getNewVal();
float v=V.getNewVal<float>(); // range = [0,100]
float t=T.getNewVal<float>(); // range = [140,500] (140=coldest, 500=warmest)
float hue=240-(t-140)/3; // add in a splash of color between blue and green to simulated change of color temperature
Pixel::Color color;
pixel->set(color.HSV(hue, 100, v*p, v*p),nPixels); // sets all nPixels to the same HSV color
return(true);
}
};
///////////////////////////////
struct DotStar_RGB : Service::LightBulb { // Addressable two-wire RGB LED Strand (e.g. DotStar)
Characteristic::On power{0,true}; Characteristic::On power{0,true};
Characteristic::Hue H{0,true}; Characteristic::Hue H{0,true};
Characteristic::Saturation S{0,true}; Characteristic::Saturation S{0,true};
Characteristic::Brightness V{100,true}; Characteristic::Brightness V{100,true};
Dot *dot; Dot *pixel;
uint8_t nPixels; uint8_t nPixels;
Dot::Color *colors;
int phase=0;
int dir=1;
uint32_t alarmTime=0;
Dot_Light(uint8_t dataPin, uint8_t clockPin, uint8_t nPixels) : Service::LightBulb(){ DotStar_RGB(uint8_t dataPin, uint8_t clockPin, uint8_t nPixels) : Service::LightBulb(){
dot=new Dot(dataPin,clockPin); // creates Dot LED on specified pins - suitable for APA102 or SK9822 V.setRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1%
pixel=new Dot(dataPin,clockPin); // creates Dot LED on specified pins
this->nPixels=nPixels; // save number of Pixels in this LED Strand this->nPixels=nPixels; // save number of Pixels in this LED Strand
update(); // manually call update() to set pixel with restored initial values
colors=(Dot::Color *)calloc(2*nPixels-1,sizeof(Dot::Color)); // storage for dynamic pixel pattern
update(); // manually call update() to set pixel with restored initial values
} }
boolean update() override { boolean update() override {
if(!power.getNewVal()) int p=power.getNewVal();
dot->set(Dot::RGB(0,0,0),nPixels); // turn off all LEDs in Strand
float h=H.getNewVal<float>(); // range = [0,360]
float s=S.getNewVal<float>(); // range = [0,100]
float v=V.getNewVal<float>(); // range = [0,100]
Dot::Color color;
pixel->set(color.HSV(h*p, s*p, 100, v*p),nPixels); // sets all nPixels to the same HSV color, but instead of PWM, using current-limiting parameter to control overall brightness
return(true); return(true);
} }
void loop() override {
if(millis()>alarmTime && power.getVal()){
alarmTime=millis()+40;
colors[phase]=Dot::HSV(0,0,0);
if(phase==nPixels-1)
dir=-1;
else if(phase==0)
dir=1;
phase+=dir;
float h=H.getNewVal<float>(); // range = [0,360]
float s=S.getNewVal<float>(); // range = [0,100]
float v=V.getNewVal<float>(); // range = [0,100]
colors[phase]=Dot::HSV(h,s,100,v);
dot->set(colors,nPixels); // update Strand with new colors
}
}
}; };
/////////////////////////////// ///////////////////////////////
@ -136,35 +155,58 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
homeSpan.begin(Category::Lighting,"RGB Pixels"); homeSpan.begin(Category::Lighting,"Addressable LEDs");
new SpanAccessory(); // create Bridge
new Service::AccessoryInformation();
new Characteristic::Name("Addressable LEDs");
new Characteristic::Manufacturer("HomeSpan");
new Characteristic::SerialNumber("123-ABC");
new Characteristic::Model("Neo/Dot Pixels");
new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify();
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
/////////
new SpanAccessory(); new SpanAccessory();
new Service::AccessoryInformation(); new Service::AccessoryInformation();
new Characteristic::Name("NeoPixel"); new Characteristic::Name("Neo RGB");
new Characteristic::Manufacturer("HomeSpan"); new Characteristic::Manufacturer("HomeSpan");
new Characteristic::SerialNumber("123-ABC"); new Characteristic::SerialNumber("123-ABC");
new Characteristic::Model("8-LED Strand"); new Characteristic::Model("8-LED Strand");
new Characteristic::FirmwareRevision("1.0"); new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify(); new Characteristic::Identify();
new NeoPixel_RGB(NEOPIXEL_RGB_PIN,8); // create 8-LED NeoPixel RGB Strand with full color control
new Service::HAPProtocolInformation(); /////////
new Characteristic::Version("1.1.0");
new Pixel_Light(NEOPIXEL_PIN,8); // create 8-LED NeoPixel Strand
new SpanAccessory(); new SpanAccessory();
new Service::AccessoryInformation(); new Service::AccessoryInformation();
new Characteristic::Name("DotStar"); new Characteristic::Name("Neo RGBW");
new Characteristic::Manufacturer("HomeSpan");
new Characteristic::SerialNumber("123-ABC");
new Characteristic::Model("60-LED Strand");
new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify();
new NeoPixel_RGBW(NEOPIXEL_RGBW_PIN,60); // create 60-LED NeoPixel RGBW Strand with simulated color temperature control
/////////
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Name("Dot RGB");
new Characteristic::Manufacturer("HomeSpan"); new Characteristic::Manufacturer("HomeSpan");
new Characteristic::SerialNumber("123-ABC"); new Characteristic::SerialNumber("123-ABC");
new Characteristic::Model("30-LED Strand"); new Characteristic::Model("30-LED Strand");
new Characteristic::FirmwareRevision("1.0"); new Characteristic::FirmwareRevision("1.0");
new Characteristic::Identify(); new Characteristic::Identify();
new Service::HAPProtocolInformation(); new DotStar_RGB(DOTSTAR_DATA_PIN,DOTSTAR_CLOCK_PIN,30); // create 30-LED DotStar RGB Strand with full color control, but use current-limiting feature to create flicker-free dimming
new Characteristic::Version("1.1.0");
new Dot_Light(DOTSTAR_DATA_PIN,DOTSTAR_CLOCK_PIN,30); // create 30-LED DotStar Strand with Chasing-Light effect
} }

View File

@ -108,16 +108,17 @@ struct Effect3 {
#elif defined(CONFIG_IDF_TARGET_ESP32) #elif defined(CONFIG_IDF_TARGET_ESP32)
Pixel px1(23,true); // NeoPixel RGB // Pixel px1(23,true); // NeoPixel RGB
Pixel px2(21,true); // NeoPixel RGBW // Dot dot(32,5); // DotStar
Dot dot(32,5); // DotStar Pixel px2(21); // NeoPixel RGBW
Pixel neo(26);
#endif #endif
//Effect1 effect1(&px1,20,60); //Effect1 effect1(&px1,20,60);
//Effect2 effect2(&px2,20,60); //Effect2 effect2(&px2,20,60);
Effect2 effect2(&px1,20,60); //Effect2 effect2(&px1,20,60);
Effect3 effect3(&dot,20,30); //Effect3 effect3(&dot,20,30);
void setup() { void setup() {
@ -126,11 +127,22 @@ void setup() {
delay(1000); // wait for interface to flush delay(1000); // wait for interface to flush
Serial.println("\n\nHomeSpan Pixel Example\n"); Serial.println("\n\nHomeSpan Pixel Example\n");
Pixel::Color c;
int hue=0;
// Pixel px2(21); // NeoPixel RGBW
while(1){
neo.set(c.HSV(hue,100,10),8);
hue=(hue+10)%360;
delay(100);
}
} // end of setup() } // end of setup()
void loop(){ void loop(){
// effect1.update(); // effect1.update();
effect2.update(); // effect2.update();
effect3.update(); // effect3.update();
} }