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