Skip to content

Commit 828faa2

Browse files
authored
Merge pull request #19 from tekktrik/feature/add-typing
Add typing, small documentation and implementation corrections
2 parents 61ad39d + e671d54 commit 828faa2

File tree

8 files changed

+99
-32
lines changed

8 files changed

+99
-32
lines changed

adafruit_boardtest/boardtest_gpio.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import digitalio
3030
import supervisor
3131

32+
try:
33+
from typing import Any, Sequence, Tuple, List
34+
except ImportError:
35+
pass
36+
3237
__version__ = "0.0.0-auto.0"
3338
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3439

@@ -43,7 +48,7 @@
4348
NA = "N/A"
4449

4550
# Determine if given value is a number
46-
def _is_number(val):
51+
def _is_number(val: Any) -> bool:
4752
try:
4853
float(val)
4954
return True
@@ -52,13 +57,13 @@ def _is_number(val):
5257

5358

5459
# Release pins
55-
def _deinit_pins(gpios):
60+
def _deinit_pins(gpios: Sequence[digitalio.DigitalInOut]) -> None:
5661
for g in gpios:
5762
g.deinit()
5863

5964

6065
# Toggle IO pins while waiting for answer
61-
def _toggle_wait(gpios):
66+
def _toggle_wait(gpios: Sequence[digitalio.DigitalInOut]) -> bool:
6267

6368
timestamp = time.monotonic()
6469
led_state = False
@@ -79,7 +84,7 @@ def _toggle_wait(gpios):
7984
return bool(answer == "y")
8085

8186

82-
def run_test(pins):
87+
def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]:
8388

8489
"""
8590
Toggles all available GPIO on and off repeatedly.

adafruit_boardtest/boardtest_i2c.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
import board
3434
import busio
3535

36+
try:
37+
from typing import Tuple, Sequence, List
38+
except ImportError:
39+
pass
40+
3641
__version__ = "0.0.0-auto.0"
3742
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3843

@@ -51,7 +56,9 @@
5156
NA = "N/A"
5257

5358
# Open comms to I2C EEPROM by trying a write to memory address
54-
def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout=1.0):
59+
def _eeprom_i2c_wait(
60+
i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0
61+
) -> bool:
5562

5663
# Try to access the I2C EEPROM (it becomes unresonsive during a write)
5764
timestamp = time.monotonic()
@@ -66,7 +73,9 @@ def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout=1.0):
6673

6774

6875
# Write to address. Returns status (True for successful write, False otherwise)
69-
def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data):
76+
def _eeprom_i2c_write_byte(
77+
i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_data: int
78+
) -> bool:
7079

7180
# Make sure address is only one byte:
7281
if mem_addr > 255:
@@ -86,7 +95,9 @@ def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data):
8695

8796

8897
# Read from address. Returns tuple [status, result]
89-
def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout=1.0):
98+
def _eeprom_i2c_read_byte(
99+
i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0
100+
) -> Tuple[bool, bytearray]:
90101

91102
# Make sure address is only one byte:
92103
if mem_addr > 255:
@@ -103,7 +114,9 @@ def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout=1.0):
103114
return True, buf
104115

105116

106-
def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
117+
def run_test(
118+
pins: Sequence[str], sda_pin: str = SDA_PIN_NAME, scl_pin: str = SCL_PIN_NAME
119+
) -> Tuple[str, List[str]]:
107120

108121
"""
109122
Performs random writes and reads to I2C EEPROM.

adafruit_boardtest/boardtest_led.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
import digitalio
2929
import supervisor
3030

31+
try:
32+
from typing import Sequence, Tuple, List
33+
except ImportError:
34+
pass
35+
3136
__version__ = "0.0.0-auto.0"
3237
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3338

@@ -42,7 +47,7 @@
4247
NA = "N/A"
4348

4449
# Toggle IO pins while waiting for answer
45-
def _toggle_wait(led_pins):
50+
def _toggle_wait(led_pins: Sequence[str]) -> bool:
4651
timestamp = time.monotonic()
4752
led_state = False
4853
print("Are the pins listed above toggling? [y/n]")
@@ -77,7 +82,7 @@ def _toggle_wait(led_pins):
7782
return False
7883

7984

80-
def run_test(pins):
85+
def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]:
8186

8287
"""
8388
Toggles the onboard LED(s) on and off.

adafruit_boardtest/boardtest_sd.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
import adafruit_sdcard
3838
import storage
3939

40+
try:
41+
from typing import Sequence, Tuple, List
42+
except ImportError:
43+
pass
44+
4045
__version__ = "0.0.0-auto.0"
4146
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
4247

@@ -57,13 +62,14 @@
5762
NA = "N/A"
5863

5964

60-
def run_test(
61-
pins,
62-
mosi_pin=MOSI_PIN_NAME,
63-
miso_pin=MISO_PIN_NAME,
64-
sck_pin=SCK_PIN_NAME,
65-
cs_pin=CS_PIN_NAME,
66-
):
65+
def run_test( # pylint: disable=too-many-arguments,too-many-locals
66+
pins: Sequence[str],
67+
mosi_pin: str = MOSI_PIN_NAME,
68+
miso_pin: str = MISO_PIN_NAME,
69+
sck_pin: str = SCK_PIN_NAME,
70+
cs_pin: str = CS_PIN_NAME,
71+
filename: str = FILENAME,
72+
) -> Tuple[str, List[str]]:
6773

6874
"""
6975
Performs random writes and reads to file on attached SD card.
@@ -83,7 +89,7 @@ def run_test(
8389
# Tell user to connect SD card
8490
print("Insert SD card into holder and connect SPI lines to holder.")
8591
print("Connect " + cs_pin + " to the CS (DAT3) pin on the SD " + "card holder.")
86-
print("WARNING: " + FILENAME + " will be created or overwritten.")
92+
print("WARNING: " + filename + " will be created or overwritten.")
8793
print("Press enter to continue.")
8894
input()
8995

@@ -115,7 +121,7 @@ def run_test(
115121

116122
# Write test string to a text file on the card
117123
try:
118-
with open("/sd/" + FILENAME, "w") as file:
124+
with open("/sd/" + filename, "w") as file:
119125
print("Writing:\t" + test_str)
120126
file.write(test_str)
121127
except OSError:
@@ -125,7 +131,7 @@ def run_test(
125131
# Read from test file on the card
126132
read_str = ""
127133
try:
128-
with open("/sd/" + FILENAME, "r") as file:
134+
with open("/sd/" + filename, "r") as file:
129135
lines = file.readlines()
130136
for line in lines:
131137
read_str += line

adafruit_boardtest/boardtest_sd_cd.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import board
3030
import digitalio
3131

32+
try:
33+
from typing import Sequence, Tuple, List
34+
except ImportError:
35+
pass
36+
3237
__version__ = "0.0.0-auto.0"
3338
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3439

@@ -41,7 +46,9 @@
4146
NA = "N/A"
4247

4348

44-
def run_test(pins, cd_pin=SD_CD_PIN_NAME):
49+
def run_test(
50+
pins: Sequence[str], cd_pin: str = SD_CD_PIN_NAME
51+
) -> Tuple[str, List[str]]:
4552

4653
"""
4754
Checks status of CD pin as user inserts and removes SD card.

adafruit_boardtest/boardtest_spi.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
import digitalio
3737
import busio
3838

39+
try:
40+
from typing import Tuple, Sequence, List
41+
except ImportError:
42+
pass
43+
3944
__version__ = "0.0.0-auto.0"
4045
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
4146

@@ -64,7 +69,9 @@
6469
NA = "N/A"
6570

6671
# Wait for WIP bit to go low
67-
def _eeprom_spi_wait(spi, csel, timeout=1.0):
72+
def _eeprom_spi_wait(
73+
spi: busio.SPI, csel: digitalio.DigitalInOut, timeout: float = 1.0
74+
) -> bool:
6875

6976
# Continually read from STATUS register
7077
timestamp = time.monotonic()
@@ -85,7 +92,13 @@ def _eeprom_spi_wait(spi, csel, timeout=1.0):
8592

8693

8794
# Write to address. Returns status (True for successful write, False otherwise)
88-
def _eeprom_spi_write_byte(spi, csel, address, data, timeout=1.0):
95+
def _eeprom_spi_write_byte(
96+
spi: busio.SPI,
97+
csel: digitalio.DigitalInOut,
98+
address: int,
99+
data: int,
100+
timeout: float = 1.0,
101+
) -> bool:
89102

90103
# Make sure address is only one byte:
91104
if address > 255:
@@ -113,7 +126,9 @@ def _eeprom_spi_write_byte(spi, csel, address, data, timeout=1.0):
113126

114127

115128
# Read from address. Returns tuple [status, result]
116-
def _eeprom_spi_read_byte(spi, csel, address, timeout=1.0):
129+
def _eeprom_spi_read_byte(
130+
spi: busio.SPI, csel: digitalio.DigitalInOut, address: int, timeout: float = 1.0
131+
) -> Tuple[bool, bytearray]:
117132

118133
# Make sure address is only one byte:
119134
if address > 255:
@@ -134,12 +149,12 @@ def _eeprom_spi_read_byte(spi, csel, address, timeout=1.0):
134149

135150

136151
def run_test(
137-
pins,
138-
mosi_pin=MOSI_PIN_NAME,
139-
miso_pin=MISO_PIN_NAME,
140-
sck_pin=SCK_PIN_NAME,
141-
cs_pin=CS_PIN_NAME,
142-
):
152+
pins: Sequence[str],
153+
mosi_pin: str = MOSI_PIN_NAME,
154+
miso_pin: str = MISO_PIN_NAME,
155+
sck_pin: str = SCK_PIN_NAME,
156+
cs_pin: str = CS_PIN_NAME,
157+
) -> Tuple[str, List[str]]:
143158

144159
"""
145160
Performs random writes and reads to file on attached SD card.

adafruit_boardtest/boardtest_uart.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import board
3030
import busio
3131

32+
try:
33+
from typing import Sequence, Tuple, List
34+
except ImportError:
35+
pass
36+
3237
__version__ = "0.0.0-auto.0"
3338
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3439

@@ -46,14 +51,20 @@
4651
NA = "N/A"
4752

4853

49-
def run_test(pins, tx_pin=TX_PIN_NAME, rx_pin=RX_PIN_NAME, baud_rate=BAUD_RATE):
54+
def run_test(
55+
pins: Sequence[str],
56+
tx_pin: str = TX_PIN_NAME,
57+
rx_pin: str = RX_PIN_NAME,
58+
baud_rate: int = BAUD_RATE,
59+
) -> Tuple[str, List[str]]:
5060

5161
"""
5262
Performs random writes out of TX pin and reads on RX.
5363
5464
:param list[str] pins: list of pins to run the test on
5565
:param str tx_pin: pin name of UART TX
5666
:param str rx_pin: pin name of UART RX
67+
:param int baudrate: the baudrate to use
5768
:return: tuple(str, list[str]): test result followed by list of pins tested
5869
"""
5970

adafruit_boardtest/boardtest_voltage_monitor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import board
3232
import analogio
3333

34+
try:
35+
from typing import Sequence, Tuple, List
36+
except ImportError:
37+
pass
38+
3439
__version__ = "0.0.0-auto.0"
3540
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
3641

@@ -45,7 +50,7 @@
4550
NA = "N/A"
4651

4752

48-
def run_test(pins):
53+
def run_test(pins: Sequence[str]) -> Tuple[str, List[str]]:
4954

5055
"""
5156
Prints out voltage on the battery monitor or voltage monitor pin.

0 commit comments

Comments
 (0)