-
Notifications
You must be signed in to change notification settings - Fork 2
/
rxxx.h
220 lines (190 loc) · 7.56 KB
/
rxxx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/uart/uart.h"
#include "Adafruit_Fingerprint.h"
namespace esphome {
namespace rxxx {
enum AuraLEDMode : uint8_t {
BREATHING = FINGERPRINT_LED_BREATHING,
FLASHING = FINGERPRINT_LED_FLASHING,
ALWAYS_ON = FINGERPRINT_LED_ON,
ALWAYS_OFF = FINGERPRINT_LED_OFF,
GRADUAL_ON = FINGERPRINT_LED_GRADUAL_ON,
GRADUAL_OFF = FINGERPRINT_LED_GRADUAL_OFF,
RED = FINGERPRINT_LED_RED,
BLUE = FINGERPRINT_LED_BLUE,
PURPLE = FINGERPRINT_LED_PURPLE,
};
class RxxxComponent : public PollingComponent, public uart::UARTDevice {
public:
void update() override;
void setup() override;
void dump_config() override;
void set_sensing_pin(GPIOPin *sensing_pin) { this->sensing_pin_ = sensing_pin; }
void set_password(uint32_t password) { this->password_ = password; }
void set_new_password(uint32_t new_password) { this->new_password_ = &new_password; }
void set_uart(Stream *uart_device) { this->finger_ = new Adafruit_Fingerprint(uart_device, password_); }
void set_fingerprint_count_sensor(sensor::Sensor *fingerprint_count_sensor) {
this->fingerprint_count_sensor_ = fingerprint_count_sensor;
}
void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
void set_capacity_sensor(sensor::Sensor *capacity_sensor) { this->capacity_sensor_ = capacity_sensor; }
void set_security_level_sensor(sensor::Sensor *security_level_sensor) {
this->security_level_sensor_ = security_level_sensor;
}
void set_last_finger_id_sensor(sensor::Sensor *last_finger_id_sensor) {
this->last_finger_id_sensor_ = last_finger_id_sensor;
}
void set_last_confidence_sensor(sensor::Sensor *last_confidence_sensor) {
this->last_confidence_sensor_ = last_confidence_sensor;
}
void set_enrolling_binary_sensor(binary_sensor::BinarySensor *enrolling_binary_sensor) {
this->enrolling_binary_sensor_ = enrolling_binary_sensor;
}
void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
this->finger_scan_matched_callback_.add(std::move(callback));
}
void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
this->finger_scan_unmatched_callback_.add(std::move(callback));
}
void add_on_enrollment_scan_callback(std::function<void(uint8_t, uint16_t)> callback) {
this->enrollment_scan_callback_.add(std::move(callback));
}
void add_on_enrollment_done_callback(std::function<void(uint16_t)> callback) {
this->enrollment_done_callback_.add(std::move(callback));
}
void add_on_enrollment_failed_callback(std::function<void(uint16_t)> callback) {
this->enrollment_failed_callback_.add(std::move(callback));
}
void enroll_fingerprint(uint16_t finger_id, uint8_t num_buffers);
void finish_enrollment(uint8_t result);
void delete_fingerprint(uint16_t finger_id);
void delete_all_fingerprints();
void led_control(bool on);
void aura_led_control(uint8_t state, uint8_t speed, uint8_t color, uint8_t count);
protected:
void scan_and_match_();
uint8_t scan_image_(uint8_t buffer);
void get_fingerprint_count_();
Adafruit_Fingerprint *finger_;
uint32_t password_ = 0x0;
uint32_t *new_password_{nullptr};
GPIOPin *sensing_pin_{nullptr};
uint8_t enrollment_image_ = 0;
uint16_t enrollment_slot_ = 0;
uint8_t enrollment_buffers_ = 5;
bool waiting_removal_ = false;
uint32_t last_aura_led_control_ = 0;
uint16_t last_aura_led_duration_ = 0;
sensor::Sensor *fingerprint_count_sensor_{nullptr};
sensor::Sensor *status_sensor_{nullptr};
sensor::Sensor *capacity_sensor_{nullptr};
sensor::Sensor *security_level_sensor_{nullptr};
sensor::Sensor *last_finger_id_sensor_{nullptr};
sensor::Sensor *last_confidence_sensor_{nullptr};
binary_sensor::BinarySensor *enrolling_binary_sensor_{nullptr};
CallbackManager<void(uint16_t, uint16_t)> finger_scan_matched_callback_;
CallbackManager<void()> finger_scan_unmatched_callback_;
CallbackManager<void(uint8_t, uint16_t)> enrollment_scan_callback_;
CallbackManager<void(uint16_t)> enrollment_done_callback_;
CallbackManager<void(uint16_t)> enrollment_failed_callback_;
};
class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
public:
explicit FingerScanMatchedTrigger(RxxxComponent *parent) {
parent->add_on_finger_scan_matched_callback(
[this](uint16_t finger_id, uint16_t confidence) {
this->trigger(finger_id, confidence);
});
}
};
class FingerScanUnmatchedTrigger : public Trigger<> {
public:
explicit FingerScanUnmatchedTrigger(RxxxComponent *parent) {
parent->add_on_finger_scan_unmatched_callback([this]() { this->trigger(); });
}
};
class EnrollmentScanTrigger : public Trigger<uint8_t, uint16_t> {
public:
explicit EnrollmentScanTrigger(RxxxComponent *parent) {
parent->add_on_enrollment_scan_callback(
[this](uint8_t scan_number, uint16_t finger_id) {
this->trigger(scan_number, finger_id);
});
}
};
class EnrollmentDoneTrigger : public Trigger<uint16_t> {
public:
explicit EnrollmentDoneTrigger(RxxxComponent *parent) {
parent->add_on_enrollment_done_callback(
[this](uint16_t finger_id) {
this->trigger(finger_id);
});
}
};
class EnrollmentFailedTrigger : public Trigger<uint16_t> {
public:
explicit EnrollmentFailedTrigger(RxxxComponent *parent) {
parent->add_on_enrollment_failed_callback(
[this](uint16_t finger_id) {
this->trigger(finger_id);
});
}
};
template<typename... Ts> class EnrollmentAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
TEMPLATABLE_VALUE(uint16_t, finger_id)
TEMPLATABLE_VALUE(uint8_t, num_scans)
void play(Ts... x) override {
auto finger_id = this->finger_id_.value(x...);
auto num_scans = this->num_scans_.value(x...);
if (num_scans) {
this->parent_->enroll_fingerprint(finger_id, num_scans);
} else {
this->parent_->enroll_fingerprint(finger_id, 2);
}
}
};
template<typename... Ts> class CancelEnrollmentAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
void play(Ts... x) override { this->parent_->finish_enrollment(1); }
};
template<typename... Ts> class DeleteAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
TEMPLATABLE_VALUE(uint16_t, finger_id)
void play(Ts... x) override {
auto finger_id = this->finger_id_.value(x...);
this->parent_->delete_fingerprint(finger_id);
}
};
template<typename... Ts> class DeleteAllAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
void play(Ts... x) override { this->parent_->delete_all_fingerprints(); }
};
template<typename... Ts> class LEDControlAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
auto state = this->state_.value(x...);
this->parent_->led_control(state);
}
};
template<typename... Ts> class AuraLEDControlAction : public Action<Ts...>, public Parented<RxxxComponent> {
public:
TEMPLATABLE_VALUE(uint8_t, state)
TEMPLATABLE_VALUE(uint8_t, speed)
TEMPLATABLE_VALUE(uint8_t, color)
TEMPLATABLE_VALUE(uint8_t, count)
void play(Ts... x) override {
auto state = this->state_.value(x...);
auto speed = this->speed_.value(x...);
auto color = this->color_.value(x...);
auto count = this->count_.value(x...);
this->parent_->aura_led_control(state, speed, color, count);
}
};
} // namespace rxxx
} // namespace esphome