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
This commit is contained in:
George Waters 2020-09-08 20:50:23 -04:00
parent 80df78725f
commit 74deaee495
No known key found for this signature in database
GPG key ID: D993F8B1CC21DB25

View file

@ -45,24 +45,34 @@ from adafruit_pn532.adafruit_pn532 import PN532, BusyError
class PN532_UART(PN532): class PN532_UART(PN532):
"""Driver for the PN532 connected over Serial UART""" """Driver for the PN532 connected over Serial UART"""
def __init__(self, uart, *, irq=None, reset=None, debug=False): def __init__(self, uart, *, reset=None, debug=False):
"""Create an instance of the PN532 class using Serial connection. """Create an instance of the PN532 class using Serial connection.
Optional IRQ pin (not used), reset pin and debugging output. Optional reset pin and debugging output.
""" """
self.debug = debug self.debug = debug
self._irq = irq
self._uart = uart self._uart = uart
super().__init__(debug=debug, reset=reset) super().__init__(debug=debug, reset=reset)
def _wakeup(self): def _wakeup(self):
"""Send any special commands/data to wake up PN532""" """Send any special commands/data to wake up PN532"""
# self._write_frame([_HOSTTOPN532, _COMMAND_SAMCONFIGURATION, 0x01]) if self._reset_pin:
self._reset_pin.value = True
time.sleep(0.01)
self.low_power = False
self._uart.write(
b"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
) # wake up!
self.SAM_configuration() self.SAM_configuration()
def _wait_ready(self, timeout=1): def _wait_ready(self, timeout=1):
"""Wait `timeout` seconds""" """Wait `timeout` seconds"""
time.sleep(timeout) timestamp = time.monotonic()
return True while (time.monotonic() - timestamp) < timeout:
if self._uart.in_waiting > 0:
return True # No Longer Busy
time.sleep(0.01) # lets ask again soon!
# Timed out!
return False
def _read_data(self, count): def _read_data(self, count):
"""Read a specified count of bytes from the PN532.""" """Read a specified count of bytes from the PN532."""
@ -71,17 +81,9 @@ class PN532_UART(PN532):
raise BusyError("No data read from PN532") raise BusyError("No data read from PN532")
if self.debug: if self.debug:
print("Reading: ", [hex(i) for i in frame]) print("Reading: ", [hex(i) for i in frame])
else:
time.sleep(0.1)
return frame return frame
def _write_data(self, framebytes): def _write_data(self, framebytes):
"""Write a specified count of bytes to the PN532""" """Write a specified count of bytes to the PN532"""
while self._uart.read( self._uart.reset_input_buffer()
1
): # this would be a lot nicer if we could query the # of bytes
pass
self._uart.write(
"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
) # wake up!
self._uart.write(framebytes) self._uart.write(framebytes)