8static const char *
const TAG =
"hlk_fm22x";
11 ESP_LOGCONFIG(TAG,
"Setting up HLK-FM22X...");
34 if (name.length() > HLK_FM22X_NAME_SIZE - 1) {
35 ESP_LOGE(TAG,
"enroll_face(): name too long '%s'", name.c_str());
38 ESP_LOGI(TAG,
"Starting enrollment for %s", name.c_str());
39 std::array<uint8_t, 35> data{};
41 std::copy(name.begin(), name.end(), data.begin() + 1);
50 ESP_LOGI(TAG,
"Verify face");
51 static const uint8_t DATA[] = {0, 0};
56 ESP_LOGI(TAG,
"Deleting face in slot %d", face_id);
57 const uint8_t data[] = {(uint8_t) (face_id >> 8), (uint8_t) (face_id & 0xFF)};
62 ESP_LOGI(TAG,
"Deleting all stored faces");
67 ESP_LOGD(TAG,
"Getting face count");
72 ESP_LOGI(TAG,
"Resetting module");
80 ESP_LOGV(TAG,
"Send command: 0x%.2X", command);
89 this->
write((uint8_t) (START_CODE >> 8));
90 this->
write((uint8_t) (START_CODE & 0xFF));
91 this->
write((uint8_t) command);
92 uint16_t data_size =
size;
93 this->
write((uint8_t) (data_size >> 8));
94 this->
write((uint8_t) (data_size & 0xFF));
100 for (
size_t i = 0; i <
size; i++) {
101 this->
write(data[i]);
105 this->
write(checksum);
120 if ((this->
read() != (uint8_t) (START_CODE >> 8)) || (this->
read() != (uint8_t) (START_CODE & 0xFF))) {
121 ESP_LOGE(TAG,
"Invalid start code");
136 if (
length > HLK_FM22X_MAX_RESPONSE_SIZE) {
137 ESP_LOGE(TAG,
"Response too large: %u bytes",
length);
144 for (uint16_t idx = 0; idx <
length; ++idx) {
150#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
152 ESP_LOGV(TAG,
"Recv type: 0x%.2X, data: %s", response_type,
157 if (
byte != checksum) {
158 ESP_LOGE(TAG,
"Invalid checksum for data. Calculated: 0x%.2X, Received: 0x%.2X",
checksum,
byte);
161 switch (response_type) {
169 ESP_LOGW(TAG,
"Unexpected response type: 0x%.2X", response_type);
176 ESP_LOGE(TAG,
"Empty note data");
182 ESP_LOGE(TAG,
"Invalid face note data size: %zu",
length);
188 for (int16_t &i : info) {
189 i = ((int16_t) data[offset + 1] << 8) | data[offset];
192 ESP_LOGV(TAG,
"Face state: status: %d, left: %d, top: %d, right: %d, bottom: %d, yaw: %d, pitch: %d, roll: %d",
193 info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
194 this->
face_info_callback_.call(info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7]);
214 ESP_LOGW(TAG,
"Unhandled note: 0x%.2X", data[0]);
223 ESP_LOGE(TAG,
"Reply too short: %zu bytes",
length);
226 if (data[0] != (uint8_t) expected) {
227 ESP_LOGE(TAG,
"Unexpected response command. Expected: 0x%.2X, Received: 0x%.2X", expected, data[0]);
232 ESP_LOGE(TAG,
"Command <0x%.2X> failed. Error: 0x%.2X", data[0], data[1]);
252 if (
length < 4 + HLK_FM22X_NAME_SIZE) {
253 ESP_LOGE(TAG,
"VERIFY response too short: %zu bytes",
length);
256 int16_t face_id = ((int16_t) data[2] << 8) | data[3];
257 const char *name_ptr =
reinterpret_cast<const char *
>(data + 4);
258 ESP_LOGD(TAG,
"Face verified. ID: %d, name: %.*s", face_id, (
int) HLK_FM22X_NAME_SIZE, name_ptr);
269 int16_t face_id = ((int16_t) data[2] << 8) | data[3];
271 ESP_LOGI(TAG,
"Face enrolled. ID: %d, Direction: 0x%.2X", face_id,
direction);
295 ESP_LOGI(TAG,
"Deleted face");
298 ESP_LOGI(TAG,
"Deleted all faces");
301 ESP_LOGI(TAG,
"Module reset");
317 ESP_LOGCONFIG(TAG,
"HLK_FM22X:");
318 LOG_UPDATE_INTERVAL(
this);
virtual void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *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)
sensor::Sensor * last_face_id_sensor_
void handle_reply_(const uint8_t *data, size_t length)
text_sensor::TextSensor * last_face_name_text_sensor_
CallbackManager< void(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t)> face_info_callback_
void enroll_face(const std::string &name, HlkFm22xFaceDirection direction)
void send_command_(HlkFm22xCommand command, const uint8_t *data=nullptr, size_t size=0)
CallbackManager< void(uint8_t)> face_scan_invalid_callback_
std::array< uint8_t, HLK_FM22X_MAX_RESPONSE_SIZE > recv_buf_
sensor::Sensor * face_count_sensor_
CallbackManager< void()> face_scan_unmatched_callback_
void set_enrolling_(bool enrolling)
CallbackManager< void(int16_t, uint8_t)> enrollment_done_callback_
CallbackManager< void(uint8_t)> enrollment_failed_callback_
sensor::Sensor * status_sensor_
text_sensor::TextSensor * version_text_sensor_
void handle_note_(const uint8_t *data, size_t length)
CallbackManager< void(int16_t, std::string)> face_scan_matched_callback_
binary_sensor::BinarySensor * enrolling_binary_sensor_
HlkFm22xCommand active_command_
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
float get_state() const
Getter-syntax for .state.
const std::string & get_state() const
Getter-syntax for .state.
void publish_state(const std::string &state)
size_t write(uint8_t data)
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".