예제 #1
0
        /// <summary>
        /// Gets the current sensor reading from the device.
        /// </summary>
        /// <returns>Returns an ISensorReading instance that
        /// indicates current temperature and whether or not
        /// the thresholds have been exceeded.</returns>
        public Task <IDeviceSensorReading> ReadSensor()
        {
            // ***
            // *** Initialize a value to hold the device temperature
            // *** in Celsius
            // ***
            DeviceSensorReading returnValue = new DeviceSensorReading();

            // ***
            // *** The register/command for reading ambient temperature
            // *** from the device is 0x05
            // ***
            byte[] readBuffer = this.ReadFromRegister(Mcp9808Register.AmbientTemperature);

            // ***
            // *** Calculate the temperature value
            // ***
            returnValue.Temperature = RegisterConverter.ToFloat(readBuffer);

            // ***
            // *** Check the flags
            // ***
            returnValue.IsCritical            = (readBuffer[0] & 0x80) == 0x80;
            returnValue.IsAboveUpperThreshold = (readBuffer[0] & 0x40) == 0x40;
            returnValue.IsBelowLowerThreshold = (readBuffer[0] & 0x20) == 0x20;

            // ***
            // *** Return the value
            // ***
            return(Task <IDeviceSensorReading> .FromResult((IDeviceSensorReading)returnValue));
        }
예제 #2
0
        /// <summary>
        /// Sets the value of a configuration bit at the given
        /// index.
        /// </summary>
        /// <param name="bitIndex">Specifies the configuration
        /// bit index from 0 to 15.</param>
        /// <param name="value">The value of the bit to set.</param>
        public virtual void SetConfigurationBit(int bitIndex, bool value)
        {
            // ***
            // *** Read the current configuration
            // ***
            byte[] config = this.ReadFromRegister(Mcp9808Register.Configuration);

            // ***
            // *** Set the selected bit
            // ***
            config[bitIndex > 7 ? 0 : 1] = RegisterConverter.SetBit(config[bitIndex > 7 ? 0 : 1], bitIndex % 8, value);

            // ***
            // *** Write the configuration back to the register
            // ***
            this.WriteToRegister(Mcp9808Register.Configuration, config[0], config[1]);
        }
예제 #3
0
        /// <summary>
        /// Sets a bit in the given position of a byte to the give
        /// value.
        /// </summary>
        /// <param name="value">The byte value to be modified.</param>
        /// <param name="bitIndex">The index of the bit to modify. This
        /// value can be 0 to 7.</param>
        /// <param name="bit">The value to be set in the byte.</param>
        /// <returns>Returns the modified byte.</returns>
        public static byte SetBit(byte value, int bitIndex, bool bit)
        {
            if (bitIndex < 0 || bitIndex > 7)
            {
                throw new ArgumentOutOfRangeException(nameof(bitIndex));
            }
            byte mask = (byte)Math.Pow(2, bitIndex);

            if (bit && RegisterConverter.BitIsLow(value, bitIndex))
            {
                value += mask;
            }
            else if (!bit && RegisterConverter.BitIsHigh(value, bitIndex))
            {
                value -= mask;
            }

            return(value);
        }
예제 #4
0
 /// <summary>
 /// Gets the value of the configuration bit at
 /// the given index.
 /// </summary>
 /// <param name="bitIndex">Specifies the configuration
 /// bit index from 0 to 15.</param>
 /// <returns>Returns true if the bit is High and false
 /// if the bit is low.</returns>
 public virtual bool GetConfigurationBit(int bitIndex)
 {
     byte[] buffer = this.ReadFromRegister(Mcp9808Register.Configuration);
     return(RegisterConverter.BitIsHigh(buffer[bitIndex > 7 ? 0 : 1], bitIndex));
 }