diff --git a/Adafruit_PN532/PN532.py b/Adafruit_PN532/PN532.py index cedb796..85eb458 100644 --- a/Adafruit_PN532/PN532.py +++ b/Adafruit_PN532/PN532.py @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import binascii +from functools import reduce import logging import time @@ -151,12 +152,12 @@ class PN532(object): PN532 (see: http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=98070&p=720659#p720659) """ - def __init__(self, cs, sclk=None, mosi=None, miso=None, gpio=None, + def __init__(self, cs, sclk=None, mosi=None, miso=None, gpio=None, spi=None): - """Create an instance of the PN532 class using either software SPI (if + """Create an instance of the PN532 class using either software SPI (if the sclk, mosi, and miso pins are specified) or hardware SPI if a spi parameter is passed. The cs pin must be a digital GPIO pin. - Optionally specify a GPIO controller to override the default that uses + Optionally specify a GPIO controller to override the default that uses the board's GPIO pins. """ # Default to platform GPIO if not provided. diff --git a/Adafruit_PN532/__init__.py b/Adafruit_PN532/__init__.py index e449a3a..dc865cb 100644 --- a/Adafruit_PN532/__init__.py +++ b/Adafruit_PN532/__init__.py @@ -1 +1 @@ -from PN532 import * +from .PN532 import * diff --git a/examples/mcpi_listen.py b/examples/mcpi_listen.py index cf1ac9c..ea22058 100644 --- a/examples/mcpi_listen.py +++ b/examples/mcpi_listen.py @@ -71,9 +71,9 @@ pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO) pn532.begin() pn532.SAM_configuration() -print 'Minecraft Block NFC Listener' -print '' -print 'Waiting for MiFare card...' +print('Minecraft Block NFC Listener') +print('') +print('Waiting for MiFare card...') while True: # Wait for a card to be available. uid = pn532.read_passive_target() @@ -81,19 +81,19 @@ while True: if uid is None: continue # Found a card, now try to read block 4 to detect the block type. - print 'Found card with UID 0x{0}'.format(binascii.hexlify(uid)) + print('Found card with UID 0x{0}'.format(binascii.hexlify(uid))) # Authenticate and read block 4. - if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, + if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, CARD_KEY): - print 'Failed to authenticate with card!' + print('Failed to authenticate with card!') continue data = pn532.mifare_classic_read_block(4) if data is None: - print 'Failed to read data from card!' + print('Failed to read data from card!') continue # Check if card has Minecraft block data by looking for header 'MCPI' - if data[0:4] != 'MCPI': - print 'Card is not written with Minecraft block data!' + if data[0:4] != b'MCPI': + print('Card is not written with Minecraft block data!') continue # Parse out the block type and subtype. block_id = data[4] @@ -104,11 +104,11 @@ while True: if block[1] == block_id: block_name = block[0] break - print 'Found block!' - print 'Type: {0}'.format(block_name) + print('Found block!') + print('Type: {0}'.format(block_name)) if has_subtype: subtype_name = mcpi_data.SUBTYPES[block_name][subtype_id] - print 'Subtype: {0}'.format(subtype_name) + print('Subtype: {0}'.format(subtype_name)) # Try to create the block in Minecraft. # First check if connected to Minecraft world. try: @@ -118,5 +118,5 @@ while True: time.sleep(MAX_UPDATE_SEC) except socket.error: # Socket error, Minecraft probably isn't running. - print 'Could not connect to Minecraft, is the game running in a world?' + print('Could not connect to Minecraft, is the game running in a world?') continue diff --git a/examples/mcpi_write.py b/examples/mcpi_write.py index aa64f8d..486adbb 100644 --- a/examples/mcpi_write.py +++ b/examples/mcpi_write.py @@ -26,6 +26,13 @@ import Adafruit_PN532 as PN532 import mcpi_data +# Hack to make code compatible with both Python 2 and 3 (since 3 moved +# raw_input from a builtin to a different function, ugh). +try: + input = raw_input +except NameError: + pass + # PN532 configuration for a Raspberry Pi: CS = 18 @@ -45,100 +52,100 @@ pn532.begin() pn532.SAM_configuration() # Step 1, wait for card to be present. -print 'Minecraft Block NFC Writer' -print '' -print '== STEP 1 =========================' -print 'Place the card to be written on the PN532...' +print('Minecraft Block NFC Writer') +print('') +print('== STEP 1 =========================') +print('Place the card to be written on the PN532...') uid = pn532.read_passive_target() while uid is None: uid = pn532.read_passive_target() -print '' -print 'Found card with UID: 0x{0}'.format(binascii.hexlify(uid)) -print '' -print '==============================================================' -print 'WARNING: DO NOT REMOVE CARD FROM PN532 UNTIL FINISHED WRITING!' -print '==============================================================' -print '' +print('') +print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid))) +print('') +print('==============================================================') +print('WARNING: DO NOT REMOVE CARD FROM PN532 UNTIL FINISHED WRITING!') +print('==============================================================') +print('') # Step 2, pick a block type. -print '== STEP 2 =========================' -print 'Now pick a block type to write to the card.' +print('== STEP 2 =========================') +print('Now pick a block type to write to the card.') block_choice = None while block_choice is None: - print '' - print 'Type either L to list block types, or type the number of the desired block.' - print '' - choice = raw_input('Enter choice (L or block #): ') - print '' + print('') + print('Type either L to list block types, or type the number of the desired block.') + print('') + choice = input('Enter choice (L or block #): ') + print('') if choice.lower() == 'l': # Print block numbers and names. - print 'Number\tBlock name' - print '------\t----------' + print('Number\tBlock name') + print('------\t----------') for i, b in enumerate(mcpi_data.BLOCKS): block_name, block_id = b - print '{0:>6}\t{1}'.format(i, block_name) + print('{0:>6}\t{1}'.format(i, block_name)) else: # Assume a number must have been entered. try: block_choice = int(choice) except ValueError: # Something other than a number was entered. Try again. - print 'Error! Unrecognized option.' + print('Error! Unrecognized option.') continue # Check choice is within bounds of block numbers. if not (0 <= block_choice < len(mcpi_data.BLOCKS)): - print 'Error! Block number must be within 0 to {0}.'.format(len(mcpi_data.BLOCKS)-1) + print('Error! Block number must be within 0 to {0}.'.format(len(mcpi_data.BLOCKS)-1)) continue # Block was chosen, look up its name and ID. block_name, block_id = mcpi_data.BLOCKS[block_choice] -print 'You chose the block type: {0}'.format(block_name) -print '' +print('You chose the block type: {0}'.format(block_name)) +print('') # Get the block subtype if it has any available. subtype_choice = None if block_name in mcpi_data.SUBTYPES: - print 'Now pick a subtype for the block.' - print '' - print 'Number\tSubtype' - print '------\t-------' + print('Now pick a subtype for the block.') + print('') + print('Number\tSubtype') + print('------\t-------') # Print all the subtypes for this block. block_subtypes = mcpi_data.SUBTYPES[block_name] for subtype_id, subtype_name in block_subtypes.items(): - print '{0:>6}\t{1}'.format(subtype_id, subtype_name) + print('{0:>6}\t{1}'.format(subtype_id, subtype_name)) # Get a subtype id from the user. while subtype_choice is None: - print '' + print('') try: - subtype_choice = int(raw_input('Enter subtype number: ')) + subtype_choice = int(input('Enter subtype number: ')) except ValueError: # Something other than a number was entered. Try again. - print 'Error! Unrecognized subtype number.' + print('Error! Unrecognized subtype number.') continue if subtype_id not in block_subtypes: - print 'Error! Subtype number must be one shown above!' + print('Error! Subtype number must be one shown above!') continue if subtype_choice is not None: - print 'You also chose the subtype: {0}'.format(block_subtypes[subtype_choice]) - print '' + print('You also chose the subtype: {0}'.format(block_subtypes[subtype_choice])) + print('') # Confirm writing the block type. -print '== STEP 3 =========================' -print 'Confirm you are ready to write to the card:' -print 'Block: {0}'.format(block_name) +print('== STEP 3 =========================') +print('Confirm you are ready to write to the card:') +print('Block: {0}'.format(block_name)) if subtype_choice is not None: - print 'Subtype: {0}'.format(block_subtypes[subtype_choice]) -print '' -choice = raw_input('Confirm card write (Y or N)? ') + print('Subtype: {0}'.format(block_subtypes[subtype_choice])) +print('') +choice = input('Confirm card write (Y or N)? ') if choice.lower() != 'y' and choice.lower() != 'yes': - print 'Aborted!' + print('Aborted!') sys.exit(0) -print 'Writing card (DO NOT REMOVE CARD FROM PN532)...' +print('Writing card (DO NOT REMOVE CARD FROM PN532)...') # Write the card! # First authenticate block 4. -if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, +if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, CARD_KEY): - print 'Error! Failed to authenticate block 4 with the card.' + print('Error! Failed to authenticate block 4 with the card.') sys.exit(-1) # Next build the data to write to the card. # Format is as follows: @@ -147,13 +154,13 @@ if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, # - Byte 5 is 0 if block has no subtype or 1 if block has a subtype # - Byte 6 is the subtype byte (optional, only if byte 5 is 1) data = bytearray(16) -data[0:4] = 'MCPI' # Header 'MCPI' +data[0:4] = b'MCPI' # Header 'MCPI' data[4] = block_id & 0xFF if subtype_choice is not None: data[5] = 1 data[6] = subtype_choice & 0xFF # Finally write the card. if not pn532.mifare_classic_write_block(4, data): - print 'Error! Failed to write to the card.' + print('Error! Failed to write to the card.') sys.exit(-1) -print 'Wrote card successfully! You may now remove the card from the PN532.' +print('Wrote card successfully! You may now remove the card from the PN532.') diff --git a/examples/readmifare.py b/examples/readmifare.py index dbf6547..543badc 100644 --- a/examples/readmifare.py +++ b/examples/readmifare.py @@ -47,40 +47,40 @@ pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO) # any other calls to the PN532! pn532.begin() -# Get the firmware version from the chip and print it out. +# Get the firmware version from the chip and print(it out.) ic, ver, rev, support = pn532.get_firmware_version() -print 'Found PN532 with firmware version: {0}.{1}'.format(ver, rev) +print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev)) # Configure PN532 to communicate with MiFare cards. pn532.SAM_configuration() # Main loop to detect cards and read a block. -print 'Waiting for MiFare card...' +print('Waiting for MiFare card...') while True: # Check if a card is available to read. uid = pn532.read_passive_target() # Try again if no card is available. if uid is None: continue - print 'Found card with UID: 0x{0}'.format(binascii.hexlify(uid)) + print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid))) # Authenticate block 4 for reading with default key (0xFFFFFFFFFFFF). - if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, + if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]): - print 'Failed to authenticate block 4!' + print('Failed to authenticate block 4!') continue # Read block 4 data. data = pn532.mifare_classic_read_block(4) if data is None: - print 'Failed to read block 4!' + print('Failed to read block 4!') continue # Note that 16 bytes are returned, so only show the first 4 bytes for the block. - print 'Read block 4: 0x{0}'.format(binascii.hexlify(data[:4])) + print('Read block 4: 0x{0}'.format(binascii.hexlify(data[:4]))) # Example of writing data to block 4. This is commented by default to # prevent accidentally writing a card. # Set first 4 bytes of block to 0xFEEDBEEF. # data[0:4] = [0xFE, 0xED, 0xBE, 0xEF] # # Write entire 16 byte block. # pn532.mifare_classic_write_block(4, data) - # print 'Wrote to block 4, exiting program!' + # print('Wrote to block 4, exiting program!') # # Exit the program to prevent continually writing to card. # sys.exit(0) diff --git a/setup.py b/setup.py index 2348e6c..d88cf42 100644 --- a/setup.py +++ b/setup.py @@ -2,12 +2,22 @@ from ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages +classifiers = ['Development Status :: 4 - Beta', + 'Operating System :: POSIX :: Linux', + 'License :: OSI Approved :: MIT License', + 'Intended Audience :: Developers', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Topic :: Software Development', + 'Topic :: System :: Hardware'] + setup(name = 'Adafruit_PN532', - version = '1.1.0', + version = '1.2.0', author = 'Tony DiCola', author_email = 'tdicola@adafruit.com', description = 'Python library for accessing a PN532 NFC breakout over a SPI connection from a Raspberry Pi, BeagleBone Black, etc.', license = 'MIT', + classifiers = classifiers, url = 'https://github.com/adafruit/Adafruit_Python_PN532/', dependency_links = ['https://github.com/adafruit/Adafruit_Python_GPIO/tarball/master#egg=Adafruit-GPIO-0.9'], install_requires = ['Adafruit-GPIO>=0.9'],