ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
hlk_fm22x.cpp
Go to the documentation of this file.
1#include "hlk_fm22x.h"
2#include "esphome/core/log.h"
4#include <array>
5#include <cinttypes>
6
8
9static const char *const TAG = "hlk_fm22x";
10
12 ESP_LOGCONFIG(TAG, "Setting up HLK-FM22X...");
13 this->set_enrolling_(false);
14 while (this->available()) {
15 this->read();
16 }
17 this->defer([this]() { this->send_command_(HlkFm22xCommand::GET_STATUS); });
18}
19
22 if (this->wait_cycles_ > 600) {
23 ESP_LOGE(TAG, "Command 0x%.2X timed out", this->active_command_);
25 this->mark_failed();
26 } else {
27 this->reset();
28 }
29 }
30 }
31 this->recv_command_();
32}
33
35 if (name.length() > 31) {
36 ESP_LOGE(TAG, "enroll_face(): name too long '%s'", name.c_str());
37 return;
38 }
39 ESP_LOGI(TAG, "Starting enrollment for %s", name.c_str());
40 std::array<uint8_t, 35> data{};
41 data[0] = 0; // admin
42 std::copy(name.begin(), name.end(), data.begin() + 1);
43 // Remaining bytes are already zero-initialized
44 data[33] = (uint8_t) direction;
45 data[34] = 10; // timeout
46 this->send_command_(HlkFm22xCommand::ENROLL, data.data(), data.size());
47 this->set_enrolling_(true);
48}
49
51 ESP_LOGI(TAG, "Verify face");
52 static const uint8_t DATA[] = {0, 0};
53 this->send_command_(HlkFm22xCommand::VERIFY, DATA, sizeof(DATA));
54}
55
56void HlkFm22xComponent::delete_face(int16_t face_id) {
57 ESP_LOGI(TAG, "Deleting face in slot %d", face_id);
58 const uint8_t data[] = {(uint8_t) (face_id >> 8), (uint8_t) (face_id & 0xFF)};
59 this->send_command_(HlkFm22xCommand::DELETE_FACE, data, sizeof(data));
60}
61
63 ESP_LOGI(TAG, "Deleting all stored faces");
65}
66
68 ESP_LOGD(TAG, "Getting face count");
70}
71
73 ESP_LOGI(TAG, "Resetting module");
75 this->wait_cycles_ = 0;
76 this->set_enrolling_(false);
78}
79
80void HlkFm22xComponent::send_command_(HlkFm22xCommand command, const uint8_t *data, size_t size) {
81 ESP_LOGV(TAG, "Send command: 0x%.2X", command);
83 ESP_LOGW(TAG, "Command 0x%.2X already active", this->active_command_);
84 return;
85 }
86 this->wait_cycles_ = 0;
87 this->active_command_ = command;
88 while (this->available())
89 this->read();
90 this->write((uint8_t) (START_CODE >> 8));
91 this->write((uint8_t) (START_CODE & 0xFF));
92 this->write((uint8_t) command);
93 uint16_t data_size = size;
94 this->write((uint8_t) (data_size >> 8));
95 this->write((uint8_t) (data_size & 0xFF));
96
97 uint8_t checksum = 0;
98 checksum ^= (uint8_t) command;
99 checksum ^= (data_size >> 8);
100 checksum ^= (data_size & 0xFF);
101 for (size_t i = 0; i < size; i++) {
102 this->write(data[i]);
103 checksum ^= data[i];
104 }
105
106 this->write(checksum);
107 this->active_command_ = command;
108 this->wait_cycles_ = 0;
109}
110
112 uint8_t byte, checksum = 0;
113 uint16_t length = 0;
114
115 if (this->available() < 7) {
116 ++this->wait_cycles_;
117 return;
118 }
119 this->wait_cycles_ = 0;
120
121 if ((this->read() != (uint8_t) (START_CODE >> 8)) || (this->read() != (uint8_t) (START_CODE & 0xFF))) {
122 ESP_LOGE(TAG, "Invalid start code");
123 return;
124 }
125
126 byte = this->read();
127 checksum ^= byte;
128 HlkFm22xResponseType response_type = (HlkFm22xResponseType) byte;
129
130 byte = this->read();
131 checksum ^= byte;
132 length = byte << 8;
133 byte = this->read();
134 checksum ^= byte;
135 length |= byte;
136
137 std::vector<uint8_t> data;
138 data.reserve(length);
139 for (uint16_t idx = 0; idx < length; ++idx) {
140 byte = this->read();
141 checksum ^= byte;
142 data.push_back(byte);
143 }
144
145 ESP_LOGV(TAG, "Recv type: 0x%.2X, data: %s", response_type, format_hex_pretty(data).c_str());
146
147 byte = this->read();
148 if (byte != checksum) {
149 ESP_LOGE(TAG, "Invalid checksum for data. Calculated: 0x%.2X, Received: 0x%.2X", checksum, byte);
150 return;
151 }
152 switch (response_type) {
154 this->handle_note_(data);
155 break;
157 this->handle_reply_(data);
158 break;
159 default:
160 ESP_LOGW(TAG, "Unexpected response type: 0x%.2X", response_type);
161 break;
162 }
163}
164
165void HlkFm22xComponent::handle_note_(const std::vector<uint8_t> &data) {
166 switch (data[0]) {
168 if (data.size() < 17) {
169 ESP_LOGE(TAG, "Invalid face note data size: %u", data.size());
170 break;
171 }
172 {
173 int16_t info[8];
174 uint8_t offset = 1;
175 for (int16_t &i : info) {
176 i = ((int16_t) data[offset + 1] << 8) | data[offset];
177 offset += 2;
178 }
179 ESP_LOGV(TAG, "Face state: status: %d, left: %d, top: %d, right: %d, bottom: %d, yaw: %d, pitch: %d, roll: %d",
180 info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
181 this->face_info_callback_.call(info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
182 }
183 break;
185 ESP_LOGE(TAG, "Command 0x%.2X timed out", this->active_command_);
186 switch (this->active_command_) {
188 this->set_enrolling_(false);
190 break;
193 break;
194 default:
195 break;
196 }
198 this->wait_cycles_ = 0;
199 break;
200 default:
201 ESP_LOGW(TAG, "Unhandled note: 0x%.2X", data[0]);
202 break;
203 }
204}
205
206void HlkFm22xComponent::handle_reply_(const std::vector<uint8_t> &data) {
207 auto expected = this->active_command_;
209 if (data[0] != (uint8_t) expected) {
210 ESP_LOGE(TAG, "Unexpected response command. Expected: 0x%.2X, Received: 0x%.2X", expected, data[0]);
211 return;
212 }
213
214 if (data[1] != HlkFm22xResult::SUCCESS) {
215 ESP_LOGE(TAG, "Command <0x%.2X> failed. Error: 0x%.2X", data[0], data[1]);
216 switch (expected) {
218 this->set_enrolling_(false);
219 this->enrollment_failed_callback_.call(data[1]);
220 break;
222 if (data[1] == HlkFm22xResult::REJECTED) {
224 } else {
225 this->face_scan_invalid_callback_.call(data[1]);
226 }
227 break;
228 default:
229 break;
230 }
231 return;
232 }
233 switch (expected) {
235 int16_t face_id = ((int16_t) data[2] << 8) | data[3];
236 std::string name(data.begin() + 4, data.begin() + 36);
237 ESP_LOGD(TAG, "Face verified. ID: %d, name: %s", face_id, name.c_str());
238 if (this->last_face_id_sensor_ != nullptr) {
239 this->last_face_id_sensor_->publish_state(face_id);
240 }
241 if (this->last_face_name_text_sensor_ != nullptr) {
243 }
244 this->face_scan_matched_callback_.call(face_id, name);
245 break;
246 }
248 int16_t face_id = ((int16_t) data[2] << 8) | data[3];
250 ESP_LOGI(TAG, "Face enrolled. ID: %d, Direction: 0x%.2X", face_id, direction);
251 this->enrollment_done_callback_.call(face_id, (uint8_t) direction);
252 this->set_enrolling_(false);
253 this->defer([this]() { this->get_face_count_(); });
254 break;
255 }
257 if (this->status_sensor_ != nullptr) {
258 this->status_sensor_->publish_state(data[2]);
259 }
260 this->defer([this]() { this->send_command_(HlkFm22xCommand::GET_VERSION); });
261 break;
263 if (this->version_text_sensor_ != nullptr) {
264 std::string version(data.begin() + 2, data.end());
265 this->version_text_sensor_->publish_state(version);
266 }
267 this->defer([this]() { this->get_face_count_(); });
268 break;
270 if (this->face_count_sensor_ != nullptr) {
271 this->face_count_sensor_->publish_state(data[2]);
272 }
273 break;
275 ESP_LOGI(TAG, "Deleted face");
276 break;
278 ESP_LOGI(TAG, "Deleted all faces");
279 break;
281 ESP_LOGI(TAG, "Module reset");
282 this->defer([this]() { this->send_command_(HlkFm22xCommand::GET_STATUS); });
283 break;
284 default:
285 ESP_LOGW(TAG, "Unhandled command: 0x%.2X", this->active_command_);
286 break;
287 }
288}
289
291 if (this->enrolling_binary_sensor_ != nullptr) {
292 this->enrolling_binary_sensor_->publish_state(enrolling);
293 }
294}
295
297 ESP_LOGCONFIG(TAG, "HLK_FM22X:");
298 LOG_UPDATE_INTERVAL(this);
299 if (this->version_text_sensor_) {
300 LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
301 ESP_LOGCONFIG(TAG, " Current Value: %s", this->version_text_sensor_->get_state().c_str());
302 }
303 if (this->enrolling_binary_sensor_) {
304 LOG_BINARY_SENSOR(" ", "Enrolling", this->enrolling_binary_sensor_);
305 ESP_LOGCONFIG(TAG, " Current Value: %s", this->enrolling_binary_sensor_->state ? "ON" : "OFF");
306 }
307 if (this->face_count_sensor_) {
308 LOG_SENSOR(" ", "Face Count", this->face_count_sensor_);
309 ESP_LOGCONFIG(TAG, " Current Value: %u", (uint16_t) this->face_count_sensor_->get_state());
310 }
311 if (this->status_sensor_) {
312 LOG_SENSOR(" ", "Status", this->status_sensor_);
313 ESP_LOGCONFIG(TAG, " Current Value: %u", (uint8_t) this->status_sensor_->get_state());
314 }
315 if (this->last_face_id_sensor_) {
316 LOG_SENSOR(" ", "Last Face ID", this->last_face_id_sensor_);
317 ESP_LOGCONFIG(TAG, " Current Value: %u", (int16_t) this->last_face_id_sensor_->get_state());
318 }
319 if (this->last_face_name_text_sensor_) {
320 LOG_TEXT_SENSOR(" ", "Last Face Name", this->last_face_name_text_sensor_);
321 ESP_LOGCONFIG(TAG, " Current Value: %s", this->last_face_name_text_sensor_->get_state().c_str());
322 }
323}
324
325} // namespace esphome::hlk_fm22x
uint8_t checksum
Definition bl0906.h:3
virtual void mark_failed()
Mark this component as failed.
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void publish_state(bool new_state)
Publish a new state to the front-end.
void delete_face(int16_t face_id)
Definition hlk_fm22x.cpp:56
text_sensor::TextSensor * last_face_name_text_sensor_
Definition hlk_fm22x.h:131
CallbackManager< void(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t)> face_info_callback_
Definition hlk_fm22x.h:136
void enroll_face(const std::string &name, HlkFm22xFaceDirection direction)
Definition hlk_fm22x.cpp:34
void handle_reply_(const std::vector< uint8_t > &data)
void send_command_(HlkFm22xCommand command, const uint8_t *data=nullptr, size_t size=0)
Definition hlk_fm22x.cpp:80
CallbackManager< void(uint8_t)> face_scan_invalid_callback_
Definition hlk_fm22x.h:133
CallbackManager< void()> face_scan_unmatched_callback_
Definition hlk_fm22x.h:135
void handle_note_(const std::vector< uint8_t > &data)
CallbackManager< void(int16_t, uint8_t)> enrollment_done_callback_
Definition hlk_fm22x.h:137
CallbackManager< void(uint8_t)> enrollment_failed_callback_
Definition hlk_fm22x.h:138
text_sensor::TextSensor * version_text_sensor_
Definition hlk_fm22x.h:132
CallbackManager< void(int16_t, std::string)> face_scan_matched_callback_
Definition hlk_fm22x.h:134
binary_sensor::BinarySensor * enrolling_binary_sensor_
Definition hlk_fm22x.h:130
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:75
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:127
std::string get_state() const
Getter-syntax for .state.
void publish_state(const std::string &state)
size_t write(uint8_t data)
Definition uart.h:58
FanDirection direction
Definition fan.h:3
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:317
uint16_t length
Definition tt21100.cpp:0