ESPHome  2024.11.0
am43_base.cpp
Go to the documentation of this file.
1 #include "am43_base.h"
2 #include <cstring>
3 #include <cstdio>
4 
5 namespace esphome {
6 namespace am43 {
7 
8 const uint8_t START_PACKET[5] = {0x00, 0xff, 0x00, 0x00, 0x9a};
9 
10 std::string pkt_to_hex(const uint8_t *data, uint16_t len) {
11  char buf[64];
12  memset(buf, 0, 64);
13  for (int i = 0; i < len; i++)
14  sprintf(&buf[i * 2], "%02x", data[i]);
15  std::string ret = buf;
16  return ret;
17 }
18 
20  uint8_t data = 0x1;
21  return this->encode_(0xA2, &data, 1);
22 }
23 
25  uint8_t data = 0x1;
26  return this->encode_(0xAA, &data, 1);
27 }
28 
30  uint8_t data = 0x1;
31  return this->encode_(CMD_GET_POSITION, &data, 1);
32 }
33 
35  uint8_t data[2];
36  data[0] = (pin & 0xFF00) >> 8;
37  data[1] = pin & 0xFF;
38  return this->encode_(CMD_SEND_PIN, data, 2);
39 }
40 
42  uint8_t data = 0xDD;
43  return this->encode_(CMD_SET_STATE, &data, 1);
44 }
45 
47  uint8_t data = 0xEE;
48  return this->encode_(CMD_SET_STATE, &data, 1);
49 }
50 
52  uint8_t data = 0xCC;
53  return this->encode_(CMD_SET_STATE, &data, 1);
54 }
55 
57  return this->encode_(CMD_SET_POSITION, &position, 1);
58 }
59 
61  uint8_t checksum = 0;
62  int i = 0;
63  for (i = 0; i < this->packet_.length; i++)
64  checksum = checksum ^ this->packet_.data[i];
65  this->packet_.data[i] = checksum ^ 0xff;
66  this->packet_.length++;
67 }
68 
69 Am43Packet *Am43Encoder::encode_(uint8_t command, uint8_t *data, uint8_t length) {
70  memcpy(this->packet_.data, START_PACKET, 5);
71  this->packet_.data[5] = command;
72  this->packet_.data[6] = length;
73  memcpy(&this->packet_.data[7], data, length);
74  this->packet_.length = length + 7;
75  this->checksum_();
76  ESP_LOGV("am43", "ENC(%d): 0x%s", packet_.length, pkt_to_hex(packet_.data, packet_.length).c_str());
77  return &this->packet_;
78 }
79 
80 #define VERIFY_MIN_LENGTH(x) \
81  if (length < (x)) \
82  return;
83 
84 void Am43Decoder::decode(const uint8_t *data, uint16_t length) {
85  this->has_battery_level_ = false;
86  this->has_light_level_ = false;
87  this->has_set_position_response_ = false;
88  this->has_set_state_response_ = false;
89  this->has_position_ = false;
90  this->has_pin_response_ = false;
91  ESP_LOGV("am43", "DEC(%d): 0x%s", length, pkt_to_hex(data, length).c_str());
92 
93  if (length < 2 || data[0] != 0x9a)
94  return;
95  switch (data[1]) {
96  case CMD_GET_BATTERY_LEVEL: {
97  VERIFY_MIN_LENGTH(8);
98  this->battery_level_ = data[7];
99  this->has_battery_level_ = true;
100  break;
101  }
102  case CMD_GET_LIGHT_LEVEL: {
103  VERIFY_MIN_LENGTH(5);
104  this->light_level_ = 100 * ((float) data[4] / 9);
105  this->has_light_level_ = true;
106  break;
107  }
108  case CMD_GET_POSITION: {
109  VERIFY_MIN_LENGTH(6);
110  this->position_ = data[5];
111  this->has_position_ = true;
112  break;
113  }
114  case CMD_NOTIFY_POSITION: {
115  VERIFY_MIN_LENGTH(5);
116  this->position_ = data[4];
117  this->has_position_ = true;
118  break;
119  }
120  case CMD_SEND_PIN: {
121  VERIFY_MIN_LENGTH(4);
122  this->pin_ok_ = data[3] == RESPONSE_ACK;
123  this->has_pin_response_ = true;
124  break;
125  }
126  case CMD_SET_POSITION: {
127  VERIFY_MIN_LENGTH(4);
128  this->set_position_ok_ = data[3] == RESPONSE_ACK;
129  this->has_set_position_response_ = true;
130  break;
131  }
132  case CMD_SET_STATE: {
133  VERIFY_MIN_LENGTH(4);
134  this->set_state_ok_ = data[3] == RESPONSE_ACK;
135  this->has_set_state_response_ = true;
136  break;
137  }
138  default:
139  break;
140  }
141 };
142 
143 } // namespace am43
144 } // namespace esphome
Am43Packet * encode_(uint8_t command, uint8_t *data, uint8_t length)
Definition: am43_base.cpp:69
Am43Packet * get_battery_level_request()
Definition: am43_base.cpp:19
uint8_t checksum
Definition: bl0906.h:210
const uint8_t START_PACKET[5]
Definition: am43_base.cpp:8
Am43Packet * get_light_level_request()
Definition: am43_base.cpp:24
Am43Packet * get_set_position_request(uint8_t position)
Definition: am43_base.cpp:56
Am43Packet * get_position_request()
Definition: am43_base.cpp:29
Am43Packet * get_stop_request()
Definition: am43_base.cpp:51
Am43Packet * get_open_request()
Definition: am43_base.cpp:41
std::string pkt_to_hex(const uint8_t *data, uint16_t len)
Definition: am43_base.cpp:10
Am43Packet * get_send_pin_request(uint16_t pin)
Definition: am43_base.cpp:34
std::string size_t len
Definition: helpers.h:293
void decode(const uint8_t *data, uint16_t length)
Definition: am43_base.cpp:84
Am43Packet * get_close_request()
Definition: am43_base.cpp:46
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float position
Definition: cover.h:14