I2C Entity

API Documentation: [cpp] [python] [.NET] [LabVIEW]

BrainStem modules may have the ability to read, write data on up to 2 I2C bus’s

Read

i2c [ index ] . read => (unsigned char) address, (unsigned char) length <= (unsigned char*) data

Reads up to 26 bytes from the i2c bus given by the index. The parameters are the I2C address of the device on the bus, and the number of bytes to read. The result is the data that was read or an error.

Write

i2c [ index ] . write => (unsigned char) address, (unsigned char) length, (unsigned char*) data <= (unsigned char) result

Writes up to 26 bytes to the i2c bus given by the index. The parameters are the I2C address of the device on the bus, the number of bytes to write, and the data to write. The result is the result error condition or none.

Set Pullup

i2c [ index ] . setPullup => (unsigned char) bool

Sets software controlled pullup state on modules which have software controllable pullups. This setting is saved when a call to system.save is made so that Pullup settings on bus 0 can persist.

Code Examples

C++

// All commands return aErr values when errors are encountered and aErrNone on
// success.
char buff[2];
stem.i2c[0].read(0x42, 0x02, buff); // reads two from device with address 0x42.
char wrbuff[] = {0xBE, 0xEF};
stem.i2c[0].write(0x42, 0x02, wrbuff); // writes 0xBEEF to the device with address 0x42
stem.i2c[0].setPullup(true) //enables pullup on bus 0

Reflex

Currently this entity is not available from within the reflex language.

Python

result = stem.i2c[0].read(0x42, 0x02) # reads two bytes from the i2c bus. The value is given in result.value
print result.value
err = stem.i2c[0].write(0x42,0x02, b'\xbe\xef') #  writes b'\xbe\xef' to the i2c bus.
print err
err = stem.i2c[0].setPullup(True)
print err