31
31
import time
32
32
from micropython import const
33
33
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
+
34
41
__version__ = "0.0.0-auto.0"
35
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_24LC32.git"
36
43
40
47
class EEPROM :
41
48
"""
42
49
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.
43
55
"""
44
56
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 :
46
63
self ._max_size = max_size
47
64
self ._wp = write_protect
48
65
self ._wraparound = False
@@ -55,35 +72,39 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
55
72
self ._wp_pin = wp_pin
56
73
57
74
@property
58
- def write_wraparound (self ):
75
+ def write_wraparound (self ) -> bool :
59
76
"""Determines if sequential writes will wrapaound highest memory address
60
77
(``len(EEPROM) - 1``) address. If ``False``, and a requested write will
61
78
extend beyond the maximum size, an exception is raised.
62
79
"""
63
80
return self ._wraparound
64
81
65
82
@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 ):
68
85
raise ValueError ("Write wraparound must be 'True' or 'False'." )
69
86
self ._wraparound = value
70
87
71
88
@property
72
- def write_protected (self ):
89
+ def write_protected (self ) -> bool :
73
90
"""The status of write protection. Default value on initialization is
74
91
``False``.
92
+
75
93
When a ``WP`` pin is supplied during initialization, or using
76
94
``write_protect_pin``, the status is tied to that pin and enables
77
95
hardware-level protection.
96
+
78
97
When no ``WP`` pin is supplied, protection is only at the software
79
98
level in this library.
80
99
"""
81
100
return self ._wp if self ._wp_pin is None else self ._wp_pin .value
82
101
83
- def __len__ (self ):
102
+ def __len__ (self ) -> int :
84
103
"""The size of the current EEPROM chip. This is one more than the highest
85
104
address location that can be read or written to.
105
+
86
106
.. code-block:: python
107
+
87
108
eeprom = adafruit_24lc32.EEPROM_I2C()
88
109
# size returned by len()
89
110
len(eeprom)
@@ -92,9 +113,11 @@ def __len__(self):
92
113
"""
93
114
return self ._max_size
94
115
95
- def __getitem__ (self , address ) :
116
+ def __getitem__ (self , address : Union [ int , slice ]) -> bytearray :
96
117
"""Read the value at the given index, or values in a slice.
118
+
97
119
.. code-block:: python
120
+
98
121
# read single index
99
122
eeprom[0]
100
123
# read values 0 thru 9 with a slice
@@ -130,9 +153,13 @@ def __getitem__(self, address):
130
153
131
154
return read_buffer
132
155
133
- def __setitem__ (self , address , value ):
156
+ def __setitem__ (
157
+ self , address : Union [int , slice ], value : Union [int , Sequence [int ]]
158
+ ) -> None :
134
159
"""Write the value at the given starting index.
160
+
135
161
.. code-block:: python
162
+
136
163
# write single index
137
164
eeprom[0] = 1
138
165
# write values 0 thru 4 with a list
@@ -176,43 +203,58 @@ def __setitem__(self, address, value):
176
203
177
204
self ._write (address .start , value , self ._wraparound )
178
205
179
- def _read_address (self , address , read_buffer ) :
206
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
180
207
# Implemented by subclass
181
208
raise NotImplementedError
182
209
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 :
184
213
# Implemened by subclass
185
214
raise NotImplementedError
186
215
187
216
188
217
class EEPROM_I2C (EEPROM ):
189
218
"""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.
196
226
"""
197
227
198
228
# 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 :
200
236
from adafruit_bus_device .i2c_device import ( # pylint: disable=import-outside-toplevel
201
237
I2CDevice as i2cdev ,
202
238
)
203
239
204
240
self ._i2c = i2cdev (i2c_bus , address )
205
241
super ().__init__ (_MAX_SIZE_I2C , write_protect , wp_pin )
206
242
207
- def _read_address (self , address , read_buffer ) :
243
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
208
244
write_buffer = bytearray (2 )
209
245
write_buffer [0 ] = address >> 8
210
246
write_buffer [1 ] = address & 0xFF
211
247
with self ._i2c as i2c :
212
248
i2c .write_then_readinto (write_buffer , read_buffer )
213
249
return read_buffer
214
250
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
+
216
258
# Decided against using the chip's "Page Write", since that would require
217
259
# doubling the memory usage by creating a buffer that includes the passed
218
260
# 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):
248
290
249
291
# pylint: disable=no-member
250
292
@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 ):
253
295
raise ValueError ("Write protected value must be 'True' or 'False'." )
254
296
self ._wp = value
255
297
if not self ._wp_pin is None :
0 commit comments