ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
spi_led_strip.cpp
Go to the documentation of this file.
1#include "spi_led_strip.h"
3
5
6SpiLedStrip::SpiLedStrip(uint16_t num_leds) {
7 this->num_leds_ = num_leds;
8 RAMAllocator<uint8_t> allocator;
9 this->buffer_size_ = num_leds * 4 + 8;
10 this->buf_ = allocator.allocate(this->buffer_size_);
11 if (this->buf_ == nullptr) {
12 ESP_LOGE(TAG, "Failed to allocate buffer of size %u", this->buffer_size_);
13 return;
14 }
15
16 this->effect_data_ = allocator.allocate(num_leds);
17 if (this->effect_data_ == nullptr) {
18 ESP_LOGE(TAG, "Failed to allocate effect data of size %u", num_leds);
19 return;
20 }
21 memset(this->buf_, 0xFF, this->buffer_size_);
22 memset(this->buf_, 0, 4);
23}
25 if (this->effect_data_ == nullptr || this->buf_ == nullptr) {
26 this->mark_failed();
27 return;
28 }
29 this->spi_setup();
30}
32 auto traits = light::LightTraits();
33 traits.set_supported_color_modes({light::ColorMode::RGB});
34 return traits;
35}
37 esph_log_config(TAG,
38 "SPI LED Strip:\n"
39 " LEDs: %d",
40 this->num_leds_);
41 if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
42 esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
43 } else {
44 esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
45 }
46}
48 if (this->is_failed())
49 return;
50#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
51 {
52 char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes
53 size_t len = std::min(this->buffer_size_, (size_t) 16);
54 format_hex_pretty_to(strbuf, sizeof(strbuf), this->buf_, len, ' ');
55 esph_log_v(TAG, "write_state: buf = %s", strbuf);
56 }
57#endif
58 this->enable();
59 this->write_array(this->buf_, this->buffer_size_);
60 this->disable();
61}
63 size_t pos = index * 4 + 5;
64 return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
65 this->effect_data_ + index, &this->correction_};
66}
67} // namespace esphome::spi_led_strip
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2053
T * allocate(size_t n)
Definition helpers.h:2080
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
uint32_t data_rate_
Definition spi.h:412
light::LightTraits get_traits() override
void write_state(light::LightState *state) override
light::ESPColorView get_view_internal(int32_t index) const override
bool state
Definition fan.h:2
@ RGB
RGB color output.
@ DATA_RATE_1MHZ
Definition spi.h:93
const void size_t len
Definition hal.h:64
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:340
size_t size_t pos
Definition helpers.h:1038