ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
rc_switch_protocol.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome::remote_base {
5
6static const char *const TAG = "remote.rc_switch";
7
8const RCSwitchBase RC_SWITCH_PROTOCOLS[9] = {RCSwitchBase(0, 0, 0, 0, 0, 0, false),
9 RCSwitchBase(350, 10850, 350, 1050, 1050, 350, false),
10 RCSwitchBase(650, 6500, 650, 1300, 1300, 650, false),
11 RCSwitchBase(3000, 7100, 400, 1100, 900, 600, false),
12 RCSwitchBase(380, 2280, 380, 1140, 1140, 380, false),
13 RCSwitchBase(3000, 7000, 500, 1000, 1000, 500, false),
14 RCSwitchBase(10350, 450, 450, 900, 900, 450, true),
15 RCSwitchBase(300, 9300, 150, 900, 900, 150, false),
16 RCSwitchBase(250, 2500, 250, 1250, 250, 250, false)};
17
18RCSwitchBase::RCSwitchBase(uint32_t sync_high, uint32_t sync_low, uint32_t zero_high, uint32_t zero_low,
19 uint32_t one_high, uint32_t one_low, bool inverted)
20 : sync_high_(sync_high),
21 sync_low_(sync_low),
22 zero_high_(zero_high),
23 zero_low_(zero_low),
24 one_high_(one_high),
25 one_low_(one_low),
26 inverted_(inverted) {}
27
29 if (!this->inverted_) {
30 dst->mark(this->one_high_);
31 dst->space(this->one_low_);
32 } else {
33 dst->space(this->one_high_);
34 dst->mark(this->one_low_);
35 }
36}
38 if (!this->inverted_) {
39 dst->mark(this->zero_high_);
40 dst->space(this->zero_low_);
41 } else {
42 dst->space(this->zero_high_);
43 dst->mark(this->zero_low_);
44 }
45}
47 if (!this->inverted_) {
48 dst->mark(this->sync_high_);
49 dst->space(this->sync_low_);
50 } else {
51 dst->space(this->sync_high_);
52 dst->mark(this->sync_low_);
53 }
54}
55void RCSwitchBase::transmit(RemoteTransmitData *dst, uint64_t code, uint8_t len) const {
56 dst->set_carrier_frequency(38000);
57 this->sync(dst);
58 for (int16_t i = len - 1; i >= 0; i--) {
59 if (code & ((uint64_t) 1 << i)) {
60 this->one(dst);
61 } else {
62 this->zero(dst);
63 }
64 }
65}
66
68 if (!this->inverted_) {
69 if (!src.peek_mark(this->one_high_))
70 return false;
71 if (!src.peek_space(this->one_low_, 1))
72 return false;
73 } else {
74 if (!src.peek_space(this->one_high_))
75 return false;
76 if (!src.peek_mark(this->one_low_, 1))
77 return false;
78 }
79 src.advance(2);
80 return true;
81}
83 if (!this->inverted_) {
84 if (!src.peek_mark(this->zero_high_))
85 return false;
86 if (!src.peek_space(this->zero_low_, 1))
87 return false;
88 } else {
89 if (!src.peek_space(this->zero_high_))
90 return false;
91 if (!src.peek_mark(this->zero_low_, 1))
92 return false;
93 }
94 src.advance(2);
95 return true;
96}
98 if (!this->inverted_) {
99 if (!src.peek_mark(this->sync_high_))
100 return false;
101 if (!src.peek_space(this->sync_low_, 1))
102 return false;
103 } else {
104 // We can't peek a space at the beginning because signals starts with a low to high transition.
105 // this long space at the beginning is the separation between the transmissions itself, so it is actually
106 // added at the end kind of artificially (by the value given to "idle:" option by the user in the yaml)
107 if (!src.peek_mark(this->sync_low_))
108 return false;
109 src.advance(1);
110 return true;
111 }
112 src.advance(2);
113 return true;
114}
115bool RCSwitchBase::decode(RemoteReceiveData &src, uint64_t *out_data, uint8_t *out_nbits) const {
116 // ignore if sync doesn't exist
117 this->expect_sync(src);
118
119 *out_data = 0;
120 for (*out_nbits = 0; *out_nbits < 64; *out_nbits += 1) {
121 if (this->expect_zero(src)) {
122 *out_data <<= 1;
123 *out_data |= 0;
124 } else if (this->expect_one(src)) {
125 *out_data <<= 1;
126 *out_data |= 1;
127 } else {
128 return *out_nbits >= 8;
129 }
130 }
131 return true;
132}
133optional<RCSwitchData> RCSwitchBase::decode(RemoteReceiveData &src) const {
134 RCSwitchData out;
135 uint8_t out_nbits;
136 for (uint8_t i = 1; i <= 8; i++) {
137 src.reset();
138 const RCSwitchBase *protocol = &RC_SWITCH_PROTOCOLS[i];
139 if (protocol->decode(src, &out.code, &out_nbits) && out_nbits >= 3) {
140 out.protocol = i;
141 return out;
142 }
143 }
144 return {};
145}
146
147void RCSwitchBase::simple_code_to_tristate(uint16_t code, uint8_t nbits, uint64_t *out_code) {
148 *out_code = 0;
149 for (int8_t i = nbits - 1; i >= 0; i--) {
150 *out_code <<= 2;
151 if (code & (1 << i)) {
152 *out_code |= 0b01;
153 } else {
154 *out_code |= 0b00;
155 }
156 }
157}
158void RCSwitchBase::type_a_code(uint8_t switch_group, uint8_t switch_device, bool state, uint64_t *out_code,
159 uint8_t *out_nbits) {
160 uint16_t code = 0;
161 code = switch_group ^ 0b11111;
162 code <<= 5;
163 code |= switch_device ^ 0b11111;
164 code <<= 2;
165 code |= state ? 0b01 : 0b10;
166 simple_code_to_tristate(code, 12, out_code);
167 *out_nbits = 24;
168}
169void RCSwitchBase::type_b_code(uint8_t address_code, uint8_t channel_code, bool state, uint64_t *out_code,
170 uint8_t *out_nbits) {
171 uint16_t code = 0;
172 code |= (address_code == 1) ? 0 : 0b1000;
173 code |= (address_code == 2) ? 0 : 0b0100;
174 code |= (address_code == 3) ? 0 : 0b0010;
175 code |= (address_code == 4) ? 0 : 0b0001;
176 code <<= 4;
177 code |= (channel_code == 1) ? 0 : 0b1000;
178 code |= (channel_code == 2) ? 0 : 0b0100;
179 code |= (channel_code == 3) ? 0 : 0b0010;
180 code |= (channel_code == 4) ? 0 : 0b0001;
181 code <<= 4;
182 code |= 0b1110;
183 code |= state ? 0b1 : 0b0;
184 simple_code_to_tristate(code, 12, out_code);
185 *out_nbits = 24;
186}
187void RCSwitchBase::type_c_code(uint8_t family, uint8_t group, uint8_t device, bool state, uint64_t *out_code,
188 uint8_t *out_nbits) {
189 uint16_t code = 0;
190 code |= (family & 0b0001) ? 0b1000 : 0;
191 code |= (family & 0b0010) ? 0b0100 : 0;
192 code |= (family & 0b0100) ? 0b0010 : 0;
193 code |= (family & 0b1000) ? 0b0001 : 0;
194 code <<= 4;
195 code |= ((device - 1) & 0b01) ? 0b1000 : 0;
196 code |= ((device - 1) & 0b10) ? 0b0100 : 0;
197 code |= ((group - 1) & 0b01) ? 0b0010 : 0;
198 code |= ((group - 1) & 0b10) ? 0b0001 : 0;
199 code <<= 4;
200 code |= 0b0110;
201 code |= state ? 0b1 : 0b0;
202 simple_code_to_tristate(code, 12, out_code);
203 *out_nbits = 24;
204}
205void RCSwitchBase::type_d_code(uint8_t group, uint8_t device, bool state, uint64_t *out_code, uint8_t *out_nbits) {
206 *out_code = 0;
207 *out_code |= (group == 0) ? 0b11000000 : 0b01000000;
208 *out_code |= (group == 1) ? 0b00110000 : 0b00010000;
209 *out_code |= (group == 2) ? 0b00001100 : 0b00000100;
210 *out_code |= (group == 3) ? 0b00000011 : 0b00000001;
211 *out_code <<= 6;
212 *out_code |= (device == 1) ? 0b110000 : 0b010000;
213 *out_code |= (device == 2) ? 0b001100 : 0b000100;
214 *out_code |= (device == 3) ? 0b000011 : 0b000001;
215 *out_code <<= 6;
216 *out_code |= 0b000000;
217 *out_code <<= 4;
218 *out_code |= state ? 0b1100 : 0b0011;
219 *out_nbits = 24;
220}
221
222uint64_t decode_binary_string(const std::string &data) {
223 uint64_t ret = 0;
224 for (char c : data) {
225 ret <<= 1UL;
226 ret |= (c != '0');
227 }
228 return ret;
229}
230
231uint64_t decode_binary_string_mask(const std::string &data) {
232 uint64_t ret = 0;
233 for (char c : data) {
234 ret <<= 1UL;
235 ret |= (c != 'x');
236 }
237 return ret;
238}
239
241 uint64_t decoded_code;
242 uint8_t decoded_nbits;
243 if (!this->protocol_.decode(src, &decoded_code, &decoded_nbits))
244 return false;
245
246 return decoded_nbits == this->nbits_ && (decoded_code & this->mask_) == (this->code_ & this->mask_);
247}
249 for (uint8_t i = 1; i <= 8; i++) {
250 src.reset();
251 uint64_t out_data;
252 uint8_t out_nbits;
253 const RCSwitchBase *protocol = &RC_SWITCH_PROTOCOLS[i];
254 if (protocol->decode(src, &out_data, &out_nbits) && out_nbits >= 3) {
255 char buffer[65];
256 for (uint8_t j = 0; j < out_nbits; j++)
257 buffer[j] = (out_data & ((uint64_t) 1 << (out_nbits - j - 1))) ? '1' : '0';
258
259 buffer[out_nbits] = '\0';
260 ESP_LOGI(TAG, "Received RCSwitch Raw: protocol=%u data='%s'", i, buffer);
261
262 // only send first decoded protocol
263 return true;
264 }
265 }
266 return false;
267}
268
269} // namespace esphome::remote_base
void zero(RemoteTransmitData *dst) const
static void type_b_code(uint8_t address_code, uint8_t channel_code, bool state, uint64_t *out_code, uint8_t *out_nbits)
bool expect_one(RemoteReceiveData &src) const
bool expect_zero(RemoteReceiveData &src) const
bool decode(RemoteReceiveData &src, uint64_t *out_data, uint8_t *out_nbits) const
static void type_c_code(uint8_t family, uint8_t group, uint8_t device, bool state, uint64_t *out_code, uint8_t *out_nbits)
void transmit(RemoteTransmitData *dst, uint64_t code, uint8_t len) const
static void type_a_code(uint8_t switch_group, uint8_t switch_device, bool state, uint64_t *out_code, uint8_t *out_nbits)
void one(RemoteTransmitData *dst) const
void sync(RemoteTransmitData *dst) const
bool expect_sync(RemoteReceiveData &src) const
static void type_d_code(uint8_t group, uint8_t device, bool state, uint64_t *out_code, uint8_t *out_nbits)
static void simple_code_to_tristate(uint16_t code, uint8_t nbits, uint64_t *out_code)
bool dump(RemoteReceiveData src) override
bool matches(RemoteReceiveData src) override
bool peek_mark(uint32_t length, uint32_t offset=0) const
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:29
bool state
Definition fan.h:2
uint64_t decode_binary_string_mask(const std::string &data)
uint64_t decode_binary_string(const std::string &data)
const RCSwitchBase RC_SWITCH_PROTOCOLS[9]
const void size_t len
Definition hal.h:64
const void * src
Definition hal.h:64
static void uint32_t
uint16_t sync
Definition sun_gtil2.cpp:0