9 namespace remote_receiver {
11 static const char *
const TAG =
"remote_receiver.libretiny";
14 const uint32_t now =
micros();
16 const uint32_t next = (arg->buffer_write_at + 1) % arg->buffer_size;
17 const bool level = arg->pin.digital_read();
18 if (level != next % 2)
22 if (next == arg->buffer_read_at)
25 const uint32_t last_change = arg->buffer[arg->buffer_write_at];
26 const uint32_t time_since_change = now - last_change;
30 arg->buffer[arg->buffer_write_at = next] = now;
34 ESP_LOGCONFIG(TAG,
"Setting up Remote Receiver...");
36 auto &s = this->store_;
37 s.filter_us = this->filter_us_;
38 s.pin = this->pin_->to_isr();
39 s.buffer_size = this->buffer_size_;
41 this->high_freq_.start();
42 if (s.buffer_size % 2 != 0) {
47 s.buffer =
new uint32_t[s.buffer_size];
48 void *buf = (
void *) s.buffer;
49 memset(buf, 0, s.buffer_size *
sizeof(uint32_t));
52 if (this->pin_->digital_read()) {
53 s.buffer_write_at = s.buffer_read_at = 1;
55 s.buffer_write_at = s.buffer_read_at = 0;
60 ESP_LOGCONFIG(TAG,
"Remote Receiver:");
61 LOG_PIN(
" Pin: ", this->pin_);
62 if (this->pin_->digital_read()) {
63 ESP_LOGW(TAG,
"Remote Receiver Signal starts with a HIGH value. Usually this means you have to " 64 "invert the signal using 'inverted: True' in the pin schema!");
66 ESP_LOGCONFIG(TAG,
" Buffer Size: %u", this->buffer_size_);
67 ESP_LOGCONFIG(TAG,
" Tolerance: %u%s", this->tolerance_,
69 ESP_LOGCONFIG(TAG,
" Filter out pulses shorter than: %u us", this->filter_us_);
70 ESP_LOGCONFIG(TAG,
" Signal is done after %u us of no changes", this->idle_us_);
74 auto &s = this->store_;
77 const uint32_t write_at = s.buffer_write_at;
78 const uint32_t dist = (s.buffer_size + write_at - s.buffer_read_at) % s.buffer_size;
82 const uint32_t now =
micros();
83 if (now - s.buffer[write_at] < this->idle_us_) {
88 ESP_LOGVV(TAG,
"read_at=%u write_at=%u dist=%u now=%u end=%u", s.buffer_read_at, write_at, dist, now,
92 s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
93 uint32_t prev = s.buffer_read_at;
94 s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
95 const uint32_t reserve_size = 1 + (s.buffer_size + write_at - s.buffer_read_at) % s.buffer_size;
97 this->temp_.reserve(reserve_size);
98 int32_t multiplier = s.buffer_read_at % 2 == 0 ? 1 : -1;
100 for (uint32_t i = 0; prev != write_at; i++) {
101 int32_t delta = s.buffer[s.buffer_read_at] - s.buffer[prev];
102 if (uint32_t(delta) >= this->idle_us_) {
107 ESP_LOGVV(TAG,
" i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, s.buffer_read_at, s.buffer[s.buffer_read_at], prev,
108 s.buffer[prev], multiplier * delta);
109 this->temp_.push_back(multiplier * delta);
110 prev = s.buffer_read_at;
111 s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
114 s.buffer_read_at = (s.buffer_size + s.buffer_read_at - 1) % s.buffer_size;
115 this->temp_.push_back(this->idle_us_ * multiplier);
117 this->call_listeners_dumpers_();
uint32_t IRAM_ATTR HOT micros()
static void gpio_intr(RemoteReceiverComponentStore *arg)
void dump_config() override
Implementation of SPI Controller mode.