Skip to content

Commit

Permalink
Merge pull request #64 from tcfranks/main
Browse files Browse the repository at this point in the history
resubmit pr Typting Annotations
  • Loading branch information
tekktrik authored Mar 15, 2023
2 parents a7287d2 + 812235c commit 0061f33
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 47 deletions.
94 changes: 65 additions & 29 deletions adafruit_pn532/adafruit_pn532.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@

from micropython import const

try:
from typing import Optional, Tuple, Union
from typing_extensions import Literal
from circuitpython_typing import ReadableBuffer
from digitalio import DigitalInOut # pylint: disable=ungrouped-imports
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git"

Expand Down Expand Up @@ -151,7 +159,13 @@ class BusyError(Exception):
class PN532:
"""PN532 driver base, must be extended for I2C/SPI/UART interfacing"""

def __init__(self, *, debug=False, irq=None, reset=None):
def __init__(
self,
*,
debug: bool = False,
irq: Optional[DigitalInOut] = None,
reset: Optional[DigitalInOut] = None
) -> None:
"""Create an instance of the PN532 class"""
self.low_power = True
self.debug = debug
Expand All @@ -160,26 +174,26 @@ def __init__(self, *, debug=False, irq=None, reset=None):
self.reset()
_ = self.firmware_version

def _read_data(self, count):
def _read_data(self, count: int) -> Union[bytes, bytearray]:
# Read raw data from device, not including status bytes:
# Subclasses MUST implement this!
raise NotImplementedError

def _write_data(self, framebytes):
def _write_data(self, framebytes: bytes) -> None:
# Write raw bytestring data to device, not including status bytes:
# Subclasses MUST implement this!
raise NotImplementedError

def _wait_ready(self, timeout):
def _wait_ready(self, timeout: float) -> bool:
# Check if busy up to max length of 'timeout' seconds
# Subclasses MUST implement this!
raise NotImplementedError

def _wakeup(self):
def _wakeup(self) -> None:
# Send special command to wake up
raise NotImplementedError

def reset(self):
def reset(self) -> None:
"""Perform a hardware reset toggle and then wake up the PN532"""
if self._reset_pin:
if self.debug:
Expand All @@ -191,7 +205,7 @@ def reset(self):
time.sleep(0.1)
self._wakeup()

def _write_frame(self, data):
def _write_frame(self, data: bytearray) -> None:
"""Write a frame to the PN532 with the specified data bytearray."""
assert (
data is not None and 1 < len(data) < 255
Expand Down Expand Up @@ -221,7 +235,7 @@ def _write_frame(self, data):
print("Write frame: ", [hex(i) for i in frame])
self._write_data(bytes(frame))

def _read_frame(self, length):
def _read_frame(self, length: int) -> Union[bytes, bytearray]:
"""Read a response frame from the PN532 of at most length bytes in size.
Returns the data inside the frame if found, otherwise raises an exception
if there is an error parsing the frame. Note that less than length bytes
Expand Down Expand Up @@ -257,8 +271,12 @@ def _read_frame(self, length):
return response[offset + 2 : offset + 2 + frame_len]

def call_function(
self, command, response_length=0, params=[], timeout=1
): # pylint: disable=dangerous-default-value
self,
command: int,
response_length: int = 0,
params: ReadableBuffer = b"",
timeout: float = 1,
) -> Optional[Union[bytes, bytearray]]:
"""Send specified command to the PN532 and expect up to response_length
bytes back in a response. Note that less than the expected bytes might
be returned! Params can optionally specify an array of bytes to send as
Expand All @@ -273,11 +291,11 @@ def call_function(
)

def send_command(
self, command, params=[], timeout=1
): # pylint: disable=dangerous-default-value
self, command: int, params: ReadableBuffer = b"", timeout: float = 1
) -> bool:
"""Send specified command to the PN532 and wait for an acknowledgment.
Will wait up to timeout seconds for the acknowlegment and return True.
If no acknowlegment is received, False is returned.
Will wait up to timeout seconds for the acknowledgment and return True.
If no acknowledgment is received, False is returned.
"""
if self.low_power:
self._wakeup()
Expand All @@ -300,7 +318,9 @@ def send_command(
raise RuntimeError("Did not receive expected ACK from PN532!")
return True

def process_response(self, command, response_length=0, timeout=1):
def process_response(
self, command: int, response_length: int = 0, timeout: float = 1
) -> Optional[Union[bytes, bytearray]]:
"""Process the response from the PN532 and expect up to response_length
bytes back in a response. Note that less than the expected bytes might
be returned! Will wait up to timeout seconds for a response and return
Expand All @@ -317,7 +337,7 @@ def process_response(self, command, response_length=0, timeout=1):
# Return response data.
return response[2:]

def power_down(self):
def power_down(self) -> bool:
"""Put the PN532 into a low power state. If the reset pin is connected a
hard power down is performed, if not, a soft power down is performed
instead. Returns True if the PN532 was powered down successfully or
Expand All @@ -333,7 +353,7 @@ def power_down(self):
return self.low_power

@property
def firmware_version(self):
def firmware_version(self) -> Tuple[int, int, int, int]:
"""Call PN532 GetFirmwareVersion function and return a tuple with the IC,
Ver, Rev, and Support values.
"""
Expand All @@ -342,7 +362,7 @@ def firmware_version(self):
raise RuntimeError("Failed to detect the PN532")
return tuple(response)

def SAM_configuration(self): # pylint: disable=invalid-name
def SAM_configuration(self) -> None: # pylint: disable=invalid-name
"""Configure the PN532 to read MiFare cards."""
# Send SAM configuration command with configuration for:
# - 0x01, normal mode
Expand All @@ -352,7 +372,9 @@ def SAM_configuration(self): # pylint: disable=invalid-name
# check the command was executed as expected.
self.call_function(_COMMAND_SAMCONFIGURATION, params=[0x01, 0x14, 0x01])

def read_passive_target(self, card_baud=_MIFARE_ISO14443A, timeout=1):
def read_passive_target(
self, card_baud: int = _MIFARE_ISO14443A, timeout: float = 1
) -> Optional[bytearray]:
"""Wait for a MiFare card to be available and return its UID when found.
Will wait up to timeout seconds and return None if no card is found,
otherwise a bytearray with the UID of the found card is returned.
Expand All @@ -364,9 +386,11 @@ def read_passive_target(self, card_baud=_MIFARE_ISO14443A, timeout=1):
return None
return self.get_passive_target(timeout=timeout)

def listen_for_passive_target(self, card_baud=_MIFARE_ISO14443A, timeout=1):
def listen_for_passive_target(
self, card_baud: int = _MIFARE_ISO14443A, timeout: float = 1
) -> bool:
"""Send command to PN532 to begin listening for a Mifare card. This
returns True if the command was received succesfully. Note, this does
returns True if the command was received successfully. Note, this does
not also return the UID of a card! `get_passive_target` must be called
to read the UID when a card is found. If just looking to see if a card
is currently present use `read_passive_target` instead.
Expand All @@ -380,7 +404,9 @@ def listen_for_passive_target(self, card_baud=_MIFARE_ISO14443A, timeout=1):
return False # _COMMAND_INLISTPASSIVETARGET failed
return response

def get_passive_target(self, timeout=1):
def get_passive_target(
self, timeout: float = 1
) -> Optional[Union[bytes, bytearray]]:
"""Will wait up to timeout seconds and return None if no card is found,
otherwise a bytearray with the UID of the found card is returned.
`listen_for_passive_target` must have been called first in order to put
Expand All @@ -404,9 +430,13 @@ def get_passive_target(self, timeout=1):
# Return UID of card.
return response[6 : 6 + response[5]]

def mifare_classic_authenticate_block(
self, uid, block_number, key_number, key
): # pylint: disable=invalid-name
def mifare_classic_authenticate_block( # pylint: disable=invalid-name
self,
uid: ReadableBuffer,
block_number: int,
key_number: Literal[0x60, 0x61],
key: ReadableBuffer,
) -> bool:
"""Authenticate specified block number for a MiFare classic card. Uid
should be a byte array with the UID of the card, block number should be
the block to authenticate, key number should be the key type (like
Expand All @@ -429,7 +459,9 @@ def mifare_classic_authenticate_block(
)
return response[0] == 0x00

def mifare_classic_read_block(self, block_number):
def mifare_classic_read_block(
self, block_number: int
) -> Optional[Union[bytes, bytearray]]:
"""Read a block of data from the card. Block number should be the block
to read. If the block is successfully read a bytearray of length 16 with
data starting at the specified block will be returned. If the block is
Expand All @@ -447,7 +479,9 @@ def mifare_classic_read_block(self, block_number):
# Return first 4 bytes since 16 bytes are always returned.
return response[1:]

def mifare_classic_write_block(self, block_number, data):
def mifare_classic_write_block(
self, block_number: int, data: ReadableBuffer
) -> bool:
"""Write a block of data to the card. Block number should be the block
to write and data should be a byte array of length 16 with the data to
write. If the data is successfully written then True is returned,
Expand All @@ -468,7 +502,7 @@ def mifare_classic_write_block(self, block_number, data):
)
return response[0] == 0x0

def ntag2xx_write_block(self, block_number, data):
def ntag2xx_write_block(self, block_number: int, data: ReadableBuffer) -> bool:
"""Write a block of data to the card. Block number should be the block
to write and data should be a byte array of length 4 with the data to
write. If the data is successfully written then True is returned,
Expand All @@ -487,7 +521,9 @@ def ntag2xx_write_block(self, block_number, data):
)
return response[0] == 0x00

def ntag2xx_read_block(self, block_number):
def ntag2xx_read_block(
self, block_number: int
) -> Optional[Union[bytes, bytearray]]:
"""Read a block of data from the card. Block number should be the block
to read. If the block is successfully read the first 4 bytes (after the
leading 0x00 byte) will be returned.
Expand Down
28 changes: 21 additions & 7 deletions adafruit_pn532/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@
from micropython import const
from adafruit_pn532.adafruit_pn532 import PN532, BusyError

try:
from typing import Optional
from digitalio import DigitalInOut # pylint: disable=ungrouped-imports
from busio import I2C
except ImportError:
pass

_I2C_ADDRESS = const(0x24)


class PN532_I2C(PN532):
"""Driver for the PN532 connected over I2C."""

def __init__(
self, i2c, address=_I2C_ADDRESS, *, irq=None, reset=None, req=None, debug=False
):
self,
i2c: I2C,
address: int = _I2C_ADDRESS,
*,
irq: Optional[DigitalInOut] = None,
reset: Optional[DigitalInOut] = None,
req: Optional[DigitalInOut] = None,
debug: bool = False
) -> None:
"""Create an instance of the PN532 class using I2C. Note that PN532
uses clock stretching. Optional IRQ pin (not used),
resetp pin and debugging output.
Expand All @@ -41,7 +55,7 @@ def __init__(
self._i2c = i2c_device.I2CDevice(i2c, address)
super().__init__(debug=debug, irq=irq, reset=reset)

def _wakeup(self):
def _wakeup(self) -> None:
"""Send any special commands/data to wake up PN532"""
if self._reset_pin:
self._reset_pin.value = True
Expand All @@ -55,7 +69,7 @@ def _wakeup(self):
self.low_power = False
self.SAM_configuration() # Put the PN532 back in normal mode

def _wait_ready(self, timeout=1):
def _wait_ready(self, timeout: float = 1) -> bool:
"""Poll PN532 if status byte is ready, up to `timeout` seconds"""
status = bytearray(1)
timestamp = time.monotonic()
Expand All @@ -67,11 +81,11 @@ def _wait_ready(self, timeout=1):
continue
if status == b"\x01":
return True # No longer busy
time.sleep(0.01) # lets ask again soon!
time.sleep(0.01) # let's ask again soon!
# Timed out!
return False

def _read_data(self, count):
def _read_data(self, count: int) -> bytearray:
"""Read a specified count of bytes from the PN532."""
# Build a read request frame.
frame = bytearray(count + 1)
Expand All @@ -84,7 +98,7 @@ def _read_data(self, count):
print("Reading: ", [hex(i) for i in frame[1:]])
return frame[1:] # don't return the status byte

def _write_data(self, framebytes):
def _write_data(self, framebytes: bytes) -> None:
"""Write a specified count of bytes to the PN532"""
with self._i2c as i2c:
i2c.write(framebytes)
28 changes: 22 additions & 6 deletions adafruit_pn532/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
"""

try:
from typing import Optional
from circuitpython_typing import ReadableBuffer
from digitalio import DigitalInOut
from busio import SPI
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git"

Expand All @@ -28,7 +36,7 @@
_SPI_READY = const(0x01)


def reverse_bit(num):
def reverse_bit(num: int) -> int:
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
it is LSB for the PN532, but 99% of SPI implementations are MSB only!"""
result = 0
Expand All @@ -44,13 +52,21 @@ class PN532_SPI(PN532):
SPI device & chip select digitalInOut pin. Optional IRQ pin (not used),
reset pin and debugging output."""

def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
def __init__(
self,
spi: SPI,
cs_pin: DigitalInOut,
*,
irq: Optional[DigitalInOut] = None,
reset: Optional[DigitalInOut] = None,
debug: bool = False
) -> None:
"""Create an instance of the PN532 class using SPI"""
self.debug = debug
self._spi = spi_device.SPIDevice(spi, cs_pin)
super().__init__(debug=debug, irq=irq, reset=reset)

def _wakeup(self):
def _wakeup(self) -> None:
"""Send any special commands/data to wake up PN532"""
if self._reset_pin:
self._reset_pin.value = True
Expand All @@ -61,7 +77,7 @@ def _wakeup(self):
self.low_power = False
self.SAM_configuration() # Put the PN532 back in normal mode

def _wait_ready(self, timeout=1):
def _wait_ready(self, timeout: float = 1) -> bool:
"""Poll PN532 if status byte is ready, up to `timeout` seconds"""
status_cmd = bytearray([reverse_bit(_SPI_STATREAD), 0x00])
status_response = bytearray([0x00, 0x00])
Expand All @@ -77,7 +93,7 @@ def _wait_ready(self, timeout=1):
# We timed out!
return False

def _read_data(self, count):
def _read_data(self, count: int) -> bytearray:
"""Read a specified count of bytes from the PN532."""
# Build a read request frame.
frame = bytearray(count + 1)
Expand All @@ -92,7 +108,7 @@ def _read_data(self, count):
print("Reading: ", [hex(i) for i in frame[1:]])
return frame[1:]

def _write_data(self, framebytes):
def _write_data(self, framebytes: ReadableBuffer) -> None:
"""Write a specified count of bytes to the PN532"""
# start by making a frame with data write in front,
# then rest of bytes, and LSBify it
Expand Down
Loading

0 comments on commit 0061f33

Please sign in to comment.