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

133 lines
4.0 KiB
C++

#include "spi_led_strip.h"
#include "esphome/core/helpers.h"
namespace esphome::spi_led_strip {
static const LogString *rgb_order_to_string(RGBOrder rgb_order) {
switch (rgb_order) {
case RGBOrder::ORDER_RGB:
return LOG_STR("RGB");
case RGBOrder::ORDER_RBG:
return LOG_STR("RBG");
case RGBOrder::ORDER_GRB:
return LOG_STR("GRB");
case RGBOrder::ORDER_GBR:
return LOG_STR("GBR");
case RGBOrder::ORDER_BGR:
return LOG_STR("BGR");
case RGBOrder::ORDER_BRG:
return LOG_STR("BRG");
default:
return LOG_STR("UNKNOWN");
}
}
SpiLedStrip::SpiLedStrip(uint16_t num_leds, SpiLedChipset chipset) : num_leds_(num_leds), chipset_(chipset) {
RAMAllocator<uint8_t> allocator;
#ifdef USE_SPI_LED_STRIP_WS2801
if (chipset == SpiLedChipset::SPI_LED_CHIPSET_WS2801) {
this->buffer_size_ = num_leds * 3;
} else
#endif
{
this->buffer_size_ = num_leds * 4 + 8;
}
this->buf_ = allocator.allocate(this->buffer_size_);
if (this->buf_ == nullptr) {
ESP_LOGE(TAG, "Failed to allocate buffer of size %u", this->buffer_size_);
return;
}
this->effect_data_ = allocator.allocate(num_leds);
if (this->effect_data_ == nullptr) {
ESP_LOGE(TAG, "Failed to allocate effect data of size %u", num_leds);
return;
}
#ifdef USE_SPI_LED_STRIP_WS2801
if (chipset == SpiLedChipset::SPI_LED_CHIPSET_WS2801) {
memset(this->buf_, 0, this->buffer_size_);
return;
}
#endif
memset(this->buf_, 0xFF, this->buffer_size_);
memset(this->buf_, 0, 4);
}
void SpiLedStrip::setup() {
if (this->effect_data_ == nullptr || this->buf_ == nullptr) {
this->mark_failed();
return;
}
this->spi_setup();
}
light::LightTraits SpiLedStrip::get_traits() {
auto traits = light::LightTraits();
traits.set_supported_color_modes({light::ColorMode::RGB});
return traits;
}
void SpiLedStrip::dump_config() {
esph_log_config(
TAG,
"SPI LED Strip:\n"
" Chipset: %s\n"
" LEDs: %d\n"
" RGB order: %s",
this->chipset_ == SpiLedChipset::SPI_LED_CHIPSET_WS2801 ? LOG_STR_LITERAL("WS2801") : LOG_STR_LITERAL("APA102"),
this->num_leds_, LOG_STR_ARG(rgb_order_to_string(this->rgb_order_)));
if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
} else {
esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
}
}
void SpiLedStrip::write_state(light::LightState *state) {
if (this->is_failed())
return;
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
{
char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes
size_t len = std::min(this->buffer_size_, (size_t) 16);
format_hex_pretty_to(strbuf, sizeof(strbuf), this->buf_, len, ' ');
esph_log_v(TAG, "write_state: buf = %s", strbuf);
}
#endif
this->enable();
this->write_array(this->buf_, this->buffer_size_);
this->disable();
}
light::ESPColorView SpiLedStrip::get_view_internal(int32_t index) const {
// byte offsets of red, green and blue within the color bytes of one LED
uint8_t r, g, b;
switch (this->rgb_order_) {
case RGBOrder::ORDER_RGB:
r = 0, g = 1, b = 2;
break;
case RGBOrder::ORDER_RBG:
r = 0, g = 2, b = 1;
break;
case RGBOrder::ORDER_GRB:
r = 1, g = 0, b = 2;
break;
case RGBOrder::ORDER_GBR:
r = 2, g = 0, b = 1;
break;
case RGBOrder::ORDER_BRG:
r = 1, g = 2, b = 0;
break;
case RGBOrder::ORDER_BGR:
default:
r = 2, g = 1, b = 0;
break;
}
#ifdef USE_SPI_LED_STRIP_WS2801
if (this->chipset_ == SpiLedChipset::SPI_LED_CHIPSET_WS2801) {
size_t pos = index * 3;
return {this->buf_ + pos + r, this->buf_ + pos + g, this->buf_ + pos + b, nullptr,
this->effect_data_ + index, &this->correction_};
}
#endif
size_t pos = index * 4 + 5;
return {this->buf_ + pos + r, this->buf_ + pos + g, this->buf_ + pos + b, nullptr,
this->effect_data_ + index, &this->correction_};
}
} // namespace esphome::spi_led_strip