Skip to content

Commit bfe4c39

Browse files
committed
Completing type annotations
1 parent 907ab2d commit bfe4c39

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

adafruit_avrprog.py

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

3535
try:
3636
from io import TextIOWrapper
37-
from typing import Any, Dict, Optional
37+
from typing import Any, Dict, Optional, Tuple
3838

3939
from _typeshed import FileDescriptorOrPath
4040
from busio import SPI
4141
except ImportError:
4242
pass
4343

44+
from math import floor
45+
4446
from digitalio import DigitalInOut, Direction
4547

4648
_SLOW_CLOCK: int = 100000
@@ -324,14 +326,14 @@ def begin(self, clock: int = _FAST_CLOCK) -> None:
324326
self._spi.configure(baudrate=clock)
325327
self._transaction((0xAC, 0x53, 0, 0))
326328

327-
def end(self):
329+
def end(self) -> None:
328330
"""
329331
End programming mode: SPI is released, and reset pin set high.
330332
"""
331333
self._spi.unlock()
332334
self._rst.value = True
333335

334-
def read_signature(self):
336+
def read_signature(self) -> list:
335337
"""
336338
Read and return the signature of the chip as two bytes in an array.
337339
Requires calling begin() beforehand to put in programming mode.
@@ -342,7 +344,7 @@ def read_signature(self):
342344
sig.append(self._transaction((0x30, 0, i, 0))[2])
343345
return sig
344346

345-
def read(self, addr, read_buffer):
347+
def read(self, addr: int, read_buffer: bytearray) -> None:
346348
"""
347349
Read a chunk of memory from address 'addr'. The amount read is the
348350
same as the size of the bytearray 'read_buffer'. Data read is placed
@@ -366,13 +368,15 @@ def read(self, addr, read_buffer):
366368
last_addr = read_addr
367369

368370
#################### Low level
369-
def _flash_word(self, addr, low, high):
371+
def _flash_word(self, addr: int, low: int, high: int) -> None:
370372
self._transaction((0x40, addr >> 8, addr, low))
371373
self._transaction((0x48, addr >> 8, addr, high))
372374

373-
def _flash_page(self, page_buffer, page_addr, page_size):
375+
def _flash_page(
376+
self, page_buffer: bytearray, page_addr: int, page_size: int
377+
) -> None:
374378
page_addr //= 2 # address is by 'words' not bytes!
375-
for i in range(page_size / 2): # page indexed by words, not bytes
379+
for i in range(floor(page_size / 2)): # page indexed by words, not bytes
376380
lo_byte, hi_byte = page_buffer[2 * i : 2 * i + 2]
377381
self._flash_word(i, lo_byte, hi_byte)
378382

@@ -384,23 +388,25 @@ def _flash_page(self, page_buffer, page_addr, page_size):
384388
raise RuntimeError("Failed to commit page to flash")
385389
self._busy_wait()
386390

387-
def _transaction(self, command):
391+
def _transaction(self, command: Tuple[int, int, int, int]) -> bytes:
388392
reply = bytearray(4)
389-
command = bytearray([i & 0xFF for i in command])
393+
command_bytes = bytearray([i & 0xFF for i in command])
390394

391-
self._spi.write_readinto(command, reply)
392-
# s = [hex(i) for i in command]
393-
# print("Sending %s reply %s" % ([hex(i) for i in command], [hex(i) for i in reply]))
394-
if reply[2] != command[1]:
395+
self._spi.write_readinto(command_bytes, reply)
396+
# s = [hex(i) for i in command_bytes]
397+
# print("Sending %s reply %s" % ([hex(i) for i in command_bytes], [hex(i) for i in reply]))
398+
if reply[2] != command_bytes[1]:
395399
raise RuntimeError("SPI transaction failed")
396400
return reply[1:] # first byte is ignored
397401

398-
def _busy_wait(self):
402+
def _busy_wait(self) -> None:
399403
while self._transaction((0xF0, 0, 0, 0))[2] & 0x01:
400404
pass
401405

402406

403-
def read_hex_page(file_state, page_addr, page_size, page_buffer):
407+
def read_hex_page(
408+
file_state: Dict[str, Any], page_addr: int, page_size: int, page_buffer: bytearray
409+
) -> bool:
404410
# pylint: disable=too-many-branches
405411
"""
406412
Helper function that does the Intel Hex parsing. Takes in a dictionary

0 commit comments

Comments
 (0)