ESPHome  2024.11.0
remote_receiver_libretiny.cpp
Go to the documentation of this file.
1 #include "remote_receiver.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
5 
6 #ifdef USE_LIBRETINY
7 
8 namespace esphome {
9 namespace remote_receiver {
10 
11 static const char *const TAG = "remote_receiver.libretiny";
12 
13 void IRAM_ATTR HOT RemoteReceiverComponentStore::gpio_intr(RemoteReceiverComponentStore *arg) {
14  const uint32_t now = micros();
15  // If the lhs is 1 (rising edge) we should write to an uneven index and vice versa
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)
19  return;
20 
21  // If next is buffer_read, we have hit an overflow
22  if (next == arg->buffer_read_at)
23  return;
24 
25  const uint32_t last_change = arg->buffer[arg->buffer_write_at];
26  const uint32_t time_since_change = now - last_change;
27  if (time_since_change <= arg->filter_us)
28  return;
29 
30  arg->buffer[arg->buffer_write_at = next] = now;
31 }
32 
34  ESP_LOGCONFIG(TAG, "Setting up Remote Receiver...");
35  this->pin_->setup();
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_;
40 
41  this->high_freq_.start();
42  if (s.buffer_size % 2 != 0) {
43  // Make sure divisible by two. This way, we know that every 0bxxx0 index is a space and every 0bxxx1 index is a mark
44  s.buffer_size++;
45  }
46 
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));
50 
51  // First index is a space.
52  if (this->pin_->digital_read()) {
53  s.buffer_write_at = s.buffer_read_at = 1;
54  } else {
55  s.buffer_write_at = s.buffer_read_at = 0;
56  }
57  this->pin_->attach_interrupt(RemoteReceiverComponentStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);
58 }
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!");
65  }
66  ESP_LOGCONFIG(TAG, " Buffer Size: %u", this->buffer_size_);
67  ESP_LOGCONFIG(TAG, " Tolerance: %u%s", this->tolerance_,
68  (this->tolerance_mode_ == remote_base::TOLERANCE_MODE_TIME) ? " us" : "%");
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_);
71 }
72 
74  auto &s = this->store_;
75 
76  // copy write at to local variables, as it's volatile
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;
79  // signals must at least one rising and one leading edge
80  if (dist <= 1)
81  return;
82  const uint32_t now = micros();
83  if (now - s.buffer[write_at] < this->idle_us_) {
84  // The last change was fewer than the configured idle time ago.
85  return;
86  }
87 
88  ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", s.buffer_read_at, write_at, dist, now,
89  s.buffer[write_at]);
90 
91  // Skip first value, it's from the previous idle level
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;
96  this->temp_.clear();
97  this->temp_.reserve(reserve_size);
98  int32_t multiplier = s.buffer_read_at % 2 == 0 ? 1 : -1;
99 
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_) {
103  // already found a space longer than idle. There must have been two pulses
104  break;
105  }
106 
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;
112  multiplier *= -1;
113  }
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);
116 
117  this->call_listeners_dumpers_();
118 }
119 
120 } // namespace remote_receiver
121 } // namespace esphome
122 
123 #endif
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:27
static void gpio_intr(RemoteReceiverComponentStore *arg)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7