ESPHome  2024.5.3
fingerprint_grow.h
Go to the documentation of this file.
1 #pragma once
2 
8 
9 #include <vector>
10 
11 namespace esphome {
12 namespace fingerprint_grow {
13 
14 static const uint16_t START_CODE = 0xEF01;
15 
16 static const uint16_t ENROLLMENT_SLOT_UNUSED = 0xFFFF;
17 
18 // The datasheet says a max wake up time of of 200ms.
19 static const uint8_t WAIT_FOR_WAKE_UP_MS = 200;
20 
21 static const uint32_t DEFAULT_IDLE_PERIOD_TO_SLEEP_MS = 5000;
22 
24  COMMAND = 0x01,
25  DATA = 0x02,
26  ACK = 0x07,
27  END_DATA = 0x08,
28 };
29 
31  GET_IMAGE = 0x01,
32  IMAGE_2_TZ = 0x02,
33  SEARCH = 0x04,
34  REG_MODEL = 0x05,
35  STORE = 0x06,
36  LOAD = 0x07,
37  UPLOAD = 0x08,
38  DELETE = 0x0C,
39  EMPTY = 0x0D,
41  SET_PASSWORD = 0x12,
45  AURA_CONFIG = 0x35,
46  LED_ON = 0x50,
47  LED_OFF = 0x51,
48 };
49 
51  OK = 0x00,
53  NO_FINGER = 0x02,
54  IMAGE_FAIL = 0x03,
55  IMAGE_MESS = 0x06,
56  FEATURE_FAIL = 0x07,
57  NO_MATCH = 0x08,
58  NOT_FOUND = 0x09,
60  BAD_LOCATION = 0x0B,
61  DB_RANGE_FAIL = 0x0C,
64  UPLOAD_FAIL = 0x0F,
65  DELETE_FAIL = 0x10,
66  DB_CLEAR_FAIL = 0x11,
67  PASSWORD_FAIL = 0x13,
68  INVALID_IMAGE = 0x15,
69  FLASH_ERR = 0x18,
70  INVALID_REG = 0x1A,
72  BAD_PACKET = 0xFE,
73  TIMEOUT = 0xFF,
74 };
75 
77  BREATHING = 0x01,
78  FLASHING = 0x02,
79  ALWAYS_ON = 0x03,
80  ALWAYS_OFF = 0x04,
81  GRADUAL_ON = 0x05,
82  GRADUAL_OFF = 0x06,
83 };
84 
86  RED = 0x01,
87  BLUE = 0x02,
88  PURPLE = 0x03,
89  GREEN = 0x04,
90  YELLOW = 0x05,
91  CYAN = 0x06,
92  WHITE = 0x07,
93 };
94 
96  public:
97  void update() override;
98  void setup() override;
99  void dump_config() override;
100 
101  void set_address(uint32_t address) {
102  this->address_[0] = (uint8_t) (address >> 24);
103  this->address_[1] = (uint8_t) (address >> 16);
104  this->address_[2] = (uint8_t) (address >> 8);
105  this->address_[3] = (uint8_t) (address & 0xFF);
106  }
107  void set_sensing_pin(GPIOPin *sensing_pin) { this->sensing_pin_ = sensing_pin; }
108  void set_sensor_power_pin(GPIOPin *sensor_power_pin) { this->sensor_power_pin_ = sensor_power_pin; }
109  void set_password(uint32_t password) { this->password_ = password; }
110  void set_new_password(uint32_t new_password) { this->new_password_ = new_password; }
111  void set_idle_period_to_sleep_ms(uint32_t period_ms) { this->idle_period_to_sleep_ms_ = period_ms; }
112  void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor) {
113  this->fingerprint_count_sensor_ = fingerprint_count_sensor;
114  }
115  void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
116  void set_capacity_sensor(sensor::Sensor *capacity_sensor) { this->capacity_sensor_ = capacity_sensor; }
117  void set_security_level_sensor(sensor::Sensor *security_level_sensor) {
118  this->security_level_sensor_ = security_level_sensor;
119  }
120  void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor) {
121  this->last_finger_id_sensor_ = last_finger_id_sensor;
122  }
123  void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor) {
124  this->last_confidence_sensor_ = last_confidence_sensor;
125  }
127  this->enrolling_binary_sensor_ = enrolling_binary_sensor;
128  }
129  void add_on_finger_scan_start_callback(std::function<void()> callback) {
130  this->finger_scan_start_callback_.add(std::move(callback));
131  }
132  void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
133  this->finger_scan_matched_callback_.add(std::move(callback));
134  }
135  void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
136  this->finger_scan_unmatched_callback_.add(std::move(callback));
137  }
138  void add_on_finger_scan_misplaced_callback(std::function<void()> callback) {
139  this->finger_scan_misplaced_callback_.add(std::move(callback));
140  }
141  void add_on_finger_scan_invalid_callback(std::function<void()> callback) {
142  this->finger_scan_invalid_callback_.add(std::move(callback));
143  }
144  void add_on_enrollment_scan_callback(std::function<void(uint8_t, uint16_t)> callback) {
145  this->enrollment_scan_callback_.add(std::move(callback));
146  }
147  void add_on_enrollment_done_callback(std::function<void(uint16_t)> callback) {
148  this->enrollment_done_callback_.add(std::move(callback));
149  }
150 
151  void add_on_enrollment_failed_callback(std::function<void(uint16_t)> callback) {
152  this->enrollment_failed_callback_.add(std::move(callback));
153  }
154 
155  void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers);
156  void finish_enrollment(uint8_t result);
157  void delete_fingerprint(uint16_t finger_id);
159 
160  void led_control(bool state);
161  void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count);
162 
163  protected:
164  void scan_and_match_();
165  uint8_t scan_image_(uint8_t buffer);
166  uint8_t save_fingerprint_();
167  bool check_password_();
168  bool set_password_();
169  bool get_parameters_();
170  void get_fingerprint_count_();
171  uint8_t transfer_(std::vector<uint8_t> *p_data_buffer);
172  uint8_t send_command_();
173  void sensor_wakeup_();
174  void sensor_sleep_();
175 
176  std::vector<uint8_t> data_ = {};
177  uint8_t address_[4] = {0xFF, 0xFF, 0xFF, 0xFF};
178  uint16_t capacity_ = 64;
179  uint32_t password_ = 0x0;
180  uint32_t new_password_ = -1;
183  uint8_t enrollment_image_ = 0;
184  uint16_t enrollment_slot_ = ENROLLMENT_SLOT_UNUSED;
185  uint8_t enrollment_buffers_ = 5;
186  bool waiting_removal_ = false;
187  bool has_sensing_pin_ = false;
188  bool has_power_pin_ = false;
189  bool is_sensor_awake_ = false;
190  uint32_t last_transfer_ms_ = 0;
194  uint32_t idle_period_to_sleep_ms_ = UINT32_MAX;
210 };
211 
212 class FingerScanStartTrigger : public Trigger<> {
213  public:
215  parent->add_on_finger_scan_start_callback([this]() { this->trigger(); });
216  }
217 };
218 
219 class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
220  public:
223  [this](uint16_t finger_id, uint16_t confidence) { this->trigger(finger_id, confidence); });
224  }
225 };
226 
228  public:
230  parent->add_on_finger_scan_unmatched_callback([this]() { this->trigger(); });
231  }
232 };
233 
235  public:
237  parent->add_on_finger_scan_misplaced_callback([this]() { this->trigger(); });
238  }
239 };
240 
242  public:
244  parent->add_on_finger_scan_invalid_callback([this]() { this->trigger(); });
245  }
246 };
247 
248 class EnrollmentScanTrigger : public Trigger<uint8_t, uint16_t> {
249  public:
252  [this](uint8_t scan_num, uint16_t finger_id) { this->trigger(scan_num, finger_id); });
253  }
254 };
255 
256 class EnrollmentDoneTrigger : public Trigger<uint16_t> {
257  public:
259  parent->add_on_enrollment_done_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
260  }
261 };
262 
263 class EnrollmentFailedTrigger : public Trigger<uint16_t> {
264  public:
266  parent->add_on_enrollment_failed_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
267  }
268 };
269 
270 template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
271  public:
272  TEMPLATABLE_VALUE(uint16_t, finger_id)
273  TEMPLATABLE_VALUE(uint8_t, num_scans)
274 
275  void play(Ts... x) override {
276  auto finger_id = this->finger_id_.value(x...);
277  auto num_scans = this->num_scans_.value(x...);
278  if (num_scans) {
279  this->parent_->enroll_fingerprint(finger_id, num_scans);
280  } else {
281  this->parent_->enroll_fingerprint(finger_id, 2);
282  }
283  }
284 };
285 
286 template<typename... Ts>
287 class CancelEnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
288  public:
289  void play(Ts... x) override { this->parent_->finish_enrollment(1); }
290 };
291 
292 template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
293  public:
294  TEMPLATABLE_VALUE(uint16_t, finger_id)
295 
296  void play(Ts... x) override {
297  auto finger_id = this->finger_id_.value(x...);
298  this->parent_->delete_fingerprint(finger_id);
299  }
300 };
301 
302 template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
303  public:
304  void play(Ts... x) override { this->parent_->delete_all_fingerprints(); }
305 };
306 
307 template<typename... Ts> class LEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
308  public:
310 
311  void play(Ts... x) override {
312  auto state = this->state_.value(x...);
313  this->parent_->led_control(state);
314  }
315 };
316 
317 template<typename... Ts> class AuraLEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
318  public:
319  TEMPLATABLE_VALUE(uint8_t, state)
320  TEMPLATABLE_VALUE(uint8_t, speed)
321  TEMPLATABLE_VALUE(uint8_t, color)
322  TEMPLATABLE_VALUE(uint8_t, count)
323 
324  void play(Ts... x) override {
325  auto state = this->state_.value(x...);
326  auto speed = this->speed_.value(x...);
327  auto color = this->color_.value(x...);
328  auto count = this->count_.value(x...);
329 
330  this->parent_->aura_led_control(state, speed, color, count);
331  }
332 };
333 
334 } // namespace fingerprint_grow
335 } // namespace esphome
TEMPLATABLE_VALUE(uint16_t, finger_id) void play(Ts... x) override
CallbackManager< void(uint16_t, uint16_t)> finger_scan_matched_callback_
void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor)
uint16_t x
Definition: tt21100.cpp:17
CallbackManager< void(uint8_t, uint16_t)> enrollment_scan_callback_
FingerScanMatchedTrigger(FingerprintGrowComponent *parent)
void add_on_enrollment_failed_callback(std::function< void(uint16_t)> callback)
int speed
Definition: fan.h:35
void add_on_enrollment_done_callback(std::function< void(uint16_t)> callback)
void add_on_finger_scan_invalid_callback(std::function< void()> callback)
FingerScanUnmatchedTrigger(FingerprintGrowComponent *parent)
TEMPLATABLE_VALUE(bool, state) void play(Ts... x) override
void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor)
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_status_sensor(sensor::Sensor *status_sensor)
UARTComponent * parent_
Definition: uart.h:68
EnrollmentScanTrigger(FingerprintGrowComponent *parent)
EnrollmentFailedTrigger(FingerprintGrowComponent *parent)
void set_enrolling_binary_sensor(binary_sensor::BinarySensor *enrolling_binary_sensor)
void set_capacity_sensor(sensor::Sensor *capacity_sensor)
uint8_t transfer_(std::vector< uint8_t > *p_data_buffer)
void add_on_finger_scan_start_callback(std::function< void()> callback)
void add_on_finger_scan_matched_callback(std::function< void(uint16_t, uint16_t)> callback)
void set_security_level_sensor(sensor::Sensor *security_level_sensor)
void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers)
CallbackManager< void(uint16_t)> enrollment_failed_callback_
binary_sensor::BinarySensor * enrolling_binary_sensor_
void add_on_enrollment_scan_callback(std::function< void(uint8_t, uint16_t)> callback)
FingerScanMisplacedTrigger(FingerprintGrowComponent *parent)
EnrollmentDoneTrigger(FingerprintGrowComponent *parent)
FingerScanStartTrigger(FingerprintGrowComponent *parent)
CallbackManager< void(uint16_t)> enrollment_done_callback_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
FingerScanInvalidTrigger(FingerprintGrowComponent *parent)
void set_sensor_power_pin(GPIOPin *sensor_power_pin)
Base-class for all sensors.
Definition: sensor.h:57
void add_on_finger_scan_misplaced_callback(std::function< void()> callback)
void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count)
void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:525
void add_on_finger_scan_unmatched_callback(std::function< void()> callback)
bool state
Definition: fan.h:34