ESPHome 2025.10.1
Loading...
Searching...
No Matches
fingerprint_grow.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <limits>
10#include <vector>
11
12namespace esphome {
13namespace fingerprint_grow {
14
15static const uint16_t START_CODE = 0xEF01;
16
17static const uint16_t ENROLLMENT_SLOT_UNUSED = 0xFFFF;
18
19// The datasheet says a max wake up time of of 200ms.
20static const uint8_t WAIT_FOR_WAKE_UP_MS = 200;
21
22static const uint32_t DEFAULT_IDLE_PERIOD_TO_SLEEP_MS = 5000;
23
25 COMMAND = 0x01,
26 DATA = 0x02,
27 ACK = 0x07,
28 END_DATA = 0x08,
29};
30
32 GET_IMAGE = 0x01,
33 IMAGE_2_TZ = 0x02,
34 SEARCH = 0x04,
35 REG_MODEL = 0x05,
36 STORE = 0x06,
37 LOAD = 0x07,
38 UPLOAD = 0x08,
39 DELETE = 0x0C,
40 DELETE_ALL = 0x0D, // aka EMPTY
47 LED_ON = 0x50,
48 LED_OFF = 0x51,
49};
50
76
78 BREATHING = 0x01,
79 FLASHING = 0x02,
80 ALWAYS_ON = 0x03,
81 ALWAYS_OFF = 0x04,
82 GRADUAL_ON = 0x05,
84};
85
87 RED = 0x01,
88 BLUE = 0x02,
89 PURPLE = 0x03,
90 GREEN = 0x04,
91 YELLOW = 0x05,
92 CYAN = 0x06,
93 WHITE = 0x07,
94};
95
97 public:
98 void update() override;
99 void setup() override;
100 void dump_config() override;
101
102 void set_address(uint32_t address) {
103 this->address_[0] = (uint8_t) (address >> 24);
104 this->address_[1] = (uint8_t) (address >> 16);
105 this->address_[2] = (uint8_t) (address >> 8);
106 this->address_[3] = (uint8_t) (address & 0xFF);
107 }
108 void set_sensing_pin(GPIOPin *sensing_pin) { this->sensing_pin_ = sensing_pin; }
109 void set_sensor_power_pin(GPIOPin *sensor_power_pin) { this->sensor_power_pin_ = sensor_power_pin; }
110 void set_password(uint32_t password) { this->password_ = password; }
111 void set_new_password(uint32_t new_password) { this->new_password_ = new_password; }
112 void set_idle_period_to_sleep_ms(uint32_t period_ms) { this->idle_period_to_sleep_ms_ = period_ms; }
113 void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor) {
114 this->fingerprint_count_sensor_ = fingerprint_count_sensor;
115 }
116 void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
117 void set_capacity_sensor(sensor::Sensor *capacity_sensor) { this->capacity_sensor_ = capacity_sensor; }
118 void set_security_level_sensor(sensor::Sensor *security_level_sensor) {
119 this->security_level_sensor_ = security_level_sensor;
120 }
121 void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor) {
122 this->last_finger_id_sensor_ = last_finger_id_sensor;
123 }
124 void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor) {
125 this->last_confidence_sensor_ = last_confidence_sensor;
126 }
128 this->enrolling_binary_sensor_ = enrolling_binary_sensor;
129 }
130 void add_on_finger_scan_start_callback(std::function<void()> callback) {
131 this->finger_scan_start_callback_.add(std::move(callback));
132 }
133 void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
134 this->finger_scan_matched_callback_.add(std::move(callback));
135 }
136 void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
137 this->finger_scan_unmatched_callback_.add(std::move(callback));
138 }
139 void add_on_finger_scan_misplaced_callback(std::function<void()> callback) {
140 this->finger_scan_misplaced_callback_.add(std::move(callback));
141 }
142 void add_on_finger_scan_invalid_callback(std::function<void()> callback) {
143 this->finger_scan_invalid_callback_.add(std::move(callback));
144 }
145 void add_on_enrollment_scan_callback(std::function<void(uint8_t, uint16_t)> callback) {
146 this->enrollment_scan_callback_.add(std::move(callback));
147 }
148 void add_on_enrollment_done_callback(std::function<void(uint16_t)> callback) {
149 this->enrollment_done_callback_.add(std::move(callback));
150 }
151
152 void add_on_enrollment_failed_callback(std::function<void(uint16_t)> callback) {
153 this->enrollment_failed_callback_.add(std::move(callback));
154 }
155
156 void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers);
157 void finish_enrollment(uint8_t result);
158 void delete_fingerprint(uint16_t finger_id);
160
161 void led_control(bool state);
162 void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count);
163
164 protected:
165 void scan_and_match_();
166 uint8_t scan_image_(uint8_t buffer);
167 uint8_t save_fingerprint_();
168 bool check_password_();
169 bool set_password_();
170 bool get_parameters_();
172 uint8_t transfer_(std::vector<uint8_t> *p_data_buffer);
173 uint8_t send_command_();
174 void sensor_wakeup_();
175 void sensor_sleep_();
176
177 std::vector<uint8_t> data_ = {};
178 uint8_t address_[4] = {0xFF, 0xFF, 0xFF, 0xFF};
179 uint16_t capacity_ = 64;
180 uint32_t password_ = 0x0;
181 uint32_t new_password_ = std::numeric_limits<uint32_t>::max();
184 uint8_t enrollment_image_ = 0;
185 uint16_t enrollment_slot_ = ENROLLMENT_SLOT_UNUSED;
187 bool waiting_removal_ = false;
188 bool has_sensing_pin_ = false;
189 bool has_power_pin_ = false;
190 bool is_sensor_awake_ = false;
191 uint32_t last_transfer_ms_ = 0;
195 uint32_t idle_period_to_sleep_ms_ = UINT32_MAX;
211};
212
214 public:
216 parent->add_on_finger_scan_start_callback([this]() { this->trigger(); });
217 }
218};
219
220class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
221 public:
224 [this](uint16_t finger_id, uint16_t confidence) { this->trigger(finger_id, confidence); });
225 }
226};
227
229 public:
231 parent->add_on_finger_scan_unmatched_callback([this]() { this->trigger(); });
232 }
233};
234
236 public:
238 parent->add_on_finger_scan_misplaced_callback([this]() { this->trigger(); });
239 }
240};
241
243 public:
245 parent->add_on_finger_scan_invalid_callback([this]() { this->trigger(); });
246 }
247};
248
249class EnrollmentScanTrigger : public Trigger<uint8_t, uint16_t> {
250 public:
253 [this](uint8_t scan_num, uint16_t finger_id) { this->trigger(scan_num, finger_id); });
254 }
255};
256
257class EnrollmentDoneTrigger : public Trigger<uint16_t> {
258 public:
260 parent->add_on_enrollment_done_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
261 }
262};
263
264class EnrollmentFailedTrigger : public Trigger<uint16_t> {
265 public:
267 parent->add_on_enrollment_failed_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
268 }
269};
270
271template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
272 public:
273 TEMPLATABLE_VALUE(uint16_t, finger_id)
274 TEMPLATABLE_VALUE(uint8_t, num_scans)
275
276 void play(Ts... x) override {
277 auto finger_id = this->finger_id_.value(x...);
278 auto num_scans = this->num_scans_.value(x...);
279 if (num_scans) {
280 this->parent_->enroll_fingerprint(finger_id, num_scans);
281 } else {
282 this->parent_->enroll_fingerprint(finger_id, 2);
283 }
284 }
285};
286
287template<typename... Ts>
288class CancelEnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
289 public:
290 void play(Ts... x) override { this->parent_->finish_enrollment(1); }
291};
292
293template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
294 public:
295 TEMPLATABLE_VALUE(uint16_t, finger_id)
296
297 void play(Ts... x) override {
298 auto finger_id = this->finger_id_.value(x...);
299 this->parent_->delete_fingerprint(finger_id);
300 }
301};
302
303template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
304 public:
305 void play(Ts... x) override { this->parent_->delete_all_fingerprints(); }
306};
307
308template<typename... Ts> class LEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
309 public:
311
312 void play(Ts... x) override {
313 auto state = this->state_.value(x...);
314 this->parent_->led_control(state);
315 }
316};
317
318template<typename... Ts> class AuraLEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
319 public:
321 TEMPLATABLE_VALUE(uint8_t, speed)
322 TEMPLATABLE_VALUE(uint8_t, color)
323 TEMPLATABLE_VALUE(uint8_t, count)
324
325 void play(Ts... x) override {
326 auto state = this->state_.value(x...);
327 auto speed = this->speed_.value(x...);
328 auto color = this->color_.value(x...);
329 auto count = this->count_.value(x...);
330
331 this->parent_->aura_led_control(state, speed, color, count);
332 }
333};
334
335} // namespace fingerprint_grow
336} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:712
FingerprintGrowComponent * parent_
Definition helpers.h:723
This class simplifies creating components that periodically check a state.
Definition component.h:429
void trigger(Ts... x)
Definition automation.h:145
Base class for all binary_sensor-type classes.
TEMPLATABLE_VALUE(uint8_t, state) TEMPLATABLE_VALUE(uint8_t
speed count void play(Ts... x) override
TEMPLATABLE_VALUE(uint16_t, finger_id) void play(Ts... x) override
num_scans void play(Ts... x) override
TEMPLATABLE_VALUE(uint16_t, finger_id) TEMPLATABLE_VALUE(uint8_t
EnrollmentDoneTrigger(FingerprintGrowComponent *parent)
EnrollmentFailedTrigger(FingerprintGrowComponent *parent)
EnrollmentScanTrigger(FingerprintGrowComponent *parent)
FingerScanInvalidTrigger(FingerprintGrowComponent *parent)
FingerScanMatchedTrigger(FingerprintGrowComponent *parent)
FingerScanMisplacedTrigger(FingerprintGrowComponent *parent)
FingerScanStartTrigger(FingerprintGrowComponent *parent)
FingerScanUnmatchedTrigger(FingerprintGrowComponent *parent)
CallbackManager< void(uint16_t, uint16_t)> finger_scan_matched_callback_
void add_on_enrollment_done_callback(std::function< void(uint16_t)> callback)
void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor)
void set_capacity_sensor(sensor::Sensor *capacity_sensor)
void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers)
uint8_t transfer_(std::vector< uint8_t > *p_data_buffer)
CallbackManager< void(uint16_t)> enrollment_done_callback_
void add_on_enrollment_scan_callback(std::function< void(uint8_t, uint16_t)> callback)
void add_on_finger_scan_invalid_callback(std::function< void()> callback)
CallbackManager< void(uint16_t)> enrollment_failed_callback_
void add_on_finger_scan_matched_callback(std::function< void(uint16_t, uint16_t)> callback)
void add_on_finger_scan_unmatched_callback(std::function< void()> callback)
void add_on_enrollment_failed_callback(std::function< void(uint16_t)> callback)
void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor)
void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count)
CallbackManager< void(uint8_t, uint16_t)> enrollment_scan_callback_
void add_on_finger_scan_misplaced_callback(std::function< void()> callback)
void set_security_level_sensor(sensor::Sensor *security_level_sensor)
void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor)
void set_enrolling_binary_sensor(binary_sensor::BinarySensor *enrolling_binary_sensor)
void add_on_finger_scan_start_callback(std::function< void()> callback)
void set_status_sensor(sensor::Sensor *status_sensor)
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
Base-class for all sensors.
Definition sensor.h:42
int speed
Definition fan.h:1
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5