ESPHome  2025.2.0
spi_led_strip.cpp
Go to the documentation of this file.
1 #include "spi_led_strip.h"
2 
3 namespace esphome {
4 namespace spi_led_strip {
5 
6 SpiLedStrip::SpiLedStrip(uint16_t num_leds) {
7  this->num_leds_ = num_leds;
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, "SPI LED Strip:");
38  esph_log_config(TAG, " LEDs: %d", this->num_leds_);
39  if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
40  esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
41  } else {
42  esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
43  }
44 }
46  if (this->is_failed())
47  return;
48  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
49  char strbuf[49];
50  size_t len = std::min(this->buffer_size_, (size_t) (sizeof(strbuf) - 1) / 3);
51  memset(strbuf, 0, sizeof(strbuf));
52  for (size_t i = 0; i != len; i++) {
53  sprintf(strbuf + i * 3, "%02X ", this->buf_[i]);
54  }
55  esph_log_v(TAG, "write_state: buf = %s", strbuf);
56  }
57  this->enable();
58  this->write_array(this->buf_, this->buffer_size_);
59  this->disable();
60 }
62  size_t pos = index * 4 + 5;
63  return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
64  this->effect_data_ + index, &this->correction_};
65 }
66 } // namespace spi_led_strip
67 } // namespace esphome
void write_state(light::LightState *state) override
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
bool is_failed() const
Definition: component.cpp:143
light::LightTraits get_traits() override
T * allocate(size_t n)
Definition: helpers.h:703
uint32_t data_rate_
Definition: spi.h:395
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
light::ESPColorView get_view_internal(int32_t index) const override
std::string size_t len
Definition: helpers.h:301
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
An STL allocator that uses SPI or internal RAM.
Definition: helpers.h:683
bool state
Definition: fan.h:34