ESPHome 2026.1.5
Loading...
Searching...
No Matches
abbwelcome_protocol.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace remote_base {
6
7static const char *const TAG = "remote.abbwelcome";
8
9static const uint32_t BIT_ONE_SPACE_US = 102;
10static const uint32_t BIT_ZERO_MARK_US = 32; // 18-44
11static const uint32_t BIT_ZERO_SPACE_US = BIT_ONE_SPACE_US - BIT_ZERO_MARK_US;
12static const uint16_t BYTE_SPACE_US = 210;
13
14uint8_t ABBWelcomeData::calc_cs_() const {
15 uint8_t checksum = 0;
16 for (uint8_t i = 0; i < this->size() - 1; i++) {
17 uint16_t temp = checksum ^ (this->data_[i]);
18 temp = temp ^ (uint16_t) (((uint32_t) temp << 0x11) >> 0x10) ^ (uint16_t) (((uint32_t) temp << 0x12) >> 0x10) ^
19 (uint16_t) (((uint32_t) temp << 0x13) >> 0x10) ^ (uint16_t) (((uint32_t) temp << 0x14) >> 0x10) ^
20 (uint16_t) (((uint32_t) temp << 0x15) >> 0x10) ^ (uint16_t) (((uint32_t) temp << 0x16) >> 0x10) ^
21 (uint16_t) (((uint32_t) temp << 0x17) >> 0x10);
22 checksum = (temp & 0xfe) ^ ((temp >> 8) & 1);
23 }
24 return ~checksum;
25}
26
28 // space = bus high, mark = activate bus pulldown
29 dst->mark(BIT_ZERO_MARK_US);
30 uint32_t next_space = BIT_ZERO_SPACE_US;
31 for (uint8_t mask = 1 << 7; mask; mask >>= 1) {
32 if (data & mask) {
33 next_space += BIT_ONE_SPACE_US;
34 } else {
35 dst->space(next_space);
36 dst->mark(BIT_ZERO_MARK_US);
37 next_space = BIT_ZERO_SPACE_US;
38 }
39 }
40 next_space += BYTE_SPACE_US;
41 dst->space(next_space);
42}
43
46 uint32_t reserve_count = 0;
47 for (size_t i = 0; i < src.size(); i++) {
48 reserve_count += 2 * (9 - (src[i] & 1) - ((src[i] >> 1) & 1) - ((src[i] >> 2) & 1) - ((src[i] >> 3) & 1) -
49 ((src[i] >> 4) & 1) - ((src[i] >> 5) & 1) - ((src[i] >> 6) & 1) - ((src[i] >> 7) & 1));
50 }
51 dst->reserve(reserve_count);
52 for (size_t i = 0; i < src.size(); i++)
53 this->encode_byte_(dst, src[i]);
55 ESP_LOGD(TAG, "Transmitting: %s", src.format_to(buf));
56}
57
58bool ABBWelcomeProtocol::decode_byte_(RemoteReceiveData &src, bool &done, uint8_t &data) {
59 if (!src.expect_mark(BIT_ZERO_MARK_US))
60 return false;
61 uint32_t next_space = BIT_ZERO_SPACE_US;
62 for (uint8_t mask = 1 << 7; mask; mask >>= 1) {
63 // if (!src.peek_space_at_least(next_space, 0))
64 // return false;
65 if (src.expect_space(next_space)) {
66 if (!src.expect_mark(BIT_ZERO_MARK_US))
67 return false;
68 next_space = BIT_ZERO_SPACE_US;
69 } else {
70 data |= mask;
71 next_space += BIT_ONE_SPACE_US;
72 }
73 }
74 next_space += BYTE_SPACE_US;
75 // if (!src.peek_space_at_least(next_space, 0))
76 // return false;
77 done = !(src.expect_space(next_space));
78 return true;
79}
80
82 if (src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US) &&
83 src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US + BIT_ONE_SPACE_US) &&
84 src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US + BIT_ONE_SPACE_US) &&
85 src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US + BIT_ONE_SPACE_US) &&
86 src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US + BIT_ONE_SPACE_US + BYTE_SPACE_US) &&
87 src.expect_item(BIT_ZERO_MARK_US, BIT_ZERO_SPACE_US + 8 * BIT_ONE_SPACE_US + BYTE_SPACE_US)) {
88 ESP_LOGVV(TAG, "Received Header: 0x55FF");
90 out[0] = 0x55;
91 out[1] = 0xff;
92 bool done = false;
93 uint8_t length = 10;
94 uint8_t received_bytes = 2;
95 for (; (received_bytes < length) && !done; received_bytes++) {
96 uint8_t data = 0;
97 if (!this->decode_byte_(src, done, data)) {
99 ESP_LOGW(TAG, "Received incomplete packet: %s", out.format_to(buf, received_bytes));
100 return {};
101 }
102 if (received_bytes == 2) {
103 length += std::min(static_cast<uint8_t>(data & DATA_LENGTH_MASK), MAX_DATA_LENGTH);
104 if (data & 0x40) {
105 length += 2;
106 }
107 }
108 ESP_LOGVV(TAG, "Received Byte: 0x%02X", data);
109 out[received_bytes] = data;
110 }
112 if (out.is_valid()) {
113 ESP_LOGI(TAG, "Received: %s", out.format_to(buf));
114 return out;
115 }
116 ESP_LOGW(TAG, "Received malformed packet: %s", out.format_to(buf, received_bytes));
117 }
118 return {};
119}
120
123 ESP_LOGD(TAG, "Received ABBWelcome: %s", data.format_to(buf));
124}
125
126} // namespace remote_base
127} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
std::array< uint8_t, 12+MAX_DATA_LENGTH > data_
char * format_to(char(&buffer)[N], uint8_t max_print_bytes=255) const
void encode(RemoteTransmitData *dst, const ABBWelcomeData &src) override
void encode_byte_(RemoteTransmitData *dst, uint8_t data) const
void dump(const ABBWelcomeData &data) override
bool decode_byte_(RemoteReceiveData &src, bool &done, uint8_t &data)
optional< ABBWelcomeData > decode(RemoteReceiveData src) override
bool expect_item(uint32_t mark, uint32_t space)
void set_carrier_frequency(uint32_t carrier_frequency)
Definition remote_base.h:30
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t length
Definition tt21100.cpp:0