#ifndef PLATFORM_NATIVE #include "drivers/Esp32DriverRGBW.h" #include #include // Timing constants, in ns static constexpr uint32_t RESOLUTION_HZ = 20 * 1000 * 1000; // one RMT tick = 50 ns static constexpr uint32_t TICK_NS = 1000 * 1000 * 1000 / RESOLUTION_HZ; static constexpr uint32_t T0H = 300; static constexpr uint32_t T0L = 900; static constexpr uint32_t T1H = 600; static constexpr uint32_t T1L = 600; static rmt_symbol_word_t symbol(uint32_t highNs, uint32_t lowNs) { rmt_symbol_word_t s = {}; s.duration0 = highNs / TICK_NS; s.level0 = 1; s.duration1 = lowNs / TICK_NS; s.level1 = 0; return s; } void Esp32DriverRGBW::begin(int gpio) { transmitting_ = false; rmt_tx_channel_config_t channelConfig = {}; channelConfig.gpio_num = static_cast(gpio); channelConfig.clk_src = RMT_CLK_SRC_DEFAULT; channelConfig.resolution_hz = RESOLUTION_HZ; channelConfig.mem_block_symbols = 64; // one block, refilled from the interrupt channelConfig.trans_queue_depth = 4; ESP_ERROR_CHECK(rmt_new_tx_channel(&channelConfig, &channel_)); // Every byte goes out MSB first, one RMT symbol per bit - the strip's own byte order // (g, r, b, w) is already how LedStripRGBW lays the data out in memory. rmt_bytes_encoder_config_t encoderConfig = {}; encoderConfig.bit0 = symbol(T0H, T0L); encoderConfig.bit1 = symbol(T1H, T1L); encoderConfig.flags.msb_first = 1; ESP_ERROR_CHECK(rmt_new_bytes_encoder(&encoderConfig, &encoder_)); ESP_ERROR_CHECK(rmt_enable(channel_)); } void Esp32DriverRGBW::end() { waitForTransmissionToFinish(); ESP_ERROR_CHECK(rmt_disable(channel_)); ESP_ERROR_CHECK(rmt_del_encoder(encoder_)); ESP_ERROR_CHECK(rmt_del_channel(channel_)); encoder_ = nullptr; channel_ = nullptr; } void Esp32DriverRGBW::writeSync(const uint32_t *rgbwData, int numLeds) { writeAsync(rgbwData, numLeds); waitForTransmissionToFinish(); } void Esp32DriverRGBW::writeAsync(const uint32_t *rgbwData, int numLeds) { waitForTransmissionToFinish(); rmt_transmit_config_t transmitConfig = {}; transmitConfig.loop_count = 0; ESP_ERROR_CHECK(rmt_transmit(channel_, encoder_, rgbwData, numLeds * 4, &transmitConfig)); transmitting_ = true; } bool Esp32DriverRGBW::waitForTransmissionToFinish(int waitMs) { if (!transmitting_) return true; if (rmt_tx_wait_all_done(channel_, waitMs) == ESP_OK) { transmitting_ = false; return true; } else return false; } #endif