ESPHome  2024.10.2
rf_bridge.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 
9 
10 namespace esphome {
11 namespace rf_bridge {
12 
13 static const uint8_t RF_MESSAGE_SIZE = 9;
14 static const uint8_t RF_CODE_START = 0xAA;
15 static const uint8_t RF_CODE_ACK = 0xA0;
16 static const uint8_t RF_CODE_LEARN = 0xA1;
17 static const uint8_t RF_CODE_LEARN_KO = 0xA2;
18 static const uint8_t RF_CODE_LEARN_OK = 0xA3;
19 static const uint8_t RF_CODE_RFIN = 0xA4;
20 static const uint8_t RF_CODE_RFOUT = 0xA5;
21 static const uint8_t RF_CODE_ADVANCED_RFIN = 0xA6;
22 static const uint8_t RF_CODE_SNIFFING_ON = 0xA6;
23 static const uint8_t RF_CODE_SNIFFING_OFF = 0xA7;
24 static const uint8_t RF_CODE_RFOUT_NEW = 0xA8;
25 static const uint8_t RF_CODE_LEARN_NEW = 0xA9;
26 static const uint8_t RF_CODE_LEARN_KO_NEW = 0xAA;
27 static const uint8_t RF_CODE_LEARN_OK_NEW = 0xAB;
28 static const uint8_t RF_CODE_RFOUT_BUCKET = 0xB0;
29 static const uint8_t RF_CODE_RFIN_BUCKET = 0xB1;
30 static const uint8_t RF_CODE_BEEP = 0xC0;
31 static const uint8_t RF_CODE_STOP = 0x55;
32 static const uint8_t RF_DEBOUNCE = 200;
33 
34 struct RFBridgeData {
35  uint16_t sync;
36  uint16_t low;
37  uint16_t high;
38  uint32_t code;
39 };
40 
42  uint8_t length;
43  uint8_t protocol;
44  std::string code;
45 };
46 
48  public:
49  void loop() override;
50  void dump_config() override;
51  void add_on_code_received_callback(std::function<void(RFBridgeData)> callback) {
52  this->data_callback_.add(std::move(callback));
53  }
54  void add_on_advanced_code_received_callback(std::function<void(RFBridgeAdvancedData)> callback) {
55  this->advanced_data_callback_.add(std::move(callback));
56  }
57  void send_code(RFBridgeData data);
58  void send_advanced_code(const RFBridgeAdvancedData &data);
59  void learn();
60  void start_advanced_sniffing();
61  void stop_advanced_sniffing();
62  void start_bucket_sniffing();
63  void send_raw(const std::string &code);
64  void beep(uint16_t ms);
65 
66  protected:
67  void ack_();
68  void decode_();
69  bool parse_bridge_byte_(uint8_t byte);
70  void write_byte_str_(const std::string &codes);
71 
72  std::vector<uint8_t> rx_buffer_;
73  uint32_t last_bridge_byte_{0};
74 
77 };
78 
79 class RFBridgeReceivedCodeTrigger : public Trigger<RFBridgeData> {
80  public:
82  parent->add_on_code_received_callback([this](RFBridgeData data) { this->trigger(data); });
83  }
84 };
85 
86 class RFBridgeReceivedAdvancedCodeTrigger : public Trigger<RFBridgeAdvancedData> {
87  public:
89  parent->add_on_advanced_code_received_callback([this](const RFBridgeAdvancedData &data) { this->trigger(data); });
90  }
91 };
92 
93 template<typename... Ts> class RFBridgeSendCodeAction : public Action<Ts...> {
94  public:
95  RFBridgeSendCodeAction(RFBridgeComponent *parent) : parent_(parent) {}
96  TEMPLATABLE_VALUE(uint16_t, sync)
97  TEMPLATABLE_VALUE(uint16_t, low)
98  TEMPLATABLE_VALUE(uint16_t, high)
99  TEMPLATABLE_VALUE(uint32_t, code)
100 
101  void play(Ts... x) {
102  RFBridgeData data{};
103  data.sync = this->sync_.value(x...);
104  data.low = this->low_.value(x...);
105  data.high = this->high_.value(x...);
106  data.code = this->code_.value(x...);
107  this->parent_->send_code(data);
108  }
109 
110  protected:
112 };
113 
114 template<typename... Ts> class RFBridgeSendAdvancedCodeAction : public Action<Ts...> {
115  public:
116  RFBridgeSendAdvancedCodeAction(RFBridgeComponent *parent) : parent_(parent) {}
117  TEMPLATABLE_VALUE(uint8_t, length)
118  TEMPLATABLE_VALUE(uint8_t, protocol)
119  TEMPLATABLE_VALUE(std::string, code)
120 
121  void play(Ts... x) {
122  RFBridgeAdvancedData data{};
123  data.length = this->length_.value(x...);
124  data.protocol = this->protocol_.value(x...);
125  data.code = this->code_.value(x...);
126  this->parent_->send_advanced_code(data);
127  }
128 
129  protected:
131 };
132 
133 template<typename... Ts> class RFBridgeLearnAction : public Action<Ts...> {
134  public:
135  RFBridgeLearnAction(RFBridgeComponent *parent) : parent_(parent) {}
136 
137  void play(Ts... x) { this->parent_->learn(); }
138 
139  protected:
141 };
142 
143 template<typename... Ts> class RFBridgeStartAdvancedSniffingAction : public Action<Ts...> {
144  public:
146 
147  void play(Ts... x) { this->parent_->start_advanced_sniffing(); }
148 
149  protected:
151 };
152 
153 template<typename... Ts> class RFBridgeStopAdvancedSniffingAction : public Action<Ts...> {
154  public:
156 
157  void play(Ts... x) { this->parent_->stop_advanced_sniffing(); }
158 
159  protected:
161 };
162 
163 template<typename... Ts> class RFBridgeStartBucketSniffingAction : public Action<Ts...> {
164  public:
166 
167  void play(Ts... x) { this->parent_->start_bucket_sniffing(); }
168 
169  protected:
171 };
172 
173 template<typename... Ts> class RFBridgeSendRawAction : public Action<Ts...> {
174  public:
175  RFBridgeSendRawAction(RFBridgeComponent *parent) : parent_(parent) {}
176  TEMPLATABLE_VALUE(std::string, raw)
177 
178  void play(Ts... x) { this->parent_->send_raw(this->raw_.value(x...)); }
179 
180  protected:
182 };
183 
184 template<typename... Ts> class RFBridgeBeepAction : public Action<Ts...> {
185  public:
186  RFBridgeBeepAction(RFBridgeComponent *parent) : parent_(parent) {}
187  TEMPLATABLE_VALUE(uint16_t, duration)
188 
189  void play(Ts... x) { this->parent_->beep(this->duration_.value(x...)); }
190 
191  protected:
193 };
194 
195 } // namespace rf_bridge
196 } // namespace esphome
void loop()
CallbackManager< void(RFBridgeData)> data_callback_
Definition: rf_bridge.h:75
uint8_t raw[35]
Definition: bl0939.h:19
CallbackManager< void(RFBridgeAdvancedData)> advanced_data_callback_
Definition: rf_bridge.h:76
RFBridgeSendAdvancedCodeAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:116
uint16_t x
Definition: tt21100.cpp:17
void add_on_advanced_code_received_callback(std::function< void(RFBridgeAdvancedData)> callback)
Definition: rf_bridge.h:54
STL namespace.
TEMPLATABLE_VALUE(std::string, raw) void play(Ts... x)
Definition: rf_bridge.h:176
std::vector< uint8_t > rx_buffer_
Definition: rf_bridge.h:72
RFBridgeBeepAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:186
RFBridgeStartAdvancedSniffingAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:145
RFBridgeReceivedCodeTrigger(RFBridgeComponent *parent)
Definition: rf_bridge.h:81
TEMPLATABLE_VALUE(uint16_t, duration) void play(Ts... x)
Definition: rf_bridge.h:187
RFBridgeStartBucketSniffingAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:165
RFBridgeReceivedAdvancedCodeTrigger(RFBridgeComponent *parent)
Definition: rf_bridge.h:88
RFBridgeSendCodeAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:95
RFBridgeStopAdvancedSniffingAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:155
uint16_t length
Definition: tt21100.cpp:12
void add_on_code_received_callback(std::function< void(RFBridgeData)> callback)
Definition: rf_bridge.h:51
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
RFBridgeLearnAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:135
RFBridgeSendRawAction(RFBridgeComponent *parent)
Definition: rf_bridge.h:175