Files
custom-components/components/spi_led_strip/spi_led_strip.h
T
2026-10-30 10:36:50 +01:00

61 lines
1.8 KiB
C++

#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<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH,
spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
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