Skip to content

Commit 7c05ab3

Browse files
committed
Add typehints
1 parent c33d564 commit 7c05ab3

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

adafruit_fingerprint.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from micropython import const
2929

3030
try:
31+
from typing import Tuple, List, Union
32+
from busio import UART
3133
import struct
3234
except ImportError:
3335
import ustruct as struct
@@ -111,7 +113,7 @@ class Adafruit_Fingerprint:
111113
system_id = None
112114
status_register = None
113115

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)):
115117
# Create object with UART for interface, and default 32-bit password
116118
self.password = passwd
117119
self._uart = uart
@@ -120,28 +122,28 @@ def __init__(self, uart, passwd=(0, 0, 0, 0)):
120122
if self.read_sysparam() != OK:
121123
raise RuntimeError("Failed to read system parameters!")
122124

123-
def check_module(self):
125+
def check_module(self) -> bool:
124126
"""Checks the state of the fingerprint scanner module.
125127
Returns OK or error."""
126128
self._send_packet([_GETECHO])
127129
if self._get_packet(12)[0] != MODULEOK:
128130
raise RuntimeError("Something is wrong with the sensor.")
129131
return True
130132

131-
def verify_password(self):
133+
def verify_password(self) -> bool:
132134
"""Checks if the password/connection is correct, returns True/False"""
133135
self._send_packet([_VERIFYPASSWORD] + list(self.password))
134136
return self._get_packet(12)[0]
135137

136-
def count_templates(self):
138+
def count_templates(self) -> int:
137139
"""Requests the sensor to count the number of templates and stores it
138140
in ``self.template_count``. Returns the packet error code or OK success"""
139141
self._send_packet([_TEMPLATECOUNT])
140142
r = self._get_packet(14)
141143
self.template_count = struct.unpack(">H", bytes(r[1:3]))[0]
142144
return r[0]
143145

144-
def read_sysparam(self):
146+
def read_sysparam(self) -> int:
145147
"""Returns the system parameters on success via attributes."""
146148
self._send_packet([_READSYSPARA])
147149
r = self._get_packet(28)
@@ -156,7 +158,7 @@ def read_sysparam(self):
156158
self.baudrate = struct.unpack(">H", bytes(r[15:17]))[0]
157159
return r[0]
158160

159-
def set_sysparam(self, param_num, param_val):
161+
def set_sysparam(self, param_num: int, param_val: int) -> int:
160162
"""Set the system parameters (param_num)"""
161163
self._send_packet([_SETSYSPARA, param_num, param_val])
162164
r = self._get_packet(12)
@@ -170,43 +172,43 @@ def set_sysparam(self, param_num, param_val):
170172
self.data_packet_size = param_val
171173
return r[0]
172174

173-
def get_image(self):
175+
def get_image(self) -> int:
174176
"""Requests the sensor to take an image and store it memory, returns
175177
the packet error code or OK success"""
176178
self._send_packet([_GETIMAGE])
177179
return self._get_packet(12)[0]
178180

179-
def image_2_tz(self, slot=1):
181+
def image_2_tz(self, slot: int = 1) -> int:
180182
"""Requests the sensor convert the image to a template, returns
181183
the packet error code or OK success"""
182184
self._send_packet([_IMAGE2TZ, slot])
183185
return self._get_packet(12)[0]
184186

185-
def create_model(self):
187+
def create_model(self) -> int:
186188
"""Requests the sensor take the template data and turn it into a model
187189
returns the packet error code or OK success"""
188190
self._send_packet([_REGMODEL])
189191
return self._get_packet(12)[0]
190192

191-
def store_model(self, location, slot=1):
193+
def store_model(self, location: int, slot: int = 1) -> int:
192194
"""Requests the sensor store the model into flash memory and assign
193195
a location. Returns the packet error code or OK success"""
194196
self._send_packet([_STORE, slot, location >> 8, location & 0xFF])
195197
return self._get_packet(12)[0]
196198

197-
def delete_model(self, location):
199+
def delete_model(self, location: int) -> int:
198200
"""Requests the sensor delete a model from flash memory given by
199201
the argument location. Returns the packet error code or OK success"""
200202
self._send_packet([_DELETE, location >> 8, location & 0xFF, 0x00, 0x01])
201203
return self._get_packet(12)[0]
202204

203-
def load_model(self, location, slot=1):
205+
def load_model(self, location: int, slot: int = 1) -> int:
204206
"""Requests the sensor to load a model from the given memory location
205207
to the given slot. Returns the packet error code or success"""
206208
self._send_packet([_LOAD, slot, location >> 8, location & 0xFF])
207209
return self._get_packet(12)[0]
208210

209-
def get_fpdata(self, sensorbuffer="char", slot=1):
211+
def get_fpdata(self, sensorbuffer: str = "char", slot: int = 1) -> List[int]:
210212
"""Requests the sensor to transfer the fingerprint image or
211213
template. Returns the data payload only."""
212214
if slot not in (1, 2):
@@ -224,7 +226,7 @@ def get_fpdata(self, sensorbuffer="char", slot=1):
224226
self._print_debug("get_fdata res:", res, data_type="hex")
225227
return res
226228

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:
228230
"""Requests the sensor to receive data, either a fingerprint image or
229231
a character/template data. Data is the payload only."""
230232
if slot not in (1, 2):
@@ -242,13 +244,13 @@ def send_fpdata(self, data, sensorbuffer="char", slot=1):
242244
self._print_debug("sent_fdata data:", data, data_type="hex")
243245
return True
244246

245-
def empty_library(self):
247+
def empty_library(self) -> int:
246248
"""Requests the sensor to delete all models from flash memory.
247249
Returns the packet error code or OK success"""
248250
self._send_packet([_EMPTY])
249251
return self._get_packet(12)[0]
250252

251-
def read_templates(self):
253+
def read_templates(self) -> int:
252254
"""Requests the sensor to list of all template locations in use and
253255
stores them in self.templates. Returns the packet error code or
254256
OK success"""
@@ -273,7 +275,7 @@ def read_templates(self):
273275
r = temp_r
274276
return r[0]
275277

276-
def finger_fast_search(self):
278+
def finger_fast_search(self) -> int:
277279
"""Asks the sensor to search for a matching fingerprint template to the
278280
last model generated. Stores the location and confidence in self.finger_id
279281
and self.confidence. Returns the packet error code or OK success"""
@@ -296,7 +298,7 @@ def close_uart(self):
296298
"""close serial port"""
297299
self._uart.close()
298300

299-
def finger_search(self):
301+
def finger_search(self) -> int:
300302
"""Asks the sensor to search for a matching fingerprint starting at
301303
slot 1. Stores the location and confidence in self.finger_id
302304
and self.confidence. Returns the packet error code or OK success"""
@@ -310,7 +312,7 @@ def finger_search(self):
310312
self._print_debug("finger_search packet:", r, data_type="hex")
311313
return r[0]
312314

313-
def compare_templates(self):
315+
def compare_templates(self) -> int:
314316
"""Compares two fingerprint templates in char buffers 1 and 2. Stores the confidence score
315317
in self.finger_id and self.confidence. Returns the packet error code or
316318
OK success"""
@@ -320,7 +322,7 @@ def compare_templates(self):
320322
self._print_debug("compare_templates confidence:", self.confidence)
321323
return r[0]
322324

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:
324326
"""LED function -- only for R503 Sensor.
325327
Parameters: See User Manual for full details
326328
color: 1=red, 2=blue, 3=purple
@@ -333,7 +335,7 @@ def set_led(self, color=1, mode=3, speed=0x80, cycles=0):
333335

334336
##################################################
335337

336-
def _get_packet(self, expected):
338+
def _get_packet(self, expected: int) -> List[int]:
337339
"""Helper to parse out a packet from the UART and check structure.
338340
Returns just the data payload from the packet"""
339341
res = self._uart.read(expected)
@@ -366,7 +368,7 @@ def _get_packet(self, expected):
366368
self._print_debug("_get_packet reply:", reply, data_type="hex")
367369
return reply
368370

369-
def _get_data(self, expected):
371+
def _get_data(self, expected: int) -> List[int]:
370372
"""Gets packet from serial and checks structure for _DATAPACKET
371373
and _ENDDATAPACKET. Alternate method for getting data such
372374
as fingerprint image, etc. Returns the data payload."""
@@ -415,7 +417,7 @@ def _get_data(self, expected):
415417
self._print_debug("_get_data reply:", reply, data_type="hex")
416418
return reply
417419

418-
def _send_packet(self, data):
420+
def _send_packet(self, data: List[int]):
419421
packet = [_STARTCODE >> 8, _STARTCODE & 0xFF]
420422
packet = packet + self.address
421423
packet.append(_COMMANDPACKET) # the packet type
@@ -434,7 +436,7 @@ def _send_packet(self, data):
434436
self._print_debug("_send_packet data:", packet, data_type="hex")
435437
self._uart.write(bytearray(packet))
436438

437-
def _send_data(self, data):
439+
def _send_data(self, data: List[int]):
438440
self._print_debug("_send_data length:", len(data))
439441
self._print_debug("_send_data data:", data, data_type="hex")
440442
# self.read_sysparam() #moved this to init
@@ -489,7 +491,7 @@ def soft_reset(self):
489491
if self._uart.read(1)[0] != MODULEOK:
490492
raise RuntimeError("Sensor did not send a handshake signal!")
491493

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"):
493495
"""Prints debugging information. This is activated
494496
by flag _debug"""
495497
if not self._debug:

0 commit comments

Comments
 (0)