ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
rc5_protocol.cpp
Go to the documentation of this file.
1#include "rc5_protocol.h"
2#include "esphome/core/log.h"
3
4namespace esphome::remote_base {
5
6static const char *const TAG = "remote.rc5";
7
8static constexpr uint32_t BIT_TIME_US = 889;
9static constexpr uint8_t NBITS = 14;
10static constexpr uint8_t NHALFBITS = NBITS * 2;
11
13 static bool toggle = false;
14 dst->set_carrier_frequency(36000);
15
16 uint64_t out_data = 0;
17 uint8_t command = data.command;
18 if (data.command >= 64) {
19 out_data |= 0b10 << 12;
20 command = command - 64;
21 } else {
22 out_data |= 0b11 << 12;
23 }
24 out_data |= toggle << 11;
25 out_data |= data.address << 6;
26 out_data |= command;
27
28 for (uint64_t mask = 1UL << (NBITS - 1); mask != 0; mask >>= 1) {
29 if (out_data & mask) {
30 dst->space(BIT_TIME_US);
31 dst->mark(BIT_TIME_US);
32 } else {
33 dst->mark(BIT_TIME_US);
34 dst->space(BIT_TIME_US);
35 }
36 }
37 toggle = !toggle;
38}
39
41 // Expand the runs into half-bit levels (true = mark). Each run is exactly one
42 // half-bit (BIT_TIME_US) or two (2 * BIT_TIME_US); stop at anything else.
43 //
44 // halfbits[0] is reserved for the leading half-bit, which is always dropped --
45 // S1 is 1, so its first half sits at the idle level (at either polarity) and
46 // merges into the pre-frame idle. Captured half-bits start at index 1.
47 bool halfbits[NHALFBITS + 2];
48 uint8_t n = 1;
49 for (uint32_t i = 0; n <= NHALFBITS && src.is_valid(i); i++) {
50 if (src.peek_mark(BIT_TIME_US, i)) {
51 halfbits[n++] = true;
52 } else if (src.peek_space(BIT_TIME_US, i)) {
53 halfbits[n++] = false;
54 } else if (src.peek_mark(2 * BIT_TIME_US, i)) {
55 halfbits[n++] = true;
56 halfbits[n++] = true;
57 } else if (src.peek_space(2 * BIT_TIME_US, i)) {
58 halfbits[n++] = false;
59 halfbits[n++] = false;
60 } else {
61 break;
62 }
63 }
64
65 // Expect a full frame once the leading half is restored: 27 captured halves
66 // (n == 28) or 26 when the final bit also ends on idle and its trailing half
67 // is dropped too (n == 27). A dropped edge half is the inverse of its partner
68 // (a Manchester bit always transitions mid-bit), so reconstruct the leading
69 // half (always) and the trailing half (only when it was dropped).
70 if (n != NHALFBITS && n != NHALFBITS - 1) {
71 return {};
72 }
73 halfbits[0] = !halfbits[1];
74 if (n == NHALFBITS - 1) {
75 halfbits[n] = !halfbits[n - 1];
76 }
77
78 const bool carrier = halfbits[1];
79 uint16_t bits = 0;
80 for (uint8_t i = 0; i < NBITS; i++) {
81 const bool first = halfbits[2 * i];
82 const bool second = halfbits[2 * i + 1];
83 if (first == second) {
84 return {}; // no midpoint transition -> not a valid Manchester bit
85 }
86 bits = (bits << 1) | (second == carrier ? 1 : 0);
87 }
88
89 const bool field_bit = bits & (1 << 12); // S2: the inverted 7th command bit
90 return RC5Data{
91 .address = static_cast<uint8_t>((bits >> 6) & 0x1F),
92 .command = static_cast<uint8_t>((bits & 0x3F) | (field_bit ? 0 : 0x40)),
93 };
94}
95
96void RC5Protocol::dump(const RC5Data &data) {
97 ESP_LOGI(TAG, "Received RC5: address=0x%02X, command=0x%02X", data.address, data.command);
98}
99
100} // namespace esphome::remote_base
optional< RC5Data > decode(RemoteReceiveData src) override
void dump(const RC5Data &data) override
void encode(RemoteTransmitData *dst, const RC5Data &data) override
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:29
uint8_t second
const void * src
Definition hal.h:64
static void uint32_t