Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add protocol details to both Accessor docstrings. Remove buffer lengt…
…h comment. use slices instead loop. set end for i2c.write().
  • Loading branch information
FoamyGuy committed Oct 1, 2025
commit f4861a758a638b125a1312fa2577e7805c399326
20 changes: 10 additions & 10 deletions adafruit_register/register_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def _pack_address_into_buffer(self, address):
class SPIRegisterAccessor(RegisterAccessor):
"""
RegisterAccessor class for SPI bus transport. Provides interface to read/write
registers over SPI.
registers over SPI. This class automatically handles the R/W bit by setting the
highest bit of the address to 1 when reading and 0 when writing. For multi-byte
addresses the R/W bit will be set as the highest bit in the first byte of the
address.

:param SPIDevice spi_device: The SPI bus device to communicate over.
:param int address_width: The number of bytes in the address
Expand All @@ -80,7 +83,6 @@ def read_register(self, address: int, buffer: bytearray):

:param int address: The register address to read.
:param bytearray buffer: Buffer that will be used to read register data into.
Buffer must be long enough to be read all data sent by the device.
:return: None
"""

Expand Down Expand Up @@ -112,7 +114,9 @@ def write_register(
class I2CRegisterAccessor(RegisterAccessor):
"""
RegisterAccessor class for I2C bus transport. Provides interface to read/write
registers over I2C
registers over I2C. This class uses `adafruit_bus_device.I2CDevice` for
communication. I2CDevice automatically handles the R/W bit by setting
the lowest bit of the device address to 1 for reading and 0 for writing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, explain the protocol over I2C that this supports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also talk about the relationship between address and data.

:param I2CDevice i2c_device: I2C device to communicate over
:param int address_width: The number of bytes in the address
Expand All @@ -131,7 +135,6 @@ def read_register(self, address: int, buffer: bytearray):

:param int address: The register address to read.
:param bytearray buffer: Buffer that will be used to read register data into.
Buffer must be long enough to be read all data sent by the device.
:return: None
"""

Expand All @@ -145,7 +148,6 @@ def write_register(self, address: int, buffer: bytearray):

:param int address: The register address to read.
:param bytearray buffer: Buffer of data that will be written to the register.

:return: None
"""
# grow full buffer if needed
Expand All @@ -154,12 +156,10 @@ def write_register(self, address: int, buffer: bytearray):

# put address into full buffer
self._pack_address_into_buffer(address)
for i in range(self.address_width):
self._full_buffer[i] = self.address_buffer[i]
self._full_buffer[: self.address_width] = self.address_buffer

# put data into full buffer
for i, b in enumerate(buffer):
self._full_buffer[i + self.address_width] = b
self._full_buffer[self.address_width : self.address_width + len(buffer)] = buffer

with self.i2c_device as i2c:
i2c.write(self._full_buffer)
i2c.write(self._full_buffer, end=self.address_width + len(buffer))