コード例 #1
0
        public static bool TrySetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum, bool value)
        {
            byte oldValue;

            if (!device.TryGetRegister(config, timeout, register, out oldValue))
            {
                return(false);
            }

            byte newValue = (value == true ? (byte)(oldValue | (1 << bitNum)) : (byte)(oldValue & ~(1 << bitNum)));

            return(device.TrySetRegister(config, timeout, register, newValue));
        }
コード例 #2
0
        public static bool TryGetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum, out bool result)
        {
            byte value;

            if (!device.TryGetRegister(config, timeout, register, out value))
            {
                result = false;
                return(false);
            }

            result = (value & (1 << bitNum)) > 0;

            return(true);
        }
コード例 #3
0
        public static bool TrySetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte startBit, byte length, byte value)
        {
            byte oldValue;

            if (!device.TryGetRegister(config, timeout, register, out oldValue))
            {
                return(false);
            }
            byte mask = (byte)(((1 << length) - 1) << (startBit - length + 1));

            value   <<= (startBit - length + 1);
            value    &= mask;
            oldValue &= (byte)~mask;
            oldValue |= value;
            return(device.TrySetRegister(config, timeout, register, oldValue));
        }
コード例 #4
0
        public static bool TryGetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitStart, byte length, out byte data)
        {
            byte value;

            if (!device.TryGetRegister(config, timeout, register, out value))
            {
                data = 0;
                return(false);
            }
            byte mask = (byte)(((1 << length) - 1) << (bitStart - length + 1));

            value  &= mask;
            value >>= (bitStart - length + 1);

            data = value;
            return(true);
        }