ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
spi_led_strip.cpp
Go to the documentation of this file.
1#include "spi_led_strip.h"
3
5
6constexpr size_t COLOR_DATA_OFFSET = 5;
7constexpr size_t BUFFER_PADDING = 8;
8
9SpiLedStrip::SpiLedStrip(uint16_t num_leds) : buffer_size_(num_leds * 4 + BUFFER_PADDING), num_leds_(num_leds) {}
11 RAMAllocator<uint8_t> allocator;
12 uint8_t *buf = allocator.allocate(this->buffer_size_ + this->num_leds_);
13 if (buf == nullptr) {
14 this->mark_failed(LOG_STR(ESP_LOG_MSG_ALLOC_FAIL));
15 return;
16 }
17 memset(buf, 0xFF, this->buffer_size_);
18 memset(buf, 0, 4);
22 this->accessor_ = &this->internal_accessor_;
23
24 this->spi_setup();
25}
27 auto traits = light::LightTraits();
28 traits.set_supported_color_modes({light::ColorMode::RGB});
29 return traits;
30}
32 esph_log_config(TAG,
33 "SPI LED Strip:\n"
34 " LEDs: %d",
35 this->num_leds_);
36 if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
37 esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
38 } else {
39 esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
40 }
41}
43 if (this->is_failed())
44 return;
46#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
47 {
48 char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes
49 size_t len = std::min(this->buffer_size_, (size_t) 16);
50 format_hex_pretty_to(strbuf, sizeof(strbuf), buffer, len, ' ');
51 esph_log_v(TAG, "write_state: buf = %s", strbuf);
52 }
53#endif
54 this->enable();
55 this->write_array(buffer, this->buffer_size_);
56 this->disable();
57}
58} // namespace esphome::spi_led_strip
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
void clear_effect_data(size_t num_leds)
Zero the effect data of the first num_leds LEDs; does nothing before the effect data is allocated.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
uint32_t data_rate_
Definition spi.h:436
light::LightTraits get_traits() override
light::CorrectedBufferColorAccessor internal_accessor_
void write_state(light::LightState *state) override
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
@ RGB
RGB color output.
constexpr size_t COLOR_DATA_OFFSET
constexpr size_t BUFFER_PADDING
@ DATA_RATE_1MHZ
Definition spi.h:99
const void size_t len
Definition hal.h:68
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:498