ESPHome  2023.11.6
remote_base.h
Go to the documentation of this file.
1 #include <utility>
2 #include <vector>
3 
4 #pragma once
5 
7 #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 using RawTimings = std::vector<int32_t>;
19 
21  public:
22  void mark(uint32_t length) { this->data_.push_back(length); }
23  void space(uint32_t length) { this->data_.push_back(-length); }
24  void item(uint32_t mark, uint32_t space) {
25  this->mark(mark);
26  this->space(space);
27  }
28  void reserve(uint32_t len) { this->data_.reserve(len); }
29  void set_carrier_frequency(uint32_t carrier_frequency) { this->carrier_frequency_ = carrier_frequency; }
30  uint32_t get_carrier_frequency() const { return this->carrier_frequency_; }
31  const RawTimings &get_data() const { return this->data_; }
32  void set_data(const RawTimings &data) { this->data_ = data; }
33  void reset() {
34  this->data_.clear();
35  this->carrier_frequency_ = 0;
36  }
37 
38  protected:
40  uint32_t carrier_frequency_{0};
41 };
42 
44  public:
45  explicit RemoteReceiveData(const RawTimings &data, uint8_t tolerance)
46  : data_(data), index_(0), tolerance_(tolerance) {}
47 
48  const RawTimings &get_raw_data() const { return this->data_; }
49  uint32_t get_index() const { return index_; }
50  int32_t operator[](uint32_t index) const { return this->data_[index]; }
51  int32_t size() const { return this->data_.size(); }
52  bool is_valid(uint32_t offset) const { return this->index_ + offset < this->data_.size(); }
53  int32_t peek(uint32_t offset = 0) const { return this->data_[this->index_ + offset]; }
54  bool peek_mark(uint32_t length, uint32_t offset = 0) const;
55  bool peek_space(uint32_t length, uint32_t offset = 0) const;
56  bool peek_space_at_least(uint32_t length, uint32_t offset = 0) const;
57  bool peek_item(uint32_t mark, uint32_t space, uint32_t offset = 0) const {
58  return this->peek_space(space, offset + 1) && this->peek_mark(mark, offset);
59  }
60 
61  bool expect_mark(uint32_t length);
62  bool expect_space(uint32_t length);
63  bool expect_item(uint32_t mark, uint32_t space);
64  bool expect_pulse_with_gap(uint32_t mark, uint32_t space);
65  void advance(uint32_t amount = 1) { this->index_ += amount; }
66  void reset() { this->index_ = 0; }
67 
68  protected:
69  int32_t lower_bound_(uint32_t length) const { return int32_t(100 - this->tolerance_) * length / 100U; }
70  int32_t upper_bound_(uint32_t length) const { return int32_t(100 + this->tolerance_) * length / 100U; }
71 
72  const RawTimings &data_;
73  uint32_t index_;
74  uint8_t tolerance_;
75 };
76 
78  public:
79  explicit RemoteComponentBase(InternalGPIOPin *pin) : pin_(pin){};
80 
81  protected:
82  InternalGPIOPin *pin_;
83 };
84 
85 #ifdef USE_ESP32
87  public:
88  explicit RemoteRMTChannel(uint8_t mem_block_num = 1);
89 
90  void config_rmt(rmt_config_t &rmt);
91  void set_clock_divider(uint8_t clock_divider) { this->clock_divider_ = clock_divider; }
92 
93  protected:
94  uint32_t from_microseconds_(uint32_t us) {
95  const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
96  return us * ticks_per_ten_us / 10;
97  }
98  uint32_t to_microseconds_(uint32_t ticks) {
99  const uint32_t ticks_per_ten_us = 80000000u / this->clock_divider_ / 100000u;
100  return (ticks * 10) / ticks_per_ten_us;
101  }
103  rmt_channel_t channel_{RMT_CHANNEL_0};
104  uint8_t mem_block_num_;
105  uint8_t clock_divider_{80};
106 };
107 #endif
108 
110  public:
112  class TransmitCall {
113  public:
114  explicit TransmitCall(RemoteTransmitterBase *parent) : parent_(parent) {}
115  RemoteTransmitData *get_data() { return &this->parent_->temp_; }
116  void set_send_times(uint32_t send_times) { send_times_ = send_times; }
117  void set_send_wait(uint32_t send_wait) { send_wait_ = send_wait; }
118  void perform() { this->parent_->send_(this->send_times_, this->send_wait_); }
119 
120  protected:
122  uint32_t send_times_{1};
123  uint32_t send_wait_{0};
124  };
125 
127  this->temp_.reset();
128  return TransmitCall(this);
129  }
130 
131  protected:
132  void send_(uint32_t send_times, uint32_t send_wait);
133  virtual void send_internal(uint32_t send_times, uint32_t send_wait) = 0;
134  void send_single_() { this->send_(1, 0); }
135 
138 };
139 
141  public:
142  virtual bool on_receive(RemoteReceiveData data) = 0;
143 };
144 
146  public:
147  virtual bool dump(RemoteReceiveData src) = 0;
148  virtual bool is_secondary() { return false; }
149 };
150 
152  public:
154  void register_listener(RemoteReceiverListener *listener) { this->listeners_.push_back(listener); }
155  void register_dumper(RemoteReceiverDumperBase *dumper);
156  void set_tolerance(uint8_t tolerance) { tolerance_ = tolerance; }
157 
158  protected:
159  void call_listeners_();
160  void call_dumpers_();
162  this->call_listeners_();
163  this->call_dumpers_();
164  }
165 
166  std::vector<RemoteReceiverListener *> listeners_;
167  std::vector<RemoteReceiverDumperBase *> dumpers_;
168  std::vector<RemoteReceiverDumperBase *> secondary_dumpers_;
170  uint8_t tolerance_;
171 };
172 
174  public Component,
175  public RemoteReceiverListener {
176  public:
178  void dump_config() override;
179  virtual bool matches(RemoteReceiveData src) = 0;
180  bool on_receive(RemoteReceiveData src) override;
181 };
182 
183 /* TEMPLATES */
184 
185 template<typename T> class RemoteProtocol {
186  public:
187  virtual void encode(RemoteTransmitData *dst, const T &data) = 0;
188  virtual optional<T> decode(RemoteReceiveData src) = 0;
189  virtual void dump(const T &data) = 0;
190 };
191 
192 template<typename T, typename D> class RemoteReceiverBinarySensor : public RemoteReceiverBinarySensorBase {
193  public:
195 
196  protected:
197  bool matches(RemoteReceiveData src) override {
198  auto proto = T();
199  auto res = proto.decode(src);
200  return res.has_value() && *res == this->data_;
201  }
202 
203  public:
204  void set_data(D data) { data_ = data; }
205 
206  protected:
207  D data_;
208 };
209 
210 template<typename T, typename D> class RemoteReceiverTrigger : public Trigger<D>, public RemoteReceiverListener {
211  protected:
212  bool on_receive(RemoteReceiveData src) override {
213  auto proto = T();
214  auto res = proto.decode(src);
215  if (res.has_value()) {
216  this->trigger(*res);
217  return true;
218  }
219  return false;
220  }
221 };
222 
223 template<typename... Ts> class RemoteTransmitterActionBase : public Action<Ts...> {
224  public:
225  void set_parent(RemoteTransmitterBase *parent) { this->parent_ = parent; }
226 
227  TEMPLATABLE_VALUE(uint32_t, send_times);
228  TEMPLATABLE_VALUE(uint32_t, send_wait);
229 
230  void play(Ts... x) override {
231  auto call = this->parent_->transmit();
232  this->encode(call.get_data(), x...);
233  call.set_send_times(this->send_times_.value_or(x..., 1));
234  call.set_send_wait(this->send_wait_.value_or(x..., 0));
235  call.perform();
236  }
237 
238  protected:
239  virtual void encode(RemoteTransmitData *dst, Ts... x) = 0;
240 
242 };
243 
244 template<typename T, typename D> class RemoteReceiverDumper : public RemoteReceiverDumperBase {
245  public:
246  bool dump(RemoteReceiveData src) override {
247  auto proto = T();
248  auto decoded = proto.decode(src);
249  if (!decoded.has_value())
250  return false;
251  proto.dump(*decoded);
252  return true;
253  }
254 };
255 
256 #define DECLARE_REMOTE_PROTOCOL_(prefix) \
257  using prefix##BinarySensor = RemoteReceiverBinarySensor<prefix##Protocol, prefix##Data>; \
258  using prefix##Trigger = RemoteReceiverTrigger<prefix##Protocol, prefix##Data>; \
259  using prefix##Dumper = RemoteReceiverDumper<prefix##Protocol, prefix##Data>;
260 #define DECLARE_REMOTE_PROTOCOL(prefix) DECLARE_REMOTE_PROTOCOL_(prefix)
261 
262 } // namespace remote_base
263 } // namespace esphome
void register_listener(RemoteReceiverListener *listener)
Definition: remote_base.h:154
void set_data(const RawTimings &data)
Definition: remote_base.h:32
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
Definition: remote_base.h:137
int32_t operator[](uint32_t index) const
Definition: remote_base.h:50
const RawTimings & get_raw_data() const
Definition: remote_base.h:48
void set_carrier_frequency(uint32_t carrier_frequency)
Definition: remote_base.h:29
bool on_receive(RemoteReceiveData src) override
Definition: remote_base.h:212
uint16_t x
Definition: tt21100.cpp:17
bool is_valid(uint32_t offset) const
Definition: remote_base.h:52
void item(uint32_t mark, uint32_t space)
Definition: remote_base.h:24
RemoteReceiveData(const RawTimings &data, uint8_t tolerance)
Definition: remote_base.h:45
std::vector< RemoteReceiverDumperBase * > dumpers_
Definition: remote_base.h:167
std::vector< RemoteReceiverDumperBase * > secondary_dumpers_
Definition: remote_base.h:168
RemoteComponentBase(InternalGPIOPin *pin)
Definition: remote_base.h:79
RemoteComponentBase * remote_base_
Definition: remote_base.h:102
std::vector< int32_t > RawTimings
Definition: remote_base.h:18
bool dump(RemoteReceiveData src) override
Definition: remote_base.h:246
int32_t lower_bound_(uint32_t length) const
Definition: remote_base.h:69
int32_t peek(uint32_t offset=0) const
Definition: remote_base.h:53
std::vector< RemoteReceiverListener * > listeners_
Definition: remote_base.h:166
RemoteReceiverBase(InternalGPIOPin *pin)
Definition: remote_base.h:153
void set_clock_divider(uint8_t clock_divider)
Definition: remote_base.h:91
void set_parent(RemoteTransmitterBase *parent)
Definition: remote_base.h:225
bool matches(RemoteReceiveData src) override
Definition: remote_base.h:197
std::string size_t len
Definition: helpers.h:292
bool peek_item(uint32_t mark, uint32_t space, uint32_t offset=0) const
Definition: remote_base.h:57
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void advance(uint32_t amount=1)
Definition: remote_base.h:65
const RawTimings & get_data() const
Definition: remote_base.h:31
void set_tolerance(uint8_t tolerance)
Definition: remote_base.h:156
uint32_t to_microseconds_(uint32_t ticks)
Definition: remote_base.h:98
int32_t upper_bound_(uint32_t length) const
Definition: remote_base.h:70
uint32_t from_microseconds_(uint32_t us)
Definition: remote_base.h:94