Skip to content

Commit 5b9d11e

Browse files
committed
lint & add example
1 parent 3f4eeb5 commit 5b9d11e

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

adafruit_pn532.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def ntag2xx_write_block(self, block_number, data):
456456
params=params,
457457
response_length=1)
458458
return response[0] == 0x00
459-
459+
460460
def ntag2xx_read_block(self, block_number):
461461
"""Read a block of data from the card. Block number should be the block
462462
to read. If the block is successfully read a bytearray of length 16 with

examples/readwrite_ntag2xx.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Example of detecting and reading a block from a MiFare NFC card.
2+
# Author: Tony DiCola & Roberto Laricchia
3+
# Copyright (c) 2015 Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
"""
24+
This example shows connecting to the PN532 and writing & reading an ntag2xx
25+
type RFID tag
26+
"""
27+
28+
import board
29+
import busio
30+
from digitalio import DigitalInOut
31+
from Adafruit_Circuitpython_PN532 import adafruit_pn532
32+
33+
# I2C connection:
34+
#i2c = busio.I2C(board.SCL, board.SDA)
35+
36+
# Non-hardware reset/request with I2C
37+
#pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False)
38+
39+
# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
40+
# harware reset
41+
#reset_pin = DigitalInOut(board.D6)
42+
# On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
43+
# wakeup! this means we don't need to do the I2C clock-stretch thing
44+
#req_pin = DigitalInOut(board.D12)
45+
#pn532 = adafruit_pn532.PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
46+
47+
# SPI connection:
48+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
49+
cs_pin = DigitalInOut(board.D5)
50+
pn532 = adafruit_pn532.PN532_SPI(spi, cs_pin, debug=False)
51+
52+
# UART connection
53+
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
54+
#pn532 = adafruit_pn532.PN532_UART(uart, debug=False)
55+
56+
ic, ver, rev, support = pn532.get_firmware_version()
57+
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
58+
59+
# Configure PN532 to communicate with MiFare cards
60+
pn532.SAM_configuration()
61+
62+
print('Waiting for RFID/NFC card to write to!')
63+
while True:
64+
# Check if a card is available to read
65+
uid = pn532.read_passive_target(timeout=0.5)
66+
print('.', end="", flush=True)
67+
# Try again if no card is available.
68+
if uid is not None:
69+
break
70+
71+
print("")
72+
print('Found card with UID:', [hex(i) for i in uid])
73+
74+
# Set 4 bytes of block to 0xFEEDBEEF
75+
data = bytearray(4)
76+
data[0:4] = [0xFE, 0xED, 0xBE, 0xEF]
77+
# Write 4 byte block.
78+
pn532.ntag2xx_write_block(6, data)
79+
# Read block #6
80+
print('Wrote to block 6, now trying to read that data:',
81+
[hex(x) for x in pn532.ntag2xx_read_block(6)])

0 commit comments

Comments
 (0)