예제 #1
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);
        }
예제 #2
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));
 }