ESPHome  2024.7.0
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 <map>
17 
18 namespace esphome {
19 namespace rp2040_pio_led_strip {
20 
21 enum RGBOrder : uint8_t {
28 };
29 
30 enum Chipset : uint8_t {
37 };
38 
39 inline const char *rgb_order_to_string(RGBOrder order) {
40  switch (order) {
41  case ORDER_RGB:
42  return "RGB";
43  case ORDER_RBG:
44  return "RBG";
45  case ORDER_GRB:
46  return "GRB";
47  case ORDER_GBR:
48  return "GBR";
49  case ORDER_BGR:
50  return "BGR";
51  case ORDER_BRG:
52  return "BRG";
53  default:
54  return "UNKNOWN";
55  }
56 }
57 
58 using init_fn = void (*)(PIO pio, uint sm, uint offset, uint pin, float freq);
59 
61  public:
62  void setup() override;
63  void write_state(light::LightState *state) override;
64  float get_setup_priority() const override;
65 
66  int32_t size() const override { return this->num_leds_; }
68  auto traits = light::LightTraits();
69  this->is_rgbw_ ? traits.set_supported_color_modes({light::ColorMode::RGB_WHITE, light::ColorMode::WHITE})
70  : traits.set_supported_color_modes({light::ColorMode::RGB});
71  return traits;
72  }
73  void set_pin(uint8_t pin) { this->pin_ = pin; }
74  void set_num_leds(uint32_t num_leds) { this->num_leds_ = num_leds; }
75  void set_is_rgbw(bool is_rgbw) { this->is_rgbw_ = is_rgbw; }
76 
77  void set_max_refresh_rate(float interval_us) { this->max_refresh_rate_ = interval_us; }
78 
79  void set_pio(int pio_num) { pio_num ? this->pio_ = pio1 : this->pio_ = pio0; }
80  void set_program(const pio_program_t *program) { this->program_ = program; }
82 
83  void set_chipset(Chipset chipset) { this->chipset_ = chipset; };
84  void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; }
85  void clear_effect_data() override {
86  for (int i = 0; i < this->size(); i++) {
87  this->effect_data_[i] = 0;
88  }
89  }
90 
91  void dump_config() override;
92 
93  protected:
94  light::ESPColorView get_view_internal(int32_t index) const override;
95 
96  size_t get_buffer_size_() const { return this->num_leds_ * (3 + this->is_rgbw_); }
97 
98  uint8_t *buf_{nullptr};
99  uint8_t *effect_data_{nullptr};
100 
101  uint8_t pin_;
102  uint32_t num_leds_;
103  bool is_rgbw_;
104 
105  pio_hw_t *pio_;
106  uint sm_;
107  uint dma_chan_;
108  dma_channel_config dma_config_;
109 
112 
113  uint32_t last_refresh_{0};
115 
116  const pio_program_t *program_;
118 
119  private:
120  inline static int num_instance_[2];
121  inline static std::map<Chipset, bool> conf_count_;
122  inline static std::map<Chipset, int> chipset_offsets_;
123 };
124 
125 } // namespace rp2040_pio_led_strip
126 } // namespace esphome
127 
128 #endif // USE_RP2040
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
void write_state(light::LightState *state) override
Definition: led_strip.cpp:115
RGB color output and a separate white output.
const char * rgb_order_to_string(RGBOrder order)
Definition: led_strip.h:39
light::ESPColorView get_view_internal(int32_t index) const override
Definition: led_strip.cpp:132
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:80
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:58
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void init()
Definition: core.cpp:80
bool state
Definition: fan.h:34