@@ -117,6 +117,19 @@ def write_protected(self, value):
117
117
self ._wp_pin .value = value
118
118
119
119
def __getitem__ (self , key ):
120
+ """ Read the value at the given index, or values in a slice.
121
+
122
+ .. code-block:: python
123
+
124
+ # read single index
125
+ fram[0]
126
+
127
+ # read values 0 thru 9 with a slice
128
+ fram[0:9]
129
+
130
+ # read every other value from 0 thru 10 using a step
131
+ fram[0:10:2]
132
+ """
120
133
if isinstance (key , int ):
121
134
if key > self ._max_size :
122
135
raise ValueError ("Register '{0}' greater than maximum FRAM size."
@@ -137,15 +150,27 @@ def __getitem__(self, key):
137
150
return read_buffer
138
151
139
152
def __setitem__ (self , key , value ):
153
+ """ Write the value at the given index, or values in a slice.
154
+
155
+ .. code-block:: python
156
+
157
+ # write single index
158
+ fram[0] = 1
159
+
160
+ # write values 0 thru 4 with a slice
161
+ fram[0:4] = [0,1,2,3]
162
+
163
+ .. note:: Slice stepping is not available when writing
164
+ """
140
165
if self .write_protected :
141
166
raise RuntimeError ("FRAM currently write protected." )
142
167
143
168
if isinstance (key , int ):
144
169
if not isinstance (value , int ):
145
170
raise ValueError ("Data must be an integer." )
146
- if key . start > self ._max_size :
171
+ if key > self ._max_size :
147
172
raise ValueError ("Requested register '{0}' greater than maximum"
148
- " FRAM size. ({1})" .format (key . start ,
173
+ " FRAM size. ({1})" .format (key ,
149
174
self ._max_size ))
150
175
151
176
self ._write_register (key .start , value )
@@ -184,23 +209,23 @@ class FRAM_I2C(FRAM):
184
209
:param: int address: I2C address of FRAM. Default address is ``0x50``.
185
210
:param: bool write_protect: Turns on/off initial write protection.
186
211
Default is ``False``.
187
- :param: wp_pin: Physical pin connected to the ``WP`` breakout pin.
212
+ :param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
188
213
Must be a ``digitalio.DigitalInOut`` object.
189
214
"""
190
215
#pylint: disable=too-many-arguments
191
216
def __init__ (self , i2c_bus , address = 0x50 , write_protect = False ,
192
217
wp_pin = None ):
193
- i2c_bus . try_lock ()
194
- i2c_bus . writeto (( 0xF8 >> 1 ), bytearray ([( address << 1 )]), stop = False )
218
+ from adafruit_bus_device . i2c_device import I2CDevice as i2cdev
219
+ dev_id_addr = 0xF8 >> 1
195
220
read_buf = bytearray (3 )
196
- i2c_bus .readfrom_into ((0xF9 >> 1 ), read_buf )
221
+ with i2cdev (i2c_bus , dev_id_addr ) as dev_id :
222
+ dev_id .write (bytearray ([(address << 1 )]), stop = False )
223
+ dev_id .readinto (read_buf )
197
224
manf_id = (((read_buf [0 ] << 4 ) + (read_buf [1 ] >> 4 )))
198
225
prod_id = (((read_buf [1 ] & 0x0F ) << 8 ) + read_buf [2 ])
199
226
if (manf_id != _I2C_MANF_ID ) and (prod_id != _I2C_PROD_ID ):
200
227
raise OSError ("FRAM I2C device not found." )
201
- i2c_bus .unlock ()
202
228
203
- from adafruit_bus_device .i2c_device import I2CDevice as i2cdev
204
229
self ._i2c = i2cdev (i2c_bus , address )
205
230
super ().__init__ (_MAX_SIZE_I2C , write_protect , wp_pin )
206
231
0 commit comments