ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
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
11namespace esphome::remote_base {
12
17
18using 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); }
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; }
31 const RawTimings &get_data() const { return this->data_; }
32 void set_data(const RawTimings &data) { this->data_ = data; }
37 void set_data_from_packed_sint32(const uint8_t *data, size_t len, size_t count);
42 bool set_data_from_base64url(const std::string &base64url);
43 void reset() {
44 this->data_.clear();
45 this->carrier_frequency_ = 0;
46 }
47
48 protected:
51};
52
54 public:
55 explicit RemoteReceiveData(const RawTimings &data, uint32_t tolerance, ToleranceMode tolerance_mode)
56 : data_(data), index_(0), tolerance_(tolerance), tolerance_mode_(tolerance_mode) {}
57
58 const RawTimings &get_raw_data() const { return this->data_; }
59 uint32_t get_index() const { return index_; }
60 int32_t operator[](uint32_t index) const { return this->data_[index]; }
61 int32_t size() const { return this->data_.size(); }
62 bool is_valid(uint32_t offset = 0) const { return this->index_ + offset < this->data_.size(); }
63 int32_t peek(uint32_t offset = 0) const { return this->data_[this->index_ + offset]; }
64 bool peek_mark(uint32_t length, uint32_t offset = 0) const;
65 bool peek_mark_at_least(uint32_t length, uint32_t offset = 0) const;
66 bool peek_mark_at_most(uint32_t length, uint32_t offset = 0) const;
67 bool peek_space(uint32_t length, uint32_t offset = 0) const;
68 bool peek_space_at_least(uint32_t length, uint32_t offset = 0) const;
69 bool peek_space_at_most(uint32_t length, uint32_t offset = 0) const;
70 bool peek_item(uint32_t mark, uint32_t space, uint32_t offset = 0) const {
71 return this->peek_space(space, offset + 1) && this->peek_mark(mark, offset);
72 }
73
76 bool expect_item(uint32_t mark, uint32_t space);
77 bool expect_pulse_with_gap(uint32_t mark, uint32_t space);
78 void advance(uint32_t amount = 1) { this->index_ += amount; }
79 void reset() { this->index_ = 0; }
80
81 void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode) {
82 this->tolerance_ = tolerance;
83 this->tolerance_mode_ = tolerance_mode;
84 }
87
88 protected:
89 int32_t lower_bound_(uint32_t length) const {
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 int32_t upper_bound_(uint32_t length) const {
99 return int32_t(length + this->tolerance_);
100 } else if (this->tolerance_mode_ == TOLERANCE_MODE_PERCENTAGE) {
101 return int32_t(100 + this->tolerance_) * length / 100U;
102 }
103 return 0;
104 }
105
110};
111
113 public:
114 explicit RemoteComponentBase(InternalGPIOPin *pin) : pin_(pin){};
115
116 protected:
118};
119
120#ifdef USE_ESP32
121#include <soc/soc_caps.h>
122#if SOC_RMT_SUPPORTED
124 public:
125 void set_clock_resolution(uint32_t clock_resolution) { this->clock_resolution_ = clock_resolution; }
126 void set_rmt_symbols(uint32_t rmt_symbols) { this->rmt_symbols_ = rmt_symbols; }
127
128 protected:
130 const uint32_t ticks_per_ten_us = this->clock_resolution_ / 100000u;
131 return us * ticks_per_ten_us / 10;
132 }
134 const uint32_t ticks_per_ten_us = this->clock_resolution_ / 100000u;
135 return (ticks * 10) / ticks_per_ten_us;
136 }
140};
141#endif // SOC_RMT_SUPPORTED
142#endif // USE_ESP32
143
145 public:
148 public:
149 explicit TransmitCall(RemoteTransmitterBase *parent) : parent_(parent) {}
151 void set_send_times(uint32_t send_times) { send_times_ = send_times; }
152 void set_send_wait(uint32_t send_wait) { send_wait_ = send_wait; }
153 void perform() { this->parent_->send_(this->send_times_, this->send_wait_); }
154
155 protected:
159 };
160
162 this->temp_.reset();
163 return TransmitCall(this);
164 }
165 template<typename Protocol>
166 void transmit(const Protocol::ProtocolData &data, uint32_t send_times = 1, uint32_t send_wait = 0) {
167 auto call = this->transmit();
168 Protocol().encode(call.get_data(), data);
169 call.set_send_times(send_times);
170 call.set_send_wait(send_wait);
171 call.perform();
172 }
173
174 protected:
175 void send_(uint32_t send_times, uint32_t send_wait);
176 virtual void send_internal(uint32_t send_times, uint32_t send_wait) = 0;
177 void send_single_() { this->send_(1, 0); }
178
181};
182
184 public:
185 virtual bool on_receive(RemoteReceiveData data) = 0;
186};
187
189 public:
190 virtual bool dump(RemoteReceiveData src) = 0;
191 virtual bool is_secondary() { return false; }
192};
193
195 public:
197 void register_listener(RemoteReceiverListener *listener) { this->listeners_.push_back(listener); }
199 void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode) {
200 this->tolerance_ = tolerance;
201 this->tolerance_mode_ = tolerance_mode;
202 }
203
204 protected:
205 void call_listeners_();
206 void call_dumpers_();
208 this->call_listeners_();
209 this->call_dumpers_();
210 }
211
212 std::vector<RemoteReceiverListener *> listeners_;
213 std::vector<RemoteReceiverDumperBase *> dumpers_;
214 std::vector<RemoteReceiverDumperBase *> secondary_dumpers_;
218};
219
221 public Component,
223 public:
225 void dump_config() override;
226 virtual bool matches(RemoteReceiveData src) = 0;
227 bool on_receive(RemoteReceiveData src) override;
228};
229
230/* TEMPLATES */
231
232template<typename T> class RemoteProtocol {
233 public:
234 using ProtocolData = T;
235 virtual void encode(RemoteTransmitData *dst, const ProtocolData &data) = 0;
236 virtual optional<ProtocolData> decode(RemoteReceiveData src) = 0;
237 virtual void dump(const ProtocolData &data) = 0;
238};
239
241 public:
243
244 protected:
246 auto proto = T();
247 auto res = proto.decode(src);
248 return res.has_value() && *res == this->data_;
249 }
250
251 public:
252 void set_data(T::ProtocolData data) { data_ = data; }
253
254 protected:
255 T::ProtocolData data_;
256};
257
258template<typename T>
259class RemoteReceiverTrigger : public Trigger<typename T::ProtocolData>, public RemoteReceiverListener {
260 protected:
262 auto proto = T();
263 auto res = proto.decode(src);
264 if (res.has_value()) {
265 this->trigger(*res);
266 return true;
267 }
268 return false;
269 }
270};
271
273 public:
276 void set_transmitter(RemoteTransmitterBase *transmitter) { this->transmitter_ = transmitter; }
277
278 protected:
279 template<typename Protocol>
280 void transmit_(const Protocol::ProtocolData &data, uint32_t send_times = 1, uint32_t send_wait = 0) {
281 this->transmitter_->transmit<Protocol>(data, send_times, send_wait);
282 }
284};
285
286template<typename... Ts> class RemoteTransmitterActionBase : public RemoteTransmittable, public Action<Ts...> {
287 TEMPLATABLE_VALUE(uint32_t, send_times)
288 TEMPLATABLE_VALUE(uint32_t, send_wait)
289
290 protected:
291 void play(const Ts &...x) override {
292 auto call = this->transmitter_->transmit();
293 this->encode(call.get_data(), x...);
294 call.set_send_times(this->send_times_.value_or(x..., 1));
295 call.set_send_wait(this->send_wait_.value_or(x..., 0));
296 call.perform();
297 }
298 virtual void encode(RemoteTransmitData *dst, Ts... x) = 0;
299};
300
301template<typename T> class RemoteReceiverDumper : public RemoteReceiverDumperBase {
302 public:
303 bool dump(RemoteReceiveData src) override {
304 auto proto = T();
305 auto decoded = proto.decode(src);
306 if (!decoded.has_value())
307 return false;
308 proto.dump(*decoded);
309 return true;
310 }
311};
312
313#define DECLARE_REMOTE_PROTOCOL_(prefix) \
314 using prefix##BinarySensor = RemoteReceiverBinarySensor<prefix##Protocol>; \
315 using prefix##Trigger = RemoteReceiverTrigger<prefix##Protocol>; \
316 using prefix##Dumper = RemoteReceiverDumper<prefix##Protocol>;
317#define DECLARE_REMOTE_PROTOCOL(prefix) DECLARE_REMOTE_PROTOCOL_(prefix)
318
319} // namespace esphome::remote_base
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:461
virtual void dump(const ProtocolData &data)=0
virtual optional< ProtocolData > decode(RemoteReceiveData src)=0
virtual void encode(RemoteTransmitData *dst, const ProtocolData &data)=0
void set_rmt_symbols(uint32_t rmt_symbols)
uint32_t to_microseconds_(uint32_t ticks)
uint32_t from_microseconds_(uint32_t us)
void set_clock_resolution(uint32_t clock_resolution)
int32_t operator[](uint32_t index) const
Definition remote_base.h:60
bool peek_mark_at_most(uint32_t length, uint32_t offset=0) const
bool expect_item(uint32_t mark, uint32_t space)
bool peek_space(uint32_t length, uint32_t offset=0) const
bool peek_space_at_most(uint32_t length, uint32_t offset=0) const
int32_t peek(uint32_t offset=0) const
Definition remote_base.h:63
bool peek_item(uint32_t mark, uint32_t space, uint32_t offset=0) const
Definition remote_base.h:70
int32_t upper_bound_(uint32_t length) const
Definition remote_base.h:97
bool is_valid(uint32_t offset=0) const
Definition remote_base.h:62
const RawTimings & get_raw_data() const
Definition remote_base.h:58
RemoteReceiveData(const RawTimings &data, uint32_t tolerance, ToleranceMode tolerance_mode)
Definition remote_base.h:55
void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode)
Definition remote_base.h:81
bool peek_space_at_least(uint32_t length, uint32_t offset=0) const
int32_t lower_bound_(uint32_t length) const
Definition remote_base.h:89
bool peek_mark(uint32_t length, uint32_t offset=0) const
bool expect_pulse_with_gap(uint32_t mark, uint32_t space)
bool peek_mark_at_least(uint32_t length, uint32_t offset=0) const
std::vector< RemoteReceiverDumperBase * > dumpers_
std::vector< RemoteReceiverListener * > listeners_
void register_dumper(RemoteReceiverDumperBase *dumper)
std::vector< RemoteReceiverDumperBase * > secondary_dumpers_
void set_tolerance(uint32_t tolerance, ToleranceMode tolerance_mode)
void register_listener(RemoteReceiverListener *listener)
virtual bool matches(RemoteReceiveData src)=0
bool on_receive(RemoteReceiveData src) override
bool matches(RemoteReceiveData src) override
virtual bool dump(RemoteReceiveData src)=0
bool dump(RemoteReceiveData src) override
virtual bool on_receive(RemoteReceiveData data)=0
bool on_receive(RemoteReceiveData src) override
bool set_data_from_base64url(const std::string &base64url)
Set data from base64url-encoded little-endian int32 values Base64url is URL-safe: uses '-' instead of...
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:29
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:24
const RawTimings & get_data() const
Definition remote_base.h:31
void set_data(const RawTimings &data)
Definition remote_base.h:32
void set_data_from_packed_sint32(const uint8_t *data, size_t len, size_t count)
Set data from packed protobuf sint32 buffer (zigzag + varint encoded)
void transmit_(const Protocol::ProtocolData &data, uint32_t send_times=1, uint32_t send_wait=0)
void set_transmitter(RemoteTransmitterBase *transmitter)
RemoteTransmittable(RemoteTransmitterBase *transmitter)
virtual void encode(RemoteTransmitData *dst, Ts... x)=0
void send_(uint32_t send_times, uint32_t send_wait)
void transmit(const Protocol::ProtocolData &data, uint32_t send_times=1, uint32_t send_wait=0)
virtual void send_internal(uint32_t send_times, uint32_t send_wait)=0
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
std::vector< int32_t > RawTimings
Definition remote_base.h:18
const void size_t len
Definition hal.h:64
const void * src
Definition hal.h:64
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5