ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
am43_base.cpp
Go to the documentation of this file.
1#include "am43_base.h"
3#include "esphome/core/log.h"
4#include <cstring>
5
6namespace esphome::am43 {
7
8ESPHOME_LOG_TAG(TAG, "am43");
9
10static constexpr uint8_t START_PACKET[5] = {0x00, 0xff, 0x00, 0x00, 0x9a};
11
13 uint8_t data = 0x1;
14 return encode(CMD_GET_BATTERY_LEVEL, &data, 1);
15}
16
18 uint8_t data = 0x1;
19 return encode(CMD_GET_LIGHT_LEVEL, &data, 1);
20}
21
23 uint8_t data = 0x1;
24 return encode(CMD_GET_POSITION, &data, 1);
25}
26
28 uint8_t data[2];
29 data[0] = (pin & 0xFF00) >> 8;
30 data[1] = pin & 0xFF;
31 return encode(CMD_SEND_PIN, data, 2);
32}
33
35 uint8_t data = 0xDD;
36 return encode(CMD_SET_STATE, &data, 1);
37}
38
40 uint8_t data = 0xEE;
41 return encode(CMD_SET_STATE, &data, 1);
42}
43
45 uint8_t data = 0xCC;
46 return encode(CMD_SET_STATE, &data, 1);
47}
48
49Am43Packet Am43Encoder::get_set_position_request(uint8_t position) { return encode(CMD_SET_POSITION, &position, 1); }
50
51Am43Packet Am43Encoder::encode(uint8_t command, const uint8_t *data, uint8_t length) {
52 Am43Packet packet;
53 memcpy(packet.data, START_PACKET, sizeof(START_PACKET));
54 packet.data[5] = command;
55 packet.data[6] = length;
56 memcpy(&packet.data[7], data, length);
57 packet.length = length + 7;
58 // The device checks the inverted XOR of everything before it
59 packet.data[packet.length] = xor_bytes(packet.data, packet.length, 0xff);
60 packet.length++;
61 char hex_buf[format_hex_size(sizeof(packet.data))];
62 ESP_LOGV(TAG, "ENC(%d): 0x%s", packet.length, format_hex_to(hex_buf, packet.data, packet.length));
63 return packet;
64}
65
66void Am43Decoder::decode(const uint8_t *data, uint16_t length) {
67 // Reset so a decoder reused across frames reports only the latest one
68 this->has_battery_level_ = false;
69 this->has_light_level_ = false;
70 this->has_set_position_response_ = false;
71 this->has_set_state_response_ = false;
72 this->has_position_ = false;
73 this->has_pin_response_ = false;
74 char hex_buf[format_hex_size(sizeof(Am43Packet::data))]; // Max expected packet size
75 ESP_LOGV(TAG, "DEC(%d): 0x%s", length, format_hex_to(hex_buf, data, length));
76
77 if (length < 2 || data[0] != 0x9a)
78 return;
79 switch (data[1]) {
80 case CMD_GET_BATTERY_LEVEL: {
81 if (length < 8)
82 return;
83 this->battery_level_ = data[7];
84 this->has_battery_level_ = true;
85 break;
86 }
87 case CMD_GET_LIGHT_LEVEL: {
88 if (length < 5)
89 return;
90 this->light_level_ = 100 * ((float) data[4] / 9);
91 this->has_light_level_ = true;
92 break;
93 }
94 case CMD_GET_POSITION: {
95 if (length < 6)
96 return;
97 this->position_ = data[5];
98 this->has_position_ = true;
99 break;
100 }
101 case CMD_NOTIFY_POSITION: {
102 if (length < 5)
103 return;
104 this->position_ = data[4];
105 this->has_position_ = true;
106 break;
107 }
108 case CMD_SEND_PIN: {
109 if (length < 4)
110 return;
111 this->pin_ok_ = data[3] == RESPONSE_ACK;
112 this->has_pin_response_ = true;
113 break;
114 }
115 case CMD_SET_POSITION: {
116 if (length < 4)
117 return;
118 this->set_position_ok_ = data[3] == RESPONSE_ACK;
119 this->has_set_position_response_ = true;
120 break;
121 }
122 case CMD_SET_STATE: {
123 if (length < 4)
124 return;
125 this->set_state_ok_ = data[3] == RESPONSE_ACK;
126 this->has_set_state_response_ = true;
127 break;
128 }
129 default:
130 break;
131 }
132}
133
134} // namespace esphome::am43
void decode(const uint8_t *data, uint16_t length)
Definition am43_base.cpp:66
static Am43Packet get_set_position_request(uint8_t position)
Definition am43_base.cpp:49
static Am43Packet get_close_request()
Definition am43_base.cpp:39
static Am43Packet get_open_request()
Definition am43_base.cpp:34
static Am43Packet get_stop_request()
Definition am43_base.cpp:44
static Am43Packet get_battery_level_request()
Definition am43_base.cpp:12
static Am43Packet get_send_pin_request(uint16_t pin)
Definition am43_base.cpp:27
static Am43Packet get_light_level_request()
Definition am43_base.cpp:17
static Am43Packet encode(uint8_t command, const uint8_t *data, uint8_t length)
Definition am43_base.cpp:51
static Am43Packet get_position_request()
Definition am43_base.cpp:22
float position
Definition cover.h:0
ESPHOME_LOG_TAG(TAG, "am43")
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
Definition helpers.h:949
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:1661
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428
uint16_t length
Definition tt21100.cpp:0