Skip to content

Instantly share code, notes, and snippets.

@josephsamela
Created May 24, 2022 01:56
Show Gist options
  • Save josephsamela/33f62913bc14ee4cdfc960f148f21781 to your computer and use it in GitHub Desktop.
Save josephsamela/33f62913bc14ee4cdfc960f148f21781 to your computer and use it in GitHub Desktop.
// Homespan firmware for ESP32 rgb led strip of NEOPIXEL/WS2812B
#if defined(CONFIG_IDF_TARGET_ESP32)
#define NEOPIXEL_RGB_PIN 32
#endif
#include "HomeSpan.h"
#include "extras/Pixel.h" // include the HomeSpan Pixel class
///////////////////////////////
struct NeoPixel_RGB : Service::LightBulb { // Addressable single-wire RGB LED Strand (e.g. NeoPixel)
Characteristic::On power{0,true};
Characteristic::Hue H{0,true};
Characteristic::Saturation S{0,true};
Characteristic::Brightness V{100,true};
Pixel *pixel;
uint8_t nPixels;
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%
pixel=new Pixel(pin); // creates Pixel LED 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 h=H.getNewVal<float>(); // range = [0,360]
float s=S.getNewVal<float>(); // range = [0,100]
float v=V.getNewVal<float>(); // range = [0,100]
Pixel::Color color;
pixel->set(color.HSV(h*p, s*p, v*p),nPixels); // sets all nPixels to the same HSV color
return(true);
}
};
void setup() {
Serial.begin(115200);
homeSpan.begin(Category::Lighting,"Pixel LEDS" DEVICE_SUFFIX);
SPAN_ACCESSORY(); // create Bridge (note this sketch uses the SPAN_ACCESSORY() macro, introduced in v1.5.1 --- see the HomeSpan API Reference for details on this convenience macro)
SPAN_ACCESSORY("Neo RGB");
new NeoPixel_RGB(NEOPIXEL_RGB_PIN,50); // create 50-LED NeoPixel RGB Strand with full color control
}
///////////////////////////////
void loop() {
homeSpan.poll();
}
///////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment