28
28
from micropython import const
29
29
30
30
try :
31
+ from typing import Tuple , List , Union
32
+ from busio import UART
31
33
import struct
32
34
except ImportError :
33
35
import ustruct as struct
@@ -111,7 +113,7 @@ class Adafruit_Fingerprint:
111
113
system_id = None
112
114
status_register = None
113
115
114
- def __init__ (self , uart , passwd = (0 , 0 , 0 , 0 )):
116
+ def __init__ (self , uart : UART , passwd : Tuple [ int , int , int , int ] = (0 , 0 , 0 , 0 )):
115
117
# Create object with UART for interface, and default 32-bit password
116
118
self .password = passwd
117
119
self ._uart = uart
@@ -120,28 +122,28 @@ def __init__(self, uart, passwd=(0, 0, 0, 0)):
120
122
if self .read_sysparam () != OK :
121
123
raise RuntimeError ("Failed to read system parameters!" )
122
124
123
- def check_module (self ):
125
+ def check_module (self ) -> bool :
124
126
"""Checks the state of the fingerprint scanner module.
125
127
Returns OK or error."""
126
128
self ._send_packet ([_GETECHO ])
127
129
if self ._get_packet (12 )[0 ] != MODULEOK :
128
130
raise RuntimeError ("Something is wrong with the sensor." )
129
131
return True
130
132
131
- def verify_password (self ):
133
+ def verify_password (self ) -> bool :
132
134
"""Checks if the password/connection is correct, returns True/False"""
133
135
self ._send_packet ([_VERIFYPASSWORD ] + list (self .password ))
134
136
return self ._get_packet (12 )[0 ]
135
137
136
- def count_templates (self ):
138
+ def count_templates (self ) -> int :
137
139
"""Requests the sensor to count the number of templates and stores it
138
140
in ``self.template_count``. Returns the packet error code or OK success"""
139
141
self ._send_packet ([_TEMPLATECOUNT ])
140
142
r = self ._get_packet (14 )
141
143
self .template_count = struct .unpack (">H" , bytes (r [1 :3 ]))[0 ]
142
144
return r [0 ]
143
145
144
- def read_sysparam (self ):
146
+ def read_sysparam (self ) -> int :
145
147
"""Returns the system parameters on success via attributes."""
146
148
self ._send_packet ([_READSYSPARA ])
147
149
r = self ._get_packet (28 )
@@ -156,7 +158,7 @@ def read_sysparam(self):
156
158
self .baudrate = struct .unpack (">H" , bytes (r [15 :17 ]))[0 ]
157
159
return r [0 ]
158
160
159
- def set_sysparam (self , param_num , param_val ) :
161
+ def set_sysparam (self , param_num : int , param_val : int ) -> int :
160
162
"""Set the system parameters (param_num)"""
161
163
self ._send_packet ([_SETSYSPARA , param_num , param_val ])
162
164
r = self ._get_packet (12 )
@@ -170,43 +172,43 @@ def set_sysparam(self, param_num, param_val):
170
172
self .data_packet_size = param_val
171
173
return r [0 ]
172
174
173
- def get_image (self ):
175
+ def get_image (self ) -> int :
174
176
"""Requests the sensor to take an image and store it memory, returns
175
177
the packet error code or OK success"""
176
178
self ._send_packet ([_GETIMAGE ])
177
179
return self ._get_packet (12 )[0 ]
178
180
179
- def image_2_tz (self , slot = 1 ) :
181
+ def image_2_tz (self , slot : int = 1 ) -> int :
180
182
"""Requests the sensor convert the image to a template, returns
181
183
the packet error code or OK success"""
182
184
self ._send_packet ([_IMAGE2TZ , slot ])
183
185
return self ._get_packet (12 )[0 ]
184
186
185
- def create_model (self ):
187
+ def create_model (self ) -> int :
186
188
"""Requests the sensor take the template data and turn it into a model
187
189
returns the packet error code or OK success"""
188
190
self ._send_packet ([_REGMODEL ])
189
191
return self ._get_packet (12 )[0 ]
190
192
191
- def store_model (self , location , slot = 1 ) :
193
+ def store_model (self , location : int , slot : int = 1 ) -> int :
192
194
"""Requests the sensor store the model into flash memory and assign
193
195
a location. Returns the packet error code or OK success"""
194
196
self ._send_packet ([_STORE , slot , location >> 8 , location & 0xFF ])
195
197
return self ._get_packet (12 )[0 ]
196
198
197
- def delete_model (self , location ) :
199
+ def delete_model (self , location : int ) -> int :
198
200
"""Requests the sensor delete a model from flash memory given by
199
201
the argument location. Returns the packet error code or OK success"""
200
202
self ._send_packet ([_DELETE , location >> 8 , location & 0xFF , 0x00 , 0x01 ])
201
203
return self ._get_packet (12 )[0 ]
202
204
203
- def load_model (self , location , slot = 1 ) :
205
+ def load_model (self , location : int , slot : int = 1 ) -> int :
204
206
"""Requests the sensor to load a model from the given memory location
205
207
to the given slot. Returns the packet error code or success"""
206
208
self ._send_packet ([_LOAD , slot , location >> 8 , location & 0xFF ])
207
209
return self ._get_packet (12 )[0 ]
208
210
209
- def get_fpdata (self , sensorbuffer = "char" , slot = 1 ) :
211
+ def get_fpdata (self , sensorbuffer : str = "char" , slot : int = 1 ) -> List [ int ] :
210
212
"""Requests the sensor to transfer the fingerprint image or
211
213
template. Returns the data payload only."""
212
214
if slot not in (1 , 2 ):
@@ -224,7 +226,7 @@ def get_fpdata(self, sensorbuffer="char", slot=1):
224
226
self ._print_debug ("get_fdata res:" , res , data_type = "hex" )
225
227
return res
226
228
227
- def send_fpdata (self , data , sensorbuffer = "char" , slot = 1 ) :
229
+ def send_fpdata (self , data : List [ int ] , sensorbuffer : str = "char" , slot : int = 1 ) -> bool :
228
230
"""Requests the sensor to receive data, either a fingerprint image or
229
231
a character/template data. Data is the payload only."""
230
232
if slot not in (1 , 2 ):
@@ -242,13 +244,13 @@ def send_fpdata(self, data, sensorbuffer="char", slot=1):
242
244
self ._print_debug ("sent_fdata data:" , data , data_type = "hex" )
243
245
return True
244
246
245
- def empty_library (self ):
247
+ def empty_library (self ) -> int :
246
248
"""Requests the sensor to delete all models from flash memory.
247
249
Returns the packet error code or OK success"""
248
250
self ._send_packet ([_EMPTY ])
249
251
return self ._get_packet (12 )[0 ]
250
252
251
- def read_templates (self ):
253
+ def read_templates (self ) -> int :
252
254
"""Requests the sensor to list of all template locations in use and
253
255
stores them in self.templates. Returns the packet error code or
254
256
OK success"""
@@ -273,7 +275,7 @@ def read_templates(self):
273
275
r = temp_r
274
276
return r [0 ]
275
277
276
- def finger_fast_search (self ):
278
+ def finger_fast_search (self ) -> int :
277
279
"""Asks the sensor to search for a matching fingerprint template to the
278
280
last model generated. Stores the location and confidence in self.finger_id
279
281
and self.confidence. Returns the packet error code or OK success"""
@@ -296,7 +298,7 @@ def close_uart(self):
296
298
"""close serial port"""
297
299
self ._uart .close ()
298
300
299
- def finger_search (self ):
301
+ def finger_search (self ) -> int :
300
302
"""Asks the sensor to search for a matching fingerprint starting at
301
303
slot 1. Stores the location and confidence in self.finger_id
302
304
and self.confidence. Returns the packet error code or OK success"""
@@ -310,7 +312,7 @@ def finger_search(self):
310
312
self ._print_debug ("finger_search packet:" , r , data_type = "hex" )
311
313
return r [0 ]
312
314
313
- def compare_templates (self ):
315
+ def compare_templates (self ) -> int :
314
316
"""Compares two fingerprint templates in char buffers 1 and 2. Stores the confidence score
315
317
in self.finger_id and self.confidence. Returns the packet error code or
316
318
OK success"""
@@ -320,7 +322,7 @@ def compare_templates(self):
320
322
self ._print_debug ("compare_templates confidence:" , self .confidence )
321
323
return r [0 ]
322
324
323
- def set_led (self , color = 1 , mode = 3 , speed = 0x80 , cycles = 0 ) :
325
+ def set_led (self , color : int = 1 , mode : int = 3 , speed : int = 0x80 , cycles : int = 0 ) -> int :
324
326
"""LED function -- only for R503 Sensor.
325
327
Parameters: See User Manual for full details
326
328
color: 1=red, 2=blue, 3=purple
@@ -333,7 +335,7 @@ def set_led(self, color=1, mode=3, speed=0x80, cycles=0):
333
335
334
336
##################################################
335
337
336
- def _get_packet (self , expected ) :
338
+ def _get_packet (self , expected : int ) -> List [ int ] :
337
339
"""Helper to parse out a packet from the UART and check structure.
338
340
Returns just the data payload from the packet"""
339
341
res = self ._uart .read (expected )
@@ -366,7 +368,7 @@ def _get_packet(self, expected):
366
368
self ._print_debug ("_get_packet reply:" , reply , data_type = "hex" )
367
369
return reply
368
370
369
- def _get_data (self , expected ) :
371
+ def _get_data (self , expected : int ) -> List [ int ] :
370
372
"""Gets packet from serial and checks structure for _DATAPACKET
371
373
and _ENDDATAPACKET. Alternate method for getting data such
372
374
as fingerprint image, etc. Returns the data payload."""
@@ -415,7 +417,7 @@ def _get_data(self, expected):
415
417
self ._print_debug ("_get_data reply:" , reply , data_type = "hex" )
416
418
return reply
417
419
418
- def _send_packet (self , data ):
420
+ def _send_packet (self , data : List [ int ] ):
419
421
packet = [_STARTCODE >> 8 , _STARTCODE & 0xFF ]
420
422
packet = packet + self .address
421
423
packet .append (_COMMANDPACKET ) # the packet type
@@ -434,7 +436,7 @@ def _send_packet(self, data):
434
436
self ._print_debug ("_send_packet data:" , packet , data_type = "hex" )
435
437
self ._uart .write (bytearray (packet ))
436
438
437
- def _send_data (self , data ):
439
+ def _send_data (self , data : List [ int ] ):
438
440
self ._print_debug ("_send_data length:" , len (data ))
439
441
self ._print_debug ("_send_data data:" , data , data_type = "hex" )
440
442
# self.read_sysparam() #moved this to init
@@ -489,7 +491,7 @@ def soft_reset(self):
489
491
if self ._uart .read (1 )[0 ] != MODULEOK :
490
492
raise RuntimeError ("Sensor did not send a handshake signal!" )
491
493
492
- def _print_debug (self , info , data , data_type = "str" ):
494
+ def _print_debug (self , info : str , data : Union [ int , str ], data_type : str = "str" ):
493
495
"""Prints debugging information. This is activated
494
496
by flag _debug"""
495
497
if not self ._debug :
0 commit comments