-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathBmxx80Base.cs
368 lines (326 loc) · 13.2 KB
/
Bmxx80Base.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers.Binary;
using System.Device.I2c;
using System.IO;
using Iot.Device.Bmxx80.CalibrationData;
using Iot.Device.Bmxx80.Register;
using UnitsNet;
namespace Iot.Device.Bmxx80
{
/// <summary>
/// Represents the core functionality of the Bmxx80 family.
/// </summary>
public abstract class Bmxx80Base : IDisposable
{
/// <summary>
/// Calibration data for the sensor.
/// </summary>
private Bmxx80CalibrationData calibrationData;
/// <summary>
/// I2C device used to communicate with the device.
/// </summary>
private I2cDevice i2cDevice;
/// <summary>
/// Chosen communication protocol.
/// </summary>
private CommunicationProtocol communicationProtocol1;
/// <summary>
/// The control register of the sensor.
/// </summary>
private byte controlRegister;
/// <summary>
/// Bmxx80 communication protocol.
/// </summary>
public enum CommunicationProtocol
{
/// <summary>
/// I²C communication protocol.
/// </summary>
I2c
}
/// <summary>
/// Gets or sets TemperatureFine carries a fine resolution temperature value over to the
/// pressure compensation formula and could be implemented as a global variable.
/// </summary>
protected double TemperatureFine { get; set; }
/// <summary>
/// The temperature calibration factor.
/// </summary>
protected virtual int TempCalibrationFactor => 1;
private Sampling _temperatureSampling;
private Sampling _pressureSampling;
/// <summary>
/// Initializes a new instance of the <see cref="Bmxx80Base"/> class.
/// </summary>
/// <param name="deviceId">The ID of the device.</param>
/// <param name="i2cDevice">The <see cref="System.Device.I2c.I2cDevice"/> to create with.</param>
/// <exception cref="ArgumentNullException">Thrown when the given <see cref="System.Device.I2c.I2cDevice"/> is null.</exception>
/// <exception cref="IOException">Thrown when the device cannot be found on the bus.</exception>
protected Bmxx80Base(byte deviceId, I2cDevice i2cDevice)
{
I2cDevice = i2cDevice ?? throw new ArgumentNullException();
I2cDevice.WriteByte((byte)Bmxx80Register.CHIPID);
byte readSignature = I2cDevice.ReadByte();
if (readSignature != deviceId)
{
throw new IOException();
}
ReadCalibrationData();
Reset();
}
/// <summary>
/// Gets or sets the pressure sampling.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="Sampling"/> is set to an undefined mode.</exception>
public Sampling PressureSampling
{
get => _pressureSampling;
set
{
byte status = Read8BitsFromRegister(ControlRegister);
status = (byte)(status & 0b1110_0011);
status = (byte)(status | (byte)value << 2);
SpanByte command = new[]
{
ControlRegister, status
};
I2cDevice.Write(command);
_pressureSampling = value;
}
}
/// <summary>
/// Gets or sets the temperature sampling.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="Sampling"/> is set to an undefined mode.</exception>
public Sampling TemperatureSampling
{
get => _temperatureSampling;
set
{
byte status = Read8BitsFromRegister(ControlRegister);
status = (byte)(status & 0b0001_1111);
status = (byte)(status | (byte)value << 5);
SpanByte command = new[]
{
ControlRegister, status
};
I2cDevice.Write(command);
_temperatureSampling = value;
}
}
/// <summary>
/// Gets or sets control register of the sensor.
/// </summary>
protected byte ControlRegister { get => controlRegister; set => controlRegister = value; }
/// <summary>
/// Gets or sets chosen communication protocol.
/// </summary>
protected CommunicationProtocol CommunicationProtocol1 { get => communicationProtocol1; set => communicationProtocol1 = value; }
/// <summary>
/// Gets or sets I2C device used to communicate with the device.
/// </summary>
protected I2cDevice I2cDevice { get => i2cDevice; set => i2cDevice = value; }
/// <summary>
/// Gets or sets calibration data for the sensor.
/// </summary>
internal Bmxx80CalibrationData CalibrationData { get => calibrationData; set => calibrationData = value; }
/// <summary>
/// When called, the device is reset using the complete power-on-reset procedure.
/// The device will reset to the default configuration.
/// </summary>
public void Reset()
{
const byte ResetCommand = 0xB6;
SpanByte command = new[]
{
(byte)Bmxx80Register.RESET, ResetCommand
};
I2cDevice.Write(command);
SetDefaultConfiguration();
}
/// <summary>
/// Reads the temperature. A return value indicates whether the reading succeeded.
/// </summary>
/// <param name="temperature">
/// Contains the measured temperature if the <see cref="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>
public abstract bool TryReadTemperature(out Temperature temperature);
/// <summary>
/// Reads the pressure. A return value indicates whether the reading succeeded.
/// </summary>
/// <param name="pressure">
/// Contains the measured pressure if the <see cref="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>
public abstract bool TryReadPressure(out Pressure pressure);
/// <summary>
/// Compensates the temperature.
/// </summary>
/// <param name="adcTemperature">The temperature value read from the device.</param>
/// <returns>The <see cref="Temperature"/>.</returns>
protected Temperature CompensateTemperature(int adcTemperature)
{
// The temperature is calculated using the compensation formula in the BMP280 datasheet.
// See: https://cdn-shop.adafruit.com/datasheets/BST-BMP280-DS001-11.pdf
double var1 = ((adcTemperature / 16384.0) - (CalibrationData.DigT1 / 1024.0)) * CalibrationData.DigT2;
double var2 = (adcTemperature / 131072.0) - (CalibrationData.DigT1 / 8192.0);
var2 *= var2 * CalibrationData.DigT3 * TempCalibrationFactor;
TemperatureFine = var1 + var2;
double temp = (var1 + var2) / 5120.0;
return Temperature.FromDegreesCelsius(temp);
}
/// <summary>
/// Reads an 8 bit value from a register.
/// </summary>
/// <param name="register">Register to read from.</param>
/// <returns>Value from register.</returns>
protected internal byte Read8BitsFromRegister(byte register)
{
if (CommunicationProtocol1 == CommunicationProtocol.I2c)
{
I2cDevice.WriteByte(register);
byte value = I2cDevice.ReadByte();
return value;
}
else
{
throw new NotImplementedException();
}
}
/// <summary>
/// Reads a 16 bit value over I2C.
/// </summary>
/// <param name="register">Register to read from.</param>
/// <param name="endianness">Interpretation of the bytes (big or little endian).</param>
/// <returns>Value from register.</returns>
protected internal ushort Read16BitsFromRegister(byte register, Endianness endianness = Endianness.LittleEndian)
{
SpanByte bytes = new byte[2];
switch (CommunicationProtocol1)
{
case CommunicationProtocol.I2c:
I2cDevice.WriteByte(register);
I2cDevice.Read(bytes);
break;
default:
throw new NotImplementedException();
}
switch (endianness)
{
case Endianness.LittleEndian:
return BinaryPrimitives.ReadUInt16LittleEndian(bytes);
case Endianness.BigEndian:
return BinaryPrimitives.ReadUInt16BigEndian(bytes);
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Reads a 24 bit value over I2C.
/// </summary>
/// <param name="register">Register to read from.</param>
/// <param name="endianness">Interpretation of the bytes (big or little endian).</param>
/// <returns>Value from register.</returns>
protected internal uint Read24BitsFromRegister(byte register, Endianness endianness = Endianness.LittleEndian)
{
SpanByte bytes = new byte[4];
switch (CommunicationProtocol1)
{
case CommunicationProtocol.I2c:
I2cDevice.WriteByte(register);
I2cDevice.Read(bytes.Slice(1));
break;
default:
throw new NotImplementedException();
}
switch (endianness)
{
case Endianness.LittleEndian:
return BinaryPrimitives.ReadUInt32LittleEndian(bytes);
case Endianness.BigEndian:
return BinaryPrimitives.ReadUInt32BigEndian(bytes);
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Converts byte to <see cref="Sampling"/>.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns><see cref="Sampling"/></returns>
protected Sampling ByteToSampling(byte value)
{
// Values >=5 equals UltraHighResolution.
if (value >= 5)
{
return Sampling.UltraHighResolution;
}
return (Sampling)value;
}
/// <summary>
/// Sets the default configuration for the sensor.
/// </summary>
protected virtual void SetDefaultConfiguration()
{
PressureSampling = Sampling.UltraLowPower;
TemperatureSampling = Sampling.UltraLowPower;
}
/// <summary>
/// Specifies the Endianness of a device.
/// </summary>
protected internal enum Endianness
{
/// <summary>
/// Indicates little endian.
/// </summary>
LittleEndian,
/// <summary>
/// Indicates big endian.
/// </summary>
BigEndian
}
private void ReadCalibrationData()
{
switch (this)
{
case Bme280 _:
CalibrationData = new Bme280CalibrationData();
ControlRegister = (byte)Bmx280Register.CTRL_MEAS;
break;
case Bmp280 _:
CalibrationData = new Bmp280CalibrationData();
ControlRegister = (byte)Bmx280Register.CTRL_MEAS;
break;
case Bme680 _:
CalibrationData = new Bme680CalibrationData();
ControlRegister = (byte)Bme680Register.CTRL_MEAS;
break;
default:
throw new Exception();
}
CalibrationData.ReadFromDevice(this);
}
/// <summary>
/// Cleanup.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases the unmanaged resources used by the Bmxx80 and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">True to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
I2cDevice?.Dispose();
I2cDevice = null;
}
}
}