forked from adafruit/Adafruit-Fingerprint-Sensor-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_Fingerprint.cpp
executable file
·342 lines (291 loc) · 9.33 KB
/
Adafruit_Fingerprint.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/***************************************************
This is a library for our optical Fingerprint sensor
Designed specifically to work with the Adafruit Fingerprint sensor
----> http://www.adafruit.com/products/751
These displays use TTL Serial to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_Fingerprint.h"
#ifdef __AVR__
#include <util/delay.h>
#include <SoftwareSerial.h>
#endif
#ifdef __AVR__
Adafruit_Fingerprint::Adafruit_Fingerprint(SoftwareSerial *ss) {
thePassword = 0;
theAddress = 0xFFFFFFFF;
hwSerial = NULL;
swSerial = ss;
mySerial = swSerial;
}
#endif
Adafruit_Fingerprint::Adafruit_Fingerprint(HardwareSerial *ss) {
thePassword = 0;
theAddress = 0xFFFFFFFF;
#ifdef __AVR__
swSerial = NULL;
#endif
hwSerial = ss;
mySerial = hwSerial;
}
void Adafruit_Fingerprint::begin(uint16_t baudrate) {
delay(1000); // one second delay to let the sensor 'boot up'
if (hwSerial) hwSerial->begin(baudrate);
#ifdef __AVR__
if (swSerial) swSerial->begin(baudrate);
#endif
}
boolean Adafruit_Fingerprint::verifyPassword(void) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_VERIFYPASSWORD;
packet[1]=(thePassword >> 24);
packet[2]=(thePassword >> 16);
packet[3]=(thePassword >> 8);
packet[4]=thePassword;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 7, packet);
uint8_t len = getReply(packet);
if ((len == 1) && (packet[0] == FINGERPRINT_ACKPACKET) && (packet[1] == FINGERPRINT_OK))
return true;
return false;
}
uint8_t Adafruit_Fingerprint::getImage(void) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_GETIMAGE;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 3, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::image2Tz(uint8_t slot) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_IMAGE2TZ;
packet[1]=slot;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 4, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::createModel(void) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_REGMODEL;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 3, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::storeModel(uint16_t id) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_STORE;
packet[1]=0x01;
packet[2]=id >> 8;
packet[3]=id & 0xFF;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 6, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
//read a fingerprint template from flash into Char Buffer 1
uint8_t Adafruit_Fingerprint::loadModel(uint16_t id) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_LOAD;
packet[1]=0x01;
packet[2]=id >> 8;
packet[3]=id & 0xFF;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 6, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
//transfer a fingerprint template from Char Buffer 1 to host computer
uint8_t Adafruit_Fingerprint::getModel(void) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_UPLOAD;
packet[1]=0x01;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 4, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::deleteModel(uint16_t id) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_DELETE;
packet[1]=id >> 8;
packet[2]=id & 0xFF;
packet[3]=0x00;
packet[4]=0x01;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 7, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::emptyDatabase(void) {
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_EMPTY;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 3, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
return packet[1];
}
uint8_t Adafruit_Fingerprint::fingerFastSearch(void) {
fingerID = 0xFFFF;
confidence = 0xFFFF;
// high speed search of slot #1 starting at page 0x0000 and page #0x00A3
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_HISPEEDSEARCH;
packet[1]=0x01;
packet[2]=0x00;
packet[3]=0x00;
packet[4]=0x00;
packet[5]=0xA3;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 8, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
fingerID = packet[2];
fingerID <<= 8;
fingerID |= packet[3];
confidence = packet[4];
confidence <<= 8;
confidence |= packet[5];
return packet[1];
}
uint8_t Adafruit_Fingerprint::getTemplateCount(void) {
templateCount = 0xFFFF;
// get number of templates in memory
uint8_t *packet=packetBuffer;
packet[0]=FINGERPRINT_TEMPLATECOUNT;
writePacket(theAddress, FINGERPRINT_COMMANDPACKET, 3, packet);
uint8_t len = getReply(packet);
if ((len != 1) && (packet[0] != FINGERPRINT_ACKPACKET))
return -1;
templateCount = packet[2];
templateCount <<= 8;
templateCount |= packet[3];
return packet[1];
}
void Adafruit_Fingerprint::writePacket(uint32_t addr, uint8_t packettype,
uint16_t len, uint8_t *packet) {
#ifdef FINGERPRINT_DEBUG
Serial.print("---> 0x");
Serial.print((uint8_t)(FINGERPRINT_STARTCODE >> 8), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)FINGERPRINT_STARTCODE, HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(addr >> 24), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(addr >> 16), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(addr >> 8), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(addr), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)packettype, HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(len >> 8), HEX);
Serial.print(" 0x");
Serial.print((uint8_t)(len), HEX);
#endif
#if ARDUINO >= 100
mySerial->write((uint8_t)(FINGERPRINT_STARTCODE >> 8));
mySerial->write((uint8_t)FINGERPRINT_STARTCODE);
mySerial->write((uint8_t)(addr >> 24));
mySerial->write((uint8_t)(addr >> 16));
mySerial->write((uint8_t)(addr >> 8));
mySerial->write((uint8_t)(addr));
mySerial->write((uint8_t)packettype);
mySerial->write((uint8_t)(len >> 8));
mySerial->write((uint8_t)(len));
#else
mySerial->print((uint8_t)(FINGERPRINT_STARTCODE >> 8), BYTE);
mySerial->print((uint8_t)FINGERPRINT_STARTCODE, BYTE);
mySerial->print((uint8_t)(addr >> 24), BYTE);
mySerial->print((uint8_t)(addr >> 16), BYTE);
mySerial->print((uint8_t)(addr >> 8), BYTE);
mySerial->print((uint8_t)(addr), BYTE);
mySerial->print((uint8_t)packettype, BYTE);
mySerial->print((uint8_t)(len >> 8), BYTE);
mySerial->print((uint8_t)(len), BYTE);
#endif
uint16_t sum = (len>>8) + (len&0xFF) + packettype;
for (uint8_t i=0; i< len-2; i++) {
#if ARDUINO >= 100
mySerial->write((uint8_t)(packet[i]));
#else
mySerial->print((uint8_t)(packet[i]), BYTE);
#endif
#ifdef FINGERPRINT_DEBUG
Serial.print(" 0x"); Serial.print(packet[i], HEX);
#endif
sum += packet[i];
}
#ifdef FINGERPRINT_DEBUG
//Serial.print("Checksum = 0x"); Serial.println(sum);
Serial.print(" 0x"); Serial.print((uint8_t)(sum>>8), HEX);
Serial.print(" 0x"); Serial.println((uint8_t)(sum), HEX);
#endif
#if ARDUINO >= 100
mySerial->write((uint8_t)(sum>>8));
mySerial->write((uint8_t)sum);
#else
mySerial->print((uint8_t)(sum>>8), BYTE);
mySerial->print((uint8_t)sum, BYTE);
#endif
}
uint8_t Adafruit_Fingerprint::getReply(uint8_t *packet, uint16_t timeout) {
uint8_t reply[20], idx;
uint16_t timer=0;
idx = 0;
#ifdef FINGERPRINT_DEBUG
Serial.print("<--- ");
#endif
while (true) {
while (!mySerial->available()) {
delay(1);
timer++;
if (timer >= timeout) return FINGERPRINT_TIMEOUT;
}
// something to read!
reply[idx] = mySerial->read();
#ifdef FINGERPRINT_DEBUG
Serial.print(" 0x"); Serial.print(reply[idx], HEX);
#endif
if ((idx == 0) && (reply[0] != (FINGERPRINT_STARTCODE >> 8)))
continue;
idx++;
// check packet!
if (idx >= 9) {
if ((reply[0] != (FINGERPRINT_STARTCODE >> 8)) ||
(reply[1] != (FINGERPRINT_STARTCODE & 0xFF)))
return FINGERPRINT_BADPACKET;
uint8_t packettype = reply[6];
//Serial.print("Packet type"); Serial.println(packettype);
uint16_t len = reply[7];
len <<= 8;
len |= reply[8];
len -= 2;
//Serial.print("Packet len"); Serial.println(len);
if (idx <= (len+10)) continue;
packet[0] = packettype;
for (uint8_t i=0; i<len; i++) {
packet[1+i] = reply[9+i];
}
#ifdef FINGERPRINT_DEBUG
Serial.println();
#endif
return len;
}
}
}