-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFingerprintSensorLib.cpp
More file actions
392 lines (314 loc) · 7.42 KB
/
Copy pathFingerprintSensorLib.cpp
File metadata and controls
392 lines (314 loc) · 7.42 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
//
//
//
#include "fingerprintSensorLib.h"
#ifdef __AVR__
#include <util/delay.h>
#include <SoftwareSerial.h>
#endif
#ifdef __AVR__
FingerprintSensorLibClass::FingerprintSensorLibClass(SoftwareSerial *soft) {
hardSerial = NULL;
softSerial = soft;
actSerial = softSerial;
}
#endif
FingerprintSensorLibClass::FingerprintSensorLibClass(HardwareSerial *hard)
{
#ifdef __AVR__
softSerial = NULL;
#endif
hardSerial = hard;
actSerial = hardSerial;
}
bool FingerprintSensorLibClass::init(uint32_t baudRate)
{
delay(1000); // Espera o sensor ligar
if (hardSerial) hardSerial->begin(baudRate);
#ifdef __AVR__
if (softSerial) softSerial->begin(baudRate);
#endif
}
bool FingerprintSensorLibClass::start(void)
{
FINGERPRINT_START();
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12)
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA HandShake");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00)
{
//Serial.println("Sensor nao encontrado!");
return false;
}
else
{
//Serial.println("Sensor pronto.");
return true;
}
}
bool FingerprintSensorLibClass::pegarImagem(void)
{
FINGERPRINT_GET_IMAGE();
Serial.print(".");
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12)
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA getImage");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00)
{
//Serial.println("Erro ao pegar imagem");
return false;
}
else
{
//Serial.println("\nImagem armazenada");
return true;
}
}
bool FingerprintSensorLibClass::img2tz(byte param)
{
FINGERPRINT_2_TZ(param);
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12)
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA img2tz");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00)
{
Serial.println("Problema na Conversão!");
return false;
}
else
{
//Serial.println("Imagem Convertida para Char.");
return true;
}
}
bool FingerprintSensorLibClass::criarModelo(void)
{
FINGERPRINT_GEN_TEMPLATE();
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12)
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA GenModel");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00)
{
//Serial.println("Erro ao pegar imagem");
return false;
}
else
{
//Serial.println("\nTemplate Gerada");
return true;
}
}
bool FingerprintSensorLibClass::excluirDB(void)
{
FINGERPRINT_DELETE_ALL();
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12)
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA Empty");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00)
{
//Serial.println("Erro ao pegar imagem");
return false;
}
else
{
//Serial.println("\nTemplates apagadas");
return true;
}
}
bool FingerprintSensorLibClass::pegarModelo(char * buff)
{
FINGERPRINT_GET_TEMPLATE();
memset(buff, 0xff, 512); //zera todo mundo
uint8_t pacoteResp[12];
memset(pacoteResp, 0xff, 12); //zero out template buffer
int index = 0;
delay(1000);
while (index < 12) // Esse laço armazena o pacote de confirmação
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
#ifdef debug
Serial.println("RESPOSTA UpChar");
for (int count = 0; count < 12; count++)
{
Serial.print("0x");
Serial.print(pacoteResp[count], HEX);
Serial.print(", ");
}
Serial.println();
Serial.println();
#endif
if (pacoteResp[9] != 0x00) // Significa que o leitor não foi capaz de executar a intrução UpChar
{
Serial.println("\n\nErro no UpChar!");
return false;
}
// começando código que vai iterar o buffer!
uint8_t count = 0;
uint8_t num = 1;
while (true)
{
for (uint8_t i = 0; i < 12; i++) // Esse laço vai armazenar a confirmação de envio de dados, que para cada pacote em cada pacote de 136
// Sendo exatamente os 11 primeiros bytes de transimissão, enqaunto os 128 restantes são armazenados no buffer
{
if (actSerial->available())
{
pacoteResp[index] = actSerial->read();
index++;
}
}
while (count < 128 * num) // esse laço vai iterando o buffer, o 128 é o numero de bytes por pacote por padrão, podendo ser alterado no 'sysParamters'
{
if (actSerial->available())
{
buff[count] = actSerial->read(); // vai armazenando os valores no buffer
}
}
if (pacoteResp[6] == 0x08) // quando recebermos a confirmação de "final do pacote" saímos do laço :)
{
break;
}
num *= 2; // Isso vai continuar a iteração do buff :P MuuuahAHahHAhHhAhAhA
}
//Serial.println("Template extraída com sucesso");
return true;
}
/*
Funções privadas, responsáveis por enviarem as intruções para o módulo
*/
void FingerprintSensorLibClass::FINGERPRINT_START(void)
{
byte FP_VFY_PWD[] = { 0xEF, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0x1, 0x0, 0x7, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1B };
actSerial->write(FP_VFY_PWD, sizeof(FP_VFY_PWD));
}
void FingerprintSensorLibClass::FINGERPRINT_GET_IMAGE()
{
byte FP_GET_IMAGE[] = { 0xef, 0x01, 0xff, 0xff, 0xff, 0xff, 0x1, 0x0, 0x3, 0x01, 0x00, 0x5 };
actSerial->write(FP_GET_IMAGE, sizeof(FP_GET_IMAGE));
}
void FingerprintSensorLibClass::FINGERPRINT_2_TZ(byte param)
{
byte sum = 0x1 + 0x0 + 0x4 + 0x2 + param; // Como o parâmetro pode variar...
byte FP_IMG_2_TZ[] = { 0xef, 0x01, 0xff, 0xff, 0xff, 0xff, 0x1, 0x0, 0x4, 0x2, param, 0x0, sum };
actSerial->write(FP_IMG_2_TZ, sizeof(FP_IMG_2_TZ));
}
void FingerprintSensorLibClass::FINGERPRINT_GEN_TEMPLATE(void)
{
byte FP_GEN_TEMPLATE[] = { 0xEF, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0x1, 0x0, 0x3, 0x5, 0x0, 0x9 };
actSerial->write(FP_GEN_TEMPLATE, sizeof(FP_GEN_TEMPLATE));
}
void FingerprintSensorLibClass::FINGERPRINT_GET_TEMPLATE(void)
{
byte FP_GET_TEMPLATE[] = { 0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x1, 0x0, 0x4, 0x8, 0x1, 0x0, 0xE5 };
actSerial->write(FP_GET_TEMPLATE, sizeof(FP_GET_TEMPLATE));
}
void FingerprintSensorLibClass::FINGERPRINT_DELETE_ALL(void)
{
byte FP_EMPTY[] = { 0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x1, 0x0, 0x3, 0xd, 0x0, 0x11 };
//EF01H Xxxx 01H 0003H 0dH 0011H
actSerial->write(FP_EMPTY, sizeof(FP_EMPTY));
}