ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
aeha_protocol.cpp
Go to the documentation of this file.
1#include "aeha_protocol.h"
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome::remote_base {
6
7static const char *const TAG = "remote.aeha";
8
9static constexpr uint16_t BITWISE = 425;
10static constexpr uint16_t HEADER_HIGH_US = BITWISE * 8;
11static constexpr uint16_t HEADER_LOW_US = BITWISE * 4;
12static constexpr uint16_t BIT_HIGH_US = BITWISE;
13static constexpr uint16_t BIT_ONE_LOW_US = BITWISE * 3;
14static constexpr uint16_t BIT_ZERO_LOW_US = BITWISE;
15static constexpr uint16_t TRAILER = BITWISE;
16
18 dst->reserve(2 + 32 + (data.data.size() * 2) + 1);
19
20 dst->item(HEADER_HIGH_US, HEADER_LOW_US);
21
22 for (uint16_t mask = 1 << 15; mask != 0; mask >>= 1) {
23 if (data.address & mask) {
24 dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
25 } else {
26 dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
27 }
28 }
29
30 for (uint8_t bit : data.data) {
31 for (uint8_t mask = 1 << 7; mask != 0; mask >>= 1) {
32 if (bit & mask) {
33 dst->item(BIT_HIGH_US, BIT_ONE_LOW_US);
34 } else {
35 dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US);
36 }
37 }
38 }
39
40 dst->mark(TRAILER);
41}
42optional<AEHAData> AEHAProtocol::decode(RemoteReceiveData src) {
43 AEHAData out{
44 .address = 0,
45 .data = {},
46 };
47 if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US))
48 return {};
49
50 for (uint16_t mask = 1 << 15; mask != 0; mask >>= 1) {
51 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
52 out.address |= mask;
53 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
54 out.address &= ~mask;
55 } else {
56 return {};
57 }
58 }
59
60 for (uint8_t pos = 0; pos < 35; pos++) {
61 uint8_t data = 0;
62 for (uint8_t mask = 1 << 7; mask != 0; mask >>= 1) {
63 if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) {
64 data |= mask;
65 } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) {
66 data &= ~mask;
67 } else if (pos > 1 && src.expect_mark(TRAILER)) {
68 return out;
69 } else {
70 return {};
71 }
72 }
73
74 out.data.push_back(data);
75 }
76
77 if (src.expect_mark(TRAILER)) {
78 return out;
79 }
80
81 return {};
82}
83
84std::string AEHAProtocol::format_data_(const std::vector<uint8_t> &data) {
85 std::string out;
86 for (uint8_t byte : data) {
87 char buf[8]; // "0x%02X," = 5 chars + null + margin
88 snprintf(buf, sizeof(buf), "0x%02X,", byte);
89 out += buf;
90 }
91 out.pop_back();
92 return out;
93}
94
95void AEHAProtocol::dump(const AEHAData &data) {
96 auto data_str = format_data_(data.data);
97 ESP_LOGI(TAG, "Received AEHA: address=0x%04X, data=[%s]", data.address, data_str.c_str());
98}
99
100} // namespace esphome::remote_base
void dump(const AEHAData &data) override
void encode(RemoteTransmitData *dst, const AEHAData &data) override
optional< AEHAData > decode(RemoteReceiveData src) override
bool expect_item(uint32_t mark, uint32_t space)
void item(uint32_t mark, uint32_t space)
Definition remote_base.h:24
size_t size_t pos
Definition helpers.h:1038