@@ -295,6 +295,19 @@ def call_function(
295295 for a response and return a bytearray of response bytes, or None if no
296296 response is available within the timeout.
297297 """
298+ if not self .send_command (command , params = params , timeout = timeout ):
299+ return None
300+ return self .process_response (
301+ command , response_length = response_length , timeout = timeout
302+ )
303+
304+ def send_command (
305+ self , command , params = [], timeout = 1
306+ ): # pylint: disable=dangerous-default-value
307+ """Send specified command to the PN532 and wait for an acknowledgment.
308+ Will wait up to timeout seconds for the acknowlegment and return True.
309+ If no acknowlegment is received, False is returned.
310+ """
298311 # Build frame data with command and parameters.
299312 data = bytearray (2 + len (params ))
300313 data [0 ] = _HOSTTOPN532
@@ -306,12 +319,21 @@ def call_function(
306319 self ._write_frame (data )
307320 except OSError :
308321 self ._wakeup ()
309- return None
322+ return False
310323 if not self ._wait_ready (timeout ):
311- return None
324+ return False
312325 # Verify ACK response and wait to be ready for function response.
313326 if not _ACK == self ._read_data (len (_ACK )):
314327 raise RuntimeError ("Did not receive expected ACK from PN532!" )
328+ return True
329+
330+ def process_response (self , command , response_length = 0 , timeout = 1 ):
331+ """Process the response from the PN532 and expect up to response_length
332+ bytes back in a response. Note that less than the expected bytes might
333+ be returned! Will wait up to timeout seconds for a response and return
334+ a bytearray of response bytes, or None if no response is available
335+ within the timeout.
336+ """
315337 if not self ._wait_ready (timeout ):
316338 return None
317339 # Read response bytes.
@@ -348,15 +370,41 @@ def read_passive_target(self, card_baud=_MIFARE_ISO14443A, timeout=1):
348370 otherwise a bytearray with the UID of the found card is returned.
349371 """
350372 # Send passive read command for 1 card. Expect at most a 7 byte UUID.
373+ response = self .listen_for_passive_target (card_baud = card_baud , timeout = timeout )
374+ # If no response is available return None to indicate no card is present.
375+ if not response :
376+ return None
377+ return self .get_passive_target (timeout = timeout )
378+
379+ def listen_for_passive_target (self , card_baud = _MIFARE_ISO14443A , timeout = 1 ):
380+ """Send command to PN532 to begin listening for a Mifare card. This
381+ returns True if the command was received succesfully. Note, this does
382+ not also return the UID of a card! `get_passive_target` must be called
383+ to read the UID when a card is found. If just looking to see if a card
384+ is currently present use `read_passive_target` instead.
385+ """
386+ # Send passive read command for 1 card. Expect at most a 7 byte UUID.
351387 try :
352- response = self .call_function (
353- _COMMAND_INLISTPASSIVETARGET ,
354- params = [0x01 , card_baud ],
355- response_length = 19 ,
356- timeout = timeout ,
388+ response = self .send_command (
389+ _COMMAND_INLISTPASSIVETARGET , params = [0x01 , card_baud ], timeout = timeout
357390 )
358391 except BusyError :
359- return None # no card found!
392+ return False # _COMMAND_INLISTPASSIVETARGET failed
393+ return response
394+
395+ def get_passive_target (self , timeout = 1 ):
396+ """Will wait up to timeout seconds and return None if no card is found,
397+ otherwise a bytearray with the UID of the found card is returned.
398+ `listen_for_passive_target` must have been called first in order to put
399+ the PN532 into a listening mode.
400+
401+ It can be useful to use this when using the IRQ pin. Use the IRQ pin to
402+ detect when a card is present and then call this function to read the
403+ card's UID. This reduces the amount of time spend checking for a card.
404+ """
405+ response = self .process_response (
406+ _COMMAND_INLISTPASSIVETARGET , response_length = 19 , timeout = timeout
407+ )
360408 # If no response is available return None to indicate no card is present.
361409 if response is None :
362410 return None
0 commit comments