3#if defined(USE_ESP32) && SOC_I2S_SUPPORTS_TDM
10#include <driver/i2s_tdm.h>
49 : internal_accessor_{nullptr, nullptr, channel_colors, &this->correction_},
50 color_data_bytes_(num_leds * channel_colors.bytes_per_led()),
59 "I2S Clockless LED Strip:\n"
62 char channel_colors[5];
64 " Channel colors: %s\n"
65 " Number of LEDs: %" PRIu16
"\n"
66 " DMA buffer: %" PRIuPTR
" samples (%" PRIu32
" us)",
74 i2s_chan_config_t chan_config = {
76 .role = I2S_ROLE_MASTER,
79 .auto_clear_after_cb =
false,
80 .auto_clear_before_cb =
false,
84 esp_err_t err = i2s_new_channel(&chan_config, &this->
tx_handle_, NULL);
86 ESP_LOGE(TAG,
"Error in i2s_new_channel: %s", esp_err_to_name(err));
88 i2s_tdm_config_t tdm_config = {
92 .clk_src = I2S_CLK_SRC_DEFAULT,
93 .mclk_multiple = I2S_MCLK_MULTIPLE_384,
97 .data_bit_width = I2S_DATA_BIT_WIDTH_24BIT,
98 .slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
99 .slot_mode = I2S_SLOT_MODE_MONO,
100 .slot_mask = I2S_TDM_SLOT0,
103 .total_slot = I2S_TDM_AUTO_SLOT_NUM,
107 .mclk = I2S_GPIO_UNUSED,
108 .bclk = I2S_GPIO_UNUSED,
109 .ws = I2S_GPIO_UNUSED,
110 .dout = gpio_num_t(this->
pin_),
111 .din = I2S_GPIO_UNUSED,
120 err = i2s_channel_init_tdm_mode(this->
tx_handle_, &tdm_config);
122 ESP_LOGE(TAG,
"Error in i2s_channel_init_tdm_mode: %s", esp_err_to_name(err));
124 i2s_event_callbacks_t event_callbacks = {
127 err = i2s_channel_register_event_callback(this->
tx_handle_, &event_callbacks,
this);
129 ESP_LOGE(TAG,
"Error in i2s_channel_register_event_callback: %s", esp_err_to_name(err));
136 ESP_LOGE(TAG,
"Error in i2s_channel_enable: %s", esp_err_to_name(err));
158 for (
size_t i = 0; i < color_data_bytes; i++) {
159 const uint8_t color_byte = *(color_data++);
160 *(i2s_data++) = 0b10010010 | ((color_byte & 0x80) >> 1) | ((color_byte & 0x40) >> 3) | ((color_byte & 0x20) >> 5);
161 *(i2s_data++) = 0b01001001 | ((color_byte & 0x10) << 1) | ((color_byte & 0x08) >> 1);
162 *(i2s_data++) = 0b00100100 | ((color_byte & 0x04) << 5) | ((color_byte & 0x02) << 3) | ((color_byte & 0x01) << 1);
174 auto *
const dma_buf =
static_cast<uint8_t *
>(
event->dma_buf);
175 const size_t dma_buf_size =
event->
size;
177 bool dma_buf_filled =
false;
178 if (self->i2s_data_ready_.load(std::memory_order_acquire)) {
179 const size_t i2s_data_bytes = self->color_data_bytes_ * I2S_BYTES_PER_SAMPLE;
180 if (self->i2s_data_sent_ < i2s_data_bytes) {
181 const size_t i2s_data_remaining = i2s_data_bytes - self->i2s_data_sent_;
182 if (i2s_data_remaining >= dma_buf_size) {
183 memcpy(dma_buf, self->i2s_data_ + self->i2s_data_sent_, dma_buf_size);
185 memcpy(dma_buf, self->i2s_data_ + self->i2s_data_sent_, i2s_data_remaining);
186 memset(dma_buf + i2s_data_remaining, 0, dma_buf_size - i2s_data_remaining);
188 dma_buf_filled =
true;
190 self->i2s_data_sent_ += dma_buf_size;
192 self->i2s_data_ready_.store(
false, std::memory_order_release);
198 if (!dma_buf_filled && dma_buf[0] != 0) {
199 memset(dma_buf, 0, dma_buf_size);
void mark_failed()
Mark this component as failed.
I2SClocklessLedStrip(uint8_t *buffer, uint8_t pin, uint16_t num_leds, light::ChannelColors channel_colors)
buffer is a word aligned block of buffer_bytes().
std::atomic< bool > i2s_data_ready_
light::CorrectedBufferColorAccessor internal_accessor_
int32_t size() const override
void dump_config() override
i2s_chan_handle_t tx_handle_
void write_state(light::LightState *state) override
const size_t color_data_bytes_
float get_setup_priority() const override
static bool i2s_on_sent_callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
ESPColorAccessor * accessor_
ChannelColors channel_colors
uint8_t * set_buffer(uint8_t *buffer, size_t num_leds)
Use buffer for the color data of num_leds LEDs followed by their effect data, buffer_bytes() in all.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
ESPHOME_LOG_TAG(TAG, "i2s_clockless_led_strip")
constexpr size_t I2S_RESET_SAMPLES
constexpr uint32_t I2S_BUFFER_DURATION_US_ACTUAL
constexpr uint32_t num_i2s_samples_to_duration_us(size_t samples)
constexpr uint32_t I2S_SAMPLE_RATE_HZ
constexpr uint32_t I2S_BUFFER_DURATION_US_IDEAL
constexpr size_t I2S_RESET_BYTES
constexpr const char *const ERROR_I2S
constexpr size_t num_i2s_samples_from_duration_us(uint32_t duration_us)
constexpr uint32_t I2S_RESET_DURATION_US
constexpr size_t I2S_BUFFER_SAMPLES
constexpr float IO
For components that represent GPIO pins like PCF8573.
Which byte of an addressable LED's data carries each colour.
size_t bytes_per_led() const
const char * to_string(char *buf) const
Write the order back out as text, e.g.
spi_device_handle_t handle