pn532: add support for polling with timeout

meow
vuko 2023-12-05 21:01:20 +01:00
parent d4344b464e
commit 5175af4f5a
1 changed files with 40 additions and 3 deletions

View File

@ -35,6 +35,8 @@ _COMMAND_GETFIRMWAREVERSION = const(0x02)
_COMMAND_INLISTPASSIVETARGET = const(0x4A)
_COMMAND_INRELEASE = const(0x52)
_COMMAND_SAMCONFIGURATION = const(0x14)
_COMMAND_RFCONFIGURATION = const(0x32)
_COMMAND_POWERDOWN = const(0x16)
# Send Frames
_PREAMBLE = const(0x00)
@ -195,9 +197,23 @@ class PN532Uart(object):
if self.debug:
print("Sending SAM_CONFIGURATION")
response = self.call_function(_COMMAND_SAMCONFIGURATION, params=[0x01, 0x14, 0x01])
response = self.call_function(_COMMAND_SAMCONFIGURATION, params=[0x01])
if self.debug:
print('SAM_configuration:', [hex(i) for i in response])
print('SAM_configuration:', response.hex())
# Enable RF:
# 0x32 - Cmd: RFConfiguration
# 0x01 - Item: RF Field
# 0x03 - AutoRFCA=on, RF=on
self.call_function(_COMMAND_RFCONFIGURATION, params=[0x01, 0x03])
# Return imidiately aftery trying once:
# 0x32 - Cmd: RFConfiguration
# 0x05 - Item: MaxRetries
# 0x00 - 0 retries (1 try)
# 0x00 - 0 retries (1 try)
# 0x00 - 0 retries (1 try)
self.call_function(_COMMAND_RFCONFIGURATION, params=[0x05, 0x00, 0x00, 0x00])
def get_firmware_version(self):
"""
@ -218,13 +234,22 @@ class PN532Uart(object):
Will wait up to timeout seconds and return None if no card is found,
otherwise a bytearray with the UID of the found card is returned.
"""
# Enable RF:
# 0x32 - Cmd: RFConfiguration
# 0x01 - Item: RF Field
# 0x03 - AutoRFCA=on, RF=on
self.call_function(_COMMAND_RFCONFIGURATION, params=[0x01, 0x03])
if self.debug:
print("Sending INIT_PASSIVE_TARGET")
# Send passive read command for 1 card. Expect at most a 7 byte UUID.
response = self.call_function(_COMMAND_INLISTPASSIVETARGET, params=[0x01, card_baud])
# Check only 1 card with up to a 7 byte UID is present.
if response[0] != 0x01:
if response[0] == 0x00:
return None
if response[0] > 0x01:
raise PN532Error('More than one card detected!')
if response[5] > 7:
raise PN532Error('Found card with unexpectedly long UID!')
@ -232,6 +257,18 @@ class PN532Uart(object):
# Return UID of card.
return response[6:6+response[5]]
def power_down(self):
# Disable RF:
# 0x32 - Cmd: RFConfiguration
# 0x01 - Item: RF Field
# 0x00 - AutoRFCA=off, RF=off
self.call_function(_COMMAND_RFCONFIGURATION, params=[0x01, 0x00])
# Power down
# 0x16 - Cmd: PowerDown
# 0x10 - WakeUpEnable: 5 bit - HSU (UART)
self.call_function(_COMMAND_POWERDOWN, params=[0x10])
def release_targets(self):
if self.debug:
print("Release Targets")