Skip to content

Commit 74c265d

Browse files
committed
Annotation and hinting for BitbangIO
1 parent e91d861 commit 74c265d

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

adafruit_bitbangio.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def deinit(self) -> None:
8484
class I2C(_BitBangIO):
8585
"""Software-based implementation of the I2C protocol over GPIO pins."""
8686

87-
def __init__(self, scl: Pin, sda: Pin, *, frequency: int = 400000, timeout: float = 1) -> None:
87+
def __init__(
88+
self, scl: Pin, sda: Pin, *, frequency: int = 400000, timeout: float = 1
89+
) -> None:
8890
"""Initialize bitbang (or software) based I2C. Must provide the I2C
8991
clock, and data pin numbers.
9092
"""
@@ -122,14 +124,28 @@ def scan(self) -> List[int]:
122124
found.append(address)
123125
return found
124126

125-
def writeto(self, address: int, buffer: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
127+
def writeto(
128+
self,
129+
address: int,
130+
buffer: ReadableBuffer,
131+
*,
132+
start: int = 0,
133+
end: Optional[int] = None
134+
) -> None:
126135
"""Write data from the buffer to an address"""
127136
if end is None:
128137
end = len(buffer)
129138
if self._check_lock():
130139
self._write(address, buffer[start:end], True)
131140

132-
def readfrom_into(self, address: int, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
141+
def readfrom_into(
142+
self,
143+
address: int,
144+
buffer: WriteableBuffer,
145+
*,
146+
start: int = 0,
147+
end: Optional[int] = None
148+
) -> None:
133149
"""Read data from an address and into the buffer"""
134150
if end is None:
135151
end = len(buffer)
@@ -204,7 +220,7 @@ def _repeated_start(self) -> None:
204220
self._sda_low()
205221
self._wait()
206222

207-
def _write_byte(self, byte: int ) -> bool:
223+
def _write_byte(self, byte: int) -> bool:
208224
for bit_position in range(8):
209225
self._scl_low()
210226

@@ -283,7 +299,9 @@ def _read(self, address: int, length: int) -> bytearray:
283299
class SPI(_BitBangIO):
284300
"""Software-based implementation of the SPI protocol over GPIO pins."""
285301

286-
def __init__(self, clock: Pin, MOSI: Optional[Pin] = None, MISO: Optional[Pin] = None) -> None:
302+
def __init__(
303+
self, clock: Pin, MOSI: Optional[Pin] = None, MISO: Optional[Pin] = None
304+
) -> None:
287305
"""Initialize bit bang (or software) based SPI. Must provide the SPI
288306
clock, and optionally MOSI and MISO pin numbers. If MOSI is set to None
289307
then writes will be disabled and fail with an error, likewise for MISO
@@ -320,7 +338,14 @@ def deinit(self) -> None:
320338
if self._mosi:
321339
self._mosi.deinit()
322340

323-
def configure(self, *, baudrate: int = 100000, polarity: Literal[0, 1] = 0, phase: Literal[0, 1] = 0, bits: int = 8) -> None:
341+
def configure(
342+
self,
343+
*,
344+
baudrate: int = 100000,
345+
polarity: Literal[0, 1] = 0,
346+
phase: Literal[0, 1] = 0,
347+
bits: int = 8
348+
) -> None:
324349
"""Configures the SPI bus. Only valid when locked."""
325350
if self._check_lock():
326351
if not isinstance(baudrate, int):
@@ -345,7 +370,9 @@ def _wait(self, start: Optional[int] = None) -> float:
345370
pass
346371
return monotonic() # Return current time
347372

348-
def write(self, buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None) -> None:
373+
def write(
374+
self, buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None
375+
) -> None:
349376
"""Write the data contained in buf. Requires the SPI being locked.
350377
If the buffer is empty, nothing happens.
351378
"""
@@ -377,7 +404,13 @@ def write(self, buffer: ReadableBuffer, start: int = 0, end: Optional[int] = Non
377404
self._sclk.value = self._polarity
378405

379406
# pylint: disable=too-many-branches
380-
def readinto(self, buffer: WriteableBuffer, start: int = 0, end: Optional[int] = None, write_value: int = 0) -> None:
407+
def readinto(
408+
self,
409+
buffer: WriteableBuffer,
410+
start: int = 0,
411+
end: Optional[int] = None,
412+
write_value: int = 0,
413+
) -> None:
381414
"""Read into the buffer specified by buf while writing zeroes. Requires the SPI being
382415
locked. If the number of bytes to read is 0, nothing happens.
383416
"""

0 commit comments

Comments
 (0)