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