ESPHome  2025.2.0
led_strip.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
7 #include "esphome/core/color.h"
9 #include "esphome/core/helpers.h"
10 
11 #include <driver/gpio.h>
12 #include <esp_err.h>
13 #include <esp_idf_version.h>
14 
15 #if ESP_IDF_VERSION_MAJOR >= 5
16 #include <driver/rmt_tx.h>
17 #else
18 #include <driver/rmt.h>
19 #endif
20 
21 namespace esphome {
22 namespace esp32_rmt_led_strip {
23 
24 enum RGBOrder : uint8_t {
31 };
32 
34  public:
35  void setup() override;
36  void write_state(light::LightState *state) override;
37  float get_setup_priority() const override;
38 
39  int32_t size() const override { return this->num_leds_; }
41  auto traits = light::LightTraits();
42  if (this->is_rgbw_ || this->is_wrgb_) {
43  traits.set_supported_color_modes({light::ColorMode::RGB_WHITE, light::ColorMode::WHITE});
44  } else {
45  traits.set_supported_color_modes({light::ColorMode::RGB});
46  }
47  return traits;
48  }
49 
50  void set_pin(uint8_t pin) { this->pin_ = pin; }
51  void set_num_leds(uint16_t num_leds) { this->num_leds_ = num_leds; }
52  void set_is_rgbw(bool is_rgbw) { this->is_rgbw_ = is_rgbw; }
53  void set_is_wrgb(bool is_wrgb) { this->is_wrgb_ = is_wrgb; }
54  void set_use_psram(bool use_psram) { this->use_psram_ = use_psram; }
55 
57  void set_max_refresh_rate(uint32_t interval_us) { this->max_refresh_rate_ = interval_us; }
58 
59  void set_led_params(uint32_t bit0_high, uint32_t bit0_low, uint32_t bit1_high, uint32_t bit1_low,
60  uint32_t reset_time_high, uint32_t reset_time_low);
61 
62  void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; }
63 #if ESP_IDF_VERSION_MAJOR >= 5
64  void set_rmt_symbols(uint32_t rmt_symbols) { this->rmt_symbols_ = rmt_symbols; }
65 #else
66  void set_rmt_channel(rmt_channel_t channel) { this->channel_ = channel; }
67 #endif
68 
69  void clear_effect_data() override {
70  for (int i = 0; i < this->size(); i++)
71  this->effect_data_[i] = 0;
72  }
73 
74  void dump_config() override;
75 
76  protected:
77  light::ESPColorView get_view_internal(int32_t index) const override;
78 
79  size_t get_buffer_size_() const { return this->num_leds_ * (this->is_rgbw_ || this->is_wrgb_ ? 4 : 3); }
80 
81  uint8_t *buf_{nullptr};
82  uint8_t *effect_data_{nullptr};
83 #if ESP_IDF_VERSION_MAJOR >= 5
84  rmt_channel_handle_t channel_{nullptr};
85  rmt_encoder_handle_t encoder_{nullptr};
86  rmt_symbol_word_t *rmt_buf_{nullptr};
87  rmt_symbol_word_t bit0_, bit1_, reset_;
88  uint32_t rmt_symbols_;
89 #else
90  rmt_item32_t *rmt_buf_{nullptr};
91  rmt_item32_t bit0_, bit1_, reset_;
92  rmt_channel_t channel_{RMT_CHANNEL_0};
93 #endif
94 
95  uint8_t pin_;
96  uint16_t num_leds_;
97  bool is_rgbw_;
98  bool is_wrgb_;
99  bool use_psram_;
100 
102 
103  uint32_t last_refresh_{0};
105 };
106 
107 } // namespace esp32_rmt_led_strip
108 } // namespace esphome
109 
110 #endif // USE_ESP32
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
light::ESPColorView get_view_internal(int32_t index) const override
Definition: led_strip.cpp:204
void set_max_refresh_rate(uint32_t interval_us)
Set a maximum refresh rate in µs as some lights do not like being updated too often.
Definition: led_strip.h:57
RGB color output and a separate white output.
void set_led_params(uint32_t bit0_high, uint32_t bit0_low, uint32_t bit1_high, uint32_t bit1_low, uint32_t reset_time_high, uint32_t reset_time_low)
Definition: led_strip.cpp:114
void write_state(light::LightState *state) override
Definition: led_strip.cpp:135
White output only (use only if the light also has another color mode such as RGB).
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
bool state
Definition: fan.h:34