From 91f3385ba72b9526596eaa5df3925232f05d9562 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Thu, 9 Feb 2023 15:12:59 -0500 Subject: [PATCH 1/5] Add Missing Type Annotations --- adafruit_pn532/adafruit_pn532.py | 87 +++++++++++++++++++++----------- adafruit_pn532/i2c.py | 25 +++++++-- adafruit_pn532/spi.py | 27 +++++++--- adafruit_pn532/uart.py | 18 +++++-- requirements.txt | 1 + 5 files changed, 112 insertions(+), 46 deletions(-) diff --git a/adafruit_pn532/adafruit_pn532.py b/adafruit_pn532/adafruit_pn532.py index 232f8c0..3f76c93 100644 --- a/adafruit_pn532/adafruit_pn532.py +++ b/adafruit_pn532/adafruit_pn532.py @@ -31,6 +31,13 @@ from digitalio import Direction from micropython import const +try: + from typing import Optional, Tuple + from typing_extensions import Literal + from digitalio import DigitalInOut +except ImportError: + pass + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git" @@ -151,7 +158,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 @@ -160,26 +173,26 @@ class PN532: self.reset() _ = self.firmware_version - def _read_data(self, count): + def _read_data(self, count: int) -> 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: bytearray) -> 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: @@ -191,7 +204,7 @@ class PN532: 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 @@ -221,7 +234,7 @@ class PN532: 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) -> 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 @@ -256,9 +269,13 @@ class PN532: # Return frame data. return response[offset + 2 : offset + 2 + frame_len] - def call_function( - self, command, response_length=0, params=[], timeout=1 - ): # pylint: disable=dangerous-default-value + def call_function( # pylint: disable=dangerous-default-value + self, + command: int, + response_length: int = 0, + params: Optional[bytearray] = [], + timeout: float = 1, + ) -> 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 @@ -272,12 +289,12 @@ class PN532: command, response_length=response_length, timeout=timeout ) - def send_command( - self, command, params=[], timeout=1 - ): # pylint: disable=dangerous-default-value + def send_command( # pylint: disable=dangerous-default-value + self, command: int, params: Optional[bytearray] = [], 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. + If no acknowledgment is received, False is returned. """ if self.low_power: self._wakeup() @@ -300,7 +317,9 @@ class PN532: 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 + ) -> 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 @@ -317,7 +336,7 @@ class PN532: # 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 @@ -333,7 +352,7 @@ class PN532: 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. """ @@ -342,7 +361,7 @@ class PN532: 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 @@ -352,7 +371,9 @@ class PN532: # 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 + ) -> 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. @@ -364,9 +385,11 @@ class PN532: 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 + ): """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. @@ -380,7 +403,7 @@ class PN532: return False # _COMMAND_INLISTPASSIVETARGET failed return response - def get_passive_target(self, timeout=1): + def get_passive_target(self, timeout: float = 1) -> 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 @@ -404,9 +427,13 @@ class PN532: # 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: bytearray, + block_number: int, + key_number: Literal[0x60, 0x61], + key: bytearray, + ) -> 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 @@ -429,7 +456,7 @@ class PN532: ) return response[0] == 0x00 - def mifare_classic_read_block(self, block_number): + def mifare_classic_read_block(self, block_number: int) -> 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 @@ -447,7 +474,7 @@ class PN532: # 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: bytearray) -> 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, @@ -468,7 +495,7 @@ class PN532: ) return response[0] == 0x0 - def ntag2xx_write_block(self, block_number, data): + def ntag2xx_write_block(self, block_number: int, data: bytearray) -> 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, @@ -487,7 +514,7 @@ class PN532: ) return response[0] == 0x00 - def ntag2xx_read_block(self, block_number): + def ntag2xx_read_block(self, block_number: int) -> 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. diff --git a/adafruit_pn532/i2c.py b/adafruit_pn532/i2c.py index ff36b3e..760b042 100644 --- a/adafruit_pn532/i2c.py +++ b/adafruit_pn532/i2c.py @@ -23,13 +23,28 @@ from digitalio import Direction from micropython import const from adafruit_pn532.adafruit_pn532 import PN532, BusyError +try: + from typing import Optional + from digitalio import DigitalInOut + 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, *, irq=None, reset=None, req=None, debug=False): + def __init__( + self, + i2c: I2C, + *, + 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. @@ -39,7 +54,7 @@ class PN532_I2C(PN532): self._i2c = i2c_device.I2CDevice(i2c, _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 @@ -53,7 +68,7 @@ class PN532_I2C(PN532): 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() @@ -69,7 +84,7 @@ class PN532_I2C(PN532): # 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) @@ -82,7 +97,7 @@ class PN532_I2C(PN532): 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: bytearray) -> None: """Write a specified count of bytes to the PN532""" with self._i2c as i2c: i2c.write(framebytes) diff --git a/adafruit_pn532/spi.py b/adafruit_pn532/spi.py index d3d1ed4..f5d8f09 100644 --- a/adafruit_pn532/spi.py +++ b/adafruit_pn532/spi.py @@ -14,6 +14,13 @@ using SPI. """ +try: + from typing import Optional + 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" @@ -28,7 +35,7 @@ _SPI_DATAREAD = const(0x03) _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 @@ -44,13 +51,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 @@ -61,7 +76,7 @@ class PN532_SPI(PN532): 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]) @@ -77,7 +92,7 @@ class PN532_SPI(PN532): # 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) @@ -92,7 +107,7 @@ class PN532_SPI(PN532): print("Reading: ", [hex(i) for i in frame[1:]]) return frame[1:] - def _write_data(self, framebytes): + def _write_data(self, framebytes: bytearray) -> 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 diff --git a/adafruit_pn532/uart.py b/adafruit_pn532/uart.py index 84d749e..edaec6b 100644 --- a/adafruit_pn532/uart.py +++ b/adafruit_pn532/uart.py @@ -17,6 +17,12 @@ using UART. __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git" +try: + from typing import Optional + from digitalio import DigitalInOut + from busio import UART +except ImportError: + pass import time from adafruit_pn532.adafruit_pn532 import PN532, BusyError @@ -25,7 +31,9 @@ from adafruit_pn532.adafruit_pn532 import PN532, BusyError class PN532_UART(PN532): """Driver for the PN532 connected over Serial UART""" - def __init__(self, uart, *, reset=None, debug=False): + def __init__( + self, uart: UART, *, reset: Optional[DigitalInOut] = None, debug: bool = False + ) -> None: """Create an instance of the PN532 class using Serial connection. Optional reset pin and debugging output. """ @@ -33,7 +41,7 @@ class PN532_UART(PN532): self._uart = uart super().__init__(debug=debug, 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 @@ -44,7 +52,7 @@ class PN532_UART(PN532): ) # wake up! self.SAM_configuration() - def _wait_ready(self, timeout=1): + def _wait_ready(self, timeout: float = 1) -> bool: """Wait `timeout` seconds""" timestamp = time.monotonic() while (time.monotonic() - timestamp) < timeout: @@ -54,7 +62,7 @@ class PN532_UART(PN532): # 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.""" frame = self._uart.read(count) if not frame: @@ -63,7 +71,7 @@ class PN532_UART(PN532): print("Reading: ", [hex(i) for i in frame]) return frame - def _write_data(self, framebytes): + def _write_data(self, framebytes: bytearray) -> None: """Write a specified count of bytes to the PN532""" self._uart.reset_input_buffer() self._uart.write(framebytes) diff --git a/requirements.txt b/requirements.txt index f39dc93..f5013eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ Adafruit-Blinka adafruit-circuitpython-busdevice pyserial +typing-extensions~=4.0 From 432b6202e424d961db95e6992a9edd297ebc1f29 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Thu, 9 Feb 2023 15:22:23 -0500 Subject: [PATCH 2/5] fix ungrouped imports --- adafruit_pn532/adafruit_pn532.py | 2 +- adafruit_pn532/i2c.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_pn532/adafruit_pn532.py b/adafruit_pn532/adafruit_pn532.py index 3f76c93..f8273e5 100644 --- a/adafruit_pn532/adafruit_pn532.py +++ b/adafruit_pn532/adafruit_pn532.py @@ -34,7 +34,7 @@ from micropython import const try: from typing import Optional, Tuple from typing_extensions import Literal - from digitalio import DigitalInOut + from digitalio import DigitalInOut # pylint: disable=C0412 except ImportError: pass diff --git a/adafruit_pn532/i2c.py b/adafruit_pn532/i2c.py index 760b042..2c92d1f 100644 --- a/adafruit_pn532/i2c.py +++ b/adafruit_pn532/i2c.py @@ -25,7 +25,7 @@ from adafruit_pn532.adafruit_pn532 import PN532, BusyError try: from typing import Optional - from digitalio import DigitalInOut + from digitalio import DigitalInOut # pylint: disable=C0412 from busio import I2C except ImportError: pass From b5707f6896b1fba8054e85f70a587345f64fab08 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sat, 11 Feb 2023 11:09:29 -0500 Subject: [PATCH 3/5] corrections per review comments --- adafruit_pn532/adafruit_pn532.py | 51 +++++++++++++++++++------------- adafruit_pn532/i2c.py | 6 ++-- adafruit_pn532/spi.py | 3 +- adafruit_pn532/uart.py | 5 ++-- requirements.txt | 1 + 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/adafruit_pn532/adafruit_pn532.py b/adafruit_pn532/adafruit_pn532.py index f8273e5..35c0f86 100644 --- a/adafruit_pn532/adafruit_pn532.py +++ b/adafruit_pn532/adafruit_pn532.py @@ -32,9 +32,10 @@ from digitalio import Direction from micropython import const try: - from typing import Optional, Tuple + from typing import Optional, Tuple, Union from typing_extensions import Literal - from digitalio import DigitalInOut # pylint: disable=C0412 + from circuitpython_typing import ReadableBuffer + from digitalio import DigitalInOut # pylint: disable=ungrouped-imports except ImportError: pass @@ -173,12 +174,12 @@ class PN532: self.reset() _ = self.firmware_version - def _read_data(self, count: int) -> bytearray: + 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: bytearray) -> None: + def _write_data(self, framebytes: bytes) -> None: # Write raw bytestring data to device, not including status bytes: # Subclasses MUST implement this! raise NotImplementedError @@ -234,7 +235,7 @@ class PN532: print("Write frame: ", [hex(i) for i in frame]) self._write_data(bytes(frame)) - def _read_frame(self, length: int) -> bytearray: + 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 @@ -269,13 +270,13 @@ class PN532: # Return frame data. return response[offset + 2 : offset + 2 + frame_len] - def call_function( # pylint: disable=dangerous-default-value + def call_function( self, command: int, response_length: int = 0, - params: Optional[bytearray] = [], + params: ReadableBuffer = b"", timeout: float = 1, - ) -> bytearray: + ) -> 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 @@ -289,11 +290,11 @@ class PN532: command, response_length=response_length, timeout=timeout ) - def send_command( # pylint: disable=dangerous-default-value - self, command: int, params: Optional[bytearray] = [], timeout: float = 1 + def send_command( + 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. + Will wait up to timeout seconds for the acknowledgment and return True. If no acknowledgment is received, False is returned. """ if self.low_power: @@ -319,7 +320,7 @@ class PN532: def process_response( self, command: int, response_length: int = 0, timeout: float = 1 - ) -> bytearray: + ) -> 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 @@ -373,7 +374,7 @@ class PN532: def read_passive_target( self, card_baud: int = _MIFARE_ISO14443A, timeout: float = 1 - ) -> bytearray: + ) -> 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. @@ -387,7 +388,7 @@ class PN532: 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 successfully. Note, this does not also return the UID of a card! `get_passive_target` must be called @@ -403,7 +404,9 @@ class PN532: return False # _COMMAND_INLISTPASSIVETARGET failed return response - def get_passive_target(self, timeout: float = 1) -> bytearray: + 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 @@ -429,10 +432,10 @@ class PN532: def mifare_classic_authenticate_block( # pylint: disable=invalid-name self, - uid: bytearray, + uid: ReadableBuffer, block_number: int, key_number: Literal[0x60, 0x61], - key: bytearray, + 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 @@ -456,7 +459,9 @@ class PN532: ) return response[0] == 0x00 - def mifare_classic_read_block(self, block_number: int) -> bytearray: + 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 @@ -474,7 +479,9 @@ class PN532: # Return first 4 bytes since 16 bytes are always returned. return response[1:] - def mifare_classic_write_block(self, block_number: int, data: bytearray) -> bool: + 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, @@ -495,7 +502,7 @@ class PN532: ) return response[0] == 0x0 - def ntag2xx_write_block(self, block_number: int, data: bytearray) -> bool: + 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, @@ -514,7 +521,9 @@ class PN532: ) return response[0] == 0x00 - def ntag2xx_read_block(self, block_number: int) -> bytearray: + 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. diff --git a/adafruit_pn532/i2c.py b/adafruit_pn532/i2c.py index 2c92d1f..badfe7d 100644 --- a/adafruit_pn532/i2c.py +++ b/adafruit_pn532/i2c.py @@ -25,7 +25,7 @@ from adafruit_pn532.adafruit_pn532 import PN532, BusyError try: from typing import Optional - from digitalio import DigitalInOut # pylint: disable=C0412 + from digitalio import DigitalInOut # pylint: disable=ungrouped-imports from busio import I2C except ImportError: pass @@ -80,7 +80,7 @@ class PN532_I2C(PN532): 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 @@ -97,7 +97,7 @@ class PN532_I2C(PN532): print("Reading: ", [hex(i) for i in frame[1:]]) return frame[1:] # don't return the status byte - def _write_data(self, framebytes: bytearray) -> None: + def _write_data(self, framebytes: bytes) -> None: """Write a specified count of bytes to the PN532""" with self._i2c as i2c: i2c.write(framebytes) diff --git a/adafruit_pn532/spi.py b/adafruit_pn532/spi.py index f5d8f09..72afa18 100644 --- a/adafruit_pn532/spi.py +++ b/adafruit_pn532/spi.py @@ -16,6 +16,7 @@ using SPI. try: from typing import Optional + from circuitpython_typing import ReadableBuffer from digitalio import DigitalInOut from busio import SPI except ImportError: @@ -107,7 +108,7 @@ class PN532_SPI(PN532): print("Reading: ", [hex(i) for i in frame[1:]]) return frame[1:] - def _write_data(self, framebytes: bytearray) -> None: + 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 diff --git a/adafruit_pn532/uart.py b/adafruit_pn532/uart.py index edaec6b..57548c3 100644 --- a/adafruit_pn532/uart.py +++ b/adafruit_pn532/uart.py @@ -19,6 +19,7 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PN532.git" try: from typing import Optional + from circuitpython_typing import ReadableBuffer from digitalio import DigitalInOut from busio import UART except ImportError: @@ -62,7 +63,7 @@ class PN532_UART(PN532): # Timed out! return False - def _read_data(self, count: int) -> bytearray: + def _read_data(self, count: int) -> bytes: """Read a specified count of bytes from the PN532.""" frame = self._uart.read(count) if not frame: @@ -71,7 +72,7 @@ class PN532_UART(PN532): print("Reading: ", [hex(i) for i in frame]) return frame - def _write_data(self, framebytes: bytearray) -> None: + def _write_data(self, framebytes: ReadableBuffer) -> None: """Write a specified count of bytes to the PN532""" self._uart.reset_input_buffer() self._uart.write(framebytes) diff --git a/requirements.txt b/requirements.txt index f5013eb..5144483 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,6 @@ Adafruit-Blinka adafruit-circuitpython-busdevice +adafruit-circuitpython-typing pyserial typing-extensions~=4.0 From dc4f7d2d92b5bd34c2bf3e3750deb1c3a2514d8b Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sun, 26 Feb 2023 20:25:19 -0500 Subject: [PATCH 4/5] merge recent changes to i2c.py --- adafruit_pn532/i2c.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_pn532/i2c.py b/adafruit_pn532/i2c.py index 63c4365..d4f46b4 100644 --- a/adafruit_pn532/i2c.py +++ b/adafruit_pn532/i2c.py @@ -32,7 +32,6 @@ except ImportError: _I2C_ADDRESS = const(0x24) - class PN532_I2C(PN532): """Driver for the PN532 connected over I2C.""" From 812235c025a33577d22c3e1f5fdbc099abc86da2 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sun, 26 Feb 2023 20:34:00 -0500 Subject: [PATCH 5/5] fix formatting issues with i2c.py --- adafruit_pn532/i2c.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_pn532/i2c.py b/adafruit_pn532/i2c.py index d4f46b4..63c4365 100644 --- a/adafruit_pn532/i2c.py +++ b/adafruit_pn532/i2c.py @@ -32,6 +32,7 @@ except ImportError: _I2C_ADDRESS = const(0x24) + class PN532_I2C(PN532): """Driver for the PN532 connected over I2C."""