Skip to content
Jennifer Programming Language

i2c

The I2C (Inter-IC) bus (/dev/i2c-1, ...). Linux-only; a stub elsewhere and on jennifer-tiny.

jennifer
use i2c;

def bus as i2c.Bus init i2c.open("/dev/i2c-1", 0x76);   # 7-bit slave address
def id as bytes init i2c.readReg($bus, 0xd0, 1);        # read register 0xd0
i2c.close($bus);
CallReturnsNotes
i2c.open(path, addr)i2c.BusOpens the bus and selects the 7-bit slave addr (0..127; the usable range is 0x08..0x77).
i2c.read(bus, n)bytesReads n raw bytes from the selected slave.
i2c.write(bus, data)intWrites raw bytes; returns the count written.
i2c.readReg(bus, reg, n)bytesWrites the 1-byte register pointer reg, then reads n bytes (the common "set register, read back").
i2c.writeReg(bus, reg, data)intWrites reg followed by data in one transaction; returns the data-byte count.
i2c.close(bus)nullCloses the bus.

Slave selection is the I2C_SLAVE ioctl - the reason a Go library is needed rather than plain fs. Blocking; compose with spawn.

See also

serial, spi, gpio, fs.