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 =
static_cast<uint8_t
>(
24 std::min(this->from_microseconds_(this->
filter_us_) * this->clock_divider_, (uint32_t) 255));
26 rmt.rx_config.idle_threshold =
27 static_cast<uint16_t
>(std::min(this->from_microseconds_(this->
idle_us_), (uint32_t) 65535));
29 esp_err_t error = rmt_config(&rmt);
30 if (error != ESP_OK) {
37 error = rmt_driver_install(this->channel_, this->
buffer_size_, 0);
38 if (error != ESP_OK) {
40 if (error == ESP_ERR_INVALID_STATE) {
48 error = rmt_get_ringbuf_handle(this->channel_, &this->
ringbuf_);
49 if (error != ESP_OK) {
55 error = rmt_rx_start(this->channel_,
true);
56 if (error != ESP_OK) {
64 ESP_LOGCONFIG(TAG,
"Remote Receiver:");
65 LOG_PIN(
" Pin: ", this->
pin_);
67 ESP_LOGW(TAG,
"Remote Receiver Signal starts with a HIGH value. Usually this means you have to " 68 "invert the signal using 'inverted: True' in the pin schema!");
70 ESP_LOGCONFIG(TAG,
" Channel: %d", this->channel_);
71 ESP_LOGCONFIG(TAG,
" RMT memory blocks: %d", this->mem_block_num_);
72 ESP_LOGCONFIG(TAG,
" Clock divider: %u", this->clock_divider_);
73 ESP_LOGCONFIG(TAG,
" Tolerance: %" PRIu32
"%s", this->
tolerance_,
75 ESP_LOGCONFIG(TAG,
" Filter out pulses shorter than: %" PRIu32
" us", this->
filter_us_);
76 ESP_LOGCONFIG(TAG,
" Signal is done after %" PRIu32
" us of no changes", this->
idle_us_);
77 if (this->is_failed()) {
78 ESP_LOGE(TAG,
"Configuring RMT driver failed: %s (%s)", esp_err_to_name(this->
error_code_),
85 auto *item = (rmt_item32_t *) xRingbufferReceive(this->
ringbuf_, &len, 0);
86 if (item !=
nullptr) {
88 vRingbufferReturnItem(this->
ringbuf_, item);
90 if (this->
temp_.empty())
98 bool prev_level =
false;
99 uint32_t prev_length = 0;
102 size_t item_count = len /
sizeof(rmt_item32_t);
103 uint32_t filter_ticks = this->from_microseconds_(this->
filter_us_);
105 ESP_LOGVV(TAG,
"START:");
106 for (
size_t i = 0; i < item_count; i++) {
107 if (item[i].level0) {
108 ESP_LOGVV(TAG,
"%zu A: ON %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
111 ESP_LOGVV(TAG,
"%zu A: OFF %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
114 if (item[i].level1) {
115 ESP_LOGVV(TAG,
"%zu B: ON %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
118 ESP_LOGVV(TAG,
"%zu B: OFF %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
122 ESP_LOGVV(TAG,
"\n");
124 this->
temp_.reserve(item_count * 2);
125 for (
size_t i = 0; i < item_count; i++) {
126 if (item[i].duration0 == 0u) {
128 }
else if ((
bool(item[i].level0) == prev_level) || (item[i].duration0 < filter_ticks)) {
129 prev_length += item[i].duration0;
131 if (prev_length > 0) {
133 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
135 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
138 prev_level = bool(item[i].level0);
139 prev_length = item[i].duration0;
142 if (item[i].duration1 == 0u) {
144 }
else if ((
bool(item[i].level1) == prev_level) || (item[i].duration1 < filter_ticks)) {
145 prev_length += item[i].duration1;
147 if (prev_length > 0) {
149 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
151 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
154 prev_level = bool(item[i].level1);
155 prev_length = item[i].duration1;
158 if (prev_length > 0) {
160 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
162 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
void call_listeners_dumpers_()
virtual uint8_t get_pin() const =0
std::string str_sprintf(const char *fmt,...)
std::string error_string_
void dump_config() override
virtual bool digital_read()=0
Implementation of SPI Controller mode.
ToleranceMode tolerance_mode_
void decode_rmt_(rmt_item32_t *item, size_t len)
virtual bool is_inverted() const =0