I2C Bus extension on Fezzer

I have posted an extension to the I2CDevice class that permits the use of different devices : http://www.fezzer.com/project/180/i2c-execute-extension/

Use is quite simple : you declare as many “Configuration” as you wish and you use them in the new Execute() method.

ConfigLCD = new I2CDevice.Configuration((ushort)(0xC6 >> 1), 91);   // LCD device
ConfigEEprom = new I2CDevice.Configuration((ushort)(0xA0 >> 1), 400);  // EEprom device
I2C = new I2CDevice(ConfigLCD);  // Start with the LCD config by default
 
I2CDevice.I2CTransaction[] ActionsLCD = new I2CDevice.I2CTransaction[1];     // Create transaction
ActionsLCD[0] = I2CDevice.CreateWriteTransaction(new byte[2] { 0, (byte)19 });  // Backlight ON
I2C.Execute(ConfigLCD,ActionsLCD, 1000);   // Note the use of the extension, here
 
EEprom_Write(10,231);    // Write value 231 at address 10
Debug.Print("Read : " + EEprom_Read(10).ToString());   // Now read it back

ActionsLCD[0] = I2CDevice.CreateWriteTransaction(new byte[2] { 0, (byte)20 });    // Turn off LCD backlight
I2C.Execute(ActionsLCD, 1000);  // Note the use of the original method, here. Possible since the I2C was created with the same configuration


public static void EEprom_Write(int Address, byte data)    // EEprom write method
{
  var xActions = new I2CDevice.I2CTransaction[1];
  xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF), data });
  I2C.Execute(ConfigEEprom,xActions, 1000);    // Using extension method
  Thread.Sleep(5); // Mandatory after each Write transaction !!!
}
 
public static byte EEprom_Read(int Address)   // EEprom Read method
{
  var Data = new byte[1];
  var xActions = new I2CDevice.I2CTransaction[1];
  xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF) });
  Thread.Sleep(5);   // Mandatory after each Write transaction !!!
  I2C.Execute(ConfigEEprom, xActions, 1000);   // Using extension method
  xActions[0] = I2CDevice.CreateReadTransaction(Data);
  I2C.Execute(ConfigEEprom, xActions, 1000);  // Using extension method
 
  return Data[0];
}

I hope you will find it useful.

I have updated the extension with a new method : Broadcast().

If, like me, you have many similar devices, you may want to send the same command to all of them. For example, cleaning the LCD display or turning on the backlight.

Syntax is pretty simple :

Broadcast(I2CDevice.Configuration[] pConf, I2CDevice.I2CTransaction[] xActions, int timeout)

And so is usage :

ConfigLCD1 = new I2CDevice.Configuration((ushort)(0xC6 >> 1), 91);
ConfigLCD2 = new I2CDevice.Configuration((ushort)(0xC8 >> 1), 91);
I2C = new I2CDevice(ConfigLCD1);

I2CDevice.I2CTransaction[] BroadcastActions = new I2CDevice.I2CTransaction[1];
BroadcastActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0, (byte)19 });   // Turn on backlight
I2C.Broadcast(new I2CDevice.Configuration[] { ConfigLCD1, ConfigLCD2 }, BroadcastActions, 1000);

Thread.Sleep(1000);

I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
xActions[0] = I2CDevice.CreateWriteTransaction(new byte[4] { 0, 3, 3, 1 });
xActions[1] = I2CDevice.CreateWriteTransaction(System.Text.Encoding.UTF8.GetBytes((byte)0 + "Broadcast example..."));   // Write some text on the third line
I2C.Broadcast(new I2CDevice.Configuration[] { ConfigLCD1, ConfigLCD2 }, xActions, 1000);

This is the code that the Panda is running on the video :