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