ESPHome  2024.12.2
led_strip.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_RP2040
4 
5 #include "esphome/core/color.h"
7 #include "esphome/core/helpers.h"
8 
11 
12 #include <hardware/dma.h>
13 #include <hardware/pio.h>
14 #include <hardware/structs/pio.h>
15 #include <pico/stdio.h>
16 #include <pico/sem.h>
17 #include <map>
18 
19 namespace esphome {
20 namespace rp2040_pio_led_strip {
21 
22 enum RGBOrder : uint8_t {
29 };
30 
31 enum Chipset : uint8_t {
38 };
39 
40 inline const char *rgb_order_to_string(RGBOrder order) {
41  switch (order) {
42  case ORDER_RGB:
43  return "RGB";
44  case ORDER_RBG:
45  return "RBG";
46  case ORDER_GRB:
47  return "GRB";
48  case ORDER_GBR:
49  return "GBR";
50  case ORDER_BGR:
51  return "BGR";
52  case ORDER_BRG:
53  return "BRG";
54  default:
55  return "UNKNOWN";
56  }
57 }
58 
59 using init_fn = void (*)(PIO pio, uint sm, uint offset, uint pin, float freq);
60 
62  public:
63  void setup() override;
64  void write_state(light::LightState *state) override;
65  float get_setup_priority() const override;
66 
67  int32_t size() const override { return this->num_leds_; }
69  auto traits = light::LightTraits();
70  this->is_rgbw_ ? traits.set_supported_color_modes({light::ColorMode::RGB_WHITE, light::ColorMode::WHITE})
71  : traits.set_supported_color_modes({light::ColorMode::RGB});
72  return traits;
73  }
74  void set_pin(uint8_t pin) { this->pin_ = pin; }
75  void set_num_leds(uint32_t num_leds) { this->num_leds_ = num_leds; }
76  void set_is_rgbw(bool is_rgbw) { this->is_rgbw_ = is_rgbw; }
77 
78  void set_max_refresh_rate(float interval_us) { this->max_refresh_rate_ = interval_us; }
79 
80  void set_pio(int pio_num) { pio_num ? this->pio_ = pio1 : this->pio_ = pio0; }
81  void set_program(const pio_program_t *program) { this->program_ = program; }
83 
84  void set_chipset(Chipset chipset) { this->chipset_ = chipset; };
85  void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; }
86  void clear_effect_data() override {
87  for (int i = 0; i < this->size(); i++) {
88  this->effect_data_[i] = 0;
89  }
90  }
91 
92  void dump_config() override;
93 
94  protected:
95  light::ESPColorView get_view_internal(int32_t index) const override;
96 
97  size_t get_buffer_size_() const { return this->num_leds_ * (3 + this->is_rgbw_); }
98 
99  static void dma_write_complete_handler_();
100 
101  uint8_t *buf_{nullptr};
102  uint8_t *effect_data_{nullptr};
103 
104  uint8_t pin_;
105  uint32_t num_leds_;
106  bool is_rgbw_;
107 
108  pio_hw_t *pio_;
109  uint sm_;
110  uint dma_chan_;
111  dma_channel_config dma_config_;
112 
115 
116  uint32_t last_refresh_{0};
118 
119  const pio_program_t *program_;
121 
122  private:
123  inline static int num_instance_[2];
124  inline static std::map<Chipset, bool> conf_count_;
125  inline static std::map<Chipset, int> chipset_offsets_;
126  inline static bool dma_chan_active_[12];
127  inline static struct semaphore dma_write_complete_sem_[12];
128 };
129 
130 } // namespace rp2040_pio_led_strip
131 } // namespace esphome
132 
133 #endif // USE_RP2040
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
void write_state(light::LightState *state) override
Definition: led_strip.cpp:140
RGB color output and a separate white output.
const char * rgb_order_to_string(RGBOrder order)
Definition: led_strip.h:40
light::ESPColorView get_view_internal(int32_t index) const override
Definition: led_strip.cpp:158
White output only (use only if the light also has another color mode such as RGB).
void set_program(const pio_program_t *program)
Definition: led_strip.h:81
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
void(*)(PIO pio, uint sm, uint offset, uint pin, float freq) init_fn
Definition: led_strip.h:59
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void init()
Definition: core.cpp:80
bool state
Definition: fan.h:34