Skip to content

Commit

Permalink
Merge pull request #18 from adafruit/pylint-update
Browse files Browse the repository at this point in the history
Ran black, updated to pylint 2.x
  • Loading branch information
kattni authored Mar 17, 2020
2 parents 06f580c + e032600 commit 870469d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
113 changes: 60 additions & 53 deletions adafruit_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"""

from micropython import const

try:
import struct
except ImportError:
Expand Down Expand Up @@ -99,8 +100,10 @@
PASSVERIFY = const(0x21)
MODULEOK = const(0x55)


class Adafruit_Fingerprint:
"""UART based fingerprint sensor."""

_uart = None

password = None
Expand All @@ -116,18 +119,18 @@ class Adafruit_Fingerprint:
baudrate = None

def __init__(self, uart, passwd=(0, 0, 0, 0)):
# Create object with UART for interface, and default 32-bit password
# Create object with UART for interface, and default 32-bit password
self.password = passwd
self._uart = uart
if self.verify_password() != OK:
raise RuntimeError('Failed to find sensor, check wiring!')
raise RuntimeError("Failed to find sensor, check wiring!")

def check_module(self):
"""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.')
raise RuntimeError("Something is wrong with the sensor.")
return True

def verify_password(self):
Expand All @@ -140,20 +143,20 @@ def count_templates(self):
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]
self.template_count = struct.unpack(">H", bytes(r[1:3]))[0]
return r[0]

def read_sysparam(self):
"""Returns the system parameters on success via attributes."""
self._send_packet([_READSYSPARA])
r = self._get_packet(28)
if r[0] != OK:
raise RuntimeError('Command failed.')
self.library_size = struct.unpack('>H', bytes(r[5:7]))[0]
self.security_level = struct.unpack('>H', bytes(r[7:9]))[0]
raise RuntimeError("Command failed.")
self.library_size = struct.unpack(">H", bytes(r[5:7]))[0]
self.security_level = struct.unpack(">H", bytes(r[7:9]))[0]
self.device_address = bytes(r[9:13])
self.data_packet_size = struct.unpack('>H', bytes(r[13:15]))[0]
self.baudrate = struct.unpack('>H', bytes(r[15:17]))[0]
self.data_packet_size = struct.unpack(">H", bytes(r[13:15]))[0]
self.baudrate = struct.unpack(">H", bytes(r[15:17]))[0]
return r[0]

def get_image(self):
Expand Down Expand Up @@ -192,36 +195,36 @@ def load_model(self, location, slot=1):
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="char", slot=1):
"""Requests the sensor to transfer the fingerprint image or
template. Returns the data payload only."""
if slot != 1 or slot != 2:
# raise error or use default value?
slot = 2
if sensorbuffer == 'image':
if sensorbuffer == "image":
self._send_packet([_UPLOADIMAGE])
elif sensorbuffer == 'char':
elif sensorbuffer == "char":
self._send_packet([_UPLOAD, slot])
else:
raise RuntimeError('Uknown sensor buffer type')
raise RuntimeError("Uknown sensor buffer type")
if self._get_packet(12)[0] == 0:
res = self._get_data(9)
# print('datasize: ' + str(len(res)))
# print(res)
return res

def send_fpdata(self, data, sensorbuffer='char', slot=1):
def send_fpdata(self, data, sensorbuffer="char", slot=1):
"""Requests the sensor to receive data, either a fingerprint image or
a character/template data. Data is the payload only."""
if slot != 1 or slot != 2:
# raise error or use default value?
slot = 2
if sensorbuffer == 'image':
if sensorbuffer == "image":
self._send_packet([_DOWNLOADIMAGE])
elif sensorbuffer == 'char':
elif sensorbuffer == "char":
self._send_packet([_DOWNLOAD, slot])
else:
raise RuntimeError('Uknown sensor buffer type')
raise RuntimeError("Uknown sensor buffer type")
if self._get_packet(12)[0] == 0:
self._send_data(data)
# print('datasize: ' + str(len(res)))
Expand All @@ -238,16 +241,19 @@ def read_templates(self):
"""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"""
import math
from math import ceil # pylint: disable=import-outside-toplevel

self.templates = []
self.read_sysparam()
temp_r = [0x0c, ]
for j in range(math.ceil(self.library_size/256)):
temp_r = [
0x0C,
]
for j in range(ceil(self.library_size / 256)):
self._send_packet([_TEMPLATEREAD, j])
r = self._get_packet(44)
if r[0] == OK:
for i in range(32):
byte = r[i+1]
byte = r[i + 1]
for bit in range(8):
if byte & (1 << bit):
self.templates.append((i * 8) + bit + (j * 256))
Expand All @@ -261,52 +267,53 @@ def finger_fast_search(self):
last model generated. Stores the location and confidence in self.finger_id
and self.confidence. Returns the packet error code or OK success"""
# high speed search of slot #1 starting at page 0x0000 and page #0x00A3
#self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
# self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
# or page #0x03E9 to accommodate modules with up to 1000 capacity
#self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x03, 0xE9])
# self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x03, 0xE9])
# or base the page on module's capacity
self.read_sysparam()
capacity = self.library_size
self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, capacity >> 8,
capacity & 0xFF])
self._send_packet(
[_HISPEEDSEARCH, 0x01, 0x00, 0x00, capacity >> 8, capacity & 0xFF]
)
r = self._get_packet(16)
self.finger_id, self.confidence = struct.unpack('>HH', bytes(r[1:5]))
self.finger_id, self.confidence = struct.unpack(">HH", bytes(r[1:5]))
# print(r)
return r[0]

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

def _get_packet(self, expected):
""" 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)
#print("Got", res)
# print("Got", res)
if (not res) or (len(res) != expected):
raise RuntimeError('Failed to read data from sensor')
raise RuntimeError("Failed to read data from sensor")

# first two bytes are start code
start = struct.unpack('>H', res[0:2])[0]
start = struct.unpack(">H", res[0:2])[0]

if start != _STARTCODE:
raise RuntimeError('Incorrect packet data')
raise RuntimeError("Incorrect packet data")
# next 4 bytes are address
addr = [i for i in res[2:6]]
addr = list(i for i in res[2:6])
if addr != self.address:
raise RuntimeError('Incorrect address')
raise RuntimeError("Incorrect address")

packet_type, length = struct.unpack('>BH', res[6:9])
packet_type, length = struct.unpack(">BH", res[6:9])
if packet_type != _ACKPACKET:
raise RuntimeError('Incorrect packet data')
raise RuntimeError("Incorrect packet data")

# we should check the checksum
# but i don't know how
# not yet anyway
#packet_sum = struct.unpack('>H', res[9+(length-2):9+length])[0]
#print(packet_sum)
#print(packet_type + length + struct.unpack('>HHHH', res[9:9+(length-2)]))
# packet_sum = struct.unpack('>H', res[9+(length-2):9+length])[0]
# print(packet_sum)
# print(packet_type + length + struct.unpack('>HHHH', res[9:9+(length-2)]))

reply = [i for i in res[9:9+(length-2)]]
#print(reply)
reply = list(i for i in res[9 : 9 + (length - 2)])
# print(reply)
return reply

def _get_data(self, expected):
Expand All @@ -315,38 +322,38 @@ def _get_data(self, expected):
as fingerprint image, etc. Returns the data payload."""
res = self._uart.read(expected)
if (not res) or (len(res) != expected):
raise RuntimeError('Failed to read data from sensor')
raise RuntimeError("Failed to read data from sensor")

# first two bytes are start code
start = struct.unpack('>H', res[0:2])[0]
start = struct.unpack(">H", res[0:2])[0]
# print(start)
if start != _STARTCODE:
raise RuntimeError('Incorrect packet data')
raise RuntimeError("Incorrect packet data")
# next 4 bytes are address
addr = [i for i in res[2:6]]
addr = list(i for i in res[2:6])
# print(addr)
if addr != self.address:
raise RuntimeError('Incorrect address')
raise RuntimeError("Incorrect address")

packet_type, length = struct.unpack('>BH', res[6:9])
#print(str(packet_type) + ' ' + str(length))
packet_type, length = struct.unpack(">BH", res[6:9])
# print(str(packet_type) + ' ' + str(length))

# todo: check checksum

if packet_type != _DATAPACKET:
if packet_type != _ENDDATAPACKET:
raise RuntimeError('Incorrect packet data')
raise RuntimeError("Incorrect packet data")

if packet_type == _DATAPACKET:
res = self._uart.read(length-2)
res = self._uart.read(length - 2)
# todo: we should really inspect the headers and checksum
reply = [i for i in res[0:length]]
reply = list(i for i in res[0:length])
self._uart.read(2) # disregard checksum but we really shouldn't
reply += self._get_data(9)
elif packet_type == _ENDDATAPACKET:
res = self._uart.read(length-2)
res = self._uart.read(length - 2)
# todo: we should really inspect the headers and checksum
reply = [i for i in res[0:length]]
reply = list(i for i in res[0:length])
self._uart.read(2) # disregard checksum but we really shouldn't
# print(len(reply))
# print(reply)
Expand All @@ -367,7 +374,7 @@ def _send_packet(self, data):
packet.append(checksum >> 8)
packet.append(checksum & 0xFF)

#print("Sending: ", [hex(i) for i in packet])
# print("Sending: ", [hex(i) for i in packet])
self._uart.write(bytearray(packet))

def _send_data(self, data):
Expand Down
Loading

0 comments on commit 870469d

Please sign in to comment.