ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
led_strip.cpp
Go to the documentation of this file.
1#include "led_strip.h"
2#include <cinttypes>
3
4#ifdef USE_ESP32
5
7#include "esphome/core/log.h"
8
9#include <esp_attr.h>
10#include <esp_clk_tree.h>
11
12namespace esphome {
13namespace esp32_rmt_led_strip {
14
15static const char *const TAG = "esp32_rmt_led_strip";
16
17static const size_t RMT_SYMBOLS_PER_BYTE = 8;
18
19// Query the RMT default clock source frequency. This varies by variant:
20// APB (80MHz) on ESP32/S2/S3/C3, PLL_F80M (80MHz) on C6/P4, XTAL (32MHz) on H2.
21// Worst-case reset time is WS2811 at 300µs = 24000 ticks at 80MHz, well within
22// the 15-bit rmt_symbol_word_t duration field max of 32767.
23static uint32_t rmt_resolution_hz() {
24 uint32_t freq;
25 esp_clk_tree_src_get_freq_hz((soc_module_clk_t) RMT_CLK_SRC_DEFAULT, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq);
26 return freq;
27}
28
29#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
30static size_t IRAM_ATTR HOT encoder_callback(const void *data, size_t size, size_t symbols_written, size_t symbols_free,
31 rmt_symbol_word_t *symbols, bool *done, void *arg) {
32 auto *params = static_cast<LedParams *>(arg);
33 const auto *bytes = static_cast<const uint8_t *>(data);
34 size_t index = symbols_written / RMT_SYMBOLS_PER_BYTE;
35
36 // convert byte to symbols
37 if (index < size) {
38 if (symbols_free < RMT_SYMBOLS_PER_BYTE) {
39 return 0;
40 }
41 for (size_t i = 0; i < RMT_SYMBOLS_PER_BYTE; i++) {
42 if (bytes[index] & (1 << (7 - i))) {
43 symbols[i] = params->bit1;
44 } else {
45 symbols[i] = params->bit0;
46 }
47 }
48#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 1)
49 if ((index + 1) >= size && params->reset.duration0 == 0 && params->reset.duration1 == 0) {
50 *done = true;
51 }
52#endif
53 return RMT_SYMBOLS_PER_BYTE;
54 }
55
56 // send reset
57 if (symbols_free < 1) {
58 return 0;
59 }
60 symbols[0] = params->reset;
61 *done = true;
62 return 1;
63}
64#endif
65
67 size_t buffer_size = this->get_buffer_size_();
68
70 this->buf_ = allocator.allocate(buffer_size);
71 if (this->buf_ == nullptr) {
72 ESP_LOGE(TAG, "Cannot allocate LED buffer!");
73 this->mark_failed();
74 return;
75 }
76 memset(this->buf_, 0, buffer_size);
77
78 this->effect_data_ = allocator.allocate(this->num_leds_);
79 if (this->effect_data_ == nullptr) {
80 ESP_LOGE(TAG, "Cannot allocate effect data!");
81 this->mark_failed();
82 return;
83 }
84
85#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
86 // copy of the led buffer
87 this->rmt_buf_ = allocator.allocate(buffer_size);
88#else
90
91 // 8 bits per byte, 1 rmt_symbol_word_t per bit + 1 rmt_symbol_word_t for reset
92 this->rmt_buf_ = rmt_allocator.allocate(buffer_size * 8 + 1);
93#endif
94
95 rmt_tx_channel_config_t channel;
96 memset(&channel, 0, sizeof(channel));
97 channel.clk_src = RMT_CLK_SRC_DEFAULT;
98 channel.resolution_hz = rmt_resolution_hz();
99 channel.gpio_num = gpio_num_t(this->pin_);
100 channel.mem_block_symbols = this->rmt_symbols_;
101 channel.trans_queue_depth = 1;
102 channel.flags.invert_out = this->invert_out_;
103 channel.flags.with_dma = this->use_dma_;
104 channel.intr_priority = 0;
105 if (rmt_new_tx_channel(&channel, &this->channel_) != ESP_OK) {
106 ESP_LOGE(TAG, "Channel creation failed");
107 this->mark_failed();
108 return;
109 }
110
111#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
112 rmt_simple_encoder_config_t encoder;
113 memset(&encoder, 0, sizeof(encoder));
114 encoder.callback = encoder_callback;
115 encoder.arg = &this->params_;
116 encoder.min_chunk_size = RMT_SYMBOLS_PER_BYTE;
117 if (rmt_new_simple_encoder(&encoder, &this->encoder_) != ESP_OK) {
118 ESP_LOGE(TAG, "Encoder creation failed");
119 this->mark_failed();
120 return;
121 }
122#else
123 rmt_copy_encoder_config_t encoder;
124 memset(&encoder, 0, sizeof(encoder));
125 if (rmt_new_copy_encoder(&encoder, &this->encoder_) != ESP_OK) {
126 ESP_LOGE(TAG, "Encoder creation failed");
127 this->mark_failed();
128 return;
129 }
130#endif
131
132 if (rmt_enable(this->channel_) != ESP_OK) {
133 ESP_LOGE(TAG, "Enabling channel failed");
134 this->mark_failed();
135 return;
136 }
137}
138
140 uint32_t bit1_low, uint32_t reset_time_high, uint32_t reset_time_low) {
141 float ratio = (float) rmt_resolution_hz() / 1e09f;
142
143 // 0-bit
144 this->params_.bit0.duration0 = (uint32_t) (ratio * bit0_high);
145 this->params_.bit0.level0 = 1;
146 this->params_.bit0.duration1 = (uint32_t) (ratio * bit0_low);
147 this->params_.bit0.level1 = 0;
148 // 1-bit
149 this->params_.bit1.duration0 = (uint32_t) (ratio * bit1_high);
150 this->params_.bit1.level0 = 1;
151 this->params_.bit1.duration1 = (uint32_t) (ratio * bit1_low);
152 this->params_.bit1.level1 = 0;
153 // reset
154 this->params_.reset.duration0 = (uint32_t) (ratio * reset_time_high);
155 this->params_.reset.level0 = 1;
156 this->params_.reset.duration1 = (uint32_t) (ratio * reset_time_low);
157 this->params_.reset.level1 = 0;
158}
159
161 // protect from refreshing too often
162 uint32_t now = micros();
163 auto rate = this->max_refresh_rate_.value_or(0);
164 if (rate != 0 && (now - this->last_refresh_) < rate) {
165 // try again next loop iteration, so that this change won't get lost
166 this->schedule_show();
167 return;
168 }
169 this->last_refresh_ = now;
170 this->mark_shown_();
171
172 ESP_LOGVV(TAG, "Writing RGB values to bus");
173
174 esp_err_t error = rmt_tx_wait_all_done(this->channel_, 1000);
175 if (error != ESP_OK) {
176 ESP_LOGE(TAG, "RMT TX timeout");
177 this->status_set_warning();
178 return;
179 }
181
182#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
183 memcpy(this->rmt_buf_, this->buf_, this->get_buffer_size_());
184#else
185 size_t buffer_size = this->get_buffer_size_();
186
187 size_t size = 0;
188 size_t len = 0;
189 uint8_t *psrc = this->buf_;
190 rmt_symbol_word_t *pdest = this->rmt_buf_;
191 while (size < buffer_size) {
192 uint8_t b = *psrc;
193 for (int i = 0; i < 8; i++) {
194 pdest->val = b & (1 << (7 - i)) ? this->params_.bit1.val : this->params_.bit0.val;
195 pdest++;
196 len++;
197 }
198 size++;
199 psrc++;
200 }
201
202 if (this->params_.reset.duration0 > 0 || this->params_.reset.duration1 > 0) {
203 pdest->val = this->params_.reset.val;
204 pdest++;
205 len++;
206 }
207#endif
208
209 rmt_transmit_config_t config;
210 memset(&config, 0, sizeof(config));
211#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
212 error = rmt_transmit(this->channel_, this->encoder_, this->rmt_buf_, this->get_buffer_size_(), &config);
213#else
214 error = rmt_transmit(this->channel_, this->encoder_, this->rmt_buf_, len * sizeof(rmt_symbol_word_t), &config);
215#endif
216 if (error != ESP_OK) {
217 ESP_LOGE(TAG, "RMT TX error");
218 this->status_set_warning();
219 return;
220 }
221 this->status_clear_warning();
222}
223
225 int32_t r = 0, g = 0, b = 0;
226 switch (this->rgb_order_) {
227 case ORDER_RGB:
228 r = 0;
229 g = 1;
230 b = 2;
231 break;
232 case ORDER_RBG:
233 r = 0;
234 g = 2;
235 b = 1;
236 break;
237 case ORDER_GRB:
238 r = 1;
239 g = 0;
240 b = 2;
241 break;
242 case ORDER_GBR:
243 r = 2;
244 g = 0;
245 b = 1;
246 break;
247 case ORDER_BGR:
248 r = 2;
249 g = 1;
250 b = 0;
251 break;
252 case ORDER_BRG:
253 r = 1;
254 g = 2;
255 b = 0;
256 break;
257 }
258 uint8_t multiplier = this->is_rgbw_ || this->is_wrgb_ ? 4 : 3;
259 uint8_t white = this->is_wrgb_ ? 0 : 3;
260
261 return {this->buf_ + (index * multiplier) + r + this->is_wrgb_,
262 this->buf_ + (index * multiplier) + g + this->is_wrgb_,
263 this->buf_ + (index * multiplier) + b + this->is_wrgb_,
264 this->is_rgbw_ || this->is_wrgb_ ? this->buf_ + (index * multiplier) + white : nullptr,
265 &this->effect_data_[index],
266 &this->correction_};
267}
268
270 ESP_LOGCONFIG(TAG,
271 "ESP32 RMT LED Strip:\n"
272 " Pin: %u",
273 this->pin_);
274 ESP_LOGCONFIG(TAG, " RMT Symbols: %" PRIu32, this->rmt_symbols_);
275 const char *rgb_order;
276 switch (this->rgb_order_) {
277 case ORDER_RGB:
278 rgb_order = "RGB";
279 break;
280 case ORDER_RBG:
281 rgb_order = "RBG";
282 break;
283 case ORDER_GRB:
284 rgb_order = "GRB";
285 break;
286 case ORDER_GBR:
287 rgb_order = "GBR";
288 break;
289 case ORDER_BGR:
290 rgb_order = "BGR";
291 break;
292 case ORDER_BRG:
293 rgb_order = "BRG";
294 break;
295 default:
296 rgb_order = "UNKNOWN";
297 break;
298 }
299 ESP_LOGCONFIG(TAG,
300 " RGB Order: %s\n"
301 " Max refresh rate: %" PRIu32 "\n"
302 " Number of LEDs: %u",
303 rgb_order, this->max_refresh_rate_.value_or(0), this->num_leds_);
304}
305
307
308} // namespace esp32_rmt_led_strip
309} // namespace esphome
310
311#endif // USE_ESP32
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:306
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2212
T * allocate(size_t n)
Definition helpers.h:2229
void write_state(light::LightState *state) override
light::ESPColorView get_view_internal(int32_t index) const override
void set_led_params(uint32_t bit0_high, uint32_t bit0_low, uint32_t bit1_high, uint32_t bit1_low, uint32_t reset_time_high, uint32_t reset_time_low)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
bool state
Definition fan.h:2
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:1045
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
uint16_t size
Definition helpers.cpp:25
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
static void uint32_t