Skip to content

Commit f6d41b6

Browse files
committed
finalizing (hopefully) all type hinting
1 parent 080462e commit f6d41b6

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

adafruit_avrprog.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
try:
3636
from os import PathLike
37-
from typing import Any, Dict, Optional, Tuple, TypeAlias
37+
from typing import Any, Dict, List, Optional, Tuple, TypeAlias, Union
3838

3939
# Technically this type should come from: from _typeshed import FileDescriptorOrPath
4040
# Unfortunately _typeshed is only in the standard library in newer releases of Python, e.g. 3.11
@@ -49,7 +49,7 @@
4949

5050
from math import floor
5151

52-
from busio import SPI
52+
from busio import I2C, SPI
5353
from digitalio import DigitalInOut, Direction
5454

5555
_SLOW_CLOCK: int = 100000
@@ -107,7 +107,7 @@ class Boards:
107107
_spi: SPI = None
108108
_rst: DigitalInOut = None
109109

110-
def init(self, spi_bus: SPI, rst_pin) -> None:
110+
def init(self, spi_bus: SPI, rst_pin: Union[SPI, I2C]) -> None:
111111
"""
112112
Initialize the programmer with an SPI port that will be used to
113113
communicate with the chip. Make sure your SPI supports 'write_readinto'
@@ -206,8 +206,11 @@ def program_file(
206206
return True
207207

208208
def verify_file(
209-
self, chip: Dict[str, Any], file_name: FileDescriptorOrPath, verbose=False
210-
):
209+
self,
210+
chip: Dict[str, Any],
211+
file_name: FileDescriptorOrPath,
212+
verbose: bool = False,
213+
) -> bool:
211214
"""
212215
Perform a chip full-flash verification from a file that
213216
contains Intel HEX data. Returns True/False on success/fail.
@@ -252,12 +255,12 @@ def verify_file(
252255
self.end()
253256
return True
254257

255-
def read_fuses(self, chip: Dict[str, Any]) -> tuple:
258+
def read_fuses(self, chip: Dict[str, Any]) -> Tuple[int, int, int, int]:
256259
"""
257260
Read the 4 fuses and return them in a tuple (low, high, ext, lock)
258261
Each fuse is bitwise-&'s with the chip's fuse mask for simplicity
259262
"""
260-
mask = chip["fuse_mask"]
263+
mask: Tuple[int, int, int, int] = chip["fuse_mask"]
261264
self.begin(clock=_SLOW_CLOCK)
262265
low = self._transaction((0x50, 0, 0, 0))[2] & mask[0]
263266
high = self._transaction((0x58, 0x08, 0, 0))[2] & mask[1]
@@ -311,7 +314,7 @@ def verify_fuses(
311314
return False
312315
return True
313316

314-
def erase_chip(self):
317+
def erase_chip(self) -> None:
315318
"""
316319
Fully erases the chip.
317320
"""
@@ -340,7 +343,7 @@ def end(self) -> None:
340343
self._spi.unlock()
341344
self._rst.value = True
342345

343-
def read_signature(self) -> list:
346+
def read_signature(self) -> List[int]:
344347
"""
345348
Read and return the signature of the chip as two bytes in an array.
346349
Requires calling begin() beforehand to put in programming mode.
@@ -395,7 +398,7 @@ def _flash_page(
395398
raise RuntimeError("Failed to commit page to flash")
396399
self._busy_wait()
397400

398-
def _transaction(self, command: Tuple[int, int, int, int]) -> bytes:
401+
def _transaction(self, command: Tuple[int, int, int, int]) -> bytearray:
399402
reply = bytearray(4)
400403
command_bytes = bytearray([i & 0xFF for i in command])
401404

0 commit comments

Comments
 (0)