ESPHome  2024.4.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/pio.h>
13 #include <hardware/structs/pio.h>
14 #include <pico/stdio.h>
15 
16 namespace esphome {
17 namespace rp2040_pio_led_strip {
18 
19 enum RGBOrder : uint8_t {
26 };
27 
28 inline const char *rgb_order_to_string(RGBOrder order) {
29  switch (order) {
30  case ORDER_RGB:
31  return "RGB";
32  case ORDER_RBG:
33  return "RBG";
34  case ORDER_GRB:
35  return "GRB";
36  case ORDER_GBR:
37  return "GBR";
38  case ORDER_BGR:
39  return "BGR";
40  case ORDER_BRG:
41  return "BRG";
42  default:
43  return "UNKNOWN";
44  }
45 }
46 
47 using init_fn = void (*)(PIO pio, uint sm, uint offset, uint pin, float freq);
48 
50  public:
51  void setup() override;
52  void write_state(light::LightState *state) override;
53  float get_setup_priority() const override;
54 
55  int32_t size() const override { return this->num_leds_; }
57  auto traits = light::LightTraits();
58  this->is_rgbw_ ? traits.set_supported_color_modes({light::ColorMode::RGB_WHITE, light::ColorMode::WHITE})
59  : traits.set_supported_color_modes({light::ColorMode::RGB});
60  return traits;
61  }
62  void set_pin(uint8_t pin) { this->pin_ = pin; }
63  void set_num_leds(uint32_t num_leds) { this->num_leds_ = num_leds; }
64  void set_is_rgbw(bool is_rgbw) { this->is_rgbw_ = is_rgbw; }
65 
66  void set_max_refresh_rate(float interval_us) { this->max_refresh_rate_ = interval_us; }
67 
68  void set_pio(int pio_num) { pio_num ? this->pio_ = pio1 : this->pio_ = pio0; }
69  void set_program(const pio_program_t *program) { this->program_ = program; }
71 
72  void set_rgb_order(RGBOrder rgb_order) { this->rgb_order_ = rgb_order; }
73  void clear_effect_data() override {
74  for (int i = 0; i < this->size(); i++) {
75  this->effect_data_[i] = 0;
76  }
77  }
78 
79  void dump_config() override;
80 
81  protected:
82  light::ESPColorView get_view_internal(int32_t index) const override;
83 
84  size_t get_buffer_size_() const { return this->num_leds_ * (3 + this->is_rgbw_); }
85 
86  uint8_t *buf_{nullptr};
87  uint8_t *effect_data_{nullptr};
88 
89  uint8_t pin_;
90  uint32_t num_leds_;
91  bool is_rgbw_;
92 
93  pio_hw_t *pio_;
94  uint sm_;
95 
97 
98  uint32_t last_refresh_{0};
100 
101  const pio_program_t *program_;
103 };
104 
105 } // namespace rp2040_pio_led_strip
106 } // namespace esphome
107 
108 #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:58
RGB color output and a separate white output.
const char * rgb_order_to_string(RGBOrder order)
Definition: led_strip.h:28
light::ESPColorView get_view_internal(int32_t index) const override
Definition: led_strip.cpp:83
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:69
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:47
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void init()
Definition: core.cpp:80
bool state
Definition: fan.h:34