Example #1
0
        public int Reset()
        {
            int res;

            if (!_isAvailable)
            {
                return(-1);
            }
            lock (_i2cBus)
                res = I2CNativeLib.i2c_smbus_write_byte(_i2cBus.BusHandle, (byte)SI7021CMD.RESET);
            return(res);
        }
Example #2
0
        public ushort ReadWord(SI7021CMD cmd)
        {
            ushort data = 0;
            int    res;

            byte[] buffer = new byte[3];
            int    count  = 0;

            if (!_isAvailable)
            {
                return(0);
            }
            lock (_i2cBus)
            {
                byte bCMD = (byte)cmd;

                res = I2CNativeLib.i2c_smbus_write_byte(_i2cBus.BusHandle, bCMD);
                if (res < 0)
                {
                    Console.WriteLine("Error write command");
                    return(0);
                }

                //Wait for conversion to be finished ..
                Thread.Sleep(20);
                res = -1;
                while (res < 0 && count < 100)                 //Sometime the sensor still not finish it will return -1..
                {
                    //Use low level I2C read to continue reading until we get data from sensor..
                    //I tried block read but it didn't work.. :P
                    res = I2CNativeLib.ReadBytes(_i2cBus.BusHandle, 0x40, buffer, buffer.Length);

                    //Console.WriteLine("Read number: " + count.ToString() + "Res: " + res.ToString());

                    count++;
                    if (res > 0)
                    {
                        data  = (ushort)((int)buffer[0] << 8);
                        data += buffer[1];
                        break;
                    }
                }
                return(data);
            }
        }