Skip to content

Commit b710d97

Browse files
authored
Merge pull request #11 from tekktrik/doc/add-typing
Add typing and other tweaks
2 parents c5275bd + 62aff88 commit b710d97

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

adafruit_24lc32.py

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
import time
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
38+
except ImportError:
39+
pass
40+
3441
__version__ = "0.0.0-auto.0"
3542
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_24LC32.git"
3643

@@ -40,9 +47,19 @@
4047
class EEPROM:
4148
"""
4249
Driver base for the EEPROM Breakout.
50+
51+
:param int max_size: The maximum size of the EEPROM
52+
:param bool write_protect: Turns on/off initial write protection
53+
:param DigitalInOut wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
54+
Must be a ``DigitalInOut`` object.
4355
"""
4456

45-
def __init__(self, max_size, write_protect=False, wp_pin=None):
57+
def __init__(
58+
self,
59+
max_size: int,
60+
write_protect: bool = False,
61+
wp_pin: Optional[DigitalInOut] = None,
62+
) -> None:
4663
self._max_size = max_size
4764
self._wp = write_protect
4865
self._wraparound = False
@@ -55,35 +72,39 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
5572
self._wp_pin = wp_pin
5673

5774
@property
58-
def write_wraparound(self):
75+
def write_wraparound(self) -> bool:
5976
"""Determines if sequential writes will wrapaound highest memory address
6077
(``len(EEPROM) - 1``) address. If ``False``, and a requested write will
6178
extend beyond the maximum size, an exception is raised.
6279
"""
6380
return self._wraparound
6481

6582
@write_wraparound.setter
66-
def write_wraparound(self, value):
67-
if not value in (True, False):
83+
def write_wraparound(self, value: bool) -> None:
84+
if not isinstance(value, bool):
6885
raise ValueError("Write wraparound must be 'True' or 'False'.")
6986
self._wraparound = value
7087

7188
@property
72-
def write_protected(self):
89+
def write_protected(self) -> bool:
7390
"""The status of write protection. Default value on initialization is
7491
``False``.
92+
7593
When a ``WP`` pin is supplied during initialization, or using
7694
``write_protect_pin``, the status is tied to that pin and enables
7795
hardware-level protection.
96+
7897
When no ``WP`` pin is supplied, protection is only at the software
7998
level in this library.
8099
"""
81100
return self._wp if self._wp_pin is None else self._wp_pin.value
82101

83-
def __len__(self):
102+
def __len__(self) -> int:
84103
"""The size of the current EEPROM chip. This is one more than the highest
85104
address location that can be read or written to.
105+
86106
.. code-block:: python
107+
87108
eeprom = adafruit_24lc32.EEPROM_I2C()
88109
# size returned by len()
89110
len(eeprom)
@@ -92,9 +113,11 @@ def __len__(self):
92113
"""
93114
return self._max_size
94115

95-
def __getitem__(self, address):
116+
def __getitem__(self, address: Union[int, slice]) -> bytearray:
96117
"""Read the value at the given index, or values in a slice.
118+
97119
.. code-block:: python
120+
98121
# read single index
99122
eeprom[0]
100123
# read values 0 thru 9 with a slice
@@ -130,9 +153,13 @@ def __getitem__(self, address):
130153

131154
return read_buffer
132155

133-
def __setitem__(self, address, value):
156+
def __setitem__(
157+
self, address: Union[int, slice], value: Union[int, Sequence[int]]
158+
) -> None:
134159
"""Write the value at the given starting index.
160+
135161
.. code-block:: python
162+
136163
# write single index
137164
eeprom[0] = 1
138165
# write values 0 thru 4 with a list
@@ -176,43 +203,58 @@ def __setitem__(self, address, value):
176203

177204
self._write(address.start, value, self._wraparound)
178205

179-
def _read_address(self, address, read_buffer):
206+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
180207
# Implemented by subclass
181208
raise NotImplementedError
182209

183-
def _write(self, start_address, data, wraparound):
210+
def _write(
211+
self, start_address: int, data: Union[int, Sequence[int]], wraparound: bool
212+
) -> None:
184213
# Implemened by subclass
185214
raise NotImplementedError
186215

187216

188217
class EEPROM_I2C(EEPROM):
189218
"""I2C class for EEPROM.
190-
:param: ~busio.I2C i2c_bus: The I2C bus the EEPROM is connected to.
191-
:param: int address: I2C address of EEPROM. Default address is ``0x50``.
192-
:param: bool write_protect: Turns on/off initial write protection.
193-
Default is ``False``.
194-
:param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
195-
Must be a ``digitalio.DigitalInOut`` object.
219+
220+
:param ~busio.I2C i2c_bus: The I2C bus the EEPROM is connected to.
221+
:param int address: I2C address of EEPROM. Default address is ``0x50``.
222+
:param bool write_protect: Turns on/off initial write protection.
223+
Default is ``False``.
224+
:param wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
225+
Must be a ``DigitalInOut`` object.
196226
"""
197227

198228
# pylint: disable=too-many-arguments
199-
def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
229+
def __init__(
230+
self,
231+
i2c_bus: I2C,
232+
address: int = 0x50,
233+
write_protect: bool = False,
234+
wp_pin: Optional[DigitalInOut] = None,
235+
) -> None:
200236
from adafruit_bus_device.i2c_device import ( # pylint: disable=import-outside-toplevel
201237
I2CDevice as i2cdev,
202238
)
203239

204240
self._i2c = i2cdev(i2c_bus, address)
205241
super().__init__(_MAX_SIZE_I2C, write_protect, wp_pin)
206242

207-
def _read_address(self, address, read_buffer):
243+
def _read_address(self, address: int, read_buffer: bytearray) -> bytearray:
208244
write_buffer = bytearray(2)
209245
write_buffer[0] = address >> 8
210246
write_buffer[1] = address & 0xFF
211247
with self._i2c as i2c:
212248
i2c.write_then_readinto(write_buffer, read_buffer)
213249
return read_buffer
214250

215-
def _write(self, start_address, data, wraparound=False):
251+
def _write(
252+
self,
253+
start_address: int,
254+
data: Union[int, Sequence[int]],
255+
wraparound: bool = False,
256+
) -> None:
257+
216258
# Decided against using the chip's "Page Write", since that would require
217259
# doubling the memory usage by creating a buffer that includes the passed
218260
# in data so that it can be sent all in one `i2c.write`. The single-write
@@ -248,8 +290,8 @@ def _write(self, start_address, data, wraparound=False):
248290

249291
# pylint: disable=no-member
250292
@EEPROM.write_protected.setter
251-
def write_protected(self, value):
252-
if value not in (True, False):
293+
def write_protected(self, value: bool) -> None:
294+
if not isinstance(value, bool):
253295
raise ValueError("Write protected value must be 'True' or 'False'.")
254296
self._wp = value
255297
if not self._wp_pin is None:

0 commit comments

Comments
 (0)