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