2021-01-14 16:45:11 +00:00
|
|
|
# SPDX-FileCopyrightText: 2015-2018 Tony DiCola for Adafruit Industries
|
2018-08-28 19:36:54 +00:00
|
|
|
#
|
2021-01-14 16:45:11 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
"""
|
|
|
|
``adafruit_pn532.uart``
|
|
|
|
====================================================
|
|
|
|
|
|
|
|
This module will let you communicate with a PN532 RFID/NFC shield or breakout
|
|
|
|
using UART.
|
|
|
|
|
|
|
|
* Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
|
|
|
|
refactor by Carter Nelson
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-08-16 22:09:14 +00:00
|
|
|
__version__ = "0.0.0+auto.0"
|
2018-08-28 19:36:54 +00:00
|
|
|
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git"
|
|
|
|
|
2023-02-09 20:12:59 +00:00
|
|
|
try:
|
|
|
|
from typing import Optional
|
2023-02-11 16:09:29 +00:00
|
|
|
from circuitpython_typing import ReadableBuffer
|
2023-02-09 20:12:59 +00:00
|
|
|
from digitalio import DigitalInOut
|
|
|
|
from busio import UART
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2018-08-28 19:36:54 +00:00
|
|
|
|
|
|
|
import time
|
|
|
|
from adafruit_pn532.adafruit_pn532 import PN532, BusyError
|
|
|
|
|
2020-03-16 20:00:49 +00:00
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
class PN532_UART(PN532):
|
|
|
|
"""Driver for the PN532 connected over Serial UART"""
|
2020-03-16 20:00:49 +00:00
|
|
|
|
2023-02-09 20:12:59 +00:00
|
|
|
def __init__(
|
|
|
|
self, uart: UART, *, reset: Optional[DigitalInOut] = None, debug: bool = False
|
|
|
|
) -> None:
|
2018-08-28 19:36:54 +00:00
|
|
|
"""Create an instance of the PN532 class using Serial connection.
|
2020-09-09 00:50:23 +00:00
|
|
|
Optional reset pin and debugging output.
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
:param ~busio.UART uart: The uart object the PN532 is connected to.
|
|
|
|
:param digitalio.DigitalInOut reset: board pin the PN532 reset is connected to
|
|
|
|
:param bool debug: if True print additional debug statements. Defaults to False
|
2023-03-20 01:08:14 +00:00
|
|
|
|
|
|
|
**Quickstart: Importing and using the device**
|
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
Here is an example of using the :class:`PN532_I2C` class.
|
|
|
|
First you will need to import the libraries to use the sensor
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
.. code-block:: python
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
import board
|
|
|
|
import busio
|
|
|
|
from digitalio import DigitalInOut
|
|
|
|
from adafruit_pn532.uart import PN532_UART
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
Once this is done you can define your `busio.UART` object and define your PN532 object
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
.. code-block:: python
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=0.1)
|
|
|
|
pn532 = PN532_UART(uart, debug=False)
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2023-03-21 18:49:32 +00:00
|
|
|
Now you have access to the attributes and functions of the PN532 RFID/NFC
|
|
|
|
shield or breakout
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
uid = pn532.read_passive_target(timeout=0.5)
|
2023-03-20 01:08:14 +00:00
|
|
|
|
2018-08-28 19:36:54 +00:00
|
|
|
"""
|
|
|
|
self.debug = debug
|
|
|
|
self._uart = uart
|
|
|
|
super().__init__(debug=debug, reset=reset)
|
|
|
|
|
2023-02-09 20:12:59 +00:00
|
|
|
def _wakeup(self) -> None:
|
2018-08-28 19:36:54 +00:00
|
|
|
"""Send any special commands/data to wake up PN532"""
|
2020-09-09 00:50:23 +00:00
|
|
|
if self._reset_pin:
|
|
|
|
self._reset_pin.value = True
|
|
|
|
time.sleep(0.01)
|
|
|
|
self.low_power = False
|
|
|
|
self._uart.write(
|
|
|
|
b"\x55\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
2020-09-10 19:42:03 +00:00
|
|
|
) # wake up!
|
2018-08-28 19:36:54 +00:00
|
|
|
self.SAM_configuration()
|
|
|
|
|
2023-02-09 20:12:59 +00:00
|
|
|
def _wait_ready(self, timeout: float = 1) -> bool:
|
2018-08-28 19:36:54 +00:00
|
|
|
"""Wait `timeout` seconds"""
|
2020-09-09 00:50:23 +00:00
|
|
|
timestamp = time.monotonic()
|
|
|
|
while (time.monotonic() - timestamp) < timeout:
|
|
|
|
if self._uart.in_waiting > 0:
|
2020-09-10 19:42:03 +00:00
|
|
|
return True # No Longer Busy
|
2020-09-09 00:50:23 +00:00
|
|
|
time.sleep(0.01) # lets ask again soon!
|
|
|
|
# Timed out!
|
|
|
|
return False
|
2018-08-28 19:36:54 +00:00
|
|
|
|
2023-02-11 16:09:29 +00:00
|
|
|
def _read_data(self, count: int) -> bytes:
|
2018-08-28 19:36:54 +00:00
|
|
|
"""Read a specified count of bytes from the PN532."""
|
|
|
|
frame = self._uart.read(count)
|
|
|
|
if not frame:
|
|
|
|
raise BusyError("No data read from PN532")
|
|
|
|
if self.debug:
|
|
|
|
print("Reading: ", [hex(i) for i in frame])
|
|
|
|
return frame
|
|
|
|
|
2023-02-11 16:09:29 +00:00
|
|
|
def _write_data(self, framebytes: ReadableBuffer) -> None:
|
2018-08-28 19:36:54 +00:00
|
|
|
"""Write a specified count of bytes to the PN532"""
|
2020-09-09 00:50:23 +00:00
|
|
|
self._uart.reset_input_buffer()
|
2018-08-28 19:36:54 +00:00
|
|
|
self._uart.write(framebytes)
|