Esempio n. 1
0
        /// <summary>
        ///     Create a new TMP102 object using the default configuration for the sensor.
        /// </summary>
        /// <param name="address">I2C address of the sensor.</param>
        /// <param name="speed">Speed of the communication with the sensor.</param>
        public TMP102(byte address = 0x48, ushort speed = 100, ushort updateInterval = MinimumPollingPeriod,
                      float temperatureChangeNotificationThreshold = 0.001F)
        {
            if ((speed < 10) || (speed > 1000))
            {
                throw new ArgumentOutOfRangeException(nameof(speed), "Speed should be 10 KHz to 3,400 KHz.");
            }
            if (temperatureChangeNotificationThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(temperatureChangeNotificationThreshold), "Temperature threshold should be >= 0");
            }
            if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update period should be 0 or >= than " + MinimumPollingPeriod);
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            _updateInterval = updateInterval;

            _tmp102 = new I2cBus(address, speed);
            var configuration = _tmp102.ReadRegisters(0x01, 2);

            _sensorResolution = (configuration[1] & 0x10) > 0 ?
                                Resolution.Resolution13Bits : Resolution.Resolution12Bits;
            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Make a temperature and humidity reading.
        /// </summary>
        public void Update()
        {
            _si7021.WriteByte(Registers.MeasureHumidityNoHold);
            //
            //  Maximum conversion time is 12ms (page 5 of the datasheet).
            //
            Thread.Sleep(25);
            var data            = _si7021.ReadBytes(3);
            var humidityReading = (ushort)((data[0] << 8) + data[1]);

            Humidity = ((125 * (float)humidityReading) / 65536) - 6;
            if (Humidity < 0)
            {
                Humidity = 0;
            }
            else
            {
                if (Humidity > 100)
                {
                    Humidity = 100;
                }
            }
            data = _si7021.ReadRegisters(Registers.ReadPreviousTemperatureMeasurement, 2);
            var temperatureReading = (short)((data[0] << 8) + data[1]);

            Temperature = (float)(((175.72 * temperatureReading) / 65536) - 46.85);
        }
Esempio n. 3
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevanyt properties.
        /// </summary>
        public void Read()
        {
            var controlRegister = _mag3110.ReadRegister((byte)Registers.Control1);

            controlRegister |= 0x02;
            _mag3110.WriteRegister((byte)Registers.Control1, controlRegister);
            var data = _mag3110.ReadRegisters((byte)Registers.XMSB, 6);

            X = (short)((data[0] << 8) | data[1]);
            Y = (short)((data[2] << 8) | data[3]);
            Z = (short)((data[4] << 8) | data[5]);
        }
Esempio n. 4
0
        /// <summary>
        ///     Update the Temperature property.
        /// </summary>
        public void Update()
        {
            var temperatureData = _tmp102.ReadRegisters(0x00, 2);
            var sensorReading   = 0;

            if (SensorResolution == Resolution.Resolution12Bits)
            {
                sensorReading = (temperatureData[0] << 4) | (temperatureData[1] >> 4);
            }
            else
            {
                sensorReading = (temperatureData[0] << 5) | (temperatureData[1] >> 3);
            }
            Temperature = (float)(sensorReading * 0.0625);
        }