42
42
"""
43
43
44
44
from micropython import const
45
+
45
46
try :
46
47
import struct
47
48
except ImportError :
99
100
PASSVERIFY = const (0x21 )
100
101
MODULEOK = const (0x55 )
101
102
103
+
102
104
class Adafruit_Fingerprint :
103
105
"""UART based fingerprint sensor."""
106
+
104
107
_uart = None
105
108
106
109
password = None
@@ -116,18 +119,18 @@ class Adafruit_Fingerprint:
116
119
baudrate = None
117
120
118
121
def __init__ (self , uart , passwd = (0 , 0 , 0 , 0 )):
119
- # Create object with UART for interface, and default 32-bit password
122
+ # Create object with UART for interface, and default 32-bit password
120
123
self .password = passwd
121
124
self ._uart = uart
122
125
if self .verify_password () != OK :
123
- raise RuntimeError (' Failed to find sensor, check wiring!' )
126
+ raise RuntimeError (" Failed to find sensor, check wiring!" )
124
127
125
128
def check_module (self ):
126
129
"""Checks the state of the fingerprint scanner module.
127
130
Returns OK or error."""
128
131
self ._send_packet ([_GETECHO ])
129
132
if self ._get_packet (12 )[0 ] != MODULEOK :
130
- raise RuntimeError (' Something is wrong with the sensor.' )
133
+ raise RuntimeError (" Something is wrong with the sensor." )
131
134
return True
132
135
133
136
def verify_password (self ):
@@ -140,20 +143,20 @@ def count_templates(self):
140
143
in ``self.template_count``. Returns the packet error code or OK success"""
141
144
self ._send_packet ([_TEMPLATECOUNT ])
142
145
r = self ._get_packet (14 )
143
- self .template_count = struct .unpack ('>H' , bytes (r [1 :3 ]))[0 ]
146
+ self .template_count = struct .unpack (">H" , bytes (r [1 :3 ]))[0 ]
144
147
return r [0 ]
145
148
146
149
def read_sysparam (self ):
147
150
"""Returns the system parameters on success via attributes."""
148
151
self ._send_packet ([_READSYSPARA ])
149
152
r = self ._get_packet (28 )
150
153
if r [0 ] != OK :
151
- raise RuntimeError (' Command failed.' )
152
- self .library_size = struct .unpack ('>H' , bytes (r [5 :7 ]))[0 ]
153
- self .security_level = struct .unpack ('>H' , bytes (r [7 :9 ]))[0 ]
154
+ raise RuntimeError (" Command failed." )
155
+ self .library_size = struct .unpack (">H" , bytes (r [5 :7 ]))[0 ]
156
+ self .security_level = struct .unpack (">H" , bytes (r [7 :9 ]))[0 ]
154
157
self .device_address = bytes (r [9 :13 ])
155
- self .data_packet_size = struct .unpack ('>H' , bytes (r [13 :15 ]))[0 ]
156
- self .baudrate = struct .unpack ('>H' , bytes (r [15 :17 ]))[0 ]
158
+ self .data_packet_size = struct .unpack (">H" , bytes (r [13 :15 ]))[0 ]
159
+ self .baudrate = struct .unpack (">H" , bytes (r [15 :17 ]))[0 ]
157
160
return r [0 ]
158
161
159
162
def get_image (self ):
@@ -192,36 +195,36 @@ def load_model(self, location, slot=1):
192
195
self ._send_packet ([_LOAD , slot , location >> 8 , location & 0xFF ])
193
196
return self ._get_packet (12 )[0 ]
194
197
195
- def get_fpdata (self , sensorbuffer = ' char' , slot = 1 ):
198
+ def get_fpdata (self , sensorbuffer = " char" , slot = 1 ):
196
199
"""Requests the sensor to transfer the fingerprint image or
197
200
template. Returns the data payload only."""
198
201
if slot != 1 or slot != 2 :
199
202
# raise error or use default value?
200
203
slot = 2
201
- if sensorbuffer == ' image' :
204
+ if sensorbuffer == " image" :
202
205
self ._send_packet ([_UPLOADIMAGE ])
203
- elif sensorbuffer == ' char' :
206
+ elif sensorbuffer == " char" :
204
207
self ._send_packet ([_UPLOAD , slot ])
205
208
else :
206
- raise RuntimeError (' Uknown sensor buffer type' )
209
+ raise RuntimeError (" Uknown sensor buffer type" )
207
210
if self ._get_packet (12 )[0 ] == 0 :
208
211
res = self ._get_data (9 )
209
212
# print('datasize: ' + str(len(res)))
210
213
# print(res)
211
214
return res
212
215
213
- def send_fpdata (self , data , sensorbuffer = ' char' , slot = 1 ):
216
+ def send_fpdata (self , data , sensorbuffer = " char" , slot = 1 ):
214
217
"""Requests the sensor to receive data, either a fingerprint image or
215
218
a character/template data. Data is the payload only."""
216
219
if slot != 1 or slot != 2 :
217
220
# raise error or use default value?
218
221
slot = 2
219
- if sensorbuffer == ' image' :
222
+ if sensorbuffer == " image" :
220
223
self ._send_packet ([_DOWNLOADIMAGE ])
221
- elif sensorbuffer == ' char' :
224
+ elif sensorbuffer == " char" :
222
225
self ._send_packet ([_DOWNLOAD , slot ])
223
226
else :
224
- raise RuntimeError (' Uknown sensor buffer type' )
227
+ raise RuntimeError (" Uknown sensor buffer type" )
225
228
if self ._get_packet (12 )[0 ] == 0 :
226
229
self ._send_data (data )
227
230
# print('datasize: ' + str(len(res)))
@@ -238,16 +241,19 @@ def read_templates(self):
238
241
"""Requests the sensor to list of all template locations in use and
239
242
stores them in self.templates. Returns the packet error code or
240
243
OK success"""
241
- import math
244
+ from math import ceil # pylint: disable=import-outside-toplevel
245
+
242
246
self .templates = []
243
247
self .read_sysparam ()
244
- temp_r = [0x0c , ]
245
- for j in range (math .ceil (self .library_size / 256 )):
248
+ temp_r = [
249
+ 0x0C ,
250
+ ]
251
+ for j in range (ceil (self .library_size / 256 )):
246
252
self ._send_packet ([_TEMPLATEREAD , j ])
247
253
r = self ._get_packet (44 )
248
254
if r [0 ] == OK :
249
255
for i in range (32 ):
250
- byte = r [i + 1 ]
256
+ byte = r [i + 1 ]
251
257
for bit in range (8 ):
252
258
if byte & (1 << bit ):
253
259
self .templates .append ((i * 8 ) + bit + (j * 256 ))
@@ -261,52 +267,53 @@ def finger_fast_search(self):
261
267
last model generated. Stores the location and confidence in self.finger_id
262
268
and self.confidence. Returns the packet error code or OK success"""
263
269
# high speed search of slot #1 starting at page 0x0000 and page #0x00A3
264
- #self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
270
+ # self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
265
271
# or page #0x03E9 to accommodate modules with up to 1000 capacity
266
- #self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x03, 0xE9])
272
+ # self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x03, 0xE9])
267
273
# or base the page on module's capacity
268
274
self .read_sysparam ()
269
275
capacity = self .library_size
270
- self ._send_packet ([_HISPEEDSEARCH , 0x01 , 0x00 , 0x00 , capacity >> 8 ,
271
- capacity & 0xFF ])
276
+ self ._send_packet (
277
+ [_HISPEEDSEARCH , 0x01 , 0x00 , 0x00 , capacity >> 8 , capacity & 0xFF ]
278
+ )
272
279
r = self ._get_packet (16 )
273
- self .finger_id , self .confidence = struct .unpack (' >HH' , bytes (r [1 :5 ]))
280
+ self .finger_id , self .confidence = struct .unpack (" >HH" , bytes (r [1 :5 ]))
274
281
# print(r)
275
282
return r [0 ]
276
283
277
- ##################################################
284
+ ##################################################
278
285
279
286
def _get_packet (self , expected ):
280
287
""" Helper to parse out a packet from the UART and check structure.
281
288
Returns just the data payload from the packet"""
282
289
res = self ._uart .read (expected )
283
- #print("Got", res)
290
+ # print("Got", res)
284
291
if (not res ) or (len (res ) != expected ):
285
- raise RuntimeError (' Failed to read data from sensor' )
292
+ raise RuntimeError (" Failed to read data from sensor" )
286
293
287
294
# first two bytes are start code
288
- start = struct .unpack ('>H' , res [0 :2 ])[0 ]
295
+ start = struct .unpack (">H" , res [0 :2 ])[0 ]
289
296
290
297
if start != _STARTCODE :
291
- raise RuntimeError (' Incorrect packet data' )
298
+ raise RuntimeError (" Incorrect packet data" )
292
299
# next 4 bytes are address
293
- addr = [ i for i in res [2 :6 ]]
300
+ addr = list ( i for i in res [2 :6 ])
294
301
if addr != self .address :
295
- raise RuntimeError (' Incorrect address' )
302
+ raise RuntimeError (" Incorrect address" )
296
303
297
- packet_type , length = struct .unpack (' >BH' , res [6 :9 ])
304
+ packet_type , length = struct .unpack (" >BH" , res [6 :9 ])
298
305
if packet_type != _ACKPACKET :
299
- raise RuntimeError (' Incorrect packet data' )
306
+ raise RuntimeError (" Incorrect packet data" )
300
307
301
308
# we should check the checksum
302
309
# but i don't know how
303
310
# not yet anyway
304
- #packet_sum = struct.unpack('>H', res[9+(length-2):9+length])[0]
305
- #print(packet_sum)
306
- #print(packet_type + length + struct.unpack('>HHHH', res[9:9+(length-2)]))
311
+ # packet_sum = struct.unpack('>H', res[9+(length-2):9+length])[0]
312
+ # print(packet_sum)
313
+ # print(packet_type + length + struct.unpack('>HHHH', res[9:9+(length-2)]))
307
314
308
- reply = [ i for i in res [9 : 9 + (length - 2 )]]
309
- #print(reply)
315
+ reply = list ( i for i in res [9 : 9 + (length - 2 )])
316
+ # print(reply)
310
317
return reply
311
318
312
319
def _get_data (self , expected ):
@@ -315,38 +322,38 @@ def _get_data(self, expected):
315
322
as fingerprint image, etc. Returns the data payload."""
316
323
res = self ._uart .read (expected )
317
324
if (not res ) or (len (res ) != expected ):
318
- raise RuntimeError (' Failed to read data from sensor' )
325
+ raise RuntimeError (" Failed to read data from sensor" )
319
326
320
327
# first two bytes are start code
321
- start = struct .unpack ('>H' , res [0 :2 ])[0 ]
328
+ start = struct .unpack (">H" , res [0 :2 ])[0 ]
322
329
# print(start)
323
330
if start != _STARTCODE :
324
- raise RuntimeError (' Incorrect packet data' )
331
+ raise RuntimeError (" Incorrect packet data" )
325
332
# next 4 bytes are address
326
- addr = [ i for i in res [2 :6 ]]
333
+ addr = list ( i for i in res [2 :6 ])
327
334
# print(addr)
328
335
if addr != self .address :
329
- raise RuntimeError (' Incorrect address' )
336
+ raise RuntimeError (" Incorrect address" )
330
337
331
- packet_type , length = struct .unpack (' >BH' , res [6 :9 ])
332
- #print(str(packet_type) + ' ' + str(length))
338
+ packet_type , length = struct .unpack (" >BH" , res [6 :9 ])
339
+ # print(str(packet_type) + ' ' + str(length))
333
340
334
341
# todo: check checksum
335
342
336
343
if packet_type != _DATAPACKET :
337
344
if packet_type != _ENDDATAPACKET :
338
- raise RuntimeError (' Incorrect packet data' )
345
+ raise RuntimeError (" Incorrect packet data" )
339
346
340
347
if packet_type == _DATAPACKET :
341
- res = self ._uart .read (length - 2 )
348
+ res = self ._uart .read (length - 2 )
342
349
# todo: we should really inspect the headers and checksum
343
- reply = [ i for i in res [0 :length ]]
350
+ reply = list ( i for i in res [0 :length ])
344
351
self ._uart .read (2 ) # disregard checksum but we really shouldn't
345
352
reply += self ._get_data (9 )
346
353
elif packet_type == _ENDDATAPACKET :
347
- res = self ._uart .read (length - 2 )
354
+ res = self ._uart .read (length - 2 )
348
355
# todo: we should really inspect the headers and checksum
349
- reply = [ i for i in res [0 :length ]]
356
+ reply = list ( i for i in res [0 :length ])
350
357
self ._uart .read (2 ) # disregard checksum but we really shouldn't
351
358
# print(len(reply))
352
359
# print(reply)
@@ -367,7 +374,7 @@ def _send_packet(self, data):
367
374
packet .append (checksum >> 8 )
368
375
packet .append (checksum & 0xFF )
369
376
370
- #print("Sending: ", [hex(i) for i in packet])
377
+ # print("Sending: ", [hex(i) for i in packet])
371
378
self ._uart .write (bytearray (packet ))
372
379
373
380
def _send_data (self , data ):
0 commit comments