Skip to content

Commit 8e12133

Browse files
committed
review feedback changes
1 parent f6d41b6 commit 8e12133

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

adafruit_avrprog.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,47 @@
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AVRprog.git"
3434

3535
try:
36+
from typing import Any, Dict, List, Optional, Tuple, Union, TypedDict
37+
from typing_extensions import TypeAlias
3638
from os import PathLike
37-
from typing import Any, Dict, List, Optional, Tuple, TypeAlias, Union
39+
from busio import SPI
40+
from microcontroller import Pin
3841

3942
# Technically this type should come from: from _typeshed import FileDescriptorOrPath
4043
# Unfortunately _typeshed is only in the standard library in newer releases of Python, e.g. 3.11
4144
# Thus have to define a placeholder
42-
FileDescriptorOrPath: TypeAlias = (
43-
int | str | bytes | PathLike[str] | PathLike[bytes]
44-
)
45+
FileDescriptorOrPath: TypeAlias = Union[
46+
int, str, bytes, PathLike[str], PathLike[bytes]
47+
]
48+
4549
from io import TextIOWrapper
4650

51+
class ChipDictionary(TypedDict):
52+
"""
53+
Dictionary representing a specific target chip type
54+
"""
55+
56+
name: str
57+
sig: List[int]
58+
flash_size: int
59+
page_size: int
60+
fuse_mask: Tuple[int]
61+
62+
class FileState(TypedDict):
63+
"""
64+
Dictionary representing a File State
65+
"""
66+
67+
# pylint: disable=invalid-name
68+
line: int
69+
ext_addr: int
70+
eof: bool
71+
f: Optional[TextIOWrapper]
72+
4773
except ImportError:
4874
pass
4975

50-
from math import floor
5176

52-
from busio import I2C, SPI
5377
from digitalio import DigitalInOut, Direction
5478

5579
_SLOW_CLOCK: int = 100000
@@ -104,10 +128,10 @@ class Boards:
104128
"fuse_mask": (0xFF, 0xFF, 0x07, 0x3F),
105129
}
106130

107-
_spi: SPI = None
108-
_rst: DigitalInOut = None
131+
_spi: Optional[SPI] = None
132+
_rst: Optional[DigitalInOut] = None
109133

110-
def init(self, spi_bus: SPI, rst_pin: Union[SPI, I2C]) -> None:
134+
def init(self, spi_bus: SPI, rst_pin: Pin) -> None:
111135
"""
112136
Initialize the programmer with an SPI port that will be used to
113137
communicate with the chip. Make sure your SPI supports 'write_readinto'
@@ -118,7 +142,7 @@ def init(self, spi_bus: SPI, rst_pin: Union[SPI, I2C]) -> None:
118142
self._rst.direction = Direction.OUTPUT
119143
self._rst.value = True
120144

121-
def verify_sig(self, chip: Dict[str, Any], verbose: bool = False) -> bool:
145+
def verify_sig(self, chip: ChipDictionary, verbose: bool = False) -> bool:
122146
"""
123147
Verify that the chip is connected properly, responds to commands,
124148
and has the correct signature. Returns True/False based on success
@@ -156,7 +180,7 @@ def program_file(
156180
self.begin(clock=clock_speed)
157181

158182
# create a file state dictionary
159-
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": TextIOWrapper}
183+
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": None}
160184
with open(file_name, "r") as file_state["f"]:
161185
page_size = chip["page_size"]
162186

@@ -219,7 +243,7 @@ def verify_file(
219243
raise RuntimeError("Signature read failure")
220244

221245
# create a file state dictionary
222-
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": TextIOWrapper}
246+
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": None}
223247
with open(file_name, "r") as file_state["f"]:
224248
page_size = chip["page_size"]
225249
clock_speed = chip.get("clock_speed", _FAST_CLOCK)
@@ -386,7 +410,7 @@ def _flash_page(
386410
self, page_buffer: bytearray, page_addr: int, page_size: int
387411
) -> None:
388412
page_addr //= 2 # address is by 'words' not bytes!
389-
for i in range(floor(page_size / 2)): # page indexed by words, not bytes
413+
for i in range(page_size / 2): # page indexed by words, not bytes
390414
lo_byte, hi_byte = page_buffer[2 * i : 2 * i + 2]
391415
self._flash_word(i, lo_byte, hi_byte)
392416

0 commit comments

Comments
 (0)