Skip to content

Commit 3f75987

Browse files
authored
Fix SPI read_passive_target timeout
Moving the `with` statement seems to correct the timeout issue.
1 parent c44abc2 commit 3f75987

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

adafruit_pn532/spi.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ def _wait_ready(self, timeout=1):
7676
status = bytearray([reverse_bit(_SPI_STATREAD), 0])
7777

7878
timestamp = time.monotonic()
79-
while (time.monotonic() - timestamp) < timeout:
80-
with self._spi as spi:
81-
#time.sleep(0.02) # required
79+
with self._spi as spi:
80+
while (time.monotonic() - timestamp) < timeout:
81+
time.sleep(0.02) # required (not needed when tested on rPi 3)
8282
spi.write_readinto(status, status) #pylint: disable=no-member
83-
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
84-
return True # Not busy anymore!
85-
#else:
86-
# time.sleep(0.01) # pause a bit till we ask again
83+
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
84+
return True # Not busy anymore!
85+
else: # (not needed when tested on rPi 3)
86+
time.sleep(0.01) # pause a bit till we ask again
8787
# We timed out!
8888
return False
8989

@@ -95,21 +95,21 @@ def _read_data(self, count):
9595
frame[0] = reverse_bit(_SPI_DATAREAD)
9696

9797
with self._spi as spi:
98-
#time.sleep(0.02) # required
98+
time.sleep(0.02) # required (not needed when tested on rPi 3)
9999
spi.write_readinto(frame, frame) #pylint: disable=no-member
100100
for i, val in enumerate(frame):
101101
frame[i] = reverse_bit(val) # turn LSB data to MSB
102-
#if self.debug:
103-
# print("Reading: ", [hex(i) for i in frame[1:]])
102+
if self.debug:
103+
print("Reading: ", [hex(i) for i in frame[1:]])
104104
return frame[1:]
105105

106106
def _write_data(self, framebytes):
107107
"""Write a specified count of bytes to the PN532"""
108108
# start by making a frame with data write in front,
109109
# then rest of bytes, and LSBify it
110110
rev_frame = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
111-
#if self.debug:
112-
# print("Writing: ", [hex(i) for i in rev_frame])
111+
if self.debug:
112+
print("Writing: ", [hex(i) for i in rev_frame])
113113
with self._spi as spi:
114-
#time.sleep(0.02) # required
114+
time.sleep(0.02) # required (not needed when tested on rPi 3)
115115
spi.write(bytes(rev_frame)) #pylint: disable=no-member

0 commit comments

Comments
 (0)