Skip to content

Add Typehints #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ confidence=
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,consider-using-f-string

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
56 changes: 31 additions & 25 deletions adafruit_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from micropython import const

try:
from typing import Tuple, List, Union
from busio import UART
import struct
except ImportError:
import ustruct as struct
Expand Down Expand Up @@ -111,7 +113,7 @@ class Adafruit_Fingerprint:
system_id = None
status_register = None

def __init__(self, uart, passwd=(0, 0, 0, 0)):
def __init__(self, uart: UART, passwd: Tuple[int, int, int, int] = (0, 0, 0, 0)):
# Create object with UART for interface, and default 32-bit password
self.password = passwd
self._uart = uart
Expand All @@ -120,28 +122,28 @@ def __init__(self, uart, passwd=(0, 0, 0, 0)):
if self.read_sysparam() != OK:
raise RuntimeError("Failed to read system parameters!")

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

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

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

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

def set_sysparam(self, param_num, param_val):
def set_sysparam(self, param_num: int, param_val: int) -> int:
"""Set the system parameters (param_num)"""
self._send_packet([_SETSYSPARA, param_num, param_val])
r = self._get_packet(12)
Expand All @@ -170,43 +172,43 @@ def set_sysparam(self, param_num, param_val):
self.data_packet_size = param_val
return r[0]

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

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

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

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

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

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

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

def send_fpdata(self, data, sensorbuffer="char", slot=1):
def send_fpdata(
self, data: List[int], sensorbuffer: str = "char", slot: int = 1
) -> bool:
"""Requests the sensor to receive data, either a fingerprint image or
a character/template data. Data is the payload only."""
if slot not in (1, 2):
Expand All @@ -242,13 +246,13 @@ def send_fpdata(self, data, sensorbuffer="char", slot=1):
self._print_debug("sent_fdata data:", data, data_type="hex")
return True

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

def read_templates(self):
def read_templates(self) -> int:
"""Requests the sensor to list of all template locations in use and
stores them in self.templates. Returns the packet error code or
OK success"""
Expand All @@ -273,7 +277,7 @@ def read_templates(self):
r = temp_r
return r[0]

def finger_fast_search(self):
def finger_fast_search(self) -> int:
"""Asks the sensor to search for a matching fingerprint template to the
last model generated. Stores the location and confidence in self.finger_id
and self.confidence. Returns the packet error code or OK success"""
Expand All @@ -296,7 +300,7 @@ def close_uart(self):
"""close serial port"""
self._uart.close()

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

def compare_templates(self):
def compare_templates(self) -> int:
"""Compares two fingerprint templates in char buffers 1 and 2. Stores the confidence score
in self.finger_id and self.confidence. Returns the packet error code or
OK success"""
Expand All @@ -320,7 +324,9 @@ def compare_templates(self):
self._print_debug("compare_templates confidence:", self.confidence)
return r[0]

def set_led(self, color=1, mode=3, speed=0x80, cycles=0):
def set_led(
self, color: int = 1, mode: int = 3, speed: int = 0x80, cycles: int = 0
) -> int:
"""LED function -- only for R503 Sensor.
Parameters: See User Manual for full details
color: 1=red, 2=blue, 3=purple
Expand All @@ -333,7 +339,7 @@ def set_led(self, color=1, mode=3, speed=0x80, cycles=0):

##################################################

def _get_packet(self, expected):
def _get_packet(self, expected: int) -> List[int]:
"""Helper to parse out a packet from the UART and check structure.
Returns just the data payload from the packet"""
res = self._uart.read(expected)
Expand Down Expand Up @@ -366,7 +372,7 @@ def _get_packet(self, expected):
self._print_debug("_get_packet reply:", reply, data_type="hex")
return reply

def _get_data(self, expected):
def _get_data(self, expected: int) -> List[int]:
"""Gets packet from serial and checks structure for _DATAPACKET
and _ENDDATAPACKET. Alternate method for getting data such
as fingerprint image, etc. Returns the data payload."""
Expand Down Expand Up @@ -415,7 +421,7 @@ def _get_data(self, expected):
self._print_debug("_get_data reply:", reply, data_type="hex")
return reply

def _send_packet(self, data):
def _send_packet(self, data: List[int]):
packet = [_STARTCODE >> 8, _STARTCODE & 0xFF]
packet = packet + self.address
packet.append(_COMMANDPACKET) # the packet type
Expand All @@ -434,7 +440,7 @@ def _send_packet(self, data):
self._print_debug("_send_packet data:", packet, data_type="hex")
self._uart.write(bytearray(packet))

def _send_data(self, data):
def _send_data(self, data: List[int]):
self._print_debug("_send_data length:", len(data))
self._print_debug("_send_data data:", data, data_type="hex")
# self.read_sysparam() #moved this to init
Expand Down Expand Up @@ -489,7 +495,7 @@ def soft_reset(self):
if self._uart.read(1)[0] != MODULEOK:
raise RuntimeError("Sensor did not send a handshake signal!")

def _print_debug(self, info, data, data_type="str"):
def _print_debug(self, info: str, data: Union[int, str], data_type: str = "str"):
"""Prints debugging information. This is activated
by flag _debug"""
if not self._debug:
Expand Down