ESPHome 2025.12.2
Loading...
Searching...
No Matches
remote_receiver.cpp
Go to the documentation of this file.
1#include "remote_receiver.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6#if defined(USE_LIBRETINY) || defined(USE_ESP8266) || defined(USE_RP2040)
7
9
10static const char *const TAG = "remote_receiver";
11
12static void IRAM_ATTR HOT write_value(RemoteReceiverComponentStore *arg, uint32_t delta, bool level) {
13 // convert level to -1 or +1 and write the delta to the buffer
14 int32_t multiplier = ((int32_t) level << 1) - 1;
15 uint32_t buffer_write = arg->buffer_write;
16 arg->buffer[buffer_write++] = (int32_t) delta * multiplier;
17 if (buffer_write >= arg->buffer_size) {
18 buffer_write = 0;
19 }
20
21 // detect overflow and reset the write pointer
22 if (buffer_write == arg->buffer_read) {
23 buffer_write = arg->buffer_start;
24 arg->overflow = true;
25 }
26
27 // detect idle and start a new sequence unless there is only idle in
28 // which case reset the write pointer instead
29 if (delta >= arg->idle_us) {
30 if (arg->buffer_write == arg->buffer_start) {
31 buffer_write = arg->buffer_start;
32 } else {
33 arg->buffer_start = buffer_write;
34 }
35 }
36 arg->buffer_write = buffer_write;
37}
38
39static void IRAM_ATTR HOT commit_value(RemoteReceiverComponentStore *arg, uint32_t micros, bool level) {
40 // commit value if the level is different from the last commit level
41 if (level != arg->commit_level) {
42 write_value(arg, micros - arg->commit_micros, level);
43 arg->commit_micros = micros;
44 arg->commit_level = level;
45 }
46}
47
49 // invert the level so it matches the level of the signal before the edge
50 const bool curr_level = !arg->pin.digital_read();
51 const uint32_t curr_micros = micros();
52 const bool prev_level = arg->prev_level;
53 const uint32_t prev_micros = arg->prev_micros;
54
55 // commit the previous value if the pulse is not filtered and the level is different
56 if (curr_micros - prev_micros >= arg->filter_us && prev_level != curr_level) {
57 commit_value(arg, prev_micros, prev_level);
58 }
59 arg->prev_micros = curr_micros;
60 arg->prev_level = curr_level;
61}
62
64 this->pin_->setup();
65 this->store_.idle_us = this->idle_us_;
66 this->store_.filter_us = this->filter_us_;
67 this->store_.pin = this->pin_->to_isr();
68 this->store_.buffer = new int32_t[this->buffer_size_];
69 this->store_.buffer_size = this->buffer_size_;
70 this->store_.prev_micros = micros();
72 this->store_.prev_level = this->pin_->digital_read();
75 this->high_freq_.start();
76}
77
79 ESP_LOGCONFIG(TAG, "Remote Receiver:");
80 LOG_PIN(" Pin: ", this->pin_);
81 ESP_LOGCONFIG(TAG,
82 " Buffer Size: %u\n"
83 " Tolerance: %u%s\n"
84 " Filter out pulses shorter than: %u us\n"
85 " Signal is done after %u us of no changes",
86 this->buffer_size_, this->tolerance_,
87 (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%", this->filter_us_,
88 this->idle_us_);
89}
90
92 // check for overflow
93 auto &s = this->store_;
94 if (s.overflow) {
95 ESP_LOGW(TAG, "Buffer overflow");
96 s.overflow = false;
97 }
98
99 // if no data is available check for uncommitted data stuck in the buffer and commit
100 // the previous value if needed
101 uint32_t last_index = s.buffer_start;
102 if (last_index == s.buffer_read) {
103 InterruptLock lock;
104 if (s.buffer_read == s.buffer_start && s.buffer_write != s.buffer_start &&
105 micros() - s.prev_micros >= this->idle_us_) {
106 commit_value(&s, s.prev_micros, s.prev_level);
107 write_value(&s, s.idle_us, !s.commit_level);
108 last_index = s.buffer_start;
109 }
110 }
111 if (last_index == s.buffer_read) {
112 return;
113 }
114
115 // find the size of the packet and reserve the memory
116 uint32_t temp_read = s.buffer_read;
117 uint32_t reserve_size = 0;
118 while (temp_read != last_index && (uint32_t) std::abs(s.buffer[temp_read]) < this->idle_us_) {
119 reserve_size++;
120 temp_read++;
121 if (temp_read >= s.buffer_size) {
122 temp_read = 0;
123 }
124 }
125 this->temp_.clear();
126 this->temp_.reserve(reserve_size + 1);
127
128 // read the buffer
129 for (uint32_t i = 0; i < reserve_size + 1; i++) {
130 this->temp_.push_back((int32_t) s.buffer[s.buffer_read++]);
131 if (s.buffer_read >= s.buffer_size) {
132 s.buffer_read = 0;
133 }
134 }
135
136 // call the listeners and dumpers
138}
139
140} // namespace esphome::remote_receiver
141
142#endif
virtual void setup()=0
virtual bool digital_read()=0
void start()
Start running the loop continuously.
Definition helpers.cpp:636
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:88
virtual ISRInternalGPIOPin to_isr() const =0
Helper class to disable interrupts.
Definition helpers.h:1008
@ INTERRUPT_ANY_EDGE
Definition gpio.h:43
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:32
static void gpio_intr(RemoteReceiverComponentStore *arg)
uint32_t buffer_read
The position last read from.
volatile uint32_t buffer_start
The start position of the last sequence.
volatile int32_t * buffer
Stores pulse durations in microseconds as signed integers.
volatile uint32_t buffer_write
The position last written to.