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