5 #include <driver/rmt.h> 8 namespace remote_receiver {
10 static const char *
const TAG =
"remote_receiver.esp32";
13 ESP_LOGCONFIG(TAG,
"Setting up Remote Receiver...");
16 this->config_rmt(rmt);
18 rmt.rmt_mode = RMT_MODE_RX;
20 rmt.rx_config.filter_en =
false;
22 rmt.rx_config.filter_en =
true;
23 rmt.rx_config.filter_ticks_thresh = this->from_microseconds_(this->
filter_us_);
25 rmt.rx_config.idle_threshold = this->from_microseconds_(this->
idle_us_);
27 esp_err_t error = rmt_config(&rmt);
28 if (error != ESP_OK) {
34 error = rmt_driver_install(this->channel_, this->
buffer_size_, 0);
35 if (error != ESP_OK) {
40 error = rmt_get_ringbuf_handle(this->channel_, &this->
ringbuf_);
41 if (error != ESP_OK) {
46 error = rmt_rx_start(this->channel_,
true);
47 if (error != ESP_OK) {
54 ESP_LOGCONFIG(TAG,
"Remote Receiver:");
55 LOG_PIN(
" Pin: ", this->
pin_);
57 ESP_LOGW(TAG,
"Remote Receiver Signal starts with a HIGH value. Usually this means you have to " 58 "invert the signal using 'inverted: True' in the pin schema!");
60 ESP_LOGCONFIG(TAG,
" Channel: %d", this->channel_);
61 ESP_LOGCONFIG(TAG,
" RMT memory blocks: %d", this->mem_block_num_);
62 ESP_LOGCONFIG(TAG,
" Clock divider: %u", this->clock_divider_);
63 ESP_LOGCONFIG(TAG,
" Tolerance: %u%%", this->
tolerance_);
64 ESP_LOGCONFIG(TAG,
" Filter out pulses shorter than: %u us", this->
filter_us_);
65 ESP_LOGCONFIG(TAG,
" Signal is done after %u us of no changes", this->
idle_us_);
66 if (this->is_failed()) {
67 ESP_LOGE(TAG,
"Configuring RMT driver failed: %s", esp_err_to_name(this->
error_code_));
73 auto *item = (rmt_item32_t *) xRingbufferReceive(this->
ringbuf_, &len, 0);
74 if (item !=
nullptr) {
76 vRingbufferReturnItem(this->
ringbuf_, item);
78 if (this->
temp_.empty())
86 bool prev_level =
false;
87 uint32_t prev_length = 0;
90 size_t item_count = len /
sizeof(rmt_item32_t);
92 ESP_LOGVV(TAG,
"START:");
93 for (
size_t i = 0; i < item_count; i++) {
95 ESP_LOGVV(TAG,
"%u A: ON %uus (%u ticks)", i, this->to_microseconds_(item[i].duration0), item[i].duration0);
97 ESP_LOGVV(TAG,
"%u A: OFF %uus (%u ticks)", i, this->to_microseconds_(item[i].duration0), item[i].duration0);
100 ESP_LOGVV(TAG,
"%u B: ON %uus (%u ticks)", i, this->to_microseconds_(item[i].duration1), item[i].duration1);
102 ESP_LOGVV(TAG,
"%u B: OFF %uus (%u ticks)", i, this->to_microseconds_(item[i].duration1), item[i].duration1);
105 ESP_LOGVV(TAG,
"\n");
107 this->
temp_.reserve(item_count * 2);
108 for (
size_t i = 0; i < item_count; i++) {
109 if (item[i].duration0 == 0u) {
111 }
else if (
bool(item[i].level0) == prev_level) {
112 prev_length += item[i].duration0;
114 if (prev_length > 0) {
116 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
118 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
121 prev_level = bool(item[i].level0);
122 prev_length = item[i].duration0;
125 if (item[i].duration1 == 0u) {
127 }
else if (
bool(item[i].level1) == prev_level) {
128 prev_length += item[i].duration1;
130 if (prev_length > 0) {
132 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
134 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
137 prev_level = bool(item[i].level1);
138 prev_length = item[i].duration1;
141 if (prev_length > 0) {
143 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
145 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
void call_listeners_dumpers_()
virtual uint8_t get_pin() const =0
std::vector< int32_t > temp_
void dump_config() override
virtual bool digital_read()=0
void decode_rmt_(rmt_item32_t *item, size_t len)
virtual bool is_inverted() const =0