45
45
class PN532_UART (PN532 ):
46
46
"""Driver for the PN532 connected over Serial UART"""
47
47
48
- def __init__ (self , uart , * , irq = None , reset = None , debug = False ):
48
+ def __init__ (self , uart , * , reset = None , debug = False ):
49
49
"""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.
51
51
"""
52
52
self .debug = debug
53
- self ._irq = irq
54
53
self ._uart = uart
55
54
super ().__init__ (debug = debug , reset = reset )
56
55
57
56
def _wakeup (self ):
58
57
"""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!
60
65
self .SAM_configuration ()
61
66
62
67
def _wait_ready (self , timeout = 1 ):
63
68
"""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
66
76
67
77
def _read_data (self , count ):
68
78
"""Read a specified count of bytes from the PN532."""
@@ -71,17 +81,9 @@ def _read_data(self, count):
71
81
raise BusyError ("No data read from PN532" )
72
82
if self .debug :
73
83
print ("Reading: " , [hex (i ) for i in frame ])
74
- else :
75
- time .sleep (0.1 )
76
84
return frame
77
85
78
86
def _write_data (self , framebytes ):
79
87
"""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 ()
87
89
self ._uart .write (framebytes )
0 commit comments