ESPHome  2024.4.2
spi_led_strip.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/log.h"
7 
8 namespace esphome {
9 namespace spi_led_strip {
10 
11 static const char *const TAG = "spi_led_strip";
13  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
14  spi::DATA_RATE_1MHZ> {
15  public:
16  void setup() { this->spi_setup(); }
17 
18  int32_t size() const override { return this->num_leds_; }
19 
21  auto traits = light::LightTraits();
22  traits.set_supported_color_modes({light::ColorMode::RGB});
23  return traits;
24  }
25  void set_num_leds(uint16_t num_leds) {
26  this->num_leds_ = num_leds;
28  this->buffer_size_ = num_leds * 4 + 8;
29  this->buf_ = allocator.allocate(this->buffer_size_);
30  if (this->buf_ == nullptr) {
31  esph_log_e(TAG, "Failed to allocate buffer of size %u", this->buffer_size_);
32  this->mark_failed();
33  return;
34  }
35 
36  this->effect_data_ = allocator.allocate(num_leds);
37  if (this->effect_data_ == nullptr) {
38  esph_log_e(TAG, "Failed to allocate effect data of size %u", num_leds);
39  this->mark_failed();
40  return;
41  }
42  memset(this->buf_, 0xFF, this->buffer_size_);
43  memset(this->buf_, 0, 4);
44  }
45 
46  void dump_config() {
47  esph_log_config(TAG, "SPI LED Strip:");
48  esph_log_config(TAG, " LEDs: %d", this->num_leds_);
49  if (this->data_rate_ >= spi::DATA_RATE_1MHZ)
50  esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
51  else
52  esph_log_config(TAG, " Data rate: %ukHz", (unsigned) (this->data_rate_ / 1000));
53  }
54 
56  if (this->is_failed())
57  return;
58  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
59  char strbuf[49];
60  size_t len = std::min(this->buffer_size_, (size_t) (sizeof(strbuf) - 1) / 3);
61  memset(strbuf, 0, sizeof(strbuf));
62  for (size_t i = 0; i != len; i++) {
63  sprintf(strbuf + i * 3, "%02X ", this->buf_[i]);
64  }
65  esph_log_v(TAG, "write_state: buf = %s", strbuf);
66  }
67  this->enable();
68  this->write_array(this->buf_, this->buffer_size_);
69  this->disable();
70  }
71 
72  void clear_effect_data() override {
73  for (int i = 0; i < this->size(); i++)
74  this->effect_data_[i] = 0;
75  }
76 
77  protected:
78  light::ESPColorView get_view_internal(int32_t index) const override {
79  size_t pos = index * 4 + 5;
80  return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
81  this->effect_data_ + index, &this->correction_};
82  }
83 
84  size_t buffer_size_{};
85  uint8_t *effect_data_{nullptr};
86  uint8_t *buf_{nullptr};
87  uint16_t num_leds_;
88 };
89 
90 } // namespace spi_led_strip
91 } // namespace esphome
void write_state(light::LightState *state) override
Definition: spi_led_strip.h:55
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
void set_num_leds(uint16_t num_leds)
Definition: spi_led_strip.h:25
light::LightTraits get_traits() override
Definition: spi_led_strip.h:20
An STL allocator that uses SPI RAM.
Definition: helpers.h:645
The SPIDevice is what components using the SPI will create.
Definition: spi.h:408
uint32_t data_rate_
Definition: spi.h:393
int32_t size() const override
Definition: spi_led_strip.h:18
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
Definition: spi_led_strip.h:78
std::string size_t len
Definition: helpers.h:292
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
bool state
Definition: fan.h:34