ESPHome  2024.11.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() override { 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() override {
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  }
55 
57  if (this->is_failed())
58  return;
59  if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
60  char strbuf[49];
61  size_t len = std::min(this->buffer_size_, (size_t) (sizeof(strbuf) - 1) / 3);
62  memset(strbuf, 0, sizeof(strbuf));
63  for (size_t i = 0; i != len; i++) {
64  sprintf(strbuf + i * 3, "%02X ", this->buf_[i]);
65  }
66  esph_log_v(TAG, "write_state: buf = %s", strbuf);
67  }
68  this->enable();
69  this->write_array(this->buf_, this->buffer_size_);
70  this->disable();
71  }
72 
73  void clear_effect_data() override {
74  for (int i = 0; i < this->size(); i++)
75  this->effect_data_[i] = 0;
76  }
77 
78  protected:
79  light::ESPColorView get_view_internal(int32_t index) const override {
80  size_t pos = index * 4 + 5;
81  return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
82  this->effect_data_ + index, &this->correction_};
83  }
84 
85  size_t buffer_size_{};
86  uint8_t *effect_data_{nullptr};
87  uint8_t *buf_{nullptr};
88  uint16_t num_leds_;
89 };
90 
91 } // namespace spi_led_strip
92 } // namespace esphome
void write_state(light::LightState *state) override
Definition: spi_led_strip.h:56
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
void set_num_leds(uint16_t num_leds)
Definition: spi_led_strip.h:25
bool is_failed() const
Definition: component.cpp:143
light::LightTraits get_traits() override
Definition: spi_led_strip.h:20
T * allocate(size_t n)
Definition: helpers.h:681
The SPIDevice is what components using the SPI will create.
Definition: spi.h:391
uint32_t data_rate_
Definition: spi.h:376
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:79
std::string size_t len
Definition: helpers.h:293
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:666
bool state
Definition: fan.h:34