Esempio n. 1
0
        /// <summary>
        ///     Put the sensor into manual integration mode.
        /// </summary>
        public void ManualStart()
        {
            var timing = _tsl2561.ReadRegister(Registers.Timing);

            timing |= 0x03;
            _tsl2561.WriteRegister(Registers.Timing, timing);
            timing |= 0xf7; //  ~0x08;
            _tsl2561.WriteRegister(Registers.Timing, timing);
        }
Esempio n. 2
0
        /// <summary>
        ///     Reset the sensor.
        /// </summary>
        public void Reset()
        {
            var data = _mpl3115a2.ReadRegister(Registers.Control1);

            data |= 0x04;
            _mpl3115a2.WriteRegister(Registers.Control1, data);
        }
Esempio n. 3
0
        /// <summary>
        ///     Create a new SI1145 sensor object.
        /// </summary>
        /// <param name="address">Address of the chip on the I2C bus (default to 0x60).</param>
        /// <param name="speed">Communication speed (default to 400 KHz).</param>
        public SI1145(byte address = 0x60, ushort speed = 400)
        {
            I2cBus device = new I2cBus(address, speed);

            _si1145 = (ICommunicationBus)device;
            if (_si1145.ReadRegister(Registers.PartID) != 0x45)
            {
                throw new Exception("Invalid part ID");
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevant properties.
        /// </summary>
        public void Update()
        {
            int temp = 0;

            //
            //  Get the humidity first.
            //
            _groveTH02.WriteRegister(Registers.Config, StartMeasurement);
            //
            //  Maximum conversion time should be 40ms but loop just in case
            //  it takes longer.
            //
            Thread.Sleep(40);
            while ((_groveTH02.ReadRegister(Registers.Status) & 0x01) > 0)
            {
                ;
            }
            byte[] data = _groveTH02.ReadRegisters(Registers.DataHigh, 2);
            temp     = data[0] << 8;
            temp    |= data[1];
            temp   >>= 4;
            Humidity = (((float)temp) / 16) - 24;
            //
            //  Now get the temperature.
            //
            _groveTH02.WriteRegister(Registers.Config, StartMeasurement | MeasureTemperature);
            //
            //  Maximum conversion time should be 40ms but loop just in case
            //  it takes longer.
            //
            Thread.Sleep(40);
            while ((_groveTH02.ReadRegister(Registers.Status) & 0x01) > 0)
            {
                ;
            }
            data        = _groveTH02.ReadRegisters(Registers.DataHigh, 2);
            temp        = data[0] << 8;
            temp       |= data[1];
            temp      >>= 2;
            Temperature = (((float)temp) / 32) - 50;
        }
Esempio n. 5
0
        /// <summary>
        ///     Create a new MPL3115A2 object with the default address and speed settings.
        /// </summary>
        /// <param name="address">Address of the sensor (default = 0x60).</param>
        /// <param name="speed">Bus speed to use when communicating with the sensor (Maximum is 400 kHz).</param>
        /// <param name="updateInterval">Number of milliseconds between samples (0 indicates polling to be used)</param>
        /// <param name="temperatureChangeNotificationThreshold">Changes in temperature greater than this value will trigger an event when updatePeriod > 0.</param>
        /// <param name="pressureChangedNotificationThreshold">Changes in pressure greater than this value will trigger an event when updatePeriod > 0.</param>
        public MPL3115A2(byte address = 0x60, ushort speed = 400, ushort updateInterval = MinimumPollingPeriod,
                         float temperatureChangeNotificationThreshold = 0.001F, float pressureChangedNotificationThreshold = 10.0F)
        {
            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 (pressureChangedNotificationThreshold < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(pressureChangedNotificationThreshold), "Pressure threshold should be >= 0");
            }
            if ((updateInterval != 0) && (updateInterval < MinimumPollingPeriod))
            {
                throw new ArgumentOutOfRangeException(nameof(updateInterval), "Update period should be 0 or >= than " + MinimumPollingPeriod);
            }

            TemperatureChangeNotificationThreshold = temperatureChangeNotificationThreshold;
            PressureChangeNotificationThreshold    = pressureChangedNotificationThreshold;
            _updateInterval = updateInterval;

            var device = new I2cBus(address, speed);

            _mpl3115a2 = device;
            if (_mpl3115a2.ReadRegister(Registers.WhoAmI) != 0xc4)
            {
                throw new Exception("Unexpected device ID, expected 0xc4");
            }
            _mpl3115a2.WriteRegister(Registers.Control1,
                                     (byte)(ControlRegisterBits.Active | ControlRegisterBits.OverSample128));
            _mpl3115a2.WriteRegister(Registers.DataConfiguration,
                                     (byte)(ConfigurationRegisterBits.DataReadyEvent |
                                            ConfigurationRegisterBits.EnablePressureEvent |
                                            ConfigurationRegisterBits.EnableTemperatureEvent));
            if (updateInterval > 0)
            {
                StartUpdating();
            }
            else
            {
                Update();
            }
        }