Skip to content

Commit 1afb7c5

Browse files
authored
Merge pull request #33 from tekktrik/doc/update-documentation
Update documentation and minor tweak
2 parents 4573668 + a96b6b2 commit 1afb7c5

File tree

1 file changed

+70
-35
lines changed

1 file changed

+70
-35
lines changed

adafruit_fram.py

Lines changed: 70 additions & 35 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

@@ -55,9 +62,19 @@
5562
class FRAM:
5663
"""
5764
Driver base for the FRAM Breakout.
65+
66+
:param int max_size: The maximum size of the EEPROM
67+
:param bool write_protect: Turns on/off initial write protection
68+
:param DigitalInOut wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
69+
Must be a ``DigitalInOut`` object.
5870
"""
5971

60-
def __init__(self, max_size, write_protect=False, wp_pin=None):
72+
def __init__(
73+
self,
74+
max_size: int,
75+
write_protect: bool = False,
76+
wp_pin: Optional[DigitalInOut] = None,
77+
) -> None:
6178
self._max_size = max_size
6279
self._wp = write_protect
6380
self._wraparound = False
@@ -70,21 +87,21 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
7087
self._wp_pin = wp_pin
7188

7289
@property
73-
def write_wraparound(self):
90+
def write_wraparound(self) -> bool:
7491
"""Determines if sequential writes will wrapaound highest memory address
7592
(``len(FRAM) - 1``) address. If ``False``, and a requested write will
7693
extend beyond the maximum size, an exception is raised.
7794
"""
7895
return self._wraparound
7996

8097
@write_wraparound.setter
81-
def write_wraparound(self, value):
82-
if not value in (True, False):
98+
def write_wraparound(self, value: bool) -> None:
99+
if not isinstance(value, bool):
83100
raise ValueError("Write wraparound must be 'True' or 'False'.")
84101
self._wraparound = value
85102

86103
@property
87-
def write_protected(self):
104+
def write_protected(self) -> bool:
88105
"""The status of write protection. Default value on initialization is
89106
``False``.
90107
@@ -97,7 +114,7 @@ def write_protected(self):
97114
"""
98115
return self._wp if self._wp_pin is None else self._wp_pin.value
99116

100-
def __len__(self):
117+
def __len__(self) -> int:
101118
"""The size of the current FRAM chip. This is one more than the highest
102119
address location that can be read or written to.
103120
@@ -113,7 +130,7 @@ def __len__(self):
113130
"""
114131
return self._max_size
115132

116-
def __getitem__(self, address):
133+
def __getitem__(self, address: Union[int, slice]) -> bytearray:
117134
"""Read the value at the given index, or values in a slice.
118135
119136
.. code-block:: python
@@ -154,7 +171,7 @@ def __getitem__(self, address):
154171

155172
return read_buffer
156173

157-
def __setitem__(self, address, value):
174+
def __setitem__(self, address: Union[int, slice], value: Union[int, Sequence[int]]):
158175
"""Write the value at the given starting index.
159176
160177
.. code-block:: python
@@ -203,28 +220,36 @@ def __setitem__(self, address, value):
203220

204221
self._write(address.start, value, self._wraparound)
205222

206-
def _read_address(self, address, read_buffer):
223+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
207224
# Implemented by subclass
208225
raise NotImplementedError
209226

210-
def _write(self, start_address, data, wraparound):
227+
def _write(
228+
self, start_address: int, data: Union[int, Sequence[int]], wraparound: bool
229+
) -> None:
211230
# Implemened by subclass
212231
raise NotImplementedError
213232

214233

215234
class FRAM_I2C(FRAM):
216235
"""I2C class for FRAM.
217236
218-
:param: ~busio.I2C i2c_bus: The I2C bus the FRAM is connected to.
219-
:param: int address: I2C address of FRAM. Default address is ``0x50``.
220-
:param: bool write_protect: Turns on/off initial write protection.
237+
:param ~busio.I2C i2c_bus: The I2C bus the FRAM is connected to.
238+
:param int address: I2C address of FRAM. Default address is ``0x50``.
239+
:param bool write_protect: Turns on/off initial write protection.
221240
Default is ``False``.
222-
:param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
241+
:param wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
223242
Must be a ``digitalio.DigitalInOut`` object.
224243
"""
225244

226245
# pylint: disable=too-many-arguments
227-
def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
246+
def __init__(
247+
self,
248+
i2c_bus: I2C,
249+
address: int = 0x50,
250+
write_protect: bool = False,
251+
wp_pin: Optional[DigitalInOut] = None,
252+
) -> None:
228253
from adafruit_bus_device.i2c_device import ( # pylint: disable=import-outside-toplevel
229254
I2CDevice as i2cdev,
230255
)
@@ -241,15 +266,20 @@ def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
241266
self._i2c = i2cdev(i2c_bus, address)
242267
super().__init__(_MAX_SIZE_I2C, write_protect, wp_pin)
243268

244-
def _read_address(self, address, read_buffer):
269+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
245270
write_buffer = bytearray(2)
246271
write_buffer[0] = address >> 8
247272
write_buffer[1] = address & 0xFF
248273
with self._i2c as i2c:
249274
i2c.write_then_readinto(write_buffer, read_buffer)
250275
return read_buffer
251276

252-
def _write(self, start_address, data, wraparound=False):
277+
def _write(
278+
self,
279+
start_address: int,
280+
data: Union[int, Sequence[int]],
281+
wraparound: bool = False,
282+
) -> None:
253283
# Decided against using the chip's "Page Write", since that would require
254284
# doubling the memory usage by creating a buffer that includes the passed
255285
# in data so that it can be sent all in one `i2c.write`. The single-write
@@ -283,8 +313,8 @@ def _write(self, start_address, data, wraparound=False):
283313

284314
# pylint: disable=no-member
285315
@FRAM.write_protected.setter
286-
def write_protected(self, value):
287-
if value not in (True, False):
316+
def write_protected(self, value: bool) -> None:
317+
if not isinstance(value, bool):
288318
raise ValueError("Write protected value must be 'True' or 'False'.")
289319
self._wp = value
290320
if not self._wp_pin is None:
@@ -294,25 +324,25 @@ def write_protected(self, value):
294324
class FRAM_SPI(FRAM):
295325
"""SPI class for FRAM.
296326
297-
:param: ~busio.SPI spi_bus: The SPI bus the FRAM is connected to.
298-
:param: ~digitalio.DigitalInOut spi_cs: The SPI CS pin.
299-
:param: bool write_protect: Turns on/off initial write protection.
300-
Default is ``False``.
301-
:param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
302-
Must be a ``digitalio.DigitalInOut`` object.
327+
:param ~busio.SPI spi_bus: The SPI bus the FRAM is connected to.
328+
:param ~digitalio.DigitalInOut spi_cs: The SPI CS pin.
329+
:param bool write_protect: Turns on/off initial write protection.
330+
Default is ``False``.
331+
:param wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
332+
Must be a ``digitalio.DigitalInOut`` object.
303333
:param int baudrate: SPI baudrate to use. Default is ``1000000``.
304334
:param int max_size: Size of FRAM in Bytes. Default is ``8192``.
305335
"""
306336

307337
# pylint: disable=too-many-arguments,too-many-locals
308338
def __init__(
309339
self,
310-
spi_bus,
311-
spi_cs,
312-
write_protect=False,
313-
wp_pin=None,
314-
baudrate=100000,
315-
max_size=_MAX_SIZE_SPI,
340+
spi_bus: SPI,
341+
spi_cs: DigitalInOut,
342+
write_protect: bool = False,
343+
wp_pin: Optional[DigitalInOut] = None,
344+
baudrate: int = 100000,
345+
max_size: int = _MAX_SIZE_SPI,
316346
):
317347
from adafruit_bus_device.spi_device import ( # pylint: disable=import-outside-toplevel
318348
SPIDevice as spidev,
@@ -331,7 +361,7 @@ def __init__(
331361
self._spi = _spi
332362
super().__init__(max_size, write_protect, wp_pin)
333363

334-
def _read_address(self, address, read_buffer):
364+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
335365
write_buffer = bytearray(4)
336366
write_buffer[0] = _SPI_OPCODE_READ
337367
if self._max_size > 0xFFFF:
@@ -347,7 +377,12 @@ def _read_address(self, address, read_buffer):
347377
spi.readinto(read_buffer)
348378
return read_buffer
349379

350-
def _write(self, start_address, data, wraparound=False):
380+
def _write(
381+
self,
382+
start_address: int,
383+
data: Union[int, Sequence[int]],
384+
wraparound: bool = False,
385+
) -> None:
351386
buffer = bytearray(4)
352387
if not isinstance(data, int):
353388
data_length = len(data)
@@ -381,11 +416,11 @@ def _write(self, start_address, data, wraparound=False):
381416
spi.write(bytearray([_SPI_OPCODE_WRDI]))
382417

383418
@FRAM.write_protected.setter
384-
def write_protected(self, value):
419+
def write_protected(self, value: bool) -> None:
385420
# While it is possible to protect block ranges on the SPI chip,
386421
# it seems superfluous to do so. So, block protection always protects
387422
# the entire memory (BP0 and BP1).
388-
if value not in (True, False):
423+
if not isinstance(value, bool):
389424
raise ValueError("Write protected value must be 'True' or 'False'.")
390425
self._wp = value
391426
write_buffer = bytearray(2)

0 commit comments

Comments
 (0)