ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
remote_receiver_timer.cpp
Go to the documentation of this file.
1#include "remote_receiver.h"
2
3// The platform-independent part of timer sampling; remote_receiver_bk72xx.cpp reads the pin and the clock.
4#ifdef USE_REMOTE_RECEIVER_TIMER_SAMPLING
5
7
9 if (!this->capture_active) {
10 if (level == this->last_level)
11 return false;
12 this->level_ticks = 0;
13 this->last_level = level;
14 // a frame starts with a mark
15 if (level) {
16 this->capture_active = true;
17 this->run_active = false;
18 }
19 return false;
20 }
21
22 this->level_ticks += elapsed;
23 // A space includes the runs the filter folded into it, so a glitch does not restart the idle count. It is
24 // checked on an edge too, so a mark that arrives as the space reaches idle does not merge two frames.
25 uint32_t idle_run = this->level_ticks;
26 if (!this->last_level && this->run_active && !this->run_level)
27 idle_run += this->run_ticks;
28 if (idle_run >= this->idle_ticks)
29 return this->end_frame(level);
30
31 if (level == this->last_level)
32 return false;
33 this->finish_run(this->last_level, this->level_ticks);
34 this->level_ticks = 0;
35 this->last_level = level;
36 return false;
37}
38
40 // end the frame with one idle period, like the edge path, not the measured gap
41 if (!this->last_level && this->run_active && !this->run_level) {
42 this->run_ticks = this->idle_ticks;
43 } else {
44 this->finish_run(this->last_level, this->idle_ticks);
45 }
46 this->push_run();
47 // a mark that ended the idle space starts the next capture
48 this->capture_active = level && !this->last_level;
49 this->run_active = false;
50 this->level_ticks = 0;
51 this->last_level = level;
52 const uint16_t count = this->count;
53 this->count = 0;
54 // a capture that held only a glitch the filter folded away leaves nothing to dispatch
55 if (count == 0)
56 return false;
57 if (this->frame_overflow) {
58 this->frame_overflow = false;
59 this->overflow = true;
60 return true;
61 }
62 if (this->ready != nullptr) {
63 this->busy = true;
64 return true;
65 }
66 this->ready = this->capture;
67 this->ready_count = count;
68 this->capture = this->capture == this->buffer ? this->buffer + this->frame_entries : this->buffer;
69 return true;
70}
71
73 if (!this->run_active) {
74 this->run_level = level;
75 this->run_ticks = ticks;
76 this->run_active = true;
77 return;
78 }
79 // A run that returns to the pending level continues it, and a run shorter than the filter is folded into
80 // it, so the emitted durations keep alternating sign as the decoders expect.
81 if (level == this->run_level || ticks < this->filter_ticks) {
82 this->run_ticks += ticks;
83 return;
84 }
85 this->push_run();
86 this->run_level = level;
87 this->run_ticks = ticks;
88}
89
91 // drop a leading space, for example one left after a first mark that the filter removed
92 if (!this->run_active || (this->count == 0 && !this->run_level) || this->run_ticks < this->filter_ticks)
93 return;
94 const uint16_t count = this->count;
95 if (count >= this->frame_entries) {
96 // drop the rest of the frame; the frame end reports the overflow
97 this->frame_overflow = true;
98 return;
99 }
100 const auto run_us = static_cast<int32_t>(this->run_ticks / TIMER_TICKS_PER_US);
101 this->capture[count] = this->run_level ? run_us : -run_us;
102 this->count = count + 1;
103}
104
105} // namespace esphome::remote_receiver
106
107#endif // USE_REMOTE_RECEIVER_TIMER_SAMPLING
static void uint32_t
const int32_t *volatile ready
Finished frame for the loop, nullptr once the loop has dispatched it.
bool sample(bool level, uint32_t elapsed)
Takes one sample: the logical pin level (true for a mark) and the ticks since the previous sample.
volatile bool busy
A finished frame was dropped because the loop was still dispatching the other half.
bool frame_overflow
The frame being captured did not fit in its half.
uint16_t frame_entries
Entries per frame half; buffer_size is capped at 65535 bytes, so at most 8191.