max1704x_smbus.i2c_device

I²C device communication interface.

This module provides the I2CDevice class, which abstracts the underlying I²C backend to offer a consistent interface for reading from and writing to a device at a given bus and address. It is intended both for direct low-level transactions and as a base for higher-level device drivers.

Examples

Instantiate the I²C device on bus 1 at address 0x36, and read 1 byte from register 0x04:

>>> from i2c_device import I2CDevice
>>> dev = I2CDevice(1, 0x36)
>>> data = dev.read(0x04)
class max1704x_smbus.i2c_device.I2CDevice(i2c_bus, address, probe=True)

Thin abstraction layer for accessing an I²C device via an SMBus-compatible backend.

This class wraps an SMBus-compatible instance to provide an interface for reading from and writing to a specific I²C device address. It handles device addressing and delegates the actual bus operations to the underlying backend, allowing higher-level drivers to remain agnostic of the specific SMBus implementation in use.

__init__(i2c_bus, address, probe=True)

Initialize an I²C device interface.

Parameters:
  • i2c_bus (int) – I²C bus number to open. An SMBus instance will be created internally for this bus.

  • address (int) – 7-bit I²C address of the target device.

  • probe (bool, optional) – If True (default), the device will be probed after initialization to verify its presence.

read(register, length=1)

Read one or two bytes from the device starting at the given register.

This method reads from the I²C device to retrieve raw data from the specified register address. The number of bytes to read is limited to 1 or 2.

Parameters:
  • register (int) – Register address to read from.

  • length (int, optional) – Number of bytes to read. Must be 1 or 2 (default is 1).

Returns:

Data read from the device.

Return type:

list[int]

Raises:

ValueError – If length is not 1 or 2.

write(register, data)

Write one or two bytes to the I²C device starting at the given register.

This method writes to the I²C device to store raw data at the specified register address. The number of bytes to write is limited to 1 or 2.

Parameters:
  • register (int) – Register address to write to.

  • data (list[int]) – Bytes (as integer list) to write to the device. Must be 1 or 2 bytes.

Raises:

ValueError – If data is not 1 or 2 bytes.

Return type:

None