forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBmx280Base.cs
320 lines (279 loc) · 12.5 KB
/
Bmx280Base.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Ported from https://github.com/adafruit/Adafruit_BMP280_Library/blob/master/Adafruit_BMP280.cpp
// Formulas and code examples can also be found in the datasheet http://www.adafruit.com/datasheets/BST-BMP280-DS001-11.pdf
using System;
using System.Device.I2c;
using System.Device.Model;
using System.IO;
using Iot.Device.Bmxx80.FilteringMode;
using Iot.Device.Bmxx80.PowerMode;
using Iot.Device.Bmxx80.Register;
using Iot.Device.Common;
using UnitsNet;
namespace Iot.Device.Bmxx80
{
/// <summary>
/// Represents the core functionality of the Bmx280 family.
/// </summary>
[Interface("Represents the core functionality of the Bmx280 family.")]
public abstract class Bmx280Base : Bmxx80Base
{
/// <summary>
/// Default I2C bus address.
/// </summary>
public const byte DefaultI2cAddress = 0x77;
/// <summary>
/// Secondary I2C bus address.
/// </summary>
public const byte SecondaryI2cAddress = 0x76;
/// <summary>
/// Converts oversampling to needed measurement cycles for that oversampling.
/// </summary>
protected static readonly int[] OsToMeasCycles = { 0, 7, 9, 14, 23, 44 };
private Bmx280FilteringMode _filteringMode;
private StandbyTime _standbyTime;
/// <summary>
/// Initializes a new instance of the <see cref="Bmx280Base"/> class.
/// </summary>
/// <param name="deviceId">The ID of the device.</param>
/// <param name="i2cDevice">The <see cref="I2cDevice"/> to create with.</param>
protected Bmx280Base(byte deviceId, I2cDevice i2cDevice)
: base(deviceId, i2cDevice)
{
}
/// <summary>
/// Gets or sets the IIR filter mode.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="Bmx280FilteringMode"/> is set to an undefined mode.</exception>
[Property]
public Bmx280FilteringMode FilterMode
{
get => _filteringMode;
set
{
byte current = Read8BitsFromRegister((byte)Bmx280Register.CONFIG);
current = (byte)((current & 0b1110_0011) | (byte)value << 2);
SpanByte command = new[]
{
(byte)Bmx280Register.CONFIG, current
};
I2cDevice.Write(command);
_filteringMode = value;
}
}
/// <summary>
/// Gets or sets the standby time between two consecutive measurements.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="Bmxx80.StandbyTime"/> is set to an undefined mode.</exception>
[Property]
public StandbyTime StandbyTime
{
get => _standbyTime;
set
{
byte current = Read8BitsFromRegister((byte)Bmx280Register.CONFIG);
current = (byte)((current & 0b0001_1111) | (byte)value << 5);
SpanByte command = new[]
{
(byte)Bmx280Register.CONFIG, current
};
I2cDevice.Write(command);
_standbyTime = value;
}
}
/// <summary>
/// Reads the temperature. A return value indicates whether the reading succeeded.
/// </summary>
/// <param name="temperature">
/// Contains the measured temperature if the <see cref="Bmxx80Base.TemperatureSampling"/> was not set to <see cref="Sampling.Skipped"/>.
/// Contains <see cref="double.NaN"/> otherwise.
/// </param>
/// <returns><code>true</code> if measurement was not skipped, otherwise <code>false</code>.</returns>
[Telemetry("Temperature")]
public override bool TryReadTemperature(out Temperature temperature) => TryReadTemperatureCore(out temperature);
/// <summary>
/// Read the <see cref="Bmx280PowerMode"/> state.
/// </summary>
/// <returns>The current <see cref="Bmx280PowerMode"/>.</returns>
/// <exception cref="NotImplementedException">Thrown when the power mode does not match a defined mode in <see cref="Bmx280PowerMode"/>.</exception>
[Property("PowerMode")]
public Bmx280PowerMode ReadPowerMode()
{
byte read = Read8BitsFromRegister(ControlRegister);
// Get only the power mode bits.
var powerMode = (byte)(read & 0b0000_0011);
if ((powerMode != (byte)Bmx280PowerMode.Forced) &&
(powerMode != (byte)Bmx280PowerMode.Normal) &&
(powerMode != (byte)Bmx280PowerMode.Sleep))
{
throw new IOException();
}
return (Bmx280PowerMode)powerMode;
}
/// <summary>
/// Reads the pressure. A return value indicates whether the reading succeeded.
/// </summary>
/// <param name="pressure">
/// Contains the measured pressure in Pa if the <see cref="Bmxx80Base.PressureSampling"/> was not set to <see cref="Sampling.Skipped"/>.
/// Contains <see cref="double.NaN"/> otherwise.
/// </param>
/// <returns><code>true</code> if measurement was not skipped, otherwise <code>false</code>.</returns>
[Telemetry("Pressure")]
public override bool TryReadPressure(out Pressure pressure) => TryReadPressureCore(out pressure);
/// <summary>
/// Calculates the altitude in meters from the specified sea-level pressure(in hPa).
/// </summary>
/// <param name="seaLevelPressure">Sea-level pressure.</param>
/// <param name="altitude">
/// Contains the calculated metres above sea-level if the <see cref="Bmxx80Base.PressureSampling"/> was not set to <see cref="Sampling.Skipped"/>.
/// Contains <see cref="double.NaN"/> otherwise.
/// </param>
/// <returns><code>true</code> if pressure measurement was not skipped, otherwise <code>false</code>.</returns>
public bool TryReadAltitude(Pressure seaLevelPressure, out Length altitude)
{
// Read the pressure first.
var success = TryReadPressureCore(out var pressure);
if (!success)
{
altitude = default;
return false;
}
// Then read the temperature.
success = TryReadTemperatureCore(out var temperature);
if (!success)
{
altitude = default;
return false;
}
// Calculate and return the altitude using the hypsometric formula.
altitude = WeatherHelper.CalculateAltitude(pressure, seaLevelPressure, temperature);
return true;
}
/// <summary>
/// Calculates the altitude in meters from the mean sea-level pressure.
/// </summary>
/// <param name="altitude">
/// Contains the calculated metres above sea-level if the <see cref="Bmxx80Base.PressureSampling"/> was not set to <see cref="Sampling.Skipped"/>.
/// Contains <see cref="double.NaN"/> otherwise.
/// </param>
/// <returns><code>true</code> if pressure measurement was not skipped, otherwise <code>false</code>.</returns>
public bool TryReadAltitude(out Length altitude) => TryReadAltitude(WeatherHelper.MeanSeaLevel, out altitude);
/// <summary>
/// Get the current status of the device.
/// </summary>
/// <returns>The <see cref="DeviceStatus"/>.</returns>
[Telemetry("Status")]
public DeviceStatus ReadStatus()
{
var status = Read8BitsFromRegister((byte)Bmx280Register.STATUS);
// Bit 3.
var measuring = ((status >> 3) & 1) == 1;
// Bit 0.
var imageUpdating = (status & 1) == 1;
return new DeviceStatus
{
ImageUpdating = imageUpdating,
Measuring = measuring
};
}
/// <summary>
/// Sets the power mode to the given mode.
/// </summary>
/// <param name="powerMode">The <see cref="Bmx280PowerMode"/> to set.</param>
[Property("PowerMode")]
public void SetPowerMode(Bmx280PowerMode powerMode)
{
byte read = Read8BitsFromRegister(ControlRegister);
// Clear last 2 bits.
var cleared = (byte)(read & 0b1111_1100);
SpanByte command = new[]
{
ControlRegister, (byte)(cleared | (byte)powerMode)
};
I2cDevice.Write(command);
}
/// <summary>
/// Gets the required time in ms to perform a measurement with the current sampling modes.
/// </summary>
/// <returns>The time it takes for the chip to read data in milliseconds rounded up.</returns>
[Property("MeasurementDuration")]
public virtual int GetMeasurementDuration() => OsToMeasCycles[(int)PressureSampling] + OsToMeasCycles[(int)TemperatureSampling];
/// <summary>
/// Sets the default configuration for the sensor.
/// </summary>
[Command]
protected override void SetDefaultConfiguration()
{
base.SetDefaultConfiguration();
FilterMode = Bmx280FilteringMode.Off;
StandbyTime = StandbyTime.Ms125;
}
/// <summary>
/// Performs a temperature reading.
/// </summary>
/// <param name="temperature">Current temperature reading.</param>
/// <returns>True if reading was sucesfully performed.</returns>
protected bool TryReadTemperatureCore(out Temperature temperature)
{
if (TemperatureSampling == Sampling.Skipped)
{
temperature = default;
return false;
}
var temp = (int)Read24BitsFromRegister((byte)Bmx280Register.TEMPDATA_MSB, Endianness.BigEndian);
temperature = CompensateTemperature(temp >> 4);
return true;
}
/// <summary>
/// Performs a pressure reading.
/// </summary>
/// <param name="pressure">Current preasure reeading.</param>
/// <param name="skipTempFineRead">Don't know. Change it.</param>
/// <returns>True if the reading was sucesfully performed.</returns>
protected bool TryReadPressureCore(out Pressure pressure, bool skipTempFineRead = false)
{
if (PressureSampling == Sampling.Skipped)
{
pressure = default;
return false;
}
if (!skipTempFineRead)
{
TryReadTemperatureCore(out _);
}
// Read pressure data.
var press = (int)Read24BitsFromRegister((byte)Bmx280Register.PRESSUREDATA, Endianness.BigEndian);
// Convert the raw value to the pressure in Pa.
pressure = CompensatePressure(press >> 4);
return true;
}
/// <summary>
/// Compensates the pressure in Pa, in double format.
/// </summary>
/// <param name="adcPressure">The pressure value read from the device.</param>
/// <returns>Pressure as an instance of <see cref="Pressure"/>.</returns>
private Pressure CompensatePressure(long adcPressure)
{
// Formula from the datasheet http://www.adafruit.com/datasheets/BST-BMP280-DS001-11.pdf
// This uses the recommended approach with floating point math
double var1, var2, p;
var1 = (TemperatureFine / 2.0) - 64000.0;
var2 = var1 * var1 * CalibrationData.DigP6 / 32768.0;
var2 = var2 + ((var1 * CalibrationData.DigP5) * 2.0);
var2 = (var2 / 4.0) + (CalibrationData.DigP4 * 65536.0);
var1 = ((((CalibrationData.DigP3 * var1) * var1) / 524288.0) + (CalibrationData.DigP2 * var1)) / 524288.0;
var1 = (1.0 + (var1 / 32768.0)) * CalibrationData.DigP1;
if (var1 == 0.0)
{
return Pressure.FromPascals(0); // Avoid exception caused by division by zero
}
p = 1048576.0 - adcPressure;
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((CalibrationData.DigP9 * p) * p) / 2147483648.0;
var2 = (p * CalibrationData.DigP8) / 32768.0;
p = p + ((var1 + var2 + CalibrationData.DigP7) / 16.0);
return Pressure.FromPascals(p);
}
}
}