Skip to content

fixed read_templates() rounding up #3

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 1 commit into from
Mar 4, 2019
Merged
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
58 changes: 30 additions & 28 deletions adafruit_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def get_fpdata(self, buffer='char', slot=1):
raise RuntimeError('Uknown buffer type')
if self._get_packet(12)[0] == 0:
res = self._get_data(9)
#print('datasize: ' + str(len(res)))
#print(res)
# print('datasize: ' + str(len(res)))
# print(res)
return res

def send_fpdata(self, data, buffer='char', slot=1):
Expand All @@ -224,8 +224,8 @@ def send_fpdata(self, data, buffer='char', slot=1):
raise RuntimeError('Uknown buffer type')
if self._get_packet(12)[0] == 0:
self._send_data(data)
#print('datasize: ' + str(len(res)))
#print(res)
# print('datasize: ' + str(len(res)))
# print(res)
return True

def empty_library(self):
Expand All @@ -236,11 +236,13 @@ def empty_library(self):

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"""
stores them in self.templates. Returns the packet error code or
OK success"""
import math
self.templates = []
self.read_sysparam()
temp_r = [0x0c, ]
for j in range(int(self.library_size/256)):
for j in range(math.ceil(self.library_size/256)):
self._send_packet([_TEMPLATEREAD, j])
r = self._get_packet(44)
if r[0] == OK:
Expand Down Expand Up @@ -311,18 +313,18 @@ def _get_data(self, expected):
and _ENDDATAPACKET. Alternate method for getting data such
as fingerprint image, etc. Returns the data payload."""
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')

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

Expand All @@ -339,15 +341,15 @@ def _get_data(self, expected):
res = self._uart.read(length-2)
# todo: we should really inspect the headers and checksum
reply = [i for i in res[0:length]]
self._uart.read(2) # disregard checksum but we really shouldn't
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)
# todo: we should really inspect the headers and checksum
reply = [i for i in res[0:length]]
self._uart.read(2) # disregard checksum but we really shouldn't
self._uart.read(2) # disregard checksum but we really shouldn't
print(len(reply))
#print(reply)
# print(reply)
return reply

def _send_packet(self, data):
Expand Down Expand Up @@ -384,57 +386,57 @@ def _send_data(self, data):
for i in range(int(len(data) / (data_length - 2))):
start = i * (data_length - 2)
end = (i + 1) * (data_length - 2)
#print(start)
#print(end)
#print(i)
# print(start)
# print(end)
# print(i)

packet = [_STARTCODE >> 8, _STARTCODE & 0xFF]
packet = packet + self.address
packet.append(_DATAPACKET)
length = len(data[start:end]) + 2
#print(length)
# print(length)
packet.append(length >> 8)
packet.append(length & 0xFF)
checksum = _DATAPACKET + (length >> 8) + (length & 0xFF)

for j in range(len(data[start:end])):
packet.append(data[j])
#packet.append(struct.pack('@B', data[j]))
# packet.append(struct.pack('@B', data[j]))
checksum += data[j]

packet.append(checksum >> 8)
packet.append(checksum & 0xFF)

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

i += 1
start = i * (data_length - 2)
end = (i + 1) * (data_length - 2)
#print(start)
#print(end)
#print(i)
# print(start)
# print(end)
# print(i)

packet = [_STARTCODE >> 8, _STARTCODE & 0xFF]
packet = packet + self.address
packet.append(_ENDDATAPACKET)
length = len(data[start:end]) + 2
#print(length)
# print(length)
packet.append(length >> 8)
packet.append(length & 0xFF)
checksum = _DATAPACKET + (length >> 8) + (length & 0xFF)

for j in range(len(data[start:end])):
packet.append(data[j])
#packet.append(struct.pack('@B', data[j]))
# packet.append(struct.pack('@B', data[j]))
checksum += data[j]

packet.append(checksum >> 8)
packet.append(checksum & 0xFF)

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