Skip to content

Commit

Permalink
allow multibyte write-then-read and is backcompatible if you pass in …
Browse files Browse the repository at this point in the history
…a byte instead of a bytearray
  • Loading branch information
ladyada committed Dec 1, 2019
1 parent 6f4976d commit 90e4dcb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Adafruit_PureIO/smbus.py
Expand Up @@ -204,13 +204,25 @@ def read_i2c_block_data(self, addr, cmd, length=32):
"""
assert self._device is not None, 'Bus must be opened before operations are made against it!'
# Build ctypes values to marshall between ioctl and Python.
reg = c_uint8(cmd)

# convert register into bytearray
if not isinstance(cmd, (bytes, bytearray)):
reg = cmd # backup
cmd = bytearray(1)
cmd[0] = reg

cmdstring = create_string_buffer(len(cmd))
for i in range(len(cmd)):
cmdstring[i] = cmd[i]

result = create_string_buffer(length)

# Build ioctl request.
request = make_i2c_rdwr_data([
(addr, 0, 1, pointer(reg)), # Write cmd register.
(addr, I2C_M_RD, length, cast(result, POINTER(c_uint8))) # Read data.
])
(addr, 0, len(cmd), cast(cmdstring, POINTER(c_uint8))), # Write cmd register.
(addr, I2C_M_RD, length, cast(result, POINTER(c_uint8))) # Read data.
])

# Make ioctl call and return result data.
ioctl(self._device.fileno(), I2C_RDWR, request)
return bytearray(result.raw) # Use .raw instead of .value which will stop at a null byte!
Expand Down

0 comments on commit 90e4dcb

Please sign in to comment.