Skip to content

Commit 8166bbf

Browse files
committed
Device ID now read with I2CDevice; updated docstring for getter/setter
1 parent 4c5a58b commit 8166bbf

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

adafruit_fram.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ def write_protected(self, value):
117117
self._wp_pin.value = value
118118

119119
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+
"""
120133
if isinstance(key, int):
121134
if key > self._max_size:
122135
raise ValueError("Register '{0}' greater than maximum FRAM size."
@@ -137,15 +150,27 @@ def __getitem__(self, key):
137150
return read_buffer
138151

139152
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+
"""
140165
if self.write_protected:
141166
raise RuntimeError("FRAM currently write protected.")
142167

143168
if isinstance(key, int):
144169
if not isinstance(value, int):
145170
raise ValueError("Data must be an integer.")
146-
if key.start > self._max_size:
171+
if key > self._max_size:
147172
raise ValueError("Requested register '{0}' greater than maximum"
148-
" FRAM size. ({1})".format(key.start,
173+
" FRAM size. ({1})".format(key,
149174
self._max_size))
150175

151176
self._write_register(key.start, value)
@@ -184,23 +209,23 @@ class FRAM_I2C(FRAM):
184209
:param: int address: I2C address of FRAM. Default address is ``0x50``.
185210
:param: bool write_protect: Turns on/off initial write protection.
186211
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.
188213
Must be a ``digitalio.DigitalInOut`` object.
189214
"""
190215
#pylint: disable=too-many-arguments
191216
def __init__(self, i2c_bus, address=0x50, write_protect=False,
192217
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
195220
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)
197224
manf_id = (((read_buf[0] << 4) +(read_buf[1] >> 4)))
198225
prod_id = (((read_buf[1] & 0x0F) << 8) + read_buf[2])
199226
if (manf_id != _I2C_MANF_ID) and (prod_id != _I2C_PROD_ID):
200227
raise OSError("FRAM I2C device not found.")
201-
i2c_bus.unlock()
202228

203-
from adafruit_bus_device.i2c_device import I2CDevice as i2cdev
204229
self._i2c = i2cdev(i2c_bus, address)
205230
super().__init__(_MAX_SIZE_I2C, write_protect, wp_pin)
206231

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
77
.. automodule:: adafruit_fram
88
:members:
9+
:special-members: __getitem__, __setitem__

0 commit comments

Comments
 (0)