ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
led_strip.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_RP2040
4
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
20
29
38
39inline 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
58using 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; }
81 void set_init_function(init_fn init) { this->init_ = init; }
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 static void dma_write_complete_handler_();
99
100 uint8_t *buf_{nullptr};
101 uint8_t *effect_data_{nullptr};
102
103 uint8_t pin_;
106
107 pio_hw_t *pio_;
108 uint sm_;
110 dma_channel_config dma_config_;
111
114
117
118 const pio_program_t *program_;
120
121 private:
122 inline static int num_instance_[2];
123 inline static std::map<Chipset, bool> conf_count_;
124 inline static std::map<Chipset, int> chipset_offsets_;
125 inline static bool dma_chan_active_[12];
126 inline static struct semaphore dma_write_complete_sem_[12];
127};
128
129} // namespace esphome::rp2040_pio_led_strip
130
131#endif // USE_RP2040
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
light::ESPColorView get_view_internal(int32_t index) const override
void write_state(light::LightState *state) override
bool state
Definition fan.h:2
@ RGB_WHITE
RGB color output and a separate white output.
@ RGB
RGB color output.
@ WHITE
White output only (use only if the light also has another color mode such as RGB).
void(*)(PIO pio, uint sm, uint offset, uint pin, float freq) init_fn
Definition led_strip.h:58
const char * rgb_order_to_string(RGBOrder order)
Definition led_strip.h:39
static void uint32_t