@@ -405,61 +405,57 @@ def __init__(self, i2c, *, irq=None, reset=None, debug=False):
405
405
super ().__init__ (debug = debug , reset = reset )
406
406
407
407
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!
420
418
return False
421
419
422
420
def _read_data (self , count ):
423
421
"""Read a specified count of bytes from the PN532."""
424
422
# Build a read request frame.
425
423
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
429
427
raise BusyError
430
- self . _i2c . readinto (frame )
428
+ i2c . readinto (frame ) # ok get the data, plus statusbyte
431
429
if self .debug :
432
430
print ("Reading: " , [hex (i ) for i in frame [1 :]])
433
- return frame [1 :]
431
+ return frame [1 :] # don't return the status byte
434
432
435
433
def _write_data (self , framebytes ):
436
- with self ._i2c :
437
- self . _i2c .write (framebytes )
434
+ with self ._i2c as i2c :
435
+ i2c .write (framebytes )
438
436
439
437
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."""
441
440
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"""
444
442
self .debug = debug
445
443
self ._irq = irq
446
444
self ._spi = spi_device .SPIDevice (spi , cs_pin )
447
445
super ().__init__ (debug = debug , reset = reset )
448
446
449
447
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!
463
459
return False
464
460
465
461
def _read_data (self , count ):
@@ -470,18 +466,19 @@ def _read_data(self, count):
470
466
frame [0 ] = reverse_bit (_SPI_DATAREAD )
471
467
472
468
with self ._spi as spi :
473
- time .sleep (0.01 )
469
+ time .sleep (0.01 ) # required
474
470
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
477
473
if self .debug :
478
474
print ("Reading: " , [hex (i ) for i in frame [1 :]])
479
475
return frame [1 :]
480
476
481
477
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 ])
482
482
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
487
484
spi .write (bytes (reversed ))
0 commit comments