Ejemplo n.º 1
0
        private long ReadUncompensatedPressure()
        {
            // write register address
            bmp180.WriteBytes(new byte[] { 0xF4, (byte)(0x34 + (oversamplingSetting << 6)) });

            // insert pressure waittime using oversampling setting as index.
            Thread.Sleep(pressureWaitTime[oversamplingSetting]);

            // get MSB and LSB result
            byte[] data = new byte[3];
            data = bmp180.ReadRegisters(0xF6, 3);

            return(((data[0] << 16) | (data[1] << 8) | (data[2])) >> (8 - oversamplingSetting));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Update the temperature and pressure from the sensor and set the Pressure property.
        /// </summary>
        public async Task Update()
        {
            //
            //  Tell the sensor to take a temperature and pressure reading, wait for
            //  3ms (see section 2.2 of the datasheet) and then read the ADC values.
            //
            mpl115a2.WriteBytes(new byte[] { Registers.StartConversion, 0x00 });

            await Task.Delay(5);

            var data = mpl115a2.ReadRegisters(Registers.PressureMSB, 4);
            //
            //  Extract the sensor data, note that this is a 10-bit reading so move
            //  the data right 6 bits (see section 3.1 of the datasheet).
            //
            var pressure    = (ushort)(((data[0] << 8) + data[1]) >> 6);
            var temperature = (ushort)(((data[2] << 8) + data[3]) >> 6);

            Conditions.Temperature = (float)((temperature - 498.0) / -5.35) + 25;
            //
            //  Now use the calculations in section 3.2 to determine the
            //  current pressure reading.
            //
            const double PRESSURE_CONSTANT   = 65.0 / 1023.0;
            var          compensatedPressure = coefficients.A0 + ((coefficients.B1 + (coefficients.C12 * temperature))
                                                                  * pressure) + (coefficients.B2 * temperature);

            Conditions.Pressure = (float)(PRESSURE_CONSTANT * compensatedPressure) + 50;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Update the Temperature property.
        /// </summary>
        public void Update()
        {
            lm75.WriteByte((byte)Register.LM_TEMP);

            var data = lm75.ReadRegisters((byte)Register.LM_TEMP, 2);

            // Details in Datasheet P10
            double temp = 0;
            ushort raw  = (ushort)((data[0] << 3) | (data[1] >> 5));

            if ((data[0] & 0x80) == 0)
            {
                // temperature >= 0
                temp = raw * 0.125;
            }
            else
            {
                raw |= 0xF800;
                raw  = (ushort)(~raw + 1);

                temp = raw * (-1) * 0.125;
            }

            //only accurate to +/- 0.1 degrees
            Conditions.Temperature = (float)Math.Round(temp, 1);
        }
Ejemplo n.º 4
0
        protected async Task <AtmosphericConditions> ReadSensor()
        {
            AtmosphericConditions conditions = new AtmosphericConditions();

            return(await Task.Run(() => {
                si7021.WriteByte(HUMDITY_MEASURE_NOHOLD);
                //
                //  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]);
                conditions.Humidity = ((125 * (float)humidityReading) / 65536) - 6;
                if (conditions.Humidity < 0)
                {
                    conditions.Humidity = 0;
                }
                else
                {
                    if (conditions.Humidity > 100)
                    {
                        conditions.Humidity = 100;
                    }
                }
                data = si7021.ReadRegisters(TEMPERATURE_MEASURE_PREVIOUS, 2);
                var temperatureReading = (short)((data[0] << 8) + data[1]);
                conditions.Temperature = (float)(((175.72 * temperatureReading) / 65536) - 46.85);

                return conditions;
            }));
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Create a new TMP102 object using the default configuration for the sensor.
        /// </summary>
        /// <param name="address">I2C address of the sensor.</param>
        public Tmp102(II2cBus i2cBus, byte address = 0x48)
        {
            tmp102 = new I2cPeripheral(i2cBus, address);

            var configuration = tmp102.ReadRegisters(0x01, 2);

            _sensorResolution = (configuration[1] & 0x10) > 0 ?
                                Resolution.Resolution13Bits : Resolution.Resolution12Bits;
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevant properties.
        /// </summary>
        async Task 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.
            //

            await Task.Delay(40);

            while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0)
            {
                ;
            }
            byte[] data = groveTH02.ReadRegisters(Registers.DataHigh, 2);
            temp   = data[0] << 8;
            temp  |= data[1];
            temp >>= 4;
            Conditions.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.
            //
            await Task.Delay(40);

            while ((groveTH02.ReadRegister(Registers.Status) & 0x01) > 0)
            {
                ;
            }
            data   = groveTH02.ReadRegisters(Registers.DataHigh, 2);
            temp   = data[0] << 8;
            temp  |= data[1];
            temp >>= 2;
            Conditions.Temperature = (((float)temp) / 32) - 50;
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Read the six sensor bytes and set the values for the X, Y and Z acceleration.
        /// </summary>
        /// <remarks>
        ///     All six acceleration registers should be read at the same time to ensure consistency
        ///     of the measurements.
        /// </remarks>
        public void Update()
        {
            var data = _adxl345.ReadRegisters(Registers.X0, 6);

            X = (short)(data[0] + (data[1] << 8));
            Y = (short)(data[2] + (data[3] << 8));
            Z = (short)(data[4] + (data[5] << 8));
            if ((_updateInterval != 0) &&
                ((Math.Abs(X - _lastX) > AccelerationChangeNotificationThreshold) ||
                 (Math.Abs(Y - _lastY) > AccelerationChangeNotificationThreshold) ||
                 (Math.Abs(Z - _lastZ) > AccelerationChangeNotificationThreshold)))
            {
                Vector lastNotifiedReading = new Vector(_lastX, _lastY, _lastZ);
                Vector currentReading      = new Vector(X, Y, Z);
                _lastX = X;
                _lastY = Y;
                _lastZ = Z;
                AccelerationChanged(this, new SensorVectorEventArgs(lastNotifiedReading, currentReading));
            }
        }
Ejemplo n.º 8
0
        public double GetIlluminance()
        {
            var data = i2cPeripheral.ReadRegisters(0x03, 2);

            int exponent = ((data[0] & 0xF0) >> 4);
            int mantissa = ((data[0] & 0x0F) >> 4) | (data[1] & 0x0F);

            var luminance = Math.Pow(2, exponent) * mantissa * 0.045;

            return(luminance);
        }
Ejemplo n.º 9
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]);
        }
Ejemplo n.º 10
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);
            }
            Conditions.Temperature = (float)(sensorReading * 0.0625);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Create a new MPL115A2 temperature and humidity sensor object.
        /// </summary>
        /// <param name="address">Sensor address (default to 0x60).</param>
        /// <param name="i2cBus">I2CBus (default to 100 KHz).</param>
        public Mpl115a2(II2cBus i2cBus, byte address = 0x60)
        {
            var device = new I2cPeripheral(i2cBus, address);

            mpl115a2 = device;
            //
            //  Update the compensation data from the sensor.  The location and format of the
            //  compensation data can be found on pages 5 and 6 of the datasheet.
            //
            var data = mpl115a2.ReadRegisters(Registers.A0MSB, 8);
            var a0   = (short)(ushort)((data[0] << 8) | data[1]);
            var b1   = (short)(ushort)((data[2] << 8) | data[3]);
            var b2   = (short)(ushort)((data[4] << 8) | data[5]);
            var c12  = (short)(ushort)(((data[6] << 8) | data[7]) >> 2);

            //
            //  Convert the raw compensation coefficients from the sensor into the
            //  doubleing point equivalents to speed up the calculations when readings
            //  are made.
            //
            //  Datasheet, section 3.1
            //  a0 is signed with 12 integer bits followed by 3 fractional bits so divide by 2^3 (8)
            //
            coefficients.A0 = (double)a0 / 8;
            //
            //  b1 is 2 integer bits followed by 7 fractional bits.  The lower bits are all 0
            //  so the format is:
            //      sign i1 I0 F12...F0
            //
            //  So we need to divide by 2^13 (8192)
            //
            coefficients.B1 = (double)b1 / 8192;
            //
            //  b2 is signed integer (1 bit) followed by 14 fractional bits so divide by 2^14 (16384).
            //
            coefficients.B2 = (double)b2 / 16384;
            //
            //  c12 is signed with no integer bits but padded with 9 zeroes:
            //      sign 0.000 000 000 f12...f0
            //
            //  So we need to divide by 2^22 (4,194,304) - 13 doubleing point bits
            //  plus 9 leading zeroes.
            //
            coefficients.C12 = (double)c12 / 4194304;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Reads the current values of the magnetic field strength
        /// </summary>
        /// <returns>A <see cref="Vector"/> with the values of the magnetic field strenth on three axes, expressed in Gaus</returns>
        public Vector Read()
        {
            // In case the sensor is in single measurement mode it is necessary to update the Mode Register, wait at least 6 ms and only then read the data.
            if (Mode == OperatingMode.SingleMeasurement)
            {
                var currentModeRegisterData = (byte)OperatingMode.SingleMeasurement;
                _device.WriteRegister(MR, currentModeRegisterData);

                Thread.Sleep(7);
            }

            // Read raw sensor data and convert it to Gauss
            var data = _device.ReadRegisters(DXRA, 6);

            _currentX = ToGauss((short)((data[0] << 8) + data[1]));
            _currentY = ToGauss((short)((data[2] << 8) + data[3]));
            _currentZ = ToGauss((short)((data[4] << 8) + data[5]));

            return(new Vector(_currentX, _currentY, _currentZ));
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Display the registers.
        /// </summary>
        public void DisplayRegisters()
        {
            var data = _ds323x.ReadRegisters(0, 0x12);

            DebugInformation.DisplayRegisters(0, data);
        }
Ejemplo n.º 14
0
        protected int Read16(byte address)
        {
            var result = i2cPeripheral.ReadRegisters(address, 2);

            return((result[0] << 8) | result[1]);
        }