ESPHome 2026.2.4
Loading...
Searching...
No Matches
spi_led_strip.cpp
Go to the documentation of this file.
1#include "spi_led_strip.h"
3
4namespace esphome {
5namespace spi_led_strip {
6
7SpiLedStrip::SpiLedStrip(uint16_t num_leds) {
8 this->num_leds_ = num_leds;
9 RAMAllocator<uint8_t> allocator;
10 this->buffer_size_ = num_leds * 4 + 8;
11 this->buf_ = allocator.allocate(this->buffer_size_);
12 if (this->buf_ == nullptr) {
13 ESP_LOGE(TAG, "Failed to allocate buffer of size %u", this->buffer_size_);
14 return;
15 }
16
17 this->effect_data_ = allocator.allocate(num_leds);
18 if (this->effect_data_ == nullptr) {
19 ESP_LOGE(TAG, "Failed to allocate effect data of size %u", num_leds);
20 return;
21 }
22 memset(this->buf_, 0xFF, this->buffer_size_);
23 memset(this->buf_, 0, 4);
24}
26 if (this->effect_data_ == nullptr || this->buf_ == nullptr) {
27 this->mark_failed();
28 return;
29 }
30 this->spi_setup();
31}
33 auto traits = light::LightTraits();
34 traits.set_supported_color_modes({light::ColorMode::RGB});
35 return traits;
36}
38 esph_log_config(TAG,
39 "SPI LED Strip:\n"
40 " LEDs: %d",
41 this->num_leds_);
42 if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
43 esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
44 } else {
45 esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
46 }
47}
49 if (this->is_failed())
50 return;
51#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
52 {
53 char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes
54 size_t len = std::min(this->buffer_size_, (size_t) 16);
55 format_hex_pretty_to(strbuf, sizeof(strbuf), this->buf_, len, ' ');
56 esph_log_v(TAG, "write_state: buf = %s", strbuf);
57 }
58#endif
59 this->enable();
60 this->write_array(this->buf_, this->buffer_size_);
61 this->disable();
62}
64 size_t pos = index * 4 + 5;
65 return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
66 this->effect_data_ + index, &this->correction_};
67}
68} // namespace spi_led_strip
69} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1647
T * allocate(size_t n)
Definition helpers.h:1667
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
This class is used to represent the capabilities of a light.
uint32_t data_rate_
Definition spi.h:410
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:91
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:692
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:353
size_t size_t pos
Definition helpers.h:729