9static constexpr uint32_t PAYLOAD_TIMEOUT_MS = 20;
12 ESP_LOGCONFIG(TAG,
"Setting up HLK-FM22X...");
18void HlkFm22xComponent::update() {
33 if (name.length() > HLK_FM22X_NAME_SIZE - 1) {
34 ESP_LOGE(TAG,
"enroll_face(): name too long '%s'", name.c_str());
37 ESP_LOGI(TAG,
"Starting enrollment for %s", name.c_str());
38 std::array<uint8_t, 35>
data{};
40 std::copy(name.begin(), name.end(),
data.begin() + 1);
48void HlkFm22xComponent::scan_face() {
49 ESP_LOGI(TAG,
"Verify face");
50 static const uint8_t
DATA[] = {0, 0};
54void HlkFm22xComponent::delete_face(int16_t face_id) {
55 ESP_LOGI(TAG,
"Deleting face in slot %d", face_id);
56 const uint8_t
data[] = {(uint8_t) (face_id >> 8), (uint8_t) (face_id & 0xFF)};
60void HlkFm22xComponent::delete_all_faces() {
61 ESP_LOGI(TAG,
"Deleting all stored faces");
66 ESP_LOGD(TAG,
"Getting face count");
70void HlkFm22xComponent::reset() {
71 ESP_LOGI(TAG,
"Resetting module");
79 ESP_LOGV(TAG,
"Send command: 0x%.2X", command);
87 this->
write((uint8_t) (START_CODE >> 8));
88 this->
write((uint8_t) (START_CODE & 0xFF));
89 this->
write((uint8_t) command);
90 uint16_t data_size =
size;
91 this->
write((uint8_t) (data_size >> 8));
92 this->
write((uint8_t) (data_size & 0xFF));
98 for (
size_t i = 0; i <
size; i++) {
103 this->
write(checksum);
118 if ((this->
read() != (uint8_t) (START_CODE >> 8)) || (this->
read() != (uint8_t) (START_CODE & 0xFF))) {
119 ESP_LOGE(TAG,
"Invalid start code");
138 if (
millis() - start > PAYLOAD_TIMEOUT_MS) {
139 ESP_LOGE(TAG,
"Timeout waiting for payload (%u bytes)",
length);
150 size_t to_store = std::min(
static_cast<size_t>(
length), HLK_FM22X_MAX_RESPONSE_SIZE);
151 for (uint16_t idx = 0; idx <
length; ++idx) {
154 if (idx < to_store) {
159 ESP_LOGV_HEX(TAG, this->
recv_buf_.data(), to_store, HLK_FM22X_MAX_RESPONSE_SIZE,
"Recv type: 0x%.2X, data: %s",
163 if (
byte != checksum) {
164 ESP_LOGE(TAG,
"Invalid checksum for data. Calculated: 0x%.2X, Received: 0x%.2X",
checksum,
byte);
167 switch (response_type) {
175 ESP_LOGW(TAG,
"Unexpected response type: 0x%.2X", response_type);
182 ESP_LOGE(TAG,
"Empty note data");
188 ESP_LOGE(TAG,
"Invalid face note data size: %zu",
length);
194 for (int16_t &i : info) {
198 ESP_LOGV(TAG,
"Face state: status: %d, left: %d, top: %d, right: %d, bottom: %d, yaw: %d, pitch: %d, roll: %d",
199 info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
200 this->
face_info_callback_.call(info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
220 ESP_LOGW(TAG,
"Unhandled note: 0x%.2X", data[0]);
229 ESP_LOGE(TAG,
"Reply too short: %zu bytes",
length);
232 if (data[0] != (uint8_t) expected) {
233 ESP_LOGE(TAG,
"Unexpected response command. Expected: 0x%.2X, Received: 0x%.2X", expected, data[0]);
238 ESP_LOGE(TAG,
"Command <0x%.2X> failed. Error: 0x%.2X", data[0], data[1]);
258 if (
length < 4 + HLK_FM22X_NAME_SIZE) {
259 ESP_LOGE(TAG,
"VERIFY response too short: %zu bytes",
length);
263 const char *name_ptr =
reinterpret_cast<const char *
>(data + 4);
264 ESP_LOGD(TAG,
"Face verified. ID: %d, name: %.*s", face_id, (
int) HLK_FM22X_NAME_SIZE, name_ptr);
265 PUBLISH_STATE(this->last_face_id_sensor_, face_id);
266 PUBLISH_STATE(this->last_face_name_text_sensor_, name_ptr, HLK_FM22X_NAME_SIZE);
273 ESP_LOGI(TAG,
"Face enrolled. ID: %d, Direction: 0x%.2X", face_id,
direction);
280 PUBLISH_STATE(this->status_sensor_, data[2]);
284 if (this->version_text_sensor_ !=
nullptr &&
length > 2) {
285 this->version_text_sensor_->publish_state(
reinterpret_cast<const char *
>(data + 2),
length - 2);
290 PUBLISH_STATE(this->face_count_sensor_, data[2]);
293 ESP_LOGI(TAG,
"Deleted face");
296 ESP_LOGI(TAG,
"Deleted all faces");
299 ESP_LOGI(TAG,
"Module reset");
310void HlkFm22xComponent::dump_config() {
311 ESP_LOGCONFIG(TAG,
"HLK_FM22X:");
312 LOG_UPDATE_INTERVAL(
this);
313 if (this->version_text_sensor_) {
314 LOG_TEXT_SENSOR(
" ",
"Version", this->version_text_sensor_);
315 ESP_LOGCONFIG(TAG,
" Current Value: %s", this->version_text_sensor_->get_state().c_str());
317 if (this->enrolling_binary_sensor_) {
318 LOG_BINARY_SENSOR(
" ",
"Enrolling", this->enrolling_binary_sensor_);
319 ESP_LOGCONFIG(TAG,
" Current Value: %s",
320 this->enrolling_binary_sensor_->state ? LOG_STR_LITERAL(
"ON") : LOG_STR_LITERAL(
"OFF"));
322 if (this->face_count_sensor_) {
323 LOG_SENSOR(
" ",
"Face Count", this->face_count_sensor_);
324 ESP_LOGCONFIG(TAG,
" Current Value: %u", (uint16_t) this->face_count_sensor_->get_state());
326 if (this->status_sensor_) {
327 LOG_SENSOR(
" ",
"Status", this->status_sensor_);
328 ESP_LOGCONFIG(TAG,
" Current Value: %u", (uint8_t) this->status_sensor_->get_state());
330 if (this->last_face_id_sensor_) {
331 LOG_SENSOR(
" ",
"Last Face ID", this->last_face_id_sensor_);
332 ESP_LOGCONFIG(TAG,
" Current Value: %u", (int16_t) this->last_face_id_sensor_->get_state());
334 if (this->last_face_name_text_sensor_) {
335 LOG_TEXT_SENSOR(
" ",
"Last Face Name", this->last_face_name_text_sensor_);
336 ESP_LOGCONFIG(TAG,
" Current Value: %s", this->last_face_name_text_sensor_->get_state().c_str());