Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        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);
                }
            }
        }