31
31
# imports
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 , SPI
38
+ except ImportError :
39
+ pass
40
+
34
41
__version__ = "0.0.0-auto.0"
35
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FRAM.git"
36
43
@@ -57,7 +64,7 @@ class FRAM:
57
64
Driver base for the FRAM Breakout.
58
65
"""
59
66
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 :
61
68
self ._max_size = max_size
62
69
self ._wp = write_protect
63
70
self ._wraparound = False
@@ -70,21 +77,21 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
70
77
self ._wp_pin = wp_pin
71
78
72
79
@property
73
- def write_wraparound (self ):
80
+ def write_wraparound (self ) -> bool :
74
81
"""Determines if sequential writes will wrapaound highest memory address
75
82
(``len(FRAM) - 1``) address. If ``False``, and a requested write will
76
83
extend beyond the maximum size, an exception is raised.
77
84
"""
78
85
return self ._wraparound
79
86
80
87
@write_wraparound .setter
81
- def write_wraparound (self , value ):
82
88
if not value in (True , False ):
89
+ def write_wraparound (self , value : bool ) -> None :
83
90
raise ValueError ("Write wraparound must be 'True' or 'False'." )
84
91
self ._wraparound = value
85
92
86
93
@property
87
- def write_protected (self ):
94
+ def write_protected (self ) -> bool :
88
95
"""The status of write protection. Default value on initialization is
89
96
``False``.
90
97
@@ -97,7 +104,7 @@ def write_protected(self):
97
104
"""
98
105
return self ._wp if self ._wp_pin is None else self ._wp_pin .value
99
106
100
- def __len__ (self ):
107
+ def __len__ (self ) -> int :
101
108
"""The size of the current FRAM chip. This is one more than the highest
102
109
address location that can be read or written to.
103
110
@@ -113,7 +120,7 @@ def __len__(self):
113
120
"""
114
121
return self ._max_size
115
122
116
- def __getitem__ (self , address ) :
123
+ def __getitem__ (self , address : Union [ int , slice ]) -> bytearray :
117
124
"""Read the value at the given index, or values in a slice.
118
125
119
126
.. code-block:: python
@@ -154,7 +161,7 @@ def __getitem__(self, address):
154
161
155
162
return read_buffer
156
163
157
- def __setitem__ (self , address , value ):
164
+ def __setitem__ (self , address : Union [ int , slice ], value : Union [ int , Sequence [ int ]] ):
158
165
"""Write the value at the given starting index.
159
166
160
167
.. code-block:: python
@@ -203,11 +210,11 @@ def __setitem__(self, address, value):
203
210
204
211
self ._write (address .start , value , self ._wraparound )
205
212
206
- def _read_address (self , address , read_buffer ) :
213
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
207
214
# Implemented by subclass
208
215
raise NotImplementedError
209
216
210
- def _write (self , start_address , data , wraparound ) :
217
+ def _write (self , start_address : int , data : Union [ int , Sequence [ int ]], wraparound : bool ) -> None :
211
218
# Implemened by subclass
212
219
raise NotImplementedError
213
220
@@ -224,7 +231,7 @@ class FRAM_I2C(FRAM):
224
231
"""
225
232
226
233
# 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 :
228
235
from adafruit_bus_device .i2c_device import ( # pylint: disable=import-outside-toplevel
229
236
I2CDevice as i2cdev ,
230
237
)
@@ -241,15 +248,15 @@ def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
241
248
self ._i2c = i2cdev (i2c_bus , address )
242
249
super ().__init__ (_MAX_SIZE_I2C , write_protect , wp_pin )
243
250
244
- def _read_address (self , address , read_buffer ) :
251
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
245
252
write_buffer = bytearray (2 )
246
253
write_buffer [0 ] = address >> 8
247
254
write_buffer [1 ] = address & 0xFF
248
255
with self ._i2c as i2c :
249
256
i2c .write_then_readinto (write_buffer , read_buffer )
250
257
return read_buffer
251
258
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 :
253
260
# Decided against using the chip's "Page Write", since that would require
254
261
# doubling the memory usage by creating a buffer that includes the passed
255
262
# 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):
283
290
284
291
# pylint: disable=no-member
285
292
@FRAM .write_protected .setter
286
- def write_protected (self , value ):
287
293
if value not in (True , False ):
294
+ def write_protected (self , value : bool ) -> None :
288
295
raise ValueError ("Write protected value must be 'True' or 'False'." )
289
296
self ._wp = value
290
297
if not self ._wp_pin is None :
@@ -307,12 +314,12 @@ class FRAM_SPI(FRAM):
307
314
# pylint: disable=too-many-arguments,too-many-locals
308
315
def __init__ (
309
316
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 ,
316
323
):
317
324
from adafruit_bus_device .spi_device import ( # pylint: disable=import-outside-toplevel
318
325
SPIDevice as spidev ,
@@ -331,7 +338,7 @@ def __init__(
331
338
self ._spi = _spi
332
339
super ().__init__ (max_size , write_protect , wp_pin )
333
340
334
- def _read_address (self , address , read_buffer ) :
341
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
335
342
write_buffer = bytearray (4 )
336
343
write_buffer [0 ] = _SPI_OPCODE_READ
337
344
if self ._max_size > 0xFFFF :
@@ -347,7 +354,7 @@ def _read_address(self, address, read_buffer):
347
354
spi .readinto (read_buffer )
348
355
return read_buffer
349
356
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 :
351
358
buffer = bytearray (4 )
352
359
if not isinstance (data , int ):
353
360
data_length = len (data )
@@ -381,11 +388,11 @@ def _write(self, start_address, data, wraparound=False):
381
388
spi .write (bytearray ([_SPI_OPCODE_WRDI ]))
382
389
383
390
@FRAM .write_protected .setter
384
- def write_protected (self , value ) :
391
+ def write_protected (self , value : bool ) -> None :
385
392
# While it is possible to protect block ranges on the SPI chip,
386
393
# it seems superfluous to do so. So, block protection always protects
387
394
# the entire memory (BP0 and BP1).
388
- if value not in ( True , False ):
395
+ if not isinstance ( value , bool ):
389
396
raise ValueError ("Write protected value must be 'True' or 'False'." )
390
397
self ._wp = value
391
398
write_buffer = bytearray (2 )
0 commit comments