-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinger.cpp
More file actions
476 lines (417 loc) · 14.1 KB
/
finger.cpp
File metadata and controls
476 lines (417 loc) · 14.1 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <string.h>
#include <SoftwareSerial.h>
#include "finger.h"
uint8_t finger_RxBuf[9];
uint8_t finger_TxBuf[9];
uint8_t Finger_SleepFlag = 0;
SoftwareSerial mySerial(10, 11); // RX, TX
/***************************************************************************
* @brief initialize the SoftwareSerial to communicate with Fingerprint module
****************************************************************************/
void Finger_SoftwareSerial_Init(void)
{
mySerial.begin(19200);
}
/***************************************************************************
* @brief Send a byte of data to the serial port
* @param temp : Data to send
****************************************************************************/
void TxByte(uint8_t temp)
{
mySerial.write(temp);
}
/***************************************************************************
* @brief send a command, and wait for the response of module
* @param Scnt: The number of bytes to send
Rcnt: expect the number of bytes response module
Nms: wait timeout: Delay
* @return ACK_SUCCESS: success
other: see the macro definition
****************************************************************************/
uint8_t TxAndRxCmd(uint8_t Scnt, uint8_t Rcnt, uint16_t Nms)
{
uint8_t i, j, CheckSum;
uint16_t uart_RxCount = 0;
unsigned long time_before = 0;
unsigned long time_after = 0;
uint8_t overflow_Flag = 0;;
TxByte(CMD_HEAD);
CheckSum = 0;
for (i = 0; i < Scnt; i++)
{
TxByte(finger_TxBuf[i]);
CheckSum ^= finger_TxBuf[i];
}
TxByte(CheckSum);
TxByte(CMD_TAIL);
memset(finger_RxBuf,0,sizeof(finger_RxBuf)); ////////
mySerial.flush(); /////
// Receive time out: Nms
time_before = millis();
do
{
overflow_Flag = 0;
if(mySerial.available())
{
finger_RxBuf[uart_RxCount++] = mySerial.read();
}
time_after = millis();
if(time_before > time_after) //if overflow (go back to zero)
{
time_before = millis(); // get time_before again
overflow_Flag = 1;
}
} while (((uart_RxCount < Rcnt) && (time_after - time_before < Nms)) || (overflow_Flag == 1));
if (uart_RxCount != Rcnt)return ACK_TIMEOUT;
if (finger_RxBuf[0] != CMD_HEAD) return ACK_FAIL;
if (finger_RxBuf[Rcnt - 1] != CMD_TAIL) return ACK_FAIL;
if (finger_RxBuf[1] != (finger_TxBuf[0])) return ACK_FAIL;
CheckSum = 0;
for (j = 1; j < uart_RxCount - 1; j++) CheckSum ^= finger_RxBuf[j];
if (CheckSum != 0) return ACK_FAIL;
return ACK_SUCCESS;
}
/***************************************************************************
* @brief Query the number of existing fingerprints
* @return 0xFF: error
other: success, the value is the number of existing fingerprints
****************************************************************************/
uint8_t GetUserCount(void)
{
uint8_t m;
finger_TxBuf[0] = CMD_USER_CNT;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = 0;
finger_TxBuf[3] = 0;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 200);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return finger_RxBuf[3];
}
else
{
return 0xFF;
}
}
/***************************************************************************
* @brief Get Compare Level
* @return 0xFF: error
other: success, the value is compare level
****************************************************************************/
uint8_t GetcompareLevel(void)
{
uint8_t m;
finger_TxBuf[0] = CMD_COM_LEV;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = 0;
finger_TxBuf[3] = 1;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 200);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return finger_RxBuf[3];
}
else
{
return 0xFF;
}
}
/***************************************************************************
* @brief Set Compare Level
* @param temp: Compare Level,the default value is 5, can be set to 0-9, the bigger, the stricter
* @return 0xFF: error
other: success, the value is compare level
****************************************************************************/
uint8_t SetcompareLevel(uint8_t temp)
{
uint8_t m;
finger_TxBuf[0] = CMD_COM_LEV;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = temp;
finger_TxBuf[3] = 0;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 200);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return finger_RxBuf[3];
}
else
{
return 0xFF;
}
}
/***************************************************************************
* @brief Get the time that fingerprint collection wait timeout
* @return 0xFF: error
other: success, the value is the time that fingerprint collection wait timeout
****************************************************************************/
uint8_t GetTimeOut(void)
{
uint8_t m;
finger_TxBuf[0] = CMD_TIMEOUT;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = 0;
finger_TxBuf[3] = 1;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 200);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return finger_RxBuf[3];
}
else
{
return 0xFF;
}
}
/***************************************************************************
* @brief Register fingerprint
* @return ACK_SUCCESS: success
other: see the macro definition
****************************************************************************/
uint8_t AddUser(void)
{
uint8_t m;
m = GetUserCount();
if (m >= USER_MAX_CNT)
return ACK_FULL;
finger_TxBuf[0] = CMD_ADD_1;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = m +1;
finger_TxBuf[3] = 3;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 6000);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
finger_TxBuf[0] = CMD_ADD_3;
m = TxAndRxCmd(5, 8, 6000);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return ACK_SUCCESS;
}
else
return ACK_FAIL;
}
else
return ACK_FAIL;
}
/***************************************************************************
* @brief Clear fingerprints
* @return ACK_SUCCESS: success
ACK_FAIL: error
****************************************************************************/
uint8_t ClearAllUser(void)
{
uint8_t m;
finger_TxBuf[0] = CMD_DEL_ALL;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = 0;
finger_TxBuf[3] = 0;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 500);
if (m == ACK_SUCCESS && finger_RxBuf[4] == ACK_SUCCESS)
{
return ACK_SUCCESS;
}
else
{
return ACK_FAIL;
}
}
/***************************************************************************
* @brief Check if user ID is between 1 and 3
* @return TRUE
FALSE
****************************************************************************/
uint8_t IsMasterUser(uint8_t UserID)
{
if ((UserID == 1) || (UserID == 2) || (UserID == 3)) return TRUE;
else return FALSE;
}
/***************************************************************************
* @brief Fingerprint matching
* @return ACK_SUCCESS: success
other: see the macro definition
****************************************************************************/
uint8_t VerifyUser(void)
{
uint8_t m;
finger_TxBuf[0] = CMD_MATCH;
finger_TxBuf[1] = 0;
finger_TxBuf[2] = 0;
finger_TxBuf[3] = 0;
finger_TxBuf[4] = 0;
m = TxAndRxCmd(5, 8, 5000);
if ((m == ACK_SUCCESS) && (IsMasterUser(finger_RxBuf[4]) == TRUE))
{
return ACK_SUCCESS;
}
else if(finger_RxBuf[4] == ACK_NO_USER)
{
return ACK_NO_USER;
}
else if(finger_RxBuf[4] == ACK_TIMEOUT)
{
return ACK_TIMEOUT;
}
else
{
return ACK_GO_OUT; // The center of the fingerprint is out of alignment with sensor
}
}
/***************************************************************************
* @brief Wait until the fingerprint module works properly
****************************************************************************/
void Finger_Wait_Until_OK(void)
{
digitalWrite(Finger_RST_Pin , LOW);
delay(300);
digitalWrite(Finger_RST_Pin , HIGH);
delay(300); // Wait for module to start
// ERROR: Please ensure that the module power supply is 3.3V or 5V,
// the serial line is correct, the baud rate defaults to 19200,
// and finally the power is switched off, and then power on again !
while(SetcompareLevel(5) != 5)
{
delay(1000);
Serial.println("***ERROR***");
}
Serial.write("*************** WaveShare Capacitive Fingerprint Reader Test ***************\r\n");
Serial.write("Compare Level: 5 (can be set to 0-9, the bigger, the stricter)\r\n");
Serial.write("Number of fingerprints already available: "); Serial.print(GetUserCount());
Serial.write("\r\n Use the serial port to send the commands to operate the module:\r\n");
Serial.write(" CMD1 : Query the number of existing fingerprints\r\n");
Serial.write(" CMD2 : Add fingerprint (Each entry needs to be read two times: \"beep\", "); Serial.write("put the finger on sensor, \"beep\", put up, \"beep\", put on again)\r\n");
Serial.write(" CMD3 : Fingerprint matching (Send the command, put your finger on sensor after \"beep\". "); Serial.write("Each time you send a command, module waits and matches once)\r\n");
Serial.write(" CMD4 : Clear fingerprints\r\n");
Serial.write(" CMD5 : Switch to sleep mode, you can use the finger Automatic wake-up function");
Serial.write(" CMD6 : Wake up and make all commands valid\r\n");
Serial.write("*************** WaveShare Capacitive Fingerprint Reader Test ***************\r\n");
}
/***************************************************************************
* @brief Analysis of the serial port 2 command (computer serial assistant)
****************************************************************************/
void Analysis_PC_Command(void)
{
static uint8_t step;
uint8_t temp;
if(Serial.available())
{
temp = Serial.read();
switch(step)
{
case 0:
if(temp == 'C') step++;
else step = 0;
break;
case 1:
if(temp == 'M') step++;
else step = 0;
break;
case 2:
if(temp == 'D') step++;
else step = 0;
break;
case 3:
switch(temp)
{
case '1':
if(Finger_SleepFlag == 1) break;
Serial.write("Number of fingerprints already available: "); Serial.println(GetUserCount());
break;
case '2':
if(Finger_SleepFlag == 1) break;
Serial.write(" Add fingerprint (Each entry needs to be read two times: \"beep\", "); Serial.write("put the finger on sensor, \"beep\", put up, \"beep\", put on again)\r\n");
switch(AddUser())
{
case ACK_SUCCESS:
Serial.println("Fingerprint added successfully !");
break;
case ACK_FAIL:
Serial.println("Failed: Please try to place the center of the fingerprint flat to sensor, or this fingerprint already exists !");
break;
case ACK_FULL:
Serial.println("Failed: The fingerprint library is full !");
break;
}
break;
case '3':
if(Finger_SleepFlag == 1) break;
Serial.println("Waiting Finger......Please try to place the center of the fingerprint flat to sensor !");
switch(VerifyUser())
{
case ACK_SUCCESS:
Serial.println("Matching successful !");
break;
case ACK_NO_USER:
Serial.println("Failed: This fingerprint was not found in the library !");
break;
case ACK_TIMEOUT:
Serial.println("Failed: Time out !");
break;
case ACK_GO_OUT:
Serial.println("Failed: Please try to place the center of the fingerprint flat to sensor !");
break;
}
break;
case '4':
if(Finger_SleepFlag == 1) break;
ClearAllUser();
Serial.println("All fingerprints have been cleared !");
break;
case '5':
if(Finger_SleepFlag == 1) break;
digitalWrite(Finger_RST_Pin , LOW);
Finger_SleepFlag = 1;
Serial.println("Module has entered sleep mode: you can use the finger Automatic wake-up function, in this mode, only CMD6 is valid, send CMD6 to pull up the RST pin of module, so that the module exits sleep !");
break;
case '6':
digitalWrite(Finger_RST_Pin , HIGH);
delay(300); // Wait for module to start
Finger_SleepFlag = 0;
Serial.println("The module is awake. All commands are valid !");
break;
default: break;
}
step = 0;
break;
default: break;
}
}
}
/***************************************************************************
* @brief
If you enter the sleep mode, then open the Automatic wake-up function of the finger,
begin to check if the finger is pressed, and then start the module and match
****************************************************************************/
void Auto_Verify_Finger(void)
{
if(digitalRead(Finger_WAKE_Pin) == HIGH) // If you press your finger
{
delay(20);
if(digitalRead(Finger_WAKE_Pin) == HIGH)
{
digitalWrite(Finger_RST_Pin , HIGH); // Pull up the RST to start the module and start matching the fingers
delay(300); // Wait for module to start
Serial.println("Waiting Finger......Please try to place the center of the fingerprint flat to sensor !");
switch(VerifyUser())
{
case ACK_SUCCESS:
Serial.println("Matching successful !");
break;
case ACK_NO_USER:
Serial.println("Failed: This fingerprint was not found in the library !");
break;
case ACK_TIMEOUT:
Serial.println("Failed: Time out !");
break;
case ACK_GO_OUT:
Serial.println("Failed: Please try to place the center of the fingerprint flat to sensor !");
break;
}
//After the matching action is completed, drag RST down to sleep
//and continue to wait for your fingers to press
digitalWrite(Finger_RST_Pin , LOW);
}
}
}