51
51
def reverse_bit (num ):
52
52
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
53
53
it is LSB for the PN532, but 99% of SPI implementations are MSB only!"""
54
- result = 0
55
- for _ in range (8 ):
56
- result <<= 1
57
- result += (num & 1 )
58
- num >>= 1
59
- return result
54
+ return int ('{:08b}' .format (num )[::- 1 ], 2 )
60
55
61
56
class PN532_SPI (PN532 ):
62
57
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
@@ -83,12 +78,12 @@ def _wait_ready(self, timeout=1):
83
78
timestamp = time .monotonic ()
84
79
while (time .monotonic () - timestamp ) < timeout :
85
80
with self ._spi as spi :
86
- time .sleep (0.02 ) # required
81
+ # time.sleep(0.02) # required
87
82
spi .write_readinto (status , status ) #pylint: disable=no-member
88
83
if reverse_bit (status [1 ]) == 0x01 : # LSB data is read in MSB
89
84
return True # Not busy anymore!
90
- else :
91
- time .sleep (0.01 ) # pause a bit till we ask again
85
+ # else:
86
+ # time.sleep(0.01) # pause a bit till we ask again
92
87
# We timed out!
93
88
return False
94
89
@@ -100,21 +95,21 @@ def _read_data(self, count):
100
95
frame [0 ] = reverse_bit (_SPI_DATAREAD )
101
96
102
97
with self ._spi as spi :
103
- time .sleep (0.02 ) # required
98
+ # time.sleep(0.02) # required
104
99
spi .write_readinto (frame , frame ) #pylint: disable=no-member
105
100
for i , val in enumerate (frame ):
106
101
frame [i ] = reverse_bit (val ) # turn LSB data to MSB
107
- if self .debug :
108
- print ("Reading: " , [hex (i ) for i in frame [1 :]])
102
+ # if self.debug:
103
+ # print("Reading: ", [hex(i) for i in frame[1:]])
109
104
return frame [1 :]
110
105
111
106
def _write_data (self , framebytes ):
112
107
"""Write a specified count of bytes to the PN532"""
113
108
# start by making a frame with data write in front,
114
109
# then rest of bytes, and LSBify it
115
110
rev_frame = [reverse_bit (x ) for x in bytes ([_SPI_DATAWRITE ]) + framebytes ]
116
- if self .debug :
117
- print ("Writing: " , [hex (i ) for i in rev_frame ])
111
+ # if self.debug:
112
+ # print("Writing: ", [hex(i) for i in rev_frame])
118
113
with self ._spi as spi :
119
- time .sleep (0.02 ) # required
114
+ # time.sleep(0.02) # required
120
115
spi .write (bytes (rev_frame )) #pylint: disable=no-member
0 commit comments