Example #1
0
        private void LoadCalibration()
        {
            byte storedCrc = _device.ReadRegister(BME280Constants.BME280_CRC_DATA_ADDR);

            var data1 = _device.ReadRegion(BME280Constants.BME280_CRC_CALIB1_ADDR, BME280Constants.BME280_CRC_CALIB1_LEN);

            var data2 = _device.ReadRegion(BME280Constants.BME280_CRC_CALIB2_ADDR, BME280Constants.BME280_CRC_CALIB2_LEN);

            var calibrationBuffer = new byte[data1.Length + data2.Length];

            data1.CopyTo(calibrationBuffer, 0);

            data2.CopyTo(calibrationBuffer, data1.Length);

            _calibration = new BME280CFData
            {
                T1 = BitConverter.ToUInt16(calibrationBuffer, 0),
                T2 = BitConverter.ToInt16(calibrationBuffer, 2),
                T3 = BitConverter.ToInt16(calibrationBuffer, 4),
                P1 = BitConverter.ToUInt16(calibrationBuffer, 6),
                P2 = BitConverter.ToInt16(calibrationBuffer, 8),
                P3 = BitConverter.ToInt16(calibrationBuffer, 10),
                P4 = BitConverter.ToInt16(calibrationBuffer, 12),
                P5 = BitConverter.ToInt16(calibrationBuffer, 14),
                P6 = BitConverter.ToInt16(calibrationBuffer, 16),
                P7 = BitConverter.ToInt16(calibrationBuffer, 18),
                P8 = BitConverter.ToInt16(calibrationBuffer, 20),
                P9 = BitConverter.ToInt16(calibrationBuffer, 22),
                H1 = calibrationBuffer[25],
                H2 = BitConverter.ToInt16(calibrationBuffer, 26),
                H3 = calibrationBuffer[28],
                H4 = (short)((calibrationBuffer[29] << 4) | (calibrationBuffer[30] & 0xF)),
                H5 = (short)((calibrationBuffer[31] << 4) | (calibrationBuffer[30] >> 4)),
                H6 = (sbyte)calibrationBuffer[32]
            };

            var calculatedCrc = CalculateCrc(calibrationBuffer);

            if (storedCrc != calculatedCrc)
            {
                throw new ApplicationException("CRC error loading configuration.");
            }
        }
Example #2
0
        private void LoadCalibration()
        {
            var crcBuffer = new byte[1];

            _device.WriteRead(new byte[] { 0xE8 }, crcBuffer);

            var calibrationBuffer = new byte[33];

            _device.WriteRead(new byte[] { 0x88 }, 0, 1, calibrationBuffer, 0, 26);

            _device.WriteRead(new byte[] { 0xE1 }, 0, 1, calibrationBuffer, 26, 7);

            _calibration = new BME280CFData
            {
                T1 = BitConverter.ToUInt16(calibrationBuffer, 0),
                T2 = BitConverter.ToInt16(calibrationBuffer, 2),
                T3 = BitConverter.ToInt16(calibrationBuffer, 4),
                P1 = BitConverter.ToUInt16(calibrationBuffer, 6),
                P2 = BitConverter.ToInt16(calibrationBuffer, 8),
                P3 = BitConverter.ToInt16(calibrationBuffer, 10),
                P4 = BitConverter.ToInt16(calibrationBuffer, 12),
                P5 = BitConverter.ToInt16(calibrationBuffer, 14),
                P6 = BitConverter.ToInt16(calibrationBuffer, 16),
                P7 = BitConverter.ToInt16(calibrationBuffer, 18),
                P8 = BitConverter.ToInt16(calibrationBuffer, 20),
                P9 = BitConverter.ToInt16(calibrationBuffer, 22),
                H1 = calibrationBuffer[25],
                H2 = BitConverter.ToInt16(calibrationBuffer, 26),
                H3 = calibrationBuffer[28],
                H4 = (short)((calibrationBuffer[29] << 4) | (calibrationBuffer[30] & 0xF)),
                H5 = (short)((calibrationBuffer[31] << 4) | (calibrationBuffer[30] >> 4)),
                H6 = (sbyte)calibrationBuffer[32]
            };

            if (crcBuffer[0] != CalculateCrc(calibrationBuffer))
            {
                throw new ApplicationException("CRC error loading configuration.");
            }
        }