ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
rf_bridge.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4#include <vector>
5
9
10namespace esphome::rf_bridge {
11
12static const uint8_t RF_MESSAGE_SIZE = 9;
13static const uint8_t RF_CODE_START = 0xAA;
14static const uint8_t RF_CODE_ACK = 0xA0;
15static const uint8_t RF_CODE_LEARN = 0xA1;
16static const uint8_t RF_CODE_LEARN_KO = 0xA2;
17static const uint8_t RF_CODE_LEARN_OK = 0xA3;
18static const uint8_t RF_CODE_RFIN = 0xA4;
19static const uint8_t RF_CODE_RFOUT = 0xA5;
20static const uint8_t RF_CODE_ADVANCED_RFIN = 0xA6;
21static const uint8_t RF_CODE_SNIFFING_ON = 0xA6;
22static const uint8_t RF_CODE_SNIFFING_OFF = 0xA7;
23static const uint8_t RF_CODE_RFOUT_NEW = 0xA8;
24static const uint8_t RF_CODE_LEARN_NEW = 0xA9;
25static const uint8_t RF_CODE_LEARN_KO_NEW = 0xAA;
26static const uint8_t RF_CODE_LEARN_OK_NEW = 0xAB;
27static const uint8_t RF_CODE_RFOUT_BUCKET = 0xB0;
28static const uint8_t RF_CODE_RFIN_BUCKET = 0xB1;
29static const uint8_t RF_CODE_BEEP = 0xC0;
30static const uint8_t RF_CODE_STOP = 0x55;
31static const uint8_t RF_DEBOUNCE = 200;
32static const size_t MAX_RX_BUFFER_SIZE = 512;
33// ~10 byte times at 19200 baud: long enough to prove the UART went quiet
34// after a possible bucket-frame terminator, short enough to finish well
35// before the next radio capture can be delivered.
36static const uint32_t BUCKET_CANDIDATE_QUIET_MS = 5;
37// Portisch drains a B1 frame's header, bucket table, and pulse data as
38// separate UART writes, so an in-progress bucket frame tolerates a longer
39// inter-region gap than the generic 50 ms inter-byte timeout.
40static const uint32_t BUCKET_FRAME_TIMEOUT_MS = 250;
41// Portisch's uart_put_RF_buckets sends at most 7 buckets plus the sync
42// bucket, so a B1 count byte above 8 (or 0) is malformed for any protocol.
43static const uint8_t B1_MAX_BUCKET_COUNT = 8;
44
46 uint16_t sync;
47 uint16_t low;
48 uint16_t high;
50};
51
53 uint8_t length;
54 uint8_t protocol;
55 std::string code;
56};
57
58class RFBridgeComponent final : public uart::UARTDevice, public Component {
59 public:
60 void loop() override;
61 void dump_config() override;
62 template<typename F> void add_on_code_received_callback(F &&callback) {
63 this->data_callback_.add(std::forward<F>(callback));
64 }
65 template<typename F> void add_on_advanced_code_received_callback(F &&callback) {
66 this->advanced_data_callback_.add(std::forward<F>(callback));
67 }
68 void send_code(RFBridgeData data);
70 void learn();
74 void send_raw(const std::string &code);
75 void beep(uint16_t ms);
76
77 protected:
78 void ack_();
79 void decode_();
80 bool parse_bridge_byte_(uint8_t byte);
82 void write_byte_str_(const std::string &codes);
83
84 std::vector<uint8_t> rx_buffer_;
87
90};
91
92template<typename... Ts> class RFBridgeSendCodeAction final : public Action<Ts...> {
93 public:
96 TEMPLATABLE_VALUE(uint16_t, low)
97 TEMPLATABLE_VALUE(uint16_t, high)
99
100 void play(const Ts &...x) {
101 RFBridgeData data{};
102 data.sync = this->sync_.value(x...);
103 data.low = this->low_.value(x...);
104 data.high = this->high_.value(x...);
105 data.code = this->code_.value(x...);
106 this->parent_->send_code(data);
107 }
108
109 protected:
111};
112
113template<typename... Ts> class RFBridgeSendAdvancedCodeAction final : public Action<Ts...> {
114 public:
117 TEMPLATABLE_VALUE(uint8_t, protocol)
118 TEMPLATABLE_VALUE(std::string, code)
119
120 void play(const Ts &...x) {
122 data.length = this->length_.value(x...);
123 data.protocol = this->protocol_.value(x...);
124 data.code = this->code_.value(x...);
125 this->parent_->send_advanced_code(data);
126 }
127
128 protected:
130};
131
132template<typename... Ts> class RFBridgeLearnAction final : public Action<Ts...> {
133 public:
135
136 void play(const Ts &...x) { this->parent_->learn(); }
137
138 protected:
140};
141
142template<typename... Ts> class RFBridgeStartAdvancedSniffingAction final : public Action<Ts...> {
143 public:
145
146 void play(const Ts &...x) { this->parent_->start_advanced_sniffing(); }
147
148 protected:
150};
151
152template<typename... Ts> class RFBridgeStopAdvancedSniffingAction final : public Action<Ts...> {
153 public:
155
156 void play(const Ts &...x) { this->parent_->stop_advanced_sniffing(); }
157
158 protected:
160};
161
162template<typename... Ts> class RFBridgeStartBucketSniffingAction final : public Action<Ts...> {
163 public:
165
166 void play(const Ts &...x) { this->parent_->start_bucket_sniffing(); }
167
168 protected:
170};
171
172template<typename... Ts> class RFBridgeSendRawAction final : public Action<Ts...> {
173 public:
175 TEMPLATABLE_VALUE(std::string, raw)
176
177 void play(const Ts &...x) { this->parent_->send_raw(this->raw_.value(x...)); }
178
179 protected:
181};
182
183template<typename... Ts> class RFBridgeBeepAction final : public Action<Ts...> {
184 public:
187
188 void play(const Ts &...x) { this->parent_->beep(this->duration_.value(x...)); }
189
190 protected:
192};
193
194} // namespace esphome::rf_bridge
uint8_t raw[35]
Definition bl0939.h:0
virtual void play(const Ts &...x)=0
RFBridgeBeepAction(RFBridgeComponent *parent)
Definition rf_bridge.h:185
TEMPLATABLE_VALUE(uint16_t, duration) void play(const Ts &...x)
Definition rf_bridge.h:186
void send_raw(const std::string &code)
CallbackManager< void(RFBridgeAdvancedData)> advanced_data_callback_
Definition rf_bridge.h:89
void send_advanced_code(const RFBridgeAdvancedData &data)
CallbackManager< void(RFBridgeData)> data_callback_
Definition rf_bridge.h:88
void write_byte_str_(const std::string &codes)
void add_on_advanced_code_received_callback(F &&callback)
Definition rf_bridge.h:65
void send_code(RFBridgeData data)
std::vector< uint8_t > rx_buffer_
Definition rf_bridge.h:84
void add_on_code_received_callback(F &&callback)
Definition rf_bridge.h:62
RFBridgeLearnAction(RFBridgeComponent *parent)
Definition rf_bridge.h:134
RFBridgeSendAdvancedCodeAction(RFBridgeComponent *parent)
Definition rf_bridge.h:115
TEMPLATABLE_VALUE(uint8_t, length) TEMPLATABLE_VALUE(uint8_t
low code void play(const Ts &...x)
Definition rf_bridge.h:100
TEMPLATABLE_VALUE(uint16_t, sync) TEMPLATABLE_VALUE(uint16_t
RFBridgeSendCodeAction(RFBridgeComponent *parent)
Definition rf_bridge.h:94
TEMPLATABLE_VALUE(std::string, raw) void play(const Ts &...x)
Definition rf_bridge.h:175
RFBridgeSendRawAction(RFBridgeComponent *parent)
Definition rf_bridge.h:174
RFBridgeStartAdvancedSniffingAction(RFBridgeComponent *parent)
Definition rf_bridge.h:144
RFBridgeStartBucketSniffingAction(RFBridgeComponent *parent)
Definition rf_bridge.h:164
RFBridgeStopAdvancedSniffingAction(RFBridgeComponent *parent)
Definition rf_bridge.h:154
uint8_t duration
Definition msa3xx.h:0
STL namespace.
static void uint32_t
uint16_t sync
Definition sun_gtil2.cpp:0
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5