using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using System.Text; namespace CPF { class I2C { public static I2CDevice g400I2C = new I2CDevice(null); private static I2CDevice.Configuration configTH = new I2CDevice.Configuration(0x40, 400); //device address is 0x40 or 1000000 (7 bit address) private static I2CDevice.Configuration configP = new I2CDevice.Configuration(0x5D, 400); private static byte[] evk7Chars = new byte[100]; private static I2CDevice.Configuration evk7I2CConfig = new I2CDevice.Configuration(0x42, 100); private static StringBuilder sbTemp = new StringBuilder(100); public static byte[] readEVK7() { g400I2C.Config = evk7I2CConfig; // Create transactions // We need 2 in this example, we are reading from the device // First transaction is writing the "read command" // Second transaction is reading the data I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[2] { 0xFD, 0xFE }; xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[2]; xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus using a timeout of one second // if the execute command returns zero, the transaction failed (this // is a good check to make sure that you are communicating with the device correctly // and don’t have a wiring issue or other problem with the I2C device) if (I2C.g400I2C.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } else { Debug.Print("Register value: " + RegisterValue[0].ToString() + " " + RegisterValue[1]); } // create write buffer (we need one byte) byte[] ffAddr = new byte[1] { 0xFF }; xActions[0] = I2CDevice.CreateWriteTransaction(ffAddr); // create read buffer to read the register byte[] ffValue = new byte[1]; xActions[1] = I2CDevice.CreateReadTransaction(ffValue); int evk7CharsIndex = 0; Array.Clear(evk7Chars, 0, evk7Chars.Length); if (I2C.g400I2C.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } else { if (evk7CharsIndex == 0) { if (ffValue[0] == 36) { evk7Chars[evk7CharsIndex] = ffValue[0]; evk7CharsIndex = evk7CharsIndex + 1; } } else { evk7Chars[evk7CharsIndex] = ffValue[0]; evk7CharsIndex = evk7CharsIndex + 1; if (ffValue[0] == 10) { sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(evk7Chars)); Debug.Print(sbTemp.ToString()); evk7CharsIndex = 0; } } } return (evk7Chars); } } }