ESPHome  2024.11.1
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 <driver/rmt.h>
13 #include <esp_err.h>
14 
15 namespace esphome {
16 namespace esp32_rmt_led_strip {
17 
18 enum RGBOrder : uint8_t {
25 };
26 
28  public:
29  void setup() override;
30  void write_state(light::LightState *state) override;
31  float get_setup_priority() const override;
32 
33  int32_t size() const override { return this->num_leds_; }
35  auto traits = light::LightTraits();
36  if (this->is_rgbw_ || this->is_wrgb_) {
37  traits.set_supported_color_modes({light::ColorMode::RGB_WHITE, light::ColorMode::WHITE});
38  } else {
39  traits.set_supported_color_modes({light::ColorMode::RGB});
40  }
41  return traits;
42  }
43 
44  void set_pin(uint8_t pin) { this->pin_ = pin; }
45  void set_num_leds(uint16_t num_leds) { this->num_leds_ = num_leds; }
46  void set_is_rgbw(bool is_rgbw) { this->is_rgbw_ = is_rgbw; }
47  void set_is_wrgb(bool is_wrgb) { this->is_wrgb_ = is_wrgb; }
48  void set_use_psram(bool use_psram) { this->use_psram_ = use_psram; }
49 
51  void set_max_refresh_rate(uint32_t interval_us) { this->max_refresh_rate_ = interval_us; }
52 
53  void set_led_params(uint32_t bit0_high, uint32_t bit0_low, uint32_t bit1_high, uint32_t bit1_low,
54  uint32_t reset_time_high, uint32_t reset_time_low);
55 
56  void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; }
57  void set_rmt_channel(rmt_channel_t channel) { this->channel_ = channel; }
58 
59  void clear_effect_data() override {
60  for (int i = 0; i < this->size(); i++)
61  this->effect_data_[i] = 0;
62  }
63 
64  void dump_config() override;
65 
66  protected:
67  light::ESPColorView get_view_internal(int32_t index) const override;
68 
69  size_t get_buffer_size_() const { return this->num_leds_ * (this->is_rgbw_ || this->is_wrgb_ ? 4 : 3); }
70 
71  uint8_t *buf_{nullptr};
72  uint8_t *effect_data_{nullptr};
73  rmt_item32_t *rmt_buf_{nullptr};
74 
75  uint8_t pin_;
76  uint16_t num_leds_;
77  bool is_rgbw_;
78  bool is_wrgb_;
79  bool use_psram_;
80 
81  rmt_item32_t bit0_, bit1_, reset_;
83  rmt_channel_t channel_;
84 
85  uint32_t last_refresh_{0};
87 };
88 
89 } // namespace esp32_rmt_led_strip
90 } // namespace esphome
91 
92 #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:141
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:51
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:69
void write_state(light::LightState *state) override
Definition: led_strip.cpp:90
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