ESPHome 2026.2.1
Loading...
Searching...
No Matches
hlk_fm22x.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <array>
11#include <utility>
12
13namespace esphome::hlk_fm22x {
14
15static const uint16_t START_CODE = 0xEFAA;
16static constexpr size_t HLK_FM22X_NAME_SIZE = 32;
17// Maximum response payload: command(1) + result(1) + face_id(2) + name(32) = 36
18static constexpr size_t HLK_FM22X_MAX_RESPONSE_SIZE = 36;
20 NONE = 0x00,
21 RESET = 0x10,
22 GET_STATUS = 0x11,
23 VERIFY = 0x12,
24 ENROLL = 0x13,
30};
31
33 REPLY = 0x00,
34 NOTE = 0x01,
35 IMAGE = 0x02,
36};
37
39 READY = 0x00,
40 FACE_STATE = 0x01,
41};
42
64
73
75 public:
76 void setup() override;
77 void update() override;
78 void dump_config() override;
79
80 void set_face_count_sensor(sensor::Sensor *face_count_sensor) { this->face_count_sensor_ = face_count_sensor; }
81 void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
82 void set_last_face_id_sensor(sensor::Sensor *last_face_id_sensor) {
83 this->last_face_id_sensor_ = last_face_id_sensor;
84 }
85 void set_last_face_name_text_sensor(text_sensor::TextSensor *last_face_name_text_sensor) {
86 this->last_face_name_text_sensor_ = last_face_name_text_sensor;
87 }
89 this->enrolling_binary_sensor_ = enrolling_binary_sensor;
90 }
92 this->version_text_sensor_ = version_text_sensor;
93 }
94 void add_on_face_scan_matched_callback(std::function<void(int16_t, std::string)> callback) {
95 this->face_scan_matched_callback_.add(std::move(callback));
96 }
97 void add_on_face_scan_unmatched_callback(std::function<void()> callback) {
98 this->face_scan_unmatched_callback_.add(std::move(callback));
99 }
100 void add_on_face_scan_invalid_callback(std::function<void(uint8_t)> callback) {
101 this->face_scan_invalid_callback_.add(std::move(callback));
102 }
104 std::function<void(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t)> callback) {
105 this->face_info_callback_.add(std::move(callback));
106 }
107 void add_on_enrollment_done_callback(std::function<void(int16_t, uint8_t)> callback) {
108 this->enrollment_done_callback_.add(std::move(callback));
109 }
110 void add_on_enrollment_failed_callback(std::function<void(uint8_t)> callback) {
111 this->enrollment_failed_callback_.add(std::move(callback));
112 }
113
114 void enroll_face(const std::string &name, HlkFm22xFaceDirection direction);
115 void scan_face();
116 void delete_face(int16_t face_id);
117 void delete_all_faces();
118 void reset();
119
120 protected:
121 void get_face_count_();
122 void send_command_(HlkFm22xCommand command, const uint8_t *data = nullptr, size_t size = 0);
123 void recv_command_();
124 void handle_note_(const uint8_t *data, size_t length);
125 void handle_reply_(const uint8_t *data, size_t length);
126 void set_enrolling_(bool enrolling);
127
128 std::array<uint8_t, HLK_FM22X_MAX_RESPONSE_SIZE> recv_buf_;
130 uint16_t wait_cycles_ = 0;
138 CallbackManager<void(int16_t, std::string)> face_scan_matched_callback_;
140 CallbackManager<void(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t)> face_info_callback_;
143};
144
145class FaceScanMatchedTrigger : public Trigger<int16_t, std::string> {
146 public:
149 [this](int16_t face_id, const std::string &name) { this->trigger(face_id, name); });
150 }
151};
152
154 public:
156 parent->add_on_face_scan_unmatched_callback([this]() { this->trigger(); });
157 }
158};
159
160class FaceScanInvalidTrigger : public Trigger<uint8_t> {
161 public:
163 parent->add_on_face_scan_invalid_callback([this](uint8_t error) { this->trigger(error); });
164 }
165};
166
167class FaceInfoTrigger : public Trigger<int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t> {
168 public:
171 [this](int16_t status, int16_t left, int16_t top, int16_t right, int16_t bottom, int16_t yaw, int16_t pitch,
172 int16_t roll) { this->trigger(status, left, top, right, bottom, yaw, pitch, roll); });
173 }
174};
175
176class EnrollmentDoneTrigger : public Trigger<int16_t, uint8_t> {
177 public:
180 [this](int16_t face_id, uint8_t direction) { this->trigger(face_id, direction); });
181 }
182};
183
184class EnrollmentFailedTrigger : public Trigger<uint8_t> {
185 public:
187 parent->add_on_enrollment_failed_callback([this](uint8_t error) { this->trigger(error); });
188 }
189};
190
191template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<HlkFm22xComponent> {
192 public:
193 TEMPLATABLE_VALUE(std::string, name)
195
196 void play(const Ts &...x) override {
197 auto name = this->name_.value(x...);
198 auto direction = (HlkFm22xFaceDirection) this->direction_.value(x...);
199 this->parent_->enroll_face(name, direction);
200 }
201};
202
203template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<HlkFm22xComponent> {
204 public:
205 TEMPLATABLE_VALUE(int16_t, face_id)
206
207 void play(const Ts &...x) override {
208 auto face_id = this->face_id_.value(x...);
209 this->parent_->delete_face(face_id);
210 }
211};
212
213template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<HlkFm22xComponent> {
214 public:
215 void play(const Ts &...x) override { this->parent_->delete_all_faces(); }
216};
217
218template<typename... Ts> class ScanAction : public Action<Ts...>, public Parented<HlkFm22xComponent> {
219 public:
220 void play(const Ts &...x) override { this->parent_->scan_face(); }
221};
222
223template<typename... Ts> class ResetAction : public Action<Ts...>, public Parented<HlkFm22xComponent> {
224 public:
225 void play(const Ts &...x) override { this->parent_->reset(); }
226};
227
228} // namespace esphome::hlk_fm22x
uint8_t status
Definition bl0942.h:8
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1471
This class simplifies creating components that periodically check a state.
Definition component.h:512
Base class for all binary_sensor-type classes.
TEMPLATABLE_VALUE(int16_t, face_id) void play(const Ts &...x) override
Definition hlk_fm22x.h:205
void play(const Ts &...x) override
Definition hlk_fm22x.h:215
TEMPLATABLE_VALUE(std::string, name) TEMPLATABLE_VALUE(uint8_t
direction void play(const Ts &...x) override
Definition hlk_fm22x.h:196
EnrollmentDoneTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:178
EnrollmentFailedTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:186
FaceInfoTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:169
FaceScanInvalidTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:162
FaceScanMatchedTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:147
FaceScanUnmatchedTrigger(HlkFm22xComponent *parent)
Definition hlk_fm22x.h:155
void delete_face(int16_t face_id)
Definition hlk_fm22x.cpp:55
void handle_reply_(const uint8_t *data, size_t length)
void add_on_enrollment_done_callback(std::function< void(int16_t, uint8_t)> callback)
Definition hlk_fm22x.h:107
text_sensor::TextSensor * last_face_name_text_sensor_
Definition hlk_fm22x.h:135
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:140
void enroll_face(const std::string &name, HlkFm22xFaceDirection direction)
Definition hlk_fm22x.cpp:33
void send_command_(HlkFm22xCommand command, const uint8_t *data=nullptr, size_t size=0)
Definition hlk_fm22x.cpp:79
CallbackManager< void(uint8_t)> face_scan_invalid_callback_
Definition hlk_fm22x.h:137
std::array< uint8_t, HLK_FM22X_MAX_RESPONSE_SIZE > recv_buf_
Definition hlk_fm22x.h:128
void set_face_count_sensor(sensor::Sensor *face_count_sensor)
Definition hlk_fm22x.h:80
CallbackManager< void()> face_scan_unmatched_callback_
Definition hlk_fm22x.h:139
void set_enrolling_binary_sensor(binary_sensor::BinarySensor *enrolling_binary_sensor)
Definition hlk_fm22x.h:88
void set_version_text_sensor(text_sensor::TextSensor *version_text_sensor)
Definition hlk_fm22x.h:91
CallbackManager< void(int16_t, uint8_t)> enrollment_done_callback_
Definition hlk_fm22x.h:141
void set_last_face_id_sensor(sensor::Sensor *last_face_id_sensor)
Definition hlk_fm22x.h:82
CallbackManager< void(uint8_t)> enrollment_failed_callback_
Definition hlk_fm22x.h:142
void add_on_face_scan_matched_callback(std::function< void(int16_t, std::string)> callback)
Definition hlk_fm22x.h:94
text_sensor::TextSensor * version_text_sensor_
Definition hlk_fm22x.h:136
void handle_note_(const uint8_t *data, size_t length)
void set_last_face_name_text_sensor(text_sensor::TextSensor *last_face_name_text_sensor)
Definition hlk_fm22x.h:85
CallbackManager< void(int16_t, std::string)> face_scan_matched_callback_
Definition hlk_fm22x.h:138
void add_on_face_scan_invalid_callback(std::function< void(uint8_t)> callback)
Definition hlk_fm22x.h:100
binary_sensor::BinarySensor * enrolling_binary_sensor_
Definition hlk_fm22x.h:134
void add_on_face_scan_unmatched_callback(std::function< void()> callback)
Definition hlk_fm22x.h:97
void add_on_face_info_callback(std::function< void(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t, int16_t)> callback)
Definition hlk_fm22x.h:103
void add_on_enrollment_failed_callback(std::function< void(uint8_t)> callback)
Definition hlk_fm22x.h:110
void set_status_sensor(sensor::Sensor *status_sensor)
Definition hlk_fm22x.h:81
void play(const Ts &...x) override
Definition hlk_fm22x.h:225
void play(const Ts &...x) override
Definition hlk_fm22x.h:220
Base-class for all sensors.
Definition sensor.h:43
FanDirection direction
Definition fan.h:5
size_t size
Definition helpers.h:729
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5