forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBmp280.cs
50 lines (44 loc) · 1.64 KB
/
Bmp280.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Device.I2c;
using System.Threading;
using Iot.Device.Bmxx80.PowerMode;
using Iot.Device.Bmxx80.ReadResult;
using UnitsNet;
namespace Iot.Device.Bmxx80
{
/// <summary>
/// Represents a BME280 temperature and barometric pressure sensor.
/// </summary>
public class Bmp280 : Bmx280Base
{
/// <summary>
/// The expected chip ID of the BMP280.
/// </summary>
private const byte DeviceId = 0x58;
/// <summary>
/// Initializes a new instance of the <see cref="Bmp280"/> class.
/// </summary>
/// <param name="i2cDevice">The <see cref="I2cDevice"/> to create with.</param>
public Bmp280(I2cDevice i2cDevice)
: base(DeviceId, i2cDevice)
{
CommunicationProtocol1 = CommunicationProtocol.I2c;
}
/// <summary>
/// Performs a synchronous reading.
/// </summary>
/// <returns><see cref="Bmp280ReadResult"/></returns>
public Bmp280ReadResult Read()
{
if (ReadPowerMode() != Bmx280PowerMode.Normal)
{
SetPowerMode(Bmx280PowerMode.Forced);
Thread.Sleep(GetMeasurementDuration());
}
var temperatureIsValid = TryReadTemperatureCore(out Temperature temperature);
var presureIsValid = TryReadPressureCore(out Pressure pressure, skipTempFineRead: true);
return new Bmp280ReadResult(temperature, temperatureIsValid, pressure, presureIsValid);
}
}
}