public IEnumerable <byte> FindDevices() { using (var xHandle = OpenController()) { Console.WriteLine("Device opened!"); for (byte i = 0; i < 128; i++) { var xResult = LibC.Ioctl(xHandle, Consts.I2C_Slave, new IntPtr(i)); if (xResult != 0) { continue; } try { var xByteRead = I2CDevice.DoReadByte(xHandle); } catch { continue; } yield return(i); } } }
internal static byte DoReadByte(SafeFileHandle handle) { var xBuff = new byte[1]; var xRead = LibC.Read(handle, xBuff, xBuff.Length); return(xBuff[0]); }
public override I2CDeviceBase OpenDevice(ushort number) { var xHandle = OpenController(); var xError = LibC.Ioctl(xHandle, Consts.I2C_Slave, new IntPtr(number)); if (xError != 0) { throw new InvalidOperationException("Ioctl returned " + xError); } return(new I2CDevice(xHandle, number)); }
private SafeFileHandle OpenController() { var xHandle = LibC.Open("/dev/i2c-" + mNumber, Consts.OPEN_READ_WRITE); if (xHandle < 0) { throw new InvalidOperationException("Open return " + xHandle); } var xSafeHandle = new SafeFileHandle(new IntPtr(xHandle), true); return(xSafeHandle); }
public override unsafe void WriteRead(byte[] buff) { fixed(byte *xBufferPointer = &buff[0]) { var xMessage = new I2CMessage(); xMessage.Length = (ushort)buff.Length; xMessage.Buffer = xBufferPointer; xMessage.SlaveAddress = mDeviceAddress; I2CMessage *xMessagePointer = &xMessage; var xMessageContainer = new I2CReadWriteData(); xMessageContainer.Messages = xMessagePointer; xMessageContainer.MessageCount = 1; I2CReadWriteData *xMessageContainerPtr = &xMessageContainer; var xResult = LibC.Ioctl(mHandle, Consts.I2C_RDWR, new IntPtr(xMessageContainerPtr)); if (xResult < 0) { throw new InvalidOperationException("Ioctl returned " + xResult); } } }
public override void Write(byte[] buff) { var xRead = LibC.Write(mHandle, buff, buff.Length); }