Skip to content

Commit 74deaee

Browse files
committed
Update UART mode
- Update `_wakeup` to come out of power_down and put the pn532 back into normal mode. - `_wait_ready` now actually checks to see if the pn532 is ready - improve how data is written out
1 parent 80df787 commit 74deaee

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

adafruit_pn532/uart.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,34 @@
4545
class PN532_UART(PN532):
4646
"""Driver for the PN532 connected over Serial UART"""
4747

48-
def __init__(self, uart, *, irq=None, reset=None, debug=False):
48+
def __init__(self, uart, *, reset=None, debug=False):
4949
"""Create an instance of the PN532 class using Serial connection.
50-
Optional IRQ pin (not used), reset pin and debugging output.
50+
Optional reset pin and debugging output.
5151
"""
5252
self.debug = debug
53-
self._irq = irq
5453
self._uart = uart
5554
super().__init__(debug=debug, reset=reset)
5655

5756
def _wakeup(self):
5857
"""Send any special commands/data to wake up PN532"""
59-
# self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01])
58+
if self._reset_pin:
59+
self._reset_pin.value = True
60+
time.sleep(0.01)
61+
self.low_power = False
62+
self._uart.write(
63+
b"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
64+
) # wake up!
6065
self.SAM_configuration()
6166

6267
def _wait_ready(self, timeout=1):
6368
"""Wait `timeout` seconds"""
64-
time.sleep(timeout)
65-
return True
69+
timestamp = time.monotonic()
70+
while (time.monotonic() - timestamp) < timeout:
71+
if self._uart.in_waiting > 0:
72+
return True # No Longer Busy
73+
time.sleep(0.01) # lets ask again soon!
74+
# Timed out!
75+
return False
6676

6777
def _read_data(self, count):
6878
"""Read a specified count of bytes from the PN532."""
@@ -71,17 +81,9 @@ def _read_data(self, count):
7181
raise BusyError("No data read from PN532")
7282
if self.debug:
7383
print("Reading: ", [hex(i) for i in frame])
74-
else:
75-
time.sleep(0.1)
7684
return frame
7785

7886
def _write_data(self, framebytes):
7987
"""Write a specified count of bytes to the PN532"""
80-
while self._uart.read(
81-
1
82-
): # this would be a lot nicer if we could query the # of bytes
83-
pass
84-
self._uart.write(
85-
"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
86-
) # wake up!
88+
self._uart.reset_input_buffer()
8789
self._uart.write(framebytes)

0 commit comments

Comments
 (0)