The pylibi2c module provides a Pythonic interface to the I2C library.
I2CDevice Class
Initialize a device object:
I2CDevice(bus, addr, tenbit=False, iaddr_bytes=1, page_bytes=8, delay=1, flags=0)
Arguments:
bus (required): The bus path (e.g., '/dev/i2c-0').addr (required): The I2C slave address.tenbit (optional): True for 10-bit addressing, False for 7-bit (default: False).iaddr_bytes (optional): Number of bytes for the internal address (default: 1).page_bytes (optional): Max bytes per page (default: 8).delay (optional): Internal operation delay in ms (default: 1).flags (optional): I2C flags (default: 0).
Attributes (Setter/Getter):
tenbit, delay, flags, page_bytes, and iaddr_bytes can be modified after initialization.
Methods
write(iaddr, buf): Writes data to the device at the specified internal address iaddr. buf must be a read-only type.ioctl_read(iaddr, len): Performs an ioctl read from the internal address iaddr for len bytes.
import pylibi2c
# Initialize
i2c = pylibi2c.I2CDevice('/dev/i2c-0', 0x50, iaddr_bytes=2)
# Configure attributes
i2c.delay = 10
i2c.page_bytes = 16
i2c.flags = pylibi2c.I2C_M_IGNORE_NAK
# Write (buf must be read-only)
size = i2c.write(0x0, b'data')
# Read
data = i2c.ioctl_read(0x0, 256)