ESPHome  2024.10.2
remote_base.h
Go to the documentation of this file.
1 #include <utility>
2 #include <vector>
3 
4 #pragma once
5 
9 #include "esphome/core/hal.h"
10 
11 #ifdef USE_ESP32
12 #include <driver/rmt.h>
13 #endif
14 
15 namespace esphome {
16 namespace remote_base {
17 
18 enum ToleranceMode : uint8_t {
21 };
22 
23 using RawTimings = std::vector<int32_t>;
24 
26  public:
27  void mark(uint32_t length) { this->data_.push_back(length); }
28  void space(uint32_t length) { this->data_.push_back(-length); }
29  void item(uint32_t mark, uint32_t space) {
30  this->mark(mark);
31  this->space(space);
32  }
33  void reserve(uint32_t len) { this->data_.reserve(len); }
34  void set_carrier_frequency(uint32_t carrier_frequency) { this->carrier_frequency_ = carrier_frequency; }
35  uint32_t get_carrier_frequency() const { return this->carrier_frequency_; }
36  const RawTimings &get_data() const { return this->data_; }
37  void set_data(const RawTimings &data) { this->data_ = data; }
38  void reset() {
39  this->data_.clear();
40  this->carrier_frequency_ = 0;
41  }
42 
43  protected:
45  uint32_t carrier_frequency_{0};
46 };
47 
49  public:
50  explicit RemoteReceiveData(const RawTimings &data, uint32_t tolerance, ToleranceMode tolerance_mode)
51  : data_(data), index_(0), tolerance_(tolerance), tolerance_mode_(tolerance_mode) {}
52 
53  const RawTimings &get_raw_data() const { return this->data_; }
54  uint32_t get_index() const { return index_; }
55  int32_t operator[](uint32_t index) const { return this->data_[index]; }
56  int32_t size() const { return this->data_.size(); }
57  bool is_valid(uint32_t offset) const { return this->index_ + offset < this->data_.size(); }
58  int32_t peek(uint32_t offset = 0) const { return this->data_[this->index_ + offset]; }
59  bool peek_mark(uint32_t length, uint32_t offset = 0) const;
60  bool peek_space(uint32_t length, uint32_t offset = 0) const;
61  bool peek_space_at_least(uint32_t length, uint32_t offset = 0) const;
62  bool peek_item(uint32_t mark, uint32_t space, uint32_t offset = 0) const {
63  return this->peek_space(space, offset + 1) && this->peek_mark(mark, offset);
64  }
65 
66  bool expect_mark(uint32_t length);
67  bool expect_space(uint32_t length);
68  bool expect_item(uint32_t mark, uint32_t space);
69  bool expect_pulse_with_gap(uint32_t mark, uint32_t space);
70  void advance(uint32_t amount = 1) { this->index_ += amount; }
71  void reset() { this->index_ = 0; }
72 
73  void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode) {
74  this->tolerance_ = tolerance;
75  this->tolerance_mode_ = tolerance_mode;
76  }
77  uint32_t get_tolerance() { return tolerance_; }
78  ToleranceMode get_tolerance_mode() { return this->tolerance_mode_; }
79 
80  protected:
81  int32_t lower_bound_(uint32_t length) const {
82  if (this->tolerance_mode_ == TOLERANCE_MODE_TIME) {
83  return int32_t(length - this->tolerance_);
84  } else if (this->tolerance_mode_ == TOLERANCE_MODE_PERCENTAGE) {
85  return int32_t(100 - this->tolerance_) * length / 100U;
86  }
87  return 0;
88  }
89  int32_t upper_bound_(uint32_t length) const {
90  if (this->tolerance_mode_ == TOLERANCE_MODE_TIME) {
91  return int32_t(length + this->tolerance_);
92  } else if (this->tolerance_mode_ == TOLERANCE_MODE_PERCENTAGE) {
93  return int32_t(100 + this->tolerance_) * length / 100U;
94  }
95  return 0;
96  }
97 
98  const RawTimings &data_;
99  uint32_t index_;
100  uint32_t tolerance_;
102 };
103 
105  public:
106  explicit RemoteComponentBase(InternalGPIOPin *pin) : pin_(pin){};
107 
108  protected:
109  InternalGPIOPin *pin_;
110 };
111 
112 #ifdef USE_ESP32
114  public:
115  explicit RemoteRMTChannel(uint8_t mem_block_num = 1);
116  explicit RemoteRMTChannel(rmt_channel_t channel, uint8_t mem_block_num = 1);
117 
118  void config_rmt(rmt_config_t &rmt);
119  void set_clock_divider(uint8_t clock_divider) { this->clock_divider_ = clock_divider; }
120 
121  protected:
122  uint32_t from_microseconds_(uint32_t us) {
123  const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
124  return us * ticks_per_ten_us / 10;
125  }
126  uint32_t to_microseconds_(uint32_t ticks) {
127  const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
128  return (ticks * 10) / ticks_per_ten_us;
129  }
131  rmt_channel_t channel_{RMT_CHANNEL_0};
132  uint8_t mem_block_num_;
133  uint8_t clock_divider_{80};
134 };
135 #endif
136 
138  public:
140  class TransmitCall {
141  public:
142  explicit TransmitCall(RemoteTransmitterBase *parent) : parent_(parent) {}
143  RemoteTransmitData *get_data() { return &this->parent_->temp_; }
144  void set_send_times(uint32_t send_times) { send_times_ = send_times; }
145  void set_send_wait(uint32_t send_wait) { send_wait_ = send_wait; }
146  void perform() { this->parent_->send_(this->send_times_, this->send_wait_); }
147 
148  protected:
150  uint32_t send_times_{1};
151  uint32_t send_wait_{0};
152  };
153 
155  this->temp_.reset();
156  return TransmitCall(this);
157  }
158  template<typename Protocol>
159  void transmit(const typename Protocol::ProtocolData &data, uint32_t send_times = 1, uint32_t send_wait = 0) {
160  auto call = this->transmit();
161  Protocol().encode(call.get_data(), data);
162  call.set_send_times(send_times);
163  call.set_send_wait(send_wait);
164  call.perform();
165  }
166 
167  protected:
168  void send_(uint32_t send_times, uint32_t send_wait);
169  virtual void send_internal(uint32_t send_times, uint32_t send_wait) = 0;
170  void send_single_() { this->send_(1, 0); }
171 
174 };
175 
177  public:
178  virtual bool on_receive(RemoteReceiveData data) = 0;
179 };
180 
182  public:
183  virtual bool dump(RemoteReceiveData src) = 0;
184  virtual bool is_secondary() { return false; }
185 };
186 
188  public:
190  void register_listener(RemoteReceiverListener *listener) { this->listeners_.push_back(listener); }
191  void register_dumper(RemoteReceiverDumperBase *dumper);
192  void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode) {
193  this->tolerance_ = tolerance;
194  this->tolerance_mode_ = tolerance_mode;
195  }
196 
197  protected:
198  void call_listeners_();
199  void call_dumpers_();
201  this->call_listeners_();
202  this->call_dumpers_();
203  }
204 
205  std::vector<RemoteReceiverListener *> listeners_;
206  std::vector<RemoteReceiverDumperBase *> dumpers_;
207  std::vector<RemoteReceiverDumperBase *> secondary_dumpers_;
209  uint32_t tolerance_{25};
211 };
212 
214  public Component,
215  public RemoteReceiverListener {
216  public:
218  void dump_config() override;
219  virtual bool matches(RemoteReceiveData src) = 0;
220  bool on_receive(RemoteReceiveData src) override;
221 };
222 
223 /* TEMPLATES */
224 
225 template<typename T> class RemoteProtocol {
226  public:
227  using ProtocolData = T;
228  virtual void encode(RemoteTransmitData *dst, const ProtocolData &data) = 0;
229  virtual optional<ProtocolData> decode(RemoteReceiveData src) = 0;
230  virtual void dump(const ProtocolData &data) = 0;
231 };
232 
233 template<typename T> class RemoteReceiverBinarySensor : public RemoteReceiverBinarySensorBase {
234  public:
236 
237  protected:
238  bool matches(RemoteReceiveData src) override {
239  auto proto = T();
240  auto res = proto.decode(src);
241  return res.has_value() && *res == this->data_;
242  }
243 
244  public:
245  void set_data(typename T::ProtocolData data) { data_ = data; }
246 
247  protected:
248  typename T::ProtocolData data_;
249 };
250 
251 template<typename T>
252 class RemoteReceiverTrigger : public Trigger<typename T::ProtocolData>, public RemoteReceiverListener {
253  protected:
254  bool on_receive(RemoteReceiveData src) override {
255  auto proto = T();
256  auto res = proto.decode(src);
257  if (res.has_value()) {
258  this->trigger(*res);
259  return true;
260  }
261  return false;
262  }
263 };
264 
266  public:
268  RemoteTransmittable(RemoteTransmitterBase *transmitter) : transmitter_(transmitter) {}
269  void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_ = transmitter; }
270 
271  protected:
272  template<typename Protocol>
273  void transmit_(const typename Protocol::ProtocolData &data, uint32_t send_times = 1, uint32_t send_wait = 0) {
274  this->transmitter_->transmit<Protocol>(data, send_times, send_wait);
275  }
277 };
278 
279 template<typename... Ts> class RemoteTransmitterActionBase : public RemoteTransmittable, public Action<Ts...> {
280  TEMPLATABLE_VALUE(uint32_t, send_times)
281  TEMPLATABLE_VALUE(uint32_t, send_wait)
282 
283  protected:
284  void play(Ts... x) override {
285  auto call = this->transmitter_->transmit();
286  this->encode(call.get_data(), x...);
287  call.set_send_times(this->send_times_.value_or(x..., 1));
288  call.set_send_wait(this->send_wait_.value_or(x..., 0));
289  call.perform();
290  }
291  virtual void encode(RemoteTransmitData *dst, Ts... x) = 0;
292 };
293 
294 template<typename T> class RemoteReceiverDumper : public RemoteReceiverDumperBase {
295  public:
296  bool dump(RemoteReceiveData src) override {
297  auto proto = T();
298  auto decoded = proto.decode(src);
299  if (!decoded.has_value())
300  return false;
301  proto.dump(*decoded);
302  return true;
303  }
304 };
305 
306 #define DECLARE_REMOTE_PROTOCOL_(prefix) \
307  using prefix##BinarySensor = RemoteReceiverBinarySensor<prefix##Protocol>; \
308  using prefix##Trigger = RemoteReceiverTrigger<prefix##Protocol>; \
309  using prefix##Dumper = RemoteReceiverDumper<prefix##Protocol>;
310 #define DECLARE_REMOTE_PROTOCOL(prefix) DECLARE_REMOTE_PROTOCOL_(prefix)
311 
312 } // namespace remote_base
313 } // namespace esphome
void register_listener(RemoteReceiverListener *listener)
Definition: remote_base.h:190
void set_data(const RawTimings &data)
Definition: remote_base.h:37
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
Definition: remote_base.h:173
int32_t operator[](uint32_t index) const
Definition: remote_base.h:55
const RawTimings & get_raw_data() const
Definition: remote_base.h:53
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:34
uint16_t x
Definition: tt21100.cpp:17
bool is_valid(uint32_t offset) const
Definition: remote_base.h:57
void item(uint32_t mark, uint32_t space)
Definition: remote_base.h:29
std::vector< RemoteReceiverDumperBase * > dumpers_
Definition: remote_base.h:206
void set_data(typename T::ProtocolData data)
Definition: remote_base.h:245
std::vector< RemoteReceiverDumperBase * > secondary_dumpers_
Definition: remote_base.h:207
void set_transmitter(RemoteTransmitterBase *transmitter)
Definition: remote_base.h:269
RemoteComponentBase(InternalGPIOPin *pin)
Definition: remote_base.h:106
RemoteComponentBase * remote_base_
Definition: remote_base.h:130
std::vector< int32_t > RawTimings
Definition: remote_base.h:23
RemoteReceiveData(const RawTimings &data, uint32_t tolerance, ToleranceMode tolerance_mode)
Definition: remote_base.h:50
void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode)
Definition: remote_base.h:73
RemoteTransmitterBase * transmitter_
Definition: remote_base.h:276
int32_t lower_bound_(uint32_t length) const
Definition: remote_base.h:81
int32_t peek(uint32_t offset=0) const
Definition: remote_base.h:58
std::vector< RemoteReceiverListener * > listeners_
Definition: remote_base.h:205
void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode)
Definition: remote_base.h:192
void transmit(const typename Protocol::ProtocolData &data, uint32_t send_times=1, uint32_t send_wait=0)
Definition: remote_base.h:159
RemoteReceiverBase(InternalGPIOPin *pin)
Definition: remote_base.h:189
bool on_receive(RemoteReceiveData src) override
Definition: remote_base.h:254
bool dump(RemoteReceiveData src) override
Definition: remote_base.h:296
void set_clock_divider(uint8_t clock_divider)
Definition: remote_base.h:119
std::string size_t len
Definition: helpers.h:292
void transmit_(const typename Protocol::ProtocolData &data, uint32_t send_times=1, uint32_t send_wait=0)
Definition: remote_base.h:273
bool peek_item(uint32_t mark, uint32_t space, uint32_t offset=0) const
Definition: remote_base.h:62
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool matches(RemoteReceiveData src) override
Definition: remote_base.h:238
void advance(uint32_t amount=1)
Definition: remote_base.h:70
const RawTimings & get_data() const
Definition: remote_base.h:36
uint32_t to_microseconds_(uint32_t ticks)
Definition: remote_base.h:126
RemoteTransmittable(RemoteTransmitterBase *transmitter)
Definition: remote_base.h:268
int32_t upper_bound_(uint32_t length) const
Definition: remote_base.h:89
uint32_t from_microseconds_(uint32_t us)
Definition: remote_base.h:122