From c6407676636e6ab54a2f5cae9a7bca7653b54c14 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 17:15:42 -0500 Subject: [PATCH 1/5] Add missing filename parameter to run_test() --- adafruit_boardtest/boardtest_sd.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_boardtest/boardtest_sd.py b/adafruit_boardtest/boardtest_sd.py index b1820cf..71029bd 100644 --- a/adafruit_boardtest/boardtest_sd.py +++ b/adafruit_boardtest/boardtest_sd.py @@ -63,6 +63,7 @@ def run_test( miso_pin=MISO_PIN_NAME, sck_pin=SCK_PIN_NAME, cs_pin=CS_PIN_NAME, + filename: str = FILENAME ): """ @@ -83,7 +84,7 @@ def run_test( # Tell user to connect SD card print("Insert SD card into holder and connect SPI lines to holder.") print("Connect " + cs_pin + " to the CS (DAT3) pin on the SD " + "card holder.") - print("WARNING: " + FILENAME + " will be created or overwritten.") + print("WARNING: " + filename + " will be created or overwritten.") print("Press enter to continue.") input() @@ -115,7 +116,7 @@ def run_test( # Write test string to a text file on the card try: - with open("/sd/" + FILENAME, "w") as file: + with open("/sd/" + filename, "w") as file: print("Writing:\t" + test_str) file.write(test_str) except OSError: @@ -125,7 +126,7 @@ def run_test( # Read from test file on the card read_str = "" try: - with open("/sd/" + FILENAME, "r") as file: + with open("/sd/" + filename, "r") as file: lines = file.readlines() for line in lines: read_str += line From b1aabdd255115298d5e4a177664f53e27e136218 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 17:26:00 -0500 Subject: [PATCH 2/5] Add documentation for baudrate parameter in run_test() --- adafruit_boardtest/boardtest_uart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_boardtest/boardtest_uart.py b/adafruit_boardtest/boardtest_uart.py index b484bdf..0f98ee7 100644 --- a/adafruit_boardtest/boardtest_uart.py +++ b/adafruit_boardtest/boardtest_uart.py @@ -54,6 +54,7 @@ def run_test(pins, tx_pin=TX_PIN_NAME, rx_pin=RX_PIN_NAME, baud_rate=BAUD_RATE): :param list[str] pins: list of pins to run the test on :param str tx_pin: pin name of UART TX :param str rx_pin: pin name of UART RX + :param int baudrate: the baudrate to use :return: tuple(str, list[str]): test result followed by list of pins tested """ From 607c16250d5a84ded246fb5dec19adbe6ea5a653 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 17:26:08 -0500 Subject: [PATCH 3/5] Add typing --- adafruit_boardtest/boardtest_gpio.py | 13 +++++++---- adafruit_boardtest/boardtest_i2c.py | 13 +++++++---- adafruit_boardtest/boardtest_led.py | 9 ++++++-- adafruit_boardtest/boardtest_sd.py | 17 +++++++++----- adafruit_boardtest/boardtest_sd_cd.py | 7 +++++- adafruit_boardtest/boardtest_spi.py | 23 +++++++++++-------- adafruit_boardtest/boardtest_uart.py | 7 +++++- .../boardtest_voltage_monitor.py | 7 +++++- 8 files changed, 68 insertions(+), 28 deletions(-) diff --git a/adafruit_boardtest/boardtest_gpio.py b/adafruit_boardtest/boardtest_gpio.py index a8ddebb..7855be1 100644 --- a/adafruit_boardtest/boardtest_gpio.py +++ b/adafruit_boardtest/boardtest_gpio.py @@ -29,6 +29,11 @@ import digitalio import supervisor +try: + from typing import Any, Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -43,7 +48,7 @@ NA = "N/A" # Determine if given value is a number -def _is_number(val): +def _is_number(val: Any) -> bool: try: float(val) return True @@ -52,13 +57,13 @@ def _is_number(val): # Release pins -def _deinit_pins(gpios): +def _deinit_pins(gpios: Sequence[digitalio.DigitalInOut]) -> None: for g in gpios: g.deinit() # Toggle IO pins while waiting for answer -def _toggle_wait(gpios): +def _toggle_wait(gpios: Sequence[digitalio.DigitalInOut]) -> bool: timestamp = time.monotonic() led_state = False @@ -79,7 +84,7 @@ def _toggle_wait(gpios): return bool(answer == "y") -def run_test(pins): +def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]: """ Toggles all available GPIO on and off repeatedly. diff --git a/adafruit_boardtest/boardtest_i2c.py b/adafruit_boardtest/boardtest_i2c.py index ea94feb..5e05e4d 100644 --- a/adafruit_boardtest/boardtest_i2c.py +++ b/adafruit_boardtest/boardtest_i2c.py @@ -33,6 +33,11 @@ import board import busio +try: + from typing import Tuple, Sequence, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -51,7 +56,7 @@ NA = "N/A" # Open comms to I2C EEPROM by trying a write to memory address -def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout=1.0): +def _eeprom_i2c_wait(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> bool: # Try to access the I2C EEPROM (it becomes unresonsive during a write) timestamp = time.monotonic() @@ -66,7 +71,7 @@ def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout=1.0): # Write to address. Returns status (True for successful write, False otherwise) -def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data): +def _eeprom_i2c_write_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_data: int) -> bool: # Make sure address is only one byte: if mem_addr > 255: @@ -86,7 +91,7 @@ def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data): # Read from address. Returns tuple [status, result] -def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout=1.0): +def _eeprom_i2c_read_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> Tuple[bool, bytearray]: # Make sure address is only one byte: if mem_addr > 255: @@ -103,7 +108,7 @@ def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout=1.0): return True, buf -def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME): +def run_test(pins: Sequence[str], sda_pin: str = SDA_PIN_NAME, scl_pin: str = SCL_PIN_NAME) -> Tuple[str, List[str]]: """ Performs random writes and reads to I2C EEPROM. diff --git a/adafruit_boardtest/boardtest_led.py b/adafruit_boardtest/boardtest_led.py index c41b282..7a4e1bc 100644 --- a/adafruit_boardtest/boardtest_led.py +++ b/adafruit_boardtest/boardtest_led.py @@ -28,6 +28,11 @@ import digitalio import supervisor +try: + from typing import Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -42,7 +47,7 @@ NA = "N/A" # Toggle IO pins while waiting for answer -def _toggle_wait(led_pins): +def _toggle_wait(led_pins: Sequence[str]) -> bool: timestamp = time.monotonic() led_state = False print("Are the pins listed above toggling? [y/n]") @@ -77,7 +82,7 @@ def _toggle_wait(led_pins): return False -def run_test(pins): +def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]: """ Toggles the onboard LED(s) on and off. diff --git a/adafruit_boardtest/boardtest_sd.py b/adafruit_boardtest/boardtest_sd.py index 71029bd..f4a6cb4 100644 --- a/adafruit_boardtest/boardtest_sd.py +++ b/adafruit_boardtest/boardtest_sd.py @@ -37,6 +37,11 @@ import adafruit_sdcard import storage +try: + from typing import Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -58,13 +63,13 @@ def run_test( - pins, - mosi_pin=MOSI_PIN_NAME, - miso_pin=MISO_PIN_NAME, - sck_pin=SCK_PIN_NAME, - cs_pin=CS_PIN_NAME, + pins: Sequence[str], + mosi_pin: str = MOSI_PIN_NAME, + miso_pin: str = MISO_PIN_NAME, + sck_pin: str = SCK_PIN_NAME, + cs_pin: str = CS_PIN_NAME, filename: str = FILENAME -): +) -> Tuple[str, List[str]]: """ Performs random writes and reads to file on attached SD card. diff --git a/adafruit_boardtest/boardtest_sd_cd.py b/adafruit_boardtest/boardtest_sd_cd.py index 094cd92..2c90356 100644 --- a/adafruit_boardtest/boardtest_sd_cd.py +++ b/adafruit_boardtest/boardtest_sd_cd.py @@ -29,6 +29,11 @@ import board import digitalio +try: + from typing import Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -41,7 +46,7 @@ NA = "N/A" -def run_test(pins, cd_pin=SD_CD_PIN_NAME): +def run_test(pins: Sequence[str], cd_pin: str = SD_CD_PIN_NAME) -> Tuple[str, List[str]]: """ Checks status of CD pin as user inserts and removes SD card. diff --git a/adafruit_boardtest/boardtest_spi.py b/adafruit_boardtest/boardtest_spi.py index d18133a..61bbd76 100644 --- a/adafruit_boardtest/boardtest_spi.py +++ b/adafruit_boardtest/boardtest_spi.py @@ -36,6 +36,11 @@ import digitalio import busio +try: + from typing import Tuple, Sequence, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -64,7 +69,7 @@ NA = "N/A" # Wait for WIP bit to go low -def _eeprom_spi_wait(spi, csel, timeout=1.0): +def _eeprom_spi_wait(spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: float = 1.0) -> bool: # Continually read from STATUS register timestamp = time.monotonic() @@ -85,7 +90,7 @@ def _eeprom_spi_wait(spi, csel, timeout=1.0): # Write to address. Returns status (True for successful write, False otherwise) -def _eeprom_spi_write_byte(spi, csel, address, data, timeout=1.0): +def _eeprom_spi_write_byte(spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, data: int, timeout: float = 1.0) -> bool: # Make sure address is only one byte: if address > 255: @@ -113,7 +118,7 @@ def _eeprom_spi_write_byte(spi, csel, address, data, timeout=1.0): # Read from address. Returns tuple [status, result] -def _eeprom_spi_read_byte(spi, csel, address, timeout=1.0): +def _eeprom_spi_read_byte(spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, timeout: float = 1.0) -> Tuple[bool, bytearray]: # Make sure address is only one byte: if address > 255: @@ -134,12 +139,12 @@ def _eeprom_spi_read_byte(spi, csel, address, timeout=1.0): def run_test( - pins, - mosi_pin=MOSI_PIN_NAME, - miso_pin=MISO_PIN_NAME, - sck_pin=SCK_PIN_NAME, - cs_pin=CS_PIN_NAME, -): + pins: Sequence[str], + mosi_pin: str = MOSI_PIN_NAME, + miso_pin: str = MISO_PIN_NAME, + sck_pin: str = SCK_PIN_NAME, + cs_pin: str = CS_PIN_NAME, +) -> Tuple[str, List[str]]: """ Performs random writes and reads to file on attached SD card. diff --git a/adafruit_boardtest/boardtest_uart.py b/adafruit_boardtest/boardtest_uart.py index 0f98ee7..db7a8f8 100644 --- a/adafruit_boardtest/boardtest_uart.py +++ b/adafruit_boardtest/boardtest_uart.py @@ -29,6 +29,11 @@ import board import busio +try: + from typing import Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -46,7 +51,7 @@ NA = "N/A" -def run_test(pins, tx_pin=TX_PIN_NAME, rx_pin=RX_PIN_NAME, baud_rate=BAUD_RATE): +def run_test(pins: Sequence[str], tx_pin: str = TX_PIN_NAME, rx_pin: str = RX_PIN_NAME, baud_rate: int = BAUD_RATE) -> Tuple[str, List[str]]: """ Performs random writes out of TX pin and reads on RX. diff --git a/adafruit_boardtest/boardtest_voltage_monitor.py b/adafruit_boardtest/boardtest_voltage_monitor.py index c56a782..f523476 100644 --- a/adafruit_boardtest/boardtest_voltage_monitor.py +++ b/adafruit_boardtest/boardtest_voltage_monitor.py @@ -31,6 +31,11 @@ import board import analogio +try: + from typing import Sequence, Tuple, List +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" @@ -45,7 +50,7 @@ NA = "N/A" -def run_test(pins): +def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]: """ Prints out voltage on the battery monitor or voltage monitor pin. From 26924611c47fc9d221d70c70e5137c7cc6da54d8 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 17:27:20 -0500 Subject: [PATCH 4/5] Reformatted per pre-commit --- adafruit_boardtest/boardtest_i2c.py | 16 ++++++++++++---- adafruit_boardtest/boardtest_sd.py | 2 +- adafruit_boardtest/boardtest_sd_cd.py | 4 +++- adafruit_boardtest/boardtest_spi.py | 16 +++++++++++++--- adafruit_boardtest/boardtest_uart.py | 7 ++++++- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/adafruit_boardtest/boardtest_i2c.py b/adafruit_boardtest/boardtest_i2c.py index 5e05e4d..1c837dd 100644 --- a/adafruit_boardtest/boardtest_i2c.py +++ b/adafruit_boardtest/boardtest_i2c.py @@ -56,7 +56,9 @@ NA = "N/A" # Open comms to I2C EEPROM by trying a write to memory address -def _eeprom_i2c_wait(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> bool: +def _eeprom_i2c_wait( + i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0 +) -> bool: # Try to access the I2C EEPROM (it becomes unresonsive during a write) timestamp = time.monotonic() @@ -71,7 +73,9 @@ def _eeprom_i2c_wait(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: floa # Write to address. Returns status (True for successful write, False otherwise) -def _eeprom_i2c_write_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_data: int) -> bool: +def _eeprom_i2c_write_byte( + i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_data: int +) -> bool: # Make sure address is only one byte: if mem_addr > 255: @@ -91,7 +95,9 @@ def _eeprom_i2c_write_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_dat # Read from address. Returns tuple [status, result] -def _eeprom_i2c_read_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> Tuple[bool, bytearray]: +def _eeprom_i2c_read_byte( + i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0 +) -> Tuple[bool, bytearray]: # Make sure address is only one byte: if mem_addr > 255: @@ -108,7 +114,9 @@ def _eeprom_i2c_read_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: return True, buf -def run_test(pins: Sequence[str], sda_pin: str = SDA_PIN_NAME, scl_pin: str = SCL_PIN_NAME) -> Tuple[str, List[str]]: +def run_test( + pins: Sequence[str], sda_pin: str = SDA_PIN_NAME, scl_pin: str = SCL_PIN_NAME +) -> Tuple[str, List[str]]: """ Performs random writes and reads to I2C EEPROM. diff --git a/adafruit_boardtest/boardtest_sd.py b/adafruit_boardtest/boardtest_sd.py index f4a6cb4..b05df76 100644 --- a/adafruit_boardtest/boardtest_sd.py +++ b/adafruit_boardtest/boardtest_sd.py @@ -68,7 +68,7 @@ def run_test( miso_pin: str = MISO_PIN_NAME, sck_pin: str = SCK_PIN_NAME, cs_pin: str = CS_PIN_NAME, - filename: str = FILENAME + filename: str = FILENAME, ) -> Tuple[str, List[str]]: """ diff --git a/adafruit_boardtest/boardtest_sd_cd.py b/adafruit_boardtest/boardtest_sd_cd.py index 2c90356..91e9e0a 100644 --- a/adafruit_boardtest/boardtest_sd_cd.py +++ b/adafruit_boardtest/boardtest_sd_cd.py @@ -46,7 +46,9 @@ NA = "N/A" -def run_test(pins: Sequence[str], cd_pin: str = SD_CD_PIN_NAME) -> Tuple[str, List[str]]: +def run_test( + pins: Sequence[str], cd_pin: str = SD_CD_PIN_NAME +) -> Tuple[str, List[str]]: """ Checks status of CD pin as user inserts and removes SD card. diff --git a/adafruit_boardtest/boardtest_spi.py b/adafruit_boardtest/boardtest_spi.py index 61bbd76..34ae7fe 100644 --- a/adafruit_boardtest/boardtest_spi.py +++ b/adafruit_boardtest/boardtest_spi.py @@ -69,7 +69,9 @@ NA = "N/A" # Wait for WIP bit to go low -def _eeprom_spi_wait(spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: float = 1.0) -> bool: +def _eeprom_spi_wait( + spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: float = 1.0 +) -> bool: # Continually read from STATUS register timestamp = time.monotonic() @@ -90,7 +92,13 @@ def _eeprom_spi_wait(spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: floa # Write to address. Returns status (True for successful write, False otherwise) -def _eeprom_spi_write_byte(spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, data: int, timeout: float = 1.0) -> bool: +def _eeprom_spi_write_byte( + spi: busio.SPI, + csel: digitalio.DigitalInOut, + address: int, + data: int, + timeout: float = 1.0, +) -> bool: # Make sure address is only one byte: if address > 255: @@ -118,7 +126,9 @@ def _eeprom_spi_write_byte(spi: busio.SPI, csel: digitalio.DigitalInOut, address # Read from address. Returns tuple [status, result] -def _eeprom_spi_read_byte(spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, timeout: float = 1.0) -> Tuple[bool, bytearray]: +def _eeprom_spi_read_byte( + spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, timeout: float = 1.0 +) -> Tuple[bool, bytearray]: # Make sure address is only one byte: if address > 255: diff --git a/adafruit_boardtest/boardtest_uart.py b/adafruit_boardtest/boardtest_uart.py index db7a8f8..52512af 100644 --- a/adafruit_boardtest/boardtest_uart.py +++ b/adafruit_boardtest/boardtest_uart.py @@ -51,7 +51,12 @@ NA = "N/A" -def run_test(pins: Sequence[str], tx_pin: str = TX_PIN_NAME, rx_pin: str = RX_PIN_NAME, baud_rate: int = BAUD_RATE) -> Tuple[str, List[str]]: +def run_test( + pins: Sequence[str], + tx_pin: str = TX_PIN_NAME, + rx_pin: str = RX_PIN_NAME, + baud_rate: int = BAUD_RATE, +) -> Tuple[str, List[str]]: """ Performs random writes out of TX pin and reads on RX. From e671d54aaeba6aa75cb1d5a565d8648ea3b49aab Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 14 Dec 2021 17:31:22 -0500 Subject: [PATCH 5/5] Disable warnings for too many arguments and local variables All arguments are needed, and the implementation of the function necessitates more local variables than suggested --- adafruit_boardtest/boardtest_sd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_boardtest/boardtest_sd.py b/adafruit_boardtest/boardtest_sd.py index b05df76..291a402 100644 --- a/adafruit_boardtest/boardtest_sd.py +++ b/adafruit_boardtest/boardtest_sd.py @@ -62,7 +62,7 @@ NA = "N/A" -def run_test( +def run_test( # pylint: disable=too-many-arguments,too-many-locals pins: Sequence[str], mosi_pin: str = MOSI_PIN_NAME, miso_pin: str = MISO_PIN_NAME,