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