Skip to content

Add typing, small documentation and implementation corrections #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions adafruit_boardtest/boardtest_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
21 changes: 17 additions & 4 deletions adafruit_boardtest/boardtest_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -51,7 +56,9 @@
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()
Expand All @@ -66,7 +73,9 @@ 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:
Expand All @@ -86,7 +95,9 @@ 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:
Expand All @@ -103,7 +114,9 @@ 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.
Expand Down
9 changes: 7 additions & 2 deletions adafruit_boardtest/boardtest_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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]")
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 16 additions & 10 deletions adafruit_boardtest/boardtest_sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -57,13 +62,14 @@
NA = "N/A"


def run_test(
pins,
mosi_pin=MOSI_PIN_NAME,
miso_pin=MISO_PIN_NAME,
sck_pin=SCK_PIN_NAME,
cs_pin=CS_PIN_NAME,
):
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,
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.
Expand All @@ -83,7 +89,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()

Expand Down Expand Up @@ -115,7 +121,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:
Expand All @@ -125,7 +131,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
Expand Down
9 changes: 8 additions & 1 deletion adafruit_boardtest/boardtest_sd_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -41,7 +46,9 @@
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.
Expand Down
33 changes: 24 additions & 9 deletions adafruit_boardtest/boardtest_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -64,7 +69,9 @@
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()
Expand All @@ -85,7 +92,13 @@ 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:
Expand Down Expand Up @@ -113,7 +126,9 @@ 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:
Expand All @@ -134,12 +149,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.
Expand Down
13 changes: 12 additions & 1 deletion adafruit_boardtest/boardtest_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -46,14 +51,20 @@
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.

: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
"""

Expand Down
7 changes: 6 additions & 1 deletion adafruit_boardtest/boardtest_voltage_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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.
Expand Down