5 #include <driver/gpio.h> 8 namespace remote_receiver {
10 static const char *
const TAG =
"remote_receiver.esp32";
11 #ifdef USE_ESP32_VARIANT_ESP32H2 12 static const uint32_t RMT_CLK_FREQ = 32000000;
14 static const uint32_t RMT_CLK_FREQ = 80000000;
17 #if ESP_IDF_VERSION_MAJOR >= 5 18 static 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;
48 channel.resolution_hz = this->clock_resolution_;
49 channel.mem_block_symbols = rmt_symbols_;
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) {
111 this->config_rmt(rmt);
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
>(
119 std::min(this->from_microseconds_(this->
filter_us_) * this->clock_divider_, (uint32_t) 255));
121 rmt.rx_config.idle_threshold =
122 static_cast<uint16_t
>(std::min(this->from_microseconds_(this->
idle_us_), (uint32_t) 65535));
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_);
175 ESP_LOGCONFIG(TAG,
" Clock divider: %u", this->clock_divider_);
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_);
181 if (this->is_failed()) {
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()) {
217 auto *item = (rmt_item32_t *) xRingbufferReceive(this->
ringbuf_, &len, 0);
218 if (item !=
nullptr) {
219 this->
decode_rmt_(item, len /
sizeof(rmt_item32_t));
220 vRingbufferReturnItem(this->
ringbuf_, item);
222 if (this->
temp_.empty())
231 #if ESP_IDF_VERSION_MAJOR >= 5 236 bool prev_level =
false;
237 uint32_t prev_length = 0;
240 uint32_t filter_ticks = this->from_microseconds_(this->
filter_us_);
242 ESP_LOGVV(TAG,
"START:");
243 for (
size_t i = 0; i < item_count; i++) {
244 if (item[i].level0) {
245 ESP_LOGVV(TAG,
"%zu A: ON %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
248 ESP_LOGVV(TAG,
"%zu A: OFF %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration0),
251 if (item[i].level1) {
252 ESP_LOGVV(TAG,
"%zu B: ON %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
255 ESP_LOGVV(TAG,
"%zu B: OFF %" PRIu32
"us (%u ticks)", i, this->to_microseconds_(item[i].duration1),
259 ESP_LOGVV(TAG,
"\n");
261 this->
temp_.reserve(item_count * 2);
262 for (
size_t i = 0; i < item_count; i++) {
263 if (item[i].duration0 == 0u) {
266 }
else if ((
bool(item[i].level0) == prev_level) || (item[i].duration0 < filter_ticks)) {
267 prev_length += item[i].duration0;
269 if (prev_length > 0) {
271 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
273 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
276 prev_level = bool(item[i].level0);
277 prev_length = item[i].duration0;
280 if (item[i].duration1 == 0u) {
283 }
else if ((
bool(item[i].level1) == prev_level) || (item[i].duration1 < filter_ticks)) {
284 prev_length += item[i].duration1;
286 if (prev_length > 0) {
288 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
290 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
293 prev_level = bool(item[i].level1);
294 prev_length = item[i].duration1;
297 if (prev_length > 0) {
299 this->
temp_.push_back(this->to_microseconds_(prev_length) * multiplier);
301 this->
temp_.push_back(-int32_t(this->to_microseconds_(prev_length)) * multiplier);
volatile uint32_t buffer_read
The position last read from.
uint32_t receive_symbols_
rmt_receive_config_t config
void call_listeners_dumpers_()
RemoteReceiverComponentStore store_
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
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
volatile uint32_t * buffer
Stores the time (in micros) that the leading/falling edge happened at.
volatile uint32_t buffer_write
The position last written to.
Implementation of SPI Controller mode.
void decode_rmt_(rmt_symbol_word_t *item, size_t item_count)
ToleranceMode tolerance_mode_
virtual bool is_inverted() const =0
rmt_channel_handle_t channel_