Skip to content

Commit aaaf8ea

Browse files
committed
Minor tweaks thanks to @deshipu.
1 parent 4ee852d commit aaaf8ea

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

adafruit_register/i2c_struct_array.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
class _BoundStructArray:
3737
"""
38-
Actual array object that `StructArray` constructs on demand for
38+
Array object that `StructArray` constructs on demand.
3939
4040
:param object obj: The device object to bind to. It must have a `i2c_device` attribute
4141
:param int register_address: The register address to read the bit from
@@ -48,9 +48,9 @@ def __init__(self, obj, register_address, struct_format, count):
4848
self.obj = obj
4949
self.count = count
5050

51-
def _header(self, index):
51+
def _get_buffer(self, index):
5252
"""Shared bounds checking and buffer creation."""
53-
if index < 0 or index >= self.count:
53+
if not 0 <= index < self.count:
5454
raise IndexError()
5555
size = struct.calcsize(self.format)
5656
# We create the buffer every time instead of keeping the buffer (which is 32 bytes at least)
@@ -60,14 +60,14 @@ def _header(self, index):
6060
return buf
6161

6262
def __getitem__(self, index):
63-
buf = self._header(index)
63+
buf = self._get_buffer(index)
6464
with self.obj.i2c_device:
6565
self.obj.i2c_device.write(buf, end=1, stop=False)
6666
self.obj.i2c_device.readinto(buf, start=1)
6767
return struct.unpack_from(self.format, buf, offset=1)
6868

6969
def __setitem__(self, index, value):
70-
buf = self._header(index)
70+
buf = self._get_buffer(index)
7171
struct.pack_into(self.format, buf, 1, *value)
7272
with self.obj.i2c_device:
7373
self.obj.i2c_device.write(buf)
@@ -84,6 +84,9 @@ class StructArray:
8484
Values are tuples that map to the values in the defined struct. See struct
8585
module documentation for struct format string and its possible value types.
8686
87+
.. note:: This assumes the device addresses correspond to 8-bit bytes. This is not suitable for
88+
devices with registers of other widths such as 16-bit.
89+
8790
:param int register_address: The register address to begin reading the array from
8891
:param str struct_format: The struct format string for this register.
8992
:param int count: Number of elements in the array
@@ -92,7 +95,7 @@ def __init__(self, register_address, struct_format, count):
9295
self.format = struct_format
9396
self.address = register_address
9497
self.count = count
95-
self.array_id = "_structarray" + str(register_address)
98+
self.array_id = "_structarray{}".format(register_address)
9699

97100
def __get__(self, obj, objtype=None):
98101
# We actually can't handle the indexing ourself due to data descriptor limits. So, we return
@@ -103,6 +106,3 @@ def __get__(self, obj, objtype=None):
103106
setattr(obj, self.array_id,
104107
_BoundStructArray(obj, self.address, self.format, self.count))
105108
return getattr(obj, self.array_id)
106-
107-
def __set__(self, obj, value):
108-
raise RuntimeError()

0 commit comments

Comments
 (0)