Skip to content

Commit 12afa4b

Browse files
committed
Working on adding type annotations
1 parent 0b23c3d commit 12afa4b

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

adafruit_avrprog.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@
3232
__version__ = "0.0.0+auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AVRprog.git"
3434

35-
from digitalio import Direction, DigitalInOut
35+
try:
36+
from io import TextIOWrapper
37+
from typing import Any, Dict, Optional
3638

37-
_SLOW_CLOCK = 100000
38-
_FAST_CLOCK = 1000000
39+
from _typeshed import FileDescriptorOrPath
40+
from busio import SPI
41+
except ImportError:
42+
pass
43+
44+
from digitalio import DigitalInOut, Direction
45+
46+
_SLOW_CLOCK: int = 100000
47+
_FAST_CLOCK: int = 1000000
3948

4049

4150
class AVRprog:
@@ -86,10 +95,10 @@ class Boards:
8695
"fuse_mask": (0xFF, 0xFF, 0x07, 0x3F),
8796
}
8897

89-
_spi = None
90-
_rst = None
98+
_spi: SPI = None
99+
_rst: DigitalInOut = None
91100

92-
def init(self, spi_bus, rst_pin):
101+
def init(self, spi_bus: SPI, rst_pin) -> None:
93102
"""
94103
Initialize the programmer with an SPI port that will be used to
95104
communicate with the chip. Make sure your SPI supports 'write_readinto'
@@ -100,7 +109,7 @@ def init(self, spi_bus, rst_pin):
100109
self._rst.direction = Direction.OUTPUT
101110
self._rst.value = True
102111

103-
def verify_sig(self, chip, verbose=False):
112+
def verify_sig(self, chip: Dict[str, Any], verbose: bool = False) -> bool:
104113
"""
105114
Verify that the chip is connected properly, responds to commands,
106115
and has the correct signature. Returns True/False based on success
@@ -115,7 +124,13 @@ def verify_sig(self, chip, verbose=False):
115124
return True
116125

117126
# pylint: disable=too-many-branches
118-
def program_file(self, chip, file_name, verbose=False, verify=True):
127+
def program_file(
128+
self,
129+
chip: Dict[str, Any],
130+
file_name: FileDescriptorOrPath,
131+
verbose: bool = False,
132+
verify: bool = True,
133+
) -> bool:
119134
"""
120135
Perform a chip erase and program from a file that
121136
contains Intel HEX data. Returns true on verify-success, False on
@@ -132,10 +147,8 @@ def program_file(self, chip, file_name, verbose=False, verify=True):
132147
self.begin(clock=clock_speed)
133148

134149
# create a file state dictionary
135-
file_state = {"line": 0, "ext_addr": 0, "eof": False}
136-
with open(file_name, "r") as file_state[ # pylint: disable=unspecified-encoding
137-
"f"
138-
]:
150+
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": TextIOWrapper}
151+
with open(file_name, "r") as file_state["f"]:
139152
page_size = chip["page_size"]
140153

141154
for page_addr in range(0, chip["flash_size"], page_size):
@@ -183,7 +196,9 @@ def program_file(self, chip, file_name, verbose=False, verify=True):
183196
self.end()
184197
return True
185198

186-
def verify_file(self, chip, file_name, verbose=False):
199+
def verify_file(
200+
self, chip: Dict[str, Any], file_name: FileDescriptorOrPath, verbose=False
201+
):
187202
"""
188203
Perform a chip full-flash verification from a file that
189204
contains Intel HEX data. Returns True/False on success/fail.
@@ -192,10 +207,8 @@ def verify_file(self, chip, file_name, verbose=False):
192207
raise RuntimeError("Signature read failure")
193208

194209
# create a file state dictionary
195-
file_state = {"line": 0, "ext_addr": 0, "eof": False}
196-
with open(file_name, "r") as file_name[ # pylint: disable=unspecified-encoding
197-
"f"
198-
]:
210+
file_state = {"line": 0, "ext_addr": 0, "eof": False, "f": TextIOWrapper}
211+
with open(file_name, "r") as file_name["f"]:
199212
page_size = chip["page_size"]
200213
clock_speed = chip.get("clock_speed", _FAST_CLOCK)
201214
self.begin(clock=clock_speed)
@@ -230,9 +243,9 @@ def verify_file(self, chip, file_name, verbose=False):
230243
self.end()
231244
return True
232245

233-
def read_fuses(self, chip):
246+
def read_fuses(self, chip: Dict[str, Any]) -> tuple:
234247
"""
235-
Read the 4 fuses and return them in a list (low, high, ext, lock)
248+
Read the 4 fuses and return them in a tuple (low, high, ext, lock)
236249
Each fuse is bitwise-&'s with the chip's fuse mask for simplicity
237250
"""
238251
mask = chip["fuse_mask"]
@@ -245,7 +258,14 @@ def read_fuses(self, chip):
245258
return (low, high, ext, lock)
246259

247260
# pylint: disable=unused-argument,too-many-arguments
248-
def write_fuses(self, chip, low=None, high=None, ext=None, lock=None):
261+
def write_fuses(
262+
self,
263+
chip: Dict[str, Any],
264+
low: Optional[int] = None,
265+
high: Optional[int] = None,
266+
ext: Optional[int] = None,
267+
lock: Optional[int] = None,
268+
) -> None:
249269
"""
250270
Write any of the 4 fuses. If the kwarg low/high/ext/lock is not
251271
passed in or is None, that fuse is skipped
@@ -260,7 +280,14 @@ def write_fuses(self, chip, low=None, high=None, ext=None, lock=None):
260280
self.end()
261281

262282
# pylint: disable=too-many-arguments
263-
def verify_fuses(self, chip, low=None, high=None, ext=None, lock=None):
283+
def verify_fuses(
284+
self,
285+
chip: Dict[str, Any],
286+
low: Optional[int] = None,
287+
high: Optional[int] = None,
288+
ext: Optional[int] = None,
289+
lock: Optional[int] = None,
290+
) -> bool:
264291
"""
265292
Verify the 4 fuses. If the kwarg low/high/ext/lock is not
266293
passed in or is None, that fuse is not checked.
@@ -286,7 +313,7 @@ def erase_chip(self):
286313

287314
#################### Mid level
288315

289-
def begin(self, clock=_FAST_CLOCK):
316+
def begin(self, clock: int = _FAST_CLOCK) -> None:
290317
"""
291318
Begin programming mode: pull reset pin low, initialize SPI, and
292319
send the initialization command to get the AVR's attention.

0 commit comments

Comments
 (0)