#pragma once #include "esphome/core/component.h" #include "esphome/core/log.h" #include "esphome/components/light/addressable_light.h" #include "esphome/components/spi/spi.h" namespace esphome::spi_led_strip { ESPHOME_LOG_TAG(TAG, "spi_led_strip"); enum class SpiLedChipset : uint8_t { // 4-byte start frame, then 0xFF + B, G, R per LED, then a 4-byte end frame SPI_LED_CHIPSET_APA102, // R, G, B per LED, no framing; the data is latched when the clock stays low for 500 us SPI_LED_CHIPSET_WS2801, }; // Order in which the color bytes of one LED are sent over the wire enum class RGBOrder : uint8_t { ORDER_RGB, ORDER_RBG, ORDER_GRB, ORDER_GBR, ORDER_BGR, ORDER_BRG, }; class SpiLedStrip final : public light::AddressableLight, public spi::SPIDevice { public: SpiLedStrip(uint16_t num_leds, SpiLedChipset chipset); void setup() override; float get_setup_priority() const override { return setup_priority::IO; } void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; } int32_t size() const override { return this->num_leds_; } light::LightTraits get_traits() override; void dump_config() override; void write_state(light::LightState *state) override; void clear_effect_data() override { memset(this->effect_data_, 0, this->num_leds_ * sizeof(this->effect_data_[0])); } protected: light::ESPColorView get_view_internal(int32_t index) const override; size_t buffer_size_{}; uint8_t *effect_data_{nullptr}; uint8_t *buf_{nullptr}; uint16_t num_leds_; SpiLedChipset chipset_; RGBOrder rgb_order_{RGBOrder::ORDER_BGR}; }; } // namespace esphome::spi_led_strip