-
Notifications
You must be signed in to change notification settings - Fork 80
/
fingerprintWrapper.cpp
382 lines (310 loc) · 6.75 KB
/
fingerprintWrapper.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "fingerprintWrapper.h"
FingerBoard::FingerBoard(HardwareSerial* hs)
{
sensor = Fingerprint(&Serial1);
sensor.begin(57600);
sensorSerial = hs;
Keyboard.begin();
}
FbStatus FingerBoard::Begin()
{
FbStatus ret = FbStatus::SUCCESS;
if (sensor.verifyPassword())
ret = FbStatus::SUCCESS;
else
ret = FbStatus::ERROR_SENSOR_NOT_FOUND;
return ret;
}
FbStatus FingerBoard::Begin(Serial_* hs)
{
debug = hs;
return Begin();
}
bool FingerBoard::CmdCheck()
{
int p;
String pswd;
while (Serial.available() > 0)
{
char nullByte = char(Serial.read());
if (nullByte == '\n')
{
comdata[data_p] = nullByte;
data_p = 0;
switch (comdata[0])
{
case 'A':
strtok(comdata, ",");
p = atoi(strtok(NULL, ","));
while (true)
{
Serial.println("Please enter the ID you want to enroll:");
if (p >= 0 && p < MAX_FINGERS)
{
Serial.print("Enrolling [");
Serial.print(p);
Serial.println("] finger...");
if (AddFinger(p))
{
Serial.println("-------------------");
Serial.println("Add ok !");
Serial.println("-------------------");
delay(2000);
}
else
{
Serial.println("-------------------");
Serial.println("Add failed !");
Serial.println("-------------------");
delay(2000);
}
break;
}
else
{
Serial.println("Please enter a valid ID !");
}
}
break;
case 'P': // not used
strtok(comdata, ",");
p = atoi(strtok(NULL, ","));
pswd = String(strtok(NULL, ";"));
passwords[p] = pswd;
Serial.print("Set finger [");
Serial.print(p);
Serial.print("]'s password to ");
Serial.println(pswd);
break;
case 'D':
DeleteAllFingers();
Serial.println("All Finger deleted !");
break;
}
}
else
{
comdata[data_p] = nullByte;
data_p++;
}
}
return true;
}
int FingerBoard::GetFingerID()
{
if (millis() - time > INTERVAL)
{
time = millis();
uint8_t p = sensor.getImage();
switch (p)
{
case FINGERPRINT_OK:
if (debug)
debug->println("Image taken");
break;
case FINGERPRINT_NOFINGER:
isTouching = false;
return -1;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_IMAGEFAIL:
if (debug)
debug->println("Imaging error");
return -1;
default:
if (debug)
debug->println("Unknown error");
return -1;
}
// OK success!
p = sensor.image2Tz();
switch (p)
{
case FINGERPRINT_OK:
if (debug)
debug->println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
if (debug)
debug->println("Image too messy");
return -1;
case FINGERPRINT_PACKETRECIEVEERR:
if (debug)
debug->println("Communication error");
return -1;
case FINGERPRINT_FEATUREFAIL:
if (debug)
debug->println("Could not find fingerprint features");
return -1;
case FINGERPRINT_INVALIDIMAGE:
if (debug)
debug->println("Could not find fingerprint features");
return -1;
default:
if (debug)
debug->println("Unknown error");
return -1;
}
// OK converted!
p = sensor.fingerFastSearch();
if (p == FINGERPRINT_OK)
{
if (debug)
debug->println("Found a print match!");
}
else if (p == FINGERPRINT_PACKETRECIEVEERR)
{
if (debug)
debug->println("Communication error");
return -1;
}
else if (p == FINGERPRINT_NOTFOUND)
{
if (debug)
debug->println("Did not find a match");
return -1;
}
else
{
if (debug)
debug->println("Unknown error");
return -1;
}
// found a match!
if (debug)
{
debug->print("Found ID #");
debug->print(sensor.fingerID);
debug->print(" with confidence of ");
debug->println(sensor.confidence);
}
}
else
return -1;
return sensor.fingerID;
}
bool FingerBoard::AddFinger(unsigned int id)
{
int FAIL_COUNT = 10;
while (FAIL_COUNT--)
{
if (FPMXX_Add_Fingerprint(id))
return true;
delay(1000);
}
return false;
}
void FingerBoard::DeleteAllFingers()
{
FPMXX_Delete_All_Fingerprint();
}
void FingerBoard::InputPassword(String pswd)
{
if (!isTouching)
{
Keyboard.println(pswd);
Keyboard.write(176); //ENTER
isTouching = true;
}
}
void FingerBoard::TypeString(String s, bool enter)
{
Keyboard.println(s);
if (enter)
Keyboard.write(176);
}
void FingerBoard::PressKey(uint8_t k)
{
Keyboard.press(k);
}
void FingerBoard::ReleaseAll()
{
Keyboard.releaseAll();
}
// Sensor drivers
void FingerBoard::FPMXX_Cmd_Save_Finger(unsigned int storeID)
{
unsigned long temp = 0;
unsigned char i;
FPMXX_Save_Finger[5] = (storeID & 0xFF00) >> 8;
FPMXX_Save_Finger[6] = (storeID & 0x00FF);
for (i = 0; i < 7; i++) //计算校验和
temp = temp + FPMXX_Save_Finger[i];
FPMXX_Save_Finger[7] = (temp & 0x00FF00) >> 8; //存放校验数据
FPMXX_Save_Finger[8] = temp & 0x0000FF;
FPMXX_Send_Cmd(9, FPMXX_Save_Finger, 12);
}
void FingerBoard::FPMXX_Send_Cmd(unsigned char length, unsigned char* address, unsigned char returnLength)
{
unsigned char i = 0;
sensorSerial->flush();
for (i = 0; i < 6; i++) //包头
{
sensorSerial->write(FPMXX_Pack_Head[i]);
}
for (i = 0; i < length; i++)
{
sensorSerial->write(*address);
address++;
}
FPMXX_Cmd_Receive_Data(returnLength);
}
void FingerBoard::FPMXX_Cmd_Receive_Data(unsigned int r_size)
{
int i = 0;
while (1)
{
if (sensorSerial->available() == r_size)
{
for (i = 0; i < r_size; i++)
{
FPMXX_RECEIVE_BUFFER[i] = sensorSerial->read();
}
break;
}
}
}
void FingerBoard::FPMXX_Cmd_StoreTemplate(unsigned int ID)
{
unsigned int temp = 0;
FPMXX_Save_Finger[5] = (ID & 0xFF00) >> 8;
FPMXX_Save_Finger[6] = (ID & 0x00FF);
for (int i = 0; i < 7; i++) //计算校验和
temp = temp + FPMXX_Save_Finger[i];
FPMXX_Save_Finger[7] = (temp & 0x00FF00) >> 8; //存放校验数据
FPMXX_Save_Finger[8] = temp & 0x0000FF;
sensorSerial->write((char*)FPMXX_Pack_Head, 6);
sensorSerial->write((char*)FPMXX_Save_Finger, 9);
FPMXX_Cmd_Receive_Data(12);
}
bool FingerBoard::FPMXX_Add_Fingerprint(unsigned int writeID)
{
FPMXX_Send_Cmd(6, FPMXX_Get_Img, 12);
//判断接收到的确认码,等于0指纹获取成功
if (FPMXX_RECEIVE_BUFFER[9] == 0)
{
delay(100);
FPMXX_Send_Cmd(7, FPMXX_Img_To_Buffer1, 12); //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
while (1)
{
FPMXX_Send_Cmd(6, FPMXX_Get_Img, 12);
//判断接收到的确认码,等于0指纹获取成功
if (FPMXX_RECEIVE_BUFFER[9] == 0)
{
delay(500);
FPMXX_Send_Cmd(7, FPMXX_Img_To_Buffer2, 12); //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
FPMXX_Cmd_StoreTemplate(writeID);
return true;
}
delay(100);
}
}
return false;
}
void FingerBoard::FPMXX_Delete_All_Fingerprint()
{
//添加清空指纹指令
FPMXX_Send_Cmd(6, FPMXX_Delete_All_Model, 12); //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
delay(2000);
}