Skip to content

Commit 430b8aa

Browse files
committed
Add type annotations
1 parent 4573668 commit 430b8aa

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

adafruit_fram.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
# imports
3232
from micropython import const
3333

34+
try:
35+
from typing import Optional, Union, Sequence
36+
from digitalio import DigitalInOut
37+
from busio import I2C, SPI
38+
except ImportError:
39+
pass
40+
3441
__version__ = "0.0.0-auto.0"
3542
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FRAM.git"
3643

@@ -57,7 +64,7 @@ class FRAM:
5764
Driver base for the FRAM Breakout.
5865
"""
5966

60-
def __init__(self, max_size, write_protect=False, wp_pin=None):
67+
def __init__(self, max_size: int, write_protect: bool = False, wp_pin: Optional[DigitalInOut] = None) -> None:
6168
self._max_size = max_size
6269
self._wp = write_protect
6370
self._wraparound = False
@@ -70,21 +77,21 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
7077
self._wp_pin = wp_pin
7178

7279
@property
73-
def write_wraparound(self):
80+
def write_wraparound(self) -> bool:
7481
"""Determines if sequential writes will wrapaound highest memory address
7582
(``len(FRAM) - 1``) address. If ``False``, and a requested write will
7683
extend beyond the maximum size, an exception is raised.
7784
"""
7885
return self._wraparound
7986

8087
@write_wraparound.setter
81-
def write_wraparound(self, value):
8288
if not value in (True, False):
89+
def write_wraparound(self, value: bool) -> None:
8390
raise ValueError("Write wraparound must be 'True' or 'False'.")
8491
self._wraparound = value
8592

8693
@property
87-
def write_protected(self):
94+
def write_protected(self) -> bool:
8895
"""The status of write protection. Default value on initialization is
8996
``False``.
9097
@@ -97,7 +104,7 @@ def write_protected(self):
97104
"""
98105
return self._wp if self._wp_pin is None else self._wp_pin.value
99106

100-
def __len__(self):
107+
def __len__(self) -> int:
101108
"""The size of the current FRAM chip. This is one more than the highest
102109
address location that can be read or written to.
103110
@@ -113,7 +120,7 @@ def __len__(self):
113120
"""
114121
return self._max_size
115122

116-
def __getitem__(self, address):
123+
def __getitem__(self, address: Union[int, slice]) -> bytearray:
117124
"""Read the value at the given index, or values in a slice.
118125
119126
.. code-block:: python
@@ -154,7 +161,7 @@ def __getitem__(self, address):
154161

155162
return read_buffer
156163

157-
def __setitem__(self, address, value):
164+
def __setitem__(self, address: Union[int, slice], value: Union[int, Sequence[int]]):
158165
"""Write the value at the given starting index.
159166
160167
.. code-block:: python
@@ -203,11 +210,11 @@ def __setitem__(self, address, value):
203210

204211
self._write(address.start, value, self._wraparound)
205212

206-
def _read_address(self, address, read_buffer):
213+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
207214
# Implemented by subclass
208215
raise NotImplementedError
209216

210-
def _write(self, start_address, data, wraparound):
217+
def _write(self, start_address: int, data: Union[int, Sequence[int]], wraparound: bool) -> None:
211218
# Implemened by subclass
212219
raise NotImplementedError
213220

@@ -224,7 +231,7 @@ class FRAM_I2C(FRAM):
224231
"""
225232

226233
# pylint: disable=too-many-arguments
227-
def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
234+
def __init__(self, i2c_bus: I2C, address: int = 0x50, write_protect: bool = False, wp_pin: Optional[DigitalInOut] = None) -> None:
228235
from adafruit_bus_device.i2c_device import ( # pylint: disable=import-outside-toplevel
229236
I2CDevice as i2cdev,
230237
)
@@ -241,15 +248,15 @@ def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
241248
self._i2c = i2cdev(i2c_bus, address)
242249
super().__init__(_MAX_SIZE_I2C, write_protect, wp_pin)
243250

244-
def _read_address(self, address, read_buffer):
251+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
245252
write_buffer = bytearray(2)
246253
write_buffer[0] = address >> 8
247254
write_buffer[1] = address & 0xFF
248255
with self._i2c as i2c:
249256
i2c.write_then_readinto(write_buffer, read_buffer)
250257
return read_buffer
251258

252-
def _write(self, start_address, data, wraparound=False):
259+
def _write(self, start_address: int, data: Union[int, Sequence[int]], wraparound: bool = False) -> None:
253260
# Decided against using the chip's "Page Write", since that would require
254261
# doubling the memory usage by creating a buffer that includes the passed
255262
# in data so that it can be sent all in one `i2c.write`. The single-write
@@ -283,8 +290,8 @@ def _write(self, start_address, data, wraparound=False):
283290

284291
# pylint: disable=no-member
285292
@FRAM.write_protected.setter
286-
def write_protected(self, value):
287293
if value not in (True, False):
294+
def write_protected(self, value: bool) -> None:
288295
raise ValueError("Write protected value must be 'True' or 'False'.")
289296
self._wp = value
290297
if not self._wp_pin is None:
@@ -307,12 +314,12 @@ class FRAM_SPI(FRAM):
307314
# pylint: disable=too-many-arguments,too-many-locals
308315
def __init__(
309316
self,
310-
spi_bus,
311-
spi_cs,
312-
write_protect=False,
313-
wp_pin=None,
314-
baudrate=100000,
315-
max_size=_MAX_SIZE_SPI,
317+
spi_bus: SPI,
318+
spi_cs: DigitalInOut,
319+
write_protect: bool = False,
320+
wp_pin: Optional[DigitalInOut] = None,
321+
baudrate: int = 100000,
322+
max_size: int = _MAX_SIZE_SPI,
316323
):
317324
from adafruit_bus_device.spi_device import ( # pylint: disable=import-outside-toplevel
318325
SPIDevice as spidev,
@@ -331,7 +338,7 @@ def __init__(
331338
self._spi = _spi
332339
super().__init__(max_size, write_protect, wp_pin)
333340

334-
def _read_address(self, address, read_buffer):
341+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
335342
write_buffer = bytearray(4)
336343
write_buffer[0] = _SPI_OPCODE_READ
337344
if self._max_size > 0xFFFF:
@@ -347,7 +354,7 @@ def _read_address(self, address, read_buffer):
347354
spi.readinto(read_buffer)
348355
return read_buffer
349356

350-
def _write(self, start_address, data, wraparound=False):
357+
def _write(self, start_address: int, data: Union[int, Sequence[int]], wraparound: bool = False) -> None:
351358
buffer = bytearray(4)
352359
if not isinstance(data, int):
353360
data_length = len(data)
@@ -381,11 +388,11 @@ def _write(self, start_address, data, wraparound=False):
381388
spi.write(bytearray([_SPI_OPCODE_WRDI]))
382389

383390
@FRAM.write_protected.setter
384-
def write_protected(self, value):
391+
def write_protected(self, value: bool) -> None:
385392
# While it is possible to protect block ranges on the SPI chip,
386393
# it seems superfluous to do so. So, block protection always protects
387394
# the entire memory (BP0 and BP1).
388-
if value not in (True, False):
395+
if not isinstance(value, bool):
389396
raise ValueError("Write protected value must be 'True' or 'False'.")
390397
self._wp = value
391398
write_buffer = bytearray(2)

0 commit comments

Comments
 (0)