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
- return int ('{:08b}' .format (num )[::- 1 ], 2 )
54
+ result = 0
55
+ for _ in range (8 ):
56
+ result <<= 1
57
+ result += (num & 1 )
58
+ num >>= 1
59
+ return result
55
60
56
61
class PN532_SPI (PN532 ):
57
62
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang
@@ -78,11 +83,11 @@ def _wait_ready(self, timeout=1):
78
83
timestamp = time .monotonic ()
79
84
with self ._spi as spi :
80
85
while (time .monotonic () - timestamp ) < timeout :
81
- time .sleep (0.02 ) # required (not needed when tested on rPi 3)
86
+ time .sleep (0.02 ) # required
82
87
spi .write_readinto (status , status ) #pylint: disable=no-member
83
88
if reverse_bit (status [1 ]) == 0x01 : # LSB data is read in MSB
84
89
return True # Not busy anymore!
85
- else : # (not needed when tested on rPi 3)
90
+ else :
86
91
time .sleep (0.01 ) # pause a bit till we ask again
87
92
# We timed out!
88
93
return False
@@ -95,7 +100,7 @@ def _read_data(self, count):
95
100
frame [0 ] = reverse_bit (_SPI_DATAREAD )
96
101
97
102
with self ._spi as spi :
98
- time .sleep (0.02 ) # required (not needed when tested on rPi 3)
103
+ time .sleep (0.02 ) # required
99
104
spi .write_readinto (frame , frame ) #pylint: disable=no-member
100
105
for i , val in enumerate (frame ):
101
106
frame [i ] = reverse_bit (val ) # turn LSB data to MSB
@@ -111,5 +116,5 @@ def _write_data(self, framebytes):
111
116
if self .debug :
112
117
print ("Writing: " , [hex (i ) for i in rev_frame ])
113
118
with self ._spi as spi :
114
- time .sleep (0.02 ) # required (not needed when tested on rPi 3)
119
+ time .sleep (0.02 ) # required
115
120
spi .write (bytes (rev_frame )) #pylint: disable=no-member
0 commit comments