I2cBus
I2C Bus is a way to transfer data between 2 digital circuits. There is typically one master circuit that is in charge of initiating transfers. And then there are one or more slaves on the bus. The BrainPad is always the bus master. A slave can be an accelerometer or motor controller.
When a master talks to a slave, it needs to know the slave’s address. Each slave must have its own unique address. The master then commands the addressed slave to either read or write data.
var i2c = I2cbus(0x43);
i2c = I2cbus(0x43)
Like all elements, I2C works with Out() and In(). The Out() writes a single byte to a slave. BrainBot, for example, has headlights that can be controlled using 4 values sent over I2C. The slave address of the robot is 0x01, and then the 4 values must be 0x01 followed by the three color levels (Red, Green, Blue). Do not confuse the 0x01 in the first sent value with the slave address, which happens to also be 0x01.
This example will use the Red (reflected as 0xff), while keeping the Green and Blue in the off state (which is 0x00). By the way, these hex numbers are just how codes are used to present values. You can simply use 0 instead of 0x00 and 255 instead of 0xff.
var i2c = I2cBus(0x01);
var headlightData = new double[4];
headlightData[0] = 0x01;
headlightData[1] = 0xff;
headlightData[2] = 0x00;
headlightData[3] = 0x00;
Out(i2c, headlightData);
i2c = I2cBus(0x01)
headlightData = bytearray(4)
headlightData[0] = 0x01
headlightData[1] = 0xff
headlightData[2] = 0x00
headlightData[3] = 0x00
Out(i2c, headlightData)

OutIn()
The OutIn() is special from Out() and In() as it always handles and expects data as bytes. This is automated in Python as it is dynamic typed but not in C#. See OutIn() Lesson for more details.
var x = I2cbus(0x01);
var headlightData = new byte[4];
headlightData[0] = 0x01;
headlightData[1] = 0xff;
headlightData[2] = 0x00;
headlightData[3] = 0x00;
OutIn(i2cBus, headlightData, null);
i2c = I2cBus(0x01)
headlightData = bytearray(4)
headlightData[0] = 0x01
headlightData[1] = 0xff
headlightData[2] = 0x00
headlightData[3] = 0x00
Out(i2c, headlightData, null)
Restart Condition
Every I2C transaction starts with a START condition, then the slave’s address, then data, and finally the STOP condition. Some sensors require a continuous write-read transaction with no STOP in the middle. Those transactions require have a RESTART condition instead. In this case, using Out() flowed by In() will not work, an OutIn() is needed instead.
BrainStorm
Where is the “bus” in I2C bus? Data is delivered down the wire by one as a stream of data. In computer world, this is called a data bus.