Skip to content
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

for computer use #9

Merged
merged 2 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions adafruit_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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:]))[0]
self.template_count = struct.unpack('>H', bytes(r[1:3]))[0]
return r[0]

def get_image(self):
Expand Down Expand Up @@ -174,7 +174,7 @@ def finger_fast_search(self):
# high speed search of slot #1 starting at page 0x0000 and page #0x00A3
self._send_packet([_HISPEEDSEARCH, 0x01, 0x00, 0x00, 0x00, 0xA3])
r = self._get_packet(16)
self.finger_id, self.confidence = struct.unpack('>HH', bytes(r[1:]))
self.finger_id, self.confidence = struct.unpack('>HH', bytes(r[1:5]))
return r[0]

##################################################
Expand All @@ -188,7 +188,7 @@ def _get_packet(self, expected):
raise RuntimeError('Failed to read data from sensor')

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

if start != _STARTCODE:
raise RuntimeError('Incorrect packet data')
Expand All @@ -197,7 +197,7 @@ def _get_packet(self, expected):
if addr != self.address:
raise RuntimeError('Incorrect address')

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

Expand Down
22 changes: 13 additions & 9 deletions examples/fingerprint_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

uart = busio.UART(board.TX, board.RX, baudrate=57600)

# If using with a computer such as Linux/RaspberryPi, Mac, Windows...
#import serial
#uart = serial.Serial("/dev/ttyUSB0", baudrate=57600, timeout=1)

finger = adafruit_fingerprint.Adafruit_Fingerprint(uart)

##################################################
Expand All @@ -31,7 +35,7 @@ def get_fingerprint():
def get_fingerprint_detail():
"""Get a finger print image, template it, and see if it matches!
This time, print out each error instead of just returning on failure"""
print("Getting image...", end="")
print("Getting image...", end="", flush=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CircuitPython 3.x, extra keyword args to print() are ignored. In 4.0, because of a merge we did from MicroPython, they are now checked, but flush= is not an implemented keyword arg, and fails in 4.0:

Adafruit CircuitPython 4.0.0-alpha-922-g9da79c880-dirty on 2018-08-19; Adafruit Metro M0 Express with samd21g18
>>> print("abc", "def", sep=".")
abc.def
>>> print("abc", "def", sepx=".")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
>>> print("abc", "def", end=".")
abc def.>>> print("abc", "def", flush=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
>>> 

So we need to handle flush in 4.0, otherwise this code will fail.

Not sure if any reasonable change can be made in this library. Seems like I should open an issue for 4.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah plz do, but i think we should default to True - basically CPython wont actually emit the output otherwise. kinda lame!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or rather, there probably isnt a way for us to False not-flush. i dunno. we can ignore it but have the kwarg there? up to y'all

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannewt have a comment here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative here is just to print each of those status messages on their own lines (remove end="").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always an option, but it makes for a nice UI to keep :)

i = finger.get_image()
if i == adafruit_fingerprint.OK:
print("Image taken")
Expand All @@ -44,7 +48,7 @@ def get_fingerprint_detail():
print("Other error")
return False

print("Templating...", end="")
print("Templating...", end="", flush=True)
i = finger.image_2_tz(1)
if i == adafruit_fingerprint.OK:
print("Templated")
Expand All @@ -59,7 +63,7 @@ def get_fingerprint_detail():
print("Other error")
return False

print("Searching...", end="")
print("Searching...", end="", flush=True)
i = finger.finger_fast_search()
# pylint: disable=no-else-return
# This block needs to be refactored when it can be tested.
Expand All @@ -78,25 +82,25 @@ def enroll_finger(location):
"""Take a 2 finger images and template it, then store in 'location'"""
for fingerimg in range(1, 3):
if fingerimg == 1:
print("Place finger on sensor...", end="")
print("Place finger on sensor...", end="", flush=True)
else:
print("Place same finger again...", end="")
print("Place same finger again...", end="", flush=True)

while True:
i = finger.get_image()
if i == adafruit_fingerprint.OK:
print("Image taken")
break
elif i == adafruit_fingerprint.NOFINGER:
print(".", end="")
print(".", end="", flush=True)
elif i == adafruit_fingerprint.IMAGEFAIL:
print("Imaging error")
return False
else:
print("Other error")
return False

print("Templating...", end="")
print("Templating...", end="", flush=True)
i = finger.image_2_tz(fingerimg)
if i == adafruit_fingerprint.OK:
print("Templated")
Expand All @@ -117,7 +121,7 @@ def enroll_finger(location):
while i != adafruit_fingerprint.NOFINGER:
i = finger.get_image()

print("Creating model...", end="")
print("Creating model...", end="", flush=True)
i = finger.create_model()
if i == adafruit_fingerprint.OK:
print("Created")
Expand All @@ -128,7 +132,7 @@ def enroll_finger(location):
print("Other error")
return False

print("Storing model #%d..." % location, end="")
print("Storing model #%d..." % location, end="", flush=True)
i = finger.store_model(location)
if i == adafruit_fingerprint.OK:
print("Stored")
Expand Down