Skip to content

Commit 6664444

Browse files
committed
light neatening
1 parent 5542a15 commit 6664444

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

adafruit_pn532.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -405,61 +405,57 @@ def __init__(self, i2c, *, irq=None, reset=None, debug=False):
405405
super().__init__(debug=debug, reset=reset)
406406

407407
def _wait_ready(self, timeout=1):
408-
if self._irq:
409-
print("TODO IRQ")
410-
else:
411-
status = bytearray(1)
412-
t = time.monotonic()
413-
while (time.monotonic() - t) < timeout:
414-
with self._i2c:
415-
self._i2c.readinto(status)
416-
if status == b'\x01':
417-
return True
418-
else:
419-
time.sleep(0.1)
408+
status = bytearray(1)
409+
t = time.monotonic()
410+
while (time.monotonic() - t) < timeout:
411+
with self._i2c:
412+
self._i2c.readinto(status)
413+
if status == b'\x01':
414+
return True # No longer busy
415+
else:
416+
time.sleep(0.1) # lets ask again soon!
417+
# Timed out!
420418
return False
421419

422420
def _read_data(self, count):
423421
"""Read a specified count of bytes from the PN532."""
424422
# Build a read request frame.
425423
frame = bytearray(count+1)
426-
with self._i2c:
427-
self._i2c.readinto(frame, end=1) # read ready byte!
428-
if frame[0] != 0x01: # not ready
424+
with self._i2c as i2c:
425+
i2c.readinto(frame, end=1) # read status byte!
426+
if frame[0] != 0x01: # not ready
429427
raise BusyError
430-
self._i2c.readinto(frame)
428+
i2c.readinto(frame) # ok get the data, plus statusbyte
431429
if self.debug:
432430
print("Reading: ", [hex(i) for i in frame[1:]])
433-
return frame[1:]
431+
return frame[1:] # don't return the status byte
434432

435433
def _write_data(self, framebytes):
436-
with self._i2c:
437-
self._i2c.write(framebytes)
434+
with self._i2c as i2c:
435+
i2c.write(framebytes)
438436

439437
class PN532_SPI(PN532):
440-
"""Driver for the PN532 connected over I2C."""
438+
"""Driver for the PN532 connected over SPI. Pass in a hardware or bitbang SPI device & chip select
439+
digitalInOut pin. Optional IRQ pin (not used), reset pin and debugging output."""
441440
def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
442-
"""Create an instance of the PN532 class using SPI
443-
"""
441+
"""Create an instance of the PN532 class using SPI"""
444442
self.debug = debug
445443
self._irq = irq
446444
self._spi = spi_device.SPIDevice(spi, cs_pin)
447445
super().__init__(debug=debug, reset=reset)
448446

449447
def _wait_ready(self, timeout=1):
450-
if self._irq:
451-
print("TODO IRQ")
452-
else:
453-
status = bytearray([reverse_bit(_SPI_STATREAD), 0])
454-
455-
t = time.monotonic()
456-
while (time.monotonic() - t) < timeout:
457-
with self._spi as spi:
458-
spi.write_readinto(status, status)
459-
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
460-
return True
461-
else:
462-
time.sleep(0.1)
448+
status = bytearray([reverse_bit(_SPI_STATREAD), 0])
449+
450+
t = time.monotonic()
451+
while (time.monotonic() - t) < timeout:
452+
with self._spi as spi:
453+
spi.write_readinto(status, status)
454+
if reverse_bit(status[1]) == 0x01: # LSB data is read in MSB
455+
return True # Not busy anymore!
456+
else:
457+
time.sleep(0.1) # pause a bit till we ask again
458+
# We timed out!
463459
return False
464460

465461
def _read_data(self, count):
@@ -470,18 +466,19 @@ def _read_data(self, count):
470466
frame[0] = reverse_bit(_SPI_DATAREAD)
471467

472468
with self._spi as spi:
473-
time.sleep(0.01)
469+
time.sleep(0.01) # required
474470
spi.write_readinto(frame, frame)
475-
for i in range(len(frame)):
476-
frame[i] = reverse_bit(frame[i]) # turn LSB data to MSB
471+
for i in range(len(frame)):
472+
frame[i] = reverse_bit(frame[i]) # turn LSB data to MSB
477473
if self.debug:
478474
print("Reading: ", [hex(i) for i in frame[1:]])
479475
return frame[1:]
480476

481477
def _write_data(self, framebytes):
478+
# start by making a frame with data write in front, then rest of bytes, and LSBify it
479+
reversed = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
480+
if self.debug:
481+
print("Writing: ", [hex(i) for i in reversed])
482482
with self._spi as spi:
483-
reversed = [reverse_bit(x) for x in bytes([_SPI_DATAWRITE]) + framebytes]
484-
time.sleep(0.01)
485-
if self.debug:
486-
print("writing: ", [hex(i) for i in reversed])
483+
time.sleep(0.01) # required
487484
spi.write(bytes(reversed))

0 commit comments

Comments
 (0)