ESPHome  2022.11.3
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  GREEN = 0x04,
80  YELLOW = 0x05,
81  CYAN = 0x06,
82  WHITE = 0x07,
83 };
84 
86  public:
87  void update() override;
88  void setup() override;
89  void dump_config() override;
90 
91  void set_address(uint32_t address) {
92  this->address_[0] = (uint8_t)(address >> 24);
93  this->address_[1] = (uint8_t)(address >> 16);
94  this->address_[2] = (uint8_t)(address >> 8);
95  this->address_[3] = (uint8_t)(address & 0xFF);
96  }
97  void set_sensing_pin(GPIOPin *sensing_pin) { this->sensing_pin_ = sensing_pin; }
98  void set_password(uint32_t password) { this->password_ = password; }
99  void set_new_password(uint32_t new_password) { this->new_password_ = new_password; }
100  void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor) {
101  this->fingerprint_count_sensor_ = fingerprint_count_sensor;
102  }
103  void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
104  void set_capacity_sensor(sensor::Sensor *capacity_sensor) { this->capacity_sensor_ = capacity_sensor; }
105  void set_security_level_sensor(sensor::Sensor *security_level_sensor) {
106  this->security_level_sensor_ = security_level_sensor;
107  }
108  void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor) {
109  this->last_finger_id_sensor_ = last_finger_id_sensor;
110  }
111  void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor) {
112  this->last_confidence_sensor_ = last_confidence_sensor;
113  }
115  this->enrolling_binary_sensor_ = enrolling_binary_sensor;
116  }
117  void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
118  this->finger_scan_matched_callback_.add(std::move(callback));
119  }
120  void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
121  this->finger_scan_unmatched_callback_.add(std::move(callback));
122  }
123  void add_on_enrollment_scan_callback(std::function<void(uint8_t, uint16_t)> callback) {
124  this->enrollment_scan_callback_.add(std::move(callback));
125  }
126  void add_on_enrollment_done_callback(std::function<void(uint16_t)> callback) {
127  this->enrollment_done_callback_.add(std::move(callback));
128  }
129 
130  void add_on_enrollment_failed_callback(std::function<void(uint16_t)> callback) {
131  this->enrollment_failed_callback_.add(std::move(callback));
132  }
133 
134  void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers);
135  void finish_enrollment(uint8_t result);
136  void delete_fingerprint(uint16_t finger_id);
138 
139  void led_control(bool state);
140  void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count);
141 
142  protected:
143  void scan_and_match_();
144  uint8_t scan_image_(uint8_t buffer);
145  uint8_t save_fingerprint_();
146  bool check_password_();
147  bool set_password_();
148  bool get_parameters_();
149  void get_fingerprint_count_();
150  uint8_t send_command_();
151 
152  std::vector<uint8_t> data_ = {};
153  uint8_t address_[4] = {0xFF, 0xFF, 0xFF, 0xFF};
154  uint16_t capacity_ = 64;
155  uint32_t password_ = 0x0;
156  uint32_t new_password_ = -1;
158  uint8_t enrollment_image_ = 0;
159  uint16_t enrollment_slot_ = 0;
160  uint8_t enrollment_buffers_ = 5;
161  bool waiting_removal_ = false;
176 };
177 
178 class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
179  public:
182  [this](uint16_t finger_id, uint16_t confidence) { this->trigger(finger_id, confidence); });
183  }
184 };
185 
187  public:
189  parent->add_on_finger_scan_unmatched_callback([this]() { this->trigger(); });
190  }
191 };
192 
193 class EnrollmentScanTrigger : public Trigger<uint8_t, uint16_t> {
194  public:
197  [this](uint8_t scan_num, uint16_t finger_id) { this->trigger(scan_num, finger_id); });
198  }
199 };
200 
201 class EnrollmentDoneTrigger : public Trigger<uint16_t> {
202  public:
204  parent->add_on_enrollment_done_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
205  }
206 };
207 
208 class EnrollmentFailedTrigger : public Trigger<uint16_t> {
209  public:
211  parent->add_on_enrollment_failed_callback([this](uint16_t finger_id) { this->trigger(finger_id); });
212  }
213 };
214 
215 template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
216  public:
217  TEMPLATABLE_VALUE(uint16_t, finger_id)
218  TEMPLATABLE_VALUE(uint8_t, num_scans)
219 
220  void play(Ts... x) override {
221  auto finger_id = this->finger_id_.value(x...);
222  auto num_scans = this->num_scans_.value(x...);
223  if (num_scans) {
224  this->parent_->enroll_fingerprint(finger_id, num_scans);
225  } else {
226  this->parent_->enroll_fingerprint(finger_id, 2);
227  }
228  }
229 };
230 
231 template<typename... Ts>
232 class CancelEnrollmentAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
233  public:
234  void play(Ts... x) override { this->parent_->finish_enrollment(1); }
235 };
236 
237 template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
238  public:
239  TEMPLATABLE_VALUE(uint16_t, finger_id)
240 
241  void play(Ts... x) override {
242  auto finger_id = this->finger_id_.value(x...);
243  this->parent_->delete_fingerprint(finger_id);
244  }
245 };
246 
247 template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
248  public:
249  void play(Ts... x) override { this->parent_->delete_all_fingerprints(); }
250 };
251 
252 template<typename... Ts> class LEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
253  public:
255 
256  void play(Ts... x) override {
257  auto state = this->state_.value(x...);
258  this->parent_->led_control(state);
259  }
260 };
261 
262 template<typename... Ts> class AuraLEDControlAction : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
263  public:
264  TEMPLATABLE_VALUE(uint8_t, state)
265  TEMPLATABLE_VALUE(uint8_t, speed)
266  TEMPLATABLE_VALUE(uint8_t, color)
267  TEMPLATABLE_VALUE(uint8_t, count)
268 
269  void play(Ts... x) override {
270  auto state = this->state_.value(x...);
271  auto speed = this->speed_.value(x...);
272  auto color = this->color_.value(x...);
273  auto count = this->count_.value(x...);
274 
275  this->parent_->aura_led_control(state, speed, color, count);
276  }
277 };
278 
279 } // namespace fingerprint_grow
280 } // 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:26
Base-class for all sensors.
Definition: sensor.h:48
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