Skip to content

Commit e032600

Browse files
committed
Ran black, updated to pylint 2.x
1 parent 27f8ce9 commit e032600

File tree

6 files changed

+183
-157
lines changed

6 files changed

+183
-157
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
source actions-ci/install.sh
4141
- name: Pip install pylint, black, & Sphinx
4242
run: |
43-
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
43+
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
4444
- name: Library version
4545
run: git describe --dirty --always --tags
4646
- name: PyLint

adafruit_fingerprint.py

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"""
4343

4444
from micropython import const
45+
4546
try:
4647
import struct
4748
except ImportError:
@@ -99,8 +100,10 @@
99100
PASSVERIFY = const(0x21)
100101
MODULEOK = const(0x55)
101102

103+
102104
class Adafruit_Fingerprint:
103105
"""UART based fingerprint sensor."""
106+
104107
_uart = None
105108

106109
password = None
@@ -116,18 +119,18 @@ class Adafruit_Fingerprint:
116119
baudrate = None
117120

118121
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
120123
self.password = passwd
121124
self._uart = uart
122125
if self.verify_password() != OK:
123-
raise RuntimeError('Failed to find sensor, check wiring!')
126+
raise RuntimeError("Failed to find sensor, check wiring!")
124127

125128
def check_module(self):
126129
"""Checks the state of the fingerprint scanner module.
127130
Returns OK or error."""
128131
self._send_packet([_GETECHO])
129132
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.")
131134
return True
132135

133136
def verify_password(self):
@@ -140,20 +143,20 @@ def count_templates(self):
140143
in ``self.template_count``. Returns the packet error code or OK success"""
141144
self._send_packet([_TEMPLATECOUNT])
142145
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]
144147
return r[0]
145148

146149
def read_sysparam(self):
147150
"""Returns the system parameters on success via attributes."""
148151
self._send_packet([_READSYSPARA])
149152
r = self._get_packet(28)
150153
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]
154157
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]
157160
return r[0]
158161

159162
def get_image(self):
@@ -192,36 +195,36 @@ def load_model(self, location, slot=1):
192195
self._send_packet([_LOAD, slot, location >> 8, location & 0xFF])
193196
return self._get_packet(12)[0]
194197

195-
def get_fpdata(self, sensorbuffer='char', slot=1):
198+
def get_fpdata(self, sensorbuffer="char", slot=1):
196199
"""Requests the sensor to transfer the fingerprint image or
197200
template. Returns the data payload only."""
198201
if slot != 1 or slot != 2:
199202
# raise error or use default value?
200203
slot = 2
201-
if sensorbuffer == 'image':
204+
if sensorbuffer == "image":
202205
self._send_packet([_UPLOADIMAGE])
203-
elif sensorbuffer == 'char':
206+
elif sensorbuffer == "char":
204207
self._send_packet([_UPLOAD, slot])
205208
else:
206-
raise RuntimeError('Uknown sensor buffer type')
209+
raise RuntimeError("Uknown sensor buffer type")
207210
if self._get_packet(12)[0] == 0:
208211
res = self._get_data(9)
209212
# print('datasize: ' + str(len(res)))
210213
# print(res)
211214
return res
212215

213-
def send_fpdata(self, data, sensorbuffer='char', slot=1):
216+
def send_fpdata(self, data, sensorbuffer="char", slot=1):
214217
"""Requests the sensor to receive data, either a fingerprint image or
215218
a character/template data. Data is the payload only."""
216219
if slot != 1 or slot != 2:
217220
# raise error or use default value?
218221
slot = 2
219-
if sensorbuffer == 'image':
222+
if sensorbuffer == "image":
220223
self._send_packet([_DOWNLOADIMAGE])
221-
elif sensorbuffer == 'char':
224+
elif sensorbuffer == "char":
222225
self._send_packet([_DOWNLOAD, slot])
223226
else:
224-
raise RuntimeError('Uknown sensor buffer type')
227+
raise RuntimeError("Uknown sensor buffer type")
225228
if self._get_packet(12)[0] == 0:
226229
self._send_data(data)
227230
# print('datasize: ' + str(len(res)))
@@ -238,16 +241,19 @@ def read_templates(self):
238241
"""Requests the sensor to list of all template locations in use and
239242
stores them in self.templates. Returns the packet error code or
240243
OK success"""
241-
import math
244+
from math import ceil # pylint: disable=import-outside-toplevel
245+
242246
self.templates = []
243247
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)):
246252
self._send_packet([_TEMPLATEREAD, j])
247253
r = self._get_packet(44)
248254
if r[0] == OK:
249255
for i in range(32):
250-
byte = r[i+1]
256+
byte = r[i + 1]
251257
for bit in range(8):
252258
if byte & (1 << bit):
253259
self.templates.append((i * 8) + bit + (j * 256))
@@ -261,52 +267,53 @@ def finger_fast_search(self):
261267
last model generated. Stores the location and confidence in self.finger_id
262268
and self.confidence. Returns the packet error code or OK success"""
263269
# 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])
265271
# 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])
267273
# or base the page on module's capacity
268274
self.read_sysparam()
269275
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+
)
272279
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]))
274281
# print(r)
275282
return r[0]
276283

277-
##################################################
284+
##################################################
278285

279286
def _get_packet(self, expected):
280287
""" Helper to parse out a packet from the UART and check structure.
281288
Returns just the data payload from the packet"""
282289
res = self._uart.read(expected)
283-
#print("Got", res)
290+
# print("Got", res)
284291
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")
286293

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

290297
if start != _STARTCODE:
291-
raise RuntimeError('Incorrect packet data')
298+
raise RuntimeError("Incorrect packet data")
292299
# next 4 bytes are address
293-
addr = [i for i in res[2:6]]
300+
addr = list(i for i in res[2:6])
294301
if addr != self.address:
295-
raise RuntimeError('Incorrect address')
302+
raise RuntimeError("Incorrect address")
296303

297-
packet_type, length = struct.unpack('>BH', res[6:9])
304+
packet_type, length = struct.unpack(">BH", res[6:9])
298305
if packet_type != _ACKPACKET:
299-
raise RuntimeError('Incorrect packet data')
306+
raise RuntimeError("Incorrect packet data")
300307

301308
# we should check the checksum
302309
# but i don't know how
303310
# 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)]))
307314

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)
310317
return reply
311318

312319
def _get_data(self, expected):
@@ -315,38 +322,38 @@ def _get_data(self, expected):
315322
as fingerprint image, etc. Returns the data payload."""
316323
res = self._uart.read(expected)
317324
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")
319326

320327
# first two bytes are start code
321-
start = struct.unpack('>H', res[0:2])[0]
328+
start = struct.unpack(">H", res[0:2])[0]
322329
# print(start)
323330
if start != _STARTCODE:
324-
raise RuntimeError('Incorrect packet data')
331+
raise RuntimeError("Incorrect packet data")
325332
# next 4 bytes are address
326-
addr = [i for i in res[2:6]]
333+
addr = list(i for i in res[2:6])
327334
# print(addr)
328335
if addr != self.address:
329-
raise RuntimeError('Incorrect address')
336+
raise RuntimeError("Incorrect address")
330337

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))
333340

334341
# todo: check checksum
335342

336343
if packet_type != _DATAPACKET:
337344
if packet_type != _ENDDATAPACKET:
338-
raise RuntimeError('Incorrect packet data')
345+
raise RuntimeError("Incorrect packet data")
339346

340347
if packet_type == _DATAPACKET:
341-
res = self._uart.read(length-2)
348+
res = self._uart.read(length - 2)
342349
# 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])
344351
self._uart.read(2) # disregard checksum but we really shouldn't
345352
reply += self._get_data(9)
346353
elif packet_type == _ENDDATAPACKET:
347-
res = self._uart.read(length-2)
354+
res = self._uart.read(length - 2)
348355
# 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])
350357
self._uart.read(2) # disregard checksum but we really shouldn't
351358
# print(len(reply))
352359
# print(reply)
@@ -367,7 +374,7 @@ def _send_packet(self, data):
367374
packet.append(checksum >> 8)
368375
packet.append(checksum & 0xFF)
369376

370-
#print("Sending: ", [hex(i) for i in packet])
377+
# print("Sending: ", [hex(i) for i in packet])
371378
self._uart.write(bytearray(packet))
372379

373380
def _send_data(self, data):

0 commit comments

Comments
 (0)