def setup(): global nfc global config global tags # test for settings.ini config = parseConfig('settings.ini') if (config == []): log(message= 'Error missing or empty settings.ini - see settings.ini.example') sys.exit(1) # test for mpc mpc_result = call(['mpc', 'status']) if (mpc_result != 0): log(message= 'Error missing mpc command - check if volumio is setup correctly on the device' ) sys.exit(1) # update nfc code list if refresh_list(): log(message='Updated local tag list from %s' % config['readnfc']['tagurl']) # test for nfc code list tags = read_cards() if tags == False: log('Error no tags from local tag file read') sys.exit(1) # test for nfc reader log('Initialize i2c...') # pn532 = Pn532_i2c() PN532_I2C = Pn532I2c(1) # log('config') # pn532.SAMconfigure() log('Initialize NFC reader...') nfc = Pn532(PN532_I2C) # log('refresh list') log('Get NFC reader data...') nfc.begin() versiondata = nfc.getFirmwareVersion() if not versiondata: log("Didn't find PN53x board: " + str(versiondata)) raise RuntimeError("Didn't find PN53x board") # halt log('Set NFC reader configuration...') nfc.setPassiveActivationRetries(0xFF) nfc.SAMConfig()
def pn532uid(): PN532_I2C = Pn532I2c(1) nfc = Pn532(PN532_I2C) nfc.begin() versiondata = nfc.getFirmwareVersion() if (not versiondata): print("Didn't find PN53x board") raise RuntimeError("Didn't find PN53x board") # halt # Got ok data, print it out! print("Found chip PN5 {:#x} Firmware ver. {:d}.{:d}".format((versiondata >> 24) & 0xFF, (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF)) # configure board to read RFID tags # nfc.SAMConfig() success, uid = nfc.readPassiveTargetID(pn532.PN532_MIFARE_ISO14443A_106KBPS) res = format(binascii.hexlify(uid)) return res
from pn532pi import Pn532Hsu # Set the desired interface to True SPI = False I2C = False HSU = True if SPI: PN532_SPI = Pn532Spi(Pn532Spi.SS0_GPIO8) nfc = Pn532(PN532_SPI) elif HSU: PN532_HSU = Pn532Hsu(0) nfc = Pn532(PN532_HSU) elif I2C: PN532_I2C = Pn532I2c(1) nfc = Pn532(PN532_I2C) _prevIDm = bytearray(b'\x00' * 8) _prevTime = 0 def millis(): return int(round(time.time() * 1000)) def setup(): print("NTAG21x R/W") print("-------Looking for pn532--------") nfc.begin()
def __init__(self): self.PN532_I2C = Pn532I2c(0) self.nfc = Pn532(self.PN532_I2C) self.setup()
SPI = False I2C = False HSU = True if SPI: PN532_SPI = Pn532Spi(Pn532Spi.SS0_GPIO8) nfc = Pn532(PN532_SPI) # When the number after #elif set as 1, it will be switch to HSU Mode elif HSU: PN532_HSU = Pn532Hsu(Pn532Hsu.RPI_MINI_UART) nfc = Pn532(PN532_HSU) # When the number after #if & #elif set as 0, it will be switch to I2C Mode elif I2C: PN532_I2C = Pn532I2c(Pn532I2c.RPI_BUS1) nfc = Pn532(PN532_I2C) NR_SHORTSECTOR = 32 # Number of short sectors on Mifare 1K/4K NR_LONGSECTOR = 8 # Number of long sectors on Mifare 4K NR_BLOCK_OF_SHORTSECTOR = 4 # Number of blocks in a short sector NR_BLOCK_OF_LONGSECTOR = 16 # Number of blocks in a long sector # Determine the sector trailer block based on sector number def BLOCK_NUMBER_OF_SECTOR_TRAILER(sector): return (sector)*NR_BLOCK_OF_SHORTSECTOR + NR_BLOCK_OF_SHORTSECTOR-1 if sector < NR_SHORTSECTOR else \ NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR + NR_BLOCK_OF_LONGSECTOR-1 # Determine the sector's first block based on the sector number