ESPHome  2022.12.8
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 
17  COMMAND = 0x01,
18  DATA = 0x02,
19  ACK = 0x07,
20  END_DATA = 0x08,
21 };
22 
24  GET_IMAGE = 0x01,
25  IMAGE_2_TZ = 0x02,
26  SEARCH = 0x04,
27  REG_MODEL = 0x05,
28  STORE = 0x06,
29  LOAD = 0x07,
30  UPLOAD = 0x08,
31  DELETE = 0x0C,
32  EMPTY = 0x0D,
34  SET_PASSWORD = 0x12,
38  AURA_CONFIG = 0x35,
39  LED_ON = 0x50,
40  LED_OFF = 0x51,
41 };
42 
44  OK = 0x00,
46  NO_FINGER = 0x02,
47  IMAGE_FAIL = 0x03,
48  IMAGE_MESS = 0x06,
49  FEATURE_FAIL = 0x07,
50  NO_MATCH = 0x08,
51  NOT_FOUND = 0x09,
53  BAD_LOCATION = 0x0B,
54  DB_RANGE_FAIL = 0x0C,
57  UPLOAD_FAIL = 0x0F,
58  DELETE_FAIL = 0x10,
59  DB_CLEAR_FAIL = 0x11,
60  PASSWORD_FAIL = 0x13,
61  INVALID_IMAGE = 0x15,
62  FLASH_ERR = 0x18,
63  INVALID_REG = 0x1A,
64  BAD_PACKET = 0xFE,
65  TIMEOUT = 0xFF,
66 };
67 
69  BREATHING = 0x01,
70  FLASHING = 0x02,
71  ALWAYS_ON = 0x03,
72  ALWAYS_OFF = 0x04,
73  GRADUAL_ON = 0x05,
74  GRADUAL_OFF = 0x06,
75 };
76 
78  RED = 0x01,
79  BLUE = 0x02,
80  PURPLE = 0x03,
81  GREEN = 0x04,
82  YELLOW = 0x05,
83  CYAN = 0x06,
84  WHITE = 0x07,
85 };
86 
88  public:
89  void update() override;
90  void setup() override;
91  void dump_config() override;
92 
93  void set_address(uint32_t address) {
94  this->address_[0] = (uint8_t)(address >> 24);
95  this->address_[1] = (uint8_t)(address >> 16);
96  this->address_[2] = (uint8_t)(address >> 8);
97  this->address_[3] = (uint8_t)(address & 0xFF);
98  }
99  void set_sensing_pin(GPIOPin *sensing_pin) { this->sensing_pin_ = sensing_pin; }
100  void set_password(uint32_t password) { this->password_ = password; }
101  void set_new_password(uint32_t new_password) { this->new_password_ = new_password; }
102  void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor) {
103  this->fingerprint_count_sensor_ = fingerprint_count_sensor;
104  }
105  void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
106  void set_capacity_sensor(sensor::Sensor *capacity_sensor) { this->capacity_sensor_ = capacity_sensor; }
107  void set_security_level_sensor(sensor::Sensor *security_level_sensor) {
108  this->security_level_sensor_ = security_level_sensor;
109  }
110  void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor) {
111  this->last_finger_id_sensor_ = last_finger_id_sensor;
112  }
113  void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor) {
114  this->last_confidence_sensor_ = last_confidence_sensor;
115  }
117  this->enrolling_binary_sensor_ = enrolling_binary_sensor;
118  }
119  void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
120  this->finger_scan_matched_callback_.add(std::move(callback));
121  }
122  void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
123  this->finger_scan_unmatched_callback_.add(std::move(callback));
124  }
125  void add_on_enrollment_scan_callback(std::function<void(uint8_t, uint16_t)> callback) {
126  this->enrollment_scan_callback_.add(std::move(callback));
127  }
128  void add_on_enrollment_done_callback(std::function<void(uint16_t)> callback) {
129  this->enrollment_done_callback_.add(std::move(callback));
130  }
131 
132  void add_on_enrollment_failed_callback(std::function<void(uint16_t)> callback) {
133  this->enrollment_failed_callback_.add(std::move(callback));
134  }
135 
136  void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers);
137  void finish_enrollment(uint8_t result);
138  void delete_fingerprint(uint16_t finger_id);
140 
141  void led_control(bool state);
142  void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count);
143 
144  protected:
145  void scan_and_match_();
146  uint8_t scan_image_(uint8_t buffer);
147  uint8_t save_fingerprint_();
148  bool check_password_();
149  bool set_password_();
150  bool get_parameters_();
151  void get_fingerprint_count_();
152  uint8_t send_command_();
153 
154  std::vector<uint8_t> data_ = {};
155  uint8_t address_[4] = {0xFF, 0xFF, 0xFF, 0xFF};
156  uint16_t capacity_ = 64;
157  uint32_t password_ = 0x0;
158  uint32_t new_password_ = -1;
160  uint8_t enrollment_image_ = 0;
161  uint16_t enrollment_slot_ = 0;
162  uint8_t enrollment_buffers_ = 5;
163  bool waiting_removal_ = false;
178 };
179 
180 class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
181  public:
184  [this](uint16_t finger_id, uint16_t confidence) { this->trigger(finger_id, confidence); });
185  }
186 };
187 
189  public:
191  parent->add_on_finger_scan_unmatched_callback([this]() { this->trigger(); });
192  }
193 };
194 
195 class EnrollmentScanTrigger : public Trigger<uint8_t, uint16_t> {
196  public:
199  [this](uint8_t scan_num, uint16_t finger_id) { this->trigger(scan_num, finger_id); });
200  }
201 };
202 
203 class EnrollmentDoneTrigger : public Trigger<uint16_t> {
204  public:
206  parent->add_on_enrollment_done_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
207  }
208 };
209 
210 class EnrollmentFailedTrigger : public Trigger<uint16_t> {
211  public:
213  parent->add_on_enrollment_failed_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
214  }
215 };
216 
217 template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
218  public:
219  TEMPLATABLE_VALUE(uint16_t, finger_id)
220  TEMPLATABLE_VALUE(uint8_t, num_scans)
221 
222  void play(Ts... x) override {
223  auto finger_id = this->finger_id_.value(x...);
224  auto num_scans = this->num_scans_.value(x...);
225  if (num_scans) {
226  this->parent_->enroll_fingerprint(finger_id, num_scans);
227  } else {
228  this->parent_->enroll_fingerprint(finger_id, 2);
229  }
230  }
231 };
232 
233 template<typename... Ts>
234 class CancelEnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
235  public:
236  void play(Ts... x) override { this->parent_->finish_enrollment(1); }
237 };
238 
239 template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
240  public:
241  TEMPLATABLE_VALUE(uint16_t, finger_id)
242 
243  void play(Ts... x) override {
244  auto finger_id = this->finger_id_.value(x...);
245  this->parent_->delete_fingerprint(finger_id);
246  }
247 };
248 
249 template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
250  public:
251  void play(Ts... x) override { this->parent_->delete_all_fingerprints(); }
252 };
253 
254 template<typename... Ts> class LEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
255  public:
257 
258  void play(Ts... x) override {
259  auto state = this->state_.value(x...);
260  this->parent_->led_control(state);
261  }
262 };
263 
264 template<typename... Ts> class AuraLEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
265  public:
266  TEMPLATABLE_VALUE(uint8_t, state)
267  TEMPLATABLE_VALUE(uint8_t, speed)
268  TEMPLATABLE_VALUE(uint8_t, color)
269  TEMPLATABLE_VALUE(uint8_t, count)
270 
271  void play(Ts... x) override {
272  auto state = this->state_.value(x...);
273  auto speed = this->speed_.value(x...);
274  auto color = this->color_.value(x...);
275  auto count = this->count_.value(x...);
276 
277  this->parent_->aura_led_control(state, speed, color, count);
278  }
279 };
280 
281 } // namespace fingerprint_grow
282 } // 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)
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)
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:267
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)
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)
EnrollmentDoneTrigger(FingerprintGrowComponent *parent)
CallbackManager< void(uint16_t)> enrollment_done_callback_
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
Base-class for all sensors.
Definition: sensor.h:50
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:500
void add_on_finger_scan_unmatched_callback(std::function< void()> callback)
bool state
Definition: fan.h:34