5#include <driver/gpio.h>
8namespace remote_receiver {
10static const char *
const TAG =
"remote_receiver.esp32";
11#ifdef USE_ESP32_VARIANT_ESP32H2
12static const uint32_t RMT_CLK_FREQ = 32000000;
14static const uint32_t RMT_CLK_FREQ = 80000000;
17#if ESP_IDF_VERSION_MAJOR >= 5
18static bool IRAM_ATTR HOT rmt_callback(rmt_channel_handle_t channel,
const rmt_rx_done_event_data_t *event,
void *arg) {
19 RemoteReceiverComponentStore *store = (RemoteReceiverComponentStore *) arg;
20 rmt_rx_done_event_data_t *event_buffer = (rmt_rx_done_event_data_t *) (store->buffer + store->buffer_write);
21 uint32_t event_size =
sizeof(rmt_rx_done_event_data_t);
22 uint32_t next_write = store->buffer_write + event_size +
event->num_symbols *
sizeof(rmt_symbol_word_t);
23 if (next_write + event_size + store->receive_size > store->buffer_size) {
26 if (store->buffer_read - next_write < event_size + store->receive_size) {
27 next_write = store->buffer_write;
28 store->overflow =
true;
30 if (event->num_symbols <= store->filter_symbols) {
31 next_write = store->buffer_write;
34 rmt_receive(channel, (uint8_t *) store->buffer + next_write + event_size, store->receive_size, &store->config);
35 event_buffer->num_symbols =
event->num_symbols;
36 event_buffer->received_symbols =
event->received_symbols;
37 store->buffer_write = next_write;
43 ESP_LOGCONFIG(TAG,
"Setting up Remote Receiver...");
44#if ESP_IDF_VERSION_MAJOR >= 5
45 rmt_rx_channel_config_t channel;
46 memset(&channel, 0,
sizeof(channel));
47 channel.clk_src = RMT_CLK_SRC_DEFAULT;
50 channel.gpio_num = gpio_num_t(this->
pin_->
get_pin());
51 channel.intr_priority = 0;
52 channel.flags.invert_in = 0;
54 channel.flags.io_loop_back = 0;
55 esp_err_t error = rmt_new_rx_channel(&channel, &this->
channel_);
56 if (error != ESP_OK) {
58 if (error == ESP_ERR_NOT_FOUND) {
72 if (error != ESP_OK) {
79 rmt_rx_event_callbacks_t callbacks;
80 memset(&callbacks, 0,
sizeof(callbacks));
81 callbacks.on_recv_done = rmt_callback;
82 error = rmt_rx_register_event_callbacks(this->
channel_, &callbacks, &this->
store_);
83 if (error != ESP_OK) {
90 uint32_t event_size =
sizeof(rmt_rx_done_event_data_t);
91 uint32_t max_filter_ns = 255u * 1000 / (RMT_CLK_FREQ / 1000000);
92 uint32_t max_idle_ns = 65535u * 1000;
93 memset(&this->
store_.
config, 0,
sizeof(this->store_.config));
100 error = rmt_receive(this->
channel_, (uint8_t *) this->
store_.
buffer + event_size, this->store_.receive_size,
101 &this->store_.config);
102 if (error != ESP_OK) {
113 rmt.rmt_mode = RMT_MODE_RX;
115 rmt.rx_config.filter_en =
false;
117 rmt.rx_config.filter_en =
true;
118 rmt.rx_config.filter_ticks_thresh =
static_cast<uint8_t
>(
121 rmt.rx_config.idle_threshold =
124 esp_err_t error = rmt_config(&rmt);
125 if (error != ESP_OK) {
133 if (error != ESP_OK) {
135 if (error == ESP_ERR_INVALID_STATE) {
144 if (error != ESP_OK) {
150 error = rmt_rx_start(this->
channel_,
true);
151 if (error != ESP_OK) {
161 ESP_LOGCONFIG(TAG,
"Remote Receiver:");
162 LOG_PIN(
" Pin: ", this->
pin_);
163#if ESP_IDF_VERSION_MAJOR >= 5
164 ESP_LOGCONFIG(TAG,
" Clock resolution: %" PRIu32
" hz", this->
clock_resolution_);
165 ESP_LOGCONFIG(TAG,
" RMT symbols: %" PRIu32, this->
rmt_symbols_);
166 ESP_LOGCONFIG(TAG,
" Filter symbols: %" PRIu32, this->
filter_symbols_);
170 ESP_LOGW(TAG,
"Remote Receiver Signal starts with a HIGH value. Usually this means you have to "
171 "invert the signal using 'inverted: True' in the pin schema!");
173 ESP_LOGCONFIG(TAG,
" Channel: %d", this->
channel_);
174 ESP_LOGCONFIG(TAG,
" RMT memory blocks: %d", this->
mem_block_num_);
177 ESP_LOGCONFIG(TAG,
" Tolerance: %" PRIu32
"%s", this->
tolerance_,
179 ESP_LOGCONFIG(TAG,
" Filter out pulses shorter than: %" PRIu32
" us", this->
filter_us_);
180 ESP_LOGCONFIG(TAG,
" Signal is done after %" PRIu32
" us of no changes", this->
idle_us_);
182 ESP_LOGE(TAG,
"Configuring RMT driver failed: %s (%s)", esp_err_to_name(this->
error_code_),
188#if ESP_IDF_VERSION_MAJOR >= 5
190 ESP_LOGE(TAG,
"Receive error");
196 ESP_LOGW(TAG,
"Buffer overflow");
201 rmt_rx_done_event_data_t *
event = (rmt_rx_done_event_data_t *) (this->
store_.
buffer + this->store_.buffer_read);
202 uint32_t event_size =
sizeof(rmt_rx_done_event_data_t);
203 uint32_t next_read = this->
store_.
buffer_read + event_size +
event->num_symbols *
sizeof(rmt_symbol_word_t);
207 this->
decode_rmt_(event->received_symbols, event->num_symbols);
210 if (!this->
temp_.empty()) {
216 auto *item = (rmt_item32_t *) xRingbufferReceive(this->
ringbuf_, &len, 0);
217 if (item !=
nullptr) {
219 vRingbufferReturnItem(this->
ringbuf_, item);
221 if (!this->
temp_.empty()) {
228#if ESP_IDF_VERSION_MAJOR >= 5
233 bool prev_level =
false;
234 bool idle_level =
false;
235 uint32_t prev_length = 0;
240 ESP_LOGVV(TAG,
"START:");
241 for (
size_t i = 0; i < item_count; i++) {
242 if (item[i].level0) {
243 ESP_LOGVV(TAG,
"%zu A: ON %" PRIu32
"us (%u ticks)", i, this->
to_microseconds_(item[i].duration0),
246 ESP_LOGVV(TAG,
"%zu A: OFF %" PRIu32
"us (%u ticks)", i, this->
to_microseconds_(item[i].duration0),
249 if (item[i].level1) {
250 ESP_LOGVV(TAG,
"%zu B: ON %" PRIu32
"us (%u ticks)", i, this->
to_microseconds_(item[i].duration1),
253 ESP_LOGVV(TAG,
"%zu B: OFF %" PRIu32
"us (%u ticks)", i, this->
to_microseconds_(item[i].duration1),
257 ESP_LOGVV(TAG,
"\n");
259 this->
temp_.reserve(item_count * 2);
260 for (
size_t i = 0; i < item_count; i++) {
261 if (item[i].duration0 == 0u) {
264 }
else if ((
bool(item[i].level0) == prev_level) || (item[i].duration0 < filter_ticks)) {
265 prev_length += item[i].duration0;
267 if (prev_length >= filter_ticks) {
274 prev_level = bool(item[i].level0);
275 prev_length = item[i].duration0;
277 idle_level = !bool(item[i].level0);
279 if (item[i].duration1 == 0u) {
282 }
else if ((
bool(item[i].level1) == prev_level) || (item[i].duration1 < filter_ticks)) {
283 prev_length += item[i].duration1;
285 if (prev_length >= filter_ticks) {
292 prev_level = bool(item[i].level1);
293 prev_length = item[i].duration1;
295 idle_level = !bool(item[i].level1);
297 if (prev_length >= filter_ticks && prev_level != idle_level) {
304 if (!this->
temp_.empty()) {
308 this->
temp_.push_back(-int32_t(this->
idle_us_) * multiplier);
virtual void mark_failed()
Mark this component as failed.
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual bool digital_read()=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void config_rmt(rmt_config_t &rmt)
uint32_t to_microseconds_(uint32_t ticks)
uint32_t clock_resolution_
uint32_t from_microseconds_(uint32_t us)
ToleranceMode tolerance_mode_
void call_listeners_dumpers_()
RemoteReceiverComponentStore store_
void dump_config() override
void decode_rmt_(rmt_symbol_word_t *item, size_t item_count)
rmt_channel_handle_t channel_
uint32_t receive_symbols_
std::string error_string_
Providing packet encoding functions for exchanging data with a remote host.
std::string str_sprintf(const char *fmt,...)
volatile uint32_t * buffer
Stores the time (in micros) that the leading/falling edge happened at.
rmt_receive_config_t config
volatile uint32_t buffer_read
The position last read from.
volatile uint32_t buffer_write
The position last written to.