8 #include <hardware/clocks.h> 9 #include <hardware/dma.h> 10 #include <hardware/pio.h> 11 #include <pico/stdlib.h> 14 namespace rp2040_pio_led_strip {
16 static const char *TAG =
"rp2040_pio_led_strip";
18 static uint8_t num_instance_[2] = {0, 0};
19 static std::map<Chipset, uint> chipset_offsets_ = {
20 {
CHIPSET_WS2812, 0}, {
CHIPSET_WS2812B, 0}, {
CHIPSET_SK6812, 0}, {
CHIPSET_SM16703, 0}, {
CHIPSET_CUSTOM, 0},
22 static std::map<Chipset, bool> conf_count_ = {
28 ESP_LOGCONFIG(TAG,
"Setting up RP2040 LED Strip...");
34 if (this->
buf_ ==
nullptr) {
35 ESP_LOGE(TAG,
"Failed to allocate buffer of size %u", buffer_size);
42 ESP_LOGE(TAG,
"Failed to allocate effect data of size %u", this->
num_leds_);
50 if (this->
pio_ ==
nullptr) {
51 ESP_LOGE(TAG,
"Failed to claim PIO instance");
60 if (num_instance_[this->
pio_ == pio0 ? 0 : 1] > 4) {
61 ESP_LOGE(TAG,
"Too many instances of PIO program");
66 num_instance_[this->
pio_ == pio0 ? 0 : 1]++;
69 if (this->conf_count_[this->
chipset_]) {
70 offset = chipset_offsets_[this->
chipset_];
73 offset = pio_add_program(this->pio_, this->
program_);
74 chipset_offsets_[this->
chipset_] = offset;
79 this->
sm_ = pio_claim_unused_sm(this->pio_,
true);
82 ESP_LOGE(TAG,
"Failed to claim PIO state machine");
89 this->
dma_chan_ = dma_claim_unused_channel(
true);
91 ESP_LOGE(TAG,
"Failed to claim DMA channel");
97 channel_config_set_transfer_data_size(
100 channel_config_set_read_increment(&this->
dma_config_,
true);
101 channel_config_set_write_increment(&this->
dma_config_,
false);
103 pio_get_dreq(this->pio_, this->
sm_,
true));
106 &this->pio_->txf[this->sm_],
116 ESP_LOGVV(TAG,
"Writing state...");
119 ESP_LOGW(TAG,
"Light is in failed state, not writing state.");
123 if (this->
buf_ ==
nullptr) {
124 ESP_LOGW(TAG,
"Buffer is null, not writing state.");
133 int32_t r = 0, g = 0, b = 0, w = 0;
166 uint8_t multiplier = this->
is_rgbw_ ? 4 : 3;
167 return {this->
buf_ + (index * multiplier) + r,
168 this->
buf_ + (index * multiplier) + g,
169 this->
buf_ + (index * multiplier) + b,
170 this->
is_rgbw_ ? this->
buf_ + (index * multiplier) + 3 :
nullptr,
176 ESP_LOGCONFIG(TAG,
"RP2040 PIO LED Strip Light Output:");
177 ESP_LOGCONFIG(TAG,
" Pin: GPIO%d", this->
pin_);
178 ESP_LOGCONFIG(TAG,
" Number of LEDs: %d", this->
num_leds_);
179 ESP_LOGCONFIG(TAG,
" RGBW: %s", YESNO(this->
is_rgbw_));
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
void write_state(light::LightState *state) override
An STL allocator that uses SPI RAM.
void dump_config() override
const char * rgb_order_to_string(RGBOrder order)
dma_channel_config dma_config_
const pio_program_t * program_
light::ESPColorView get_view_internal(int32_t index) const override
float get_setup_priority() const override
size_t get_buffer_size_() const
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
ESPColorCorrection correction_
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.