forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ccs811Sensor.cs
564 lines (490 loc) · 21.6 KB
/
Ccs811Sensor.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// 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;
using System.Device.Gpio;
using System.Device.I2c;
using System.IO;
using System.Threading;
using UnitsNet;
namespace Iot.Device.Ccs811
{
/// <summary>
/// Ultra-Low Power Digital Gas Sensor for Monitoring Indoor Air Quality
/// Documentation can be found here: https://www.sciosense.com/products/environmental-sensors/ccs811-gas-sensor-solution/.
/// </summary>
public class Ccs811Sensor : IDisposable
{
/// <summary>
/// The first default I2C address when the Address pin is put to low.
/// </summary>
public const int I2cFirstAddress = 0x5A;
/// <summary>
/// The second default I2C address when the Address pin is put to high.
/// </summary>
public const int I2cSecondAddress = 0x5B;
/// <summary>
/// The typical operating speed for the bus
/// Note that minimum is 10 KHz and the maximum is 400 KHz
/// The device can operate in Stretching mode is the transfer is too fast.
/// This stretching may not be well supported in all the hardware, in case of
/// issue, it is recommended to lower the operating frequency.
/// </summary>
public const int I2cTypicalFrequency = 100_000;
private GpioController _controller;
private I2cDevice _i2cDevice;
private int _pinWake = -1;
private int _pinInterruption = -1;
private int _pinReset = -1;
private bool _shouldDispose;
private bool _running = false;
private bool _isRunning = false;
/// <summary>
/// Event raised when interruption pin is selected.
/// </summary>
/// <param name="sender">This sensor.</param>
/// <param name="args">The measurement.</param>
public delegate void MeasurementReadyHandler(object sender, MeasurementArgs args);
/// <summary>
/// The event handler for the measurement.
/// </summary>
public event MeasurementReadyHandler? MeasurementReady;
/// <summary>
/// Initializes a new instance of the <see cref="Ccs811Sensor" /> class.
/// </summary>
/// <param name="i2cDevice">A valid I2C device.</param>
/// <param name="pinWake">An awake pin, it is optional, this pin can be set to the ground if the sensor is always on.</param>
/// <param name="pinInterruption">An interruption pin when a measurement is ready, best use when you specify a threshold.</param>
/// <param name="pinReset">An optional hard reset pin.</param>
/// <param name="shouldDispose">Should the GPIO controller be disposed at the end.</param>
public Ccs811Sensor(I2cDevice i2cDevice, int pinWake = -1, int pinInterruption = -1, int pinReset = -1, bool shouldDispose = true)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
_pinWake = pinWake;
_pinInterruption = pinInterruption;
_pinReset = pinReset;
// We need a GPIO controller only if we are using any of the pin
if ((_pinInterruption >= 0) || (_pinReset >= 0) || (_pinWake >= 0))
{
_shouldDispose = _shouldDispose || _controller is null;
_controller = new GpioController();
}
if (_controller is object)
{
_controller.OpenPin(_pinWake, PinMode.Output);
_controller.Write(_pinWake, PinValue.High);
}
if (_controller is object && _pinReset >= 0)
{
_controller.OpenPin(_pinReset, PinMode.Output);
_controller.Write(_pinReset, PinValue.Low);
// Delays from documentation CCS811-Datasheet.pdf page 8
// 15 micro second
DelayHelper.DelayMicroseconds(15, true);
_controller.Write(_pinReset, PinValue.High);
// Need to wait at least 2 milliseconds before executing anything I2C
Thread.Sleep(2);
}
// Initialization flow page 29
// https://www.sciosense.com/wp-content/uploads/2020/01/CCS811-Application-Note-Programming-and-interfacing-guide.pdf
// do a soft reset
SpanByte toReset = new byte[4]
{
0x11,
0xE5,
0x72,
0x8A
};
WriteRegister(Register.SW_RESET, toReset);
// Wait 2 milliseconds as per documentation
Thread.Sleep(2);
if (HardwareIdentification != 0x81)
{
throw new IOException($"CCS811 does not have a valid ID: {HardwareIdentification}. ID must be 0x81.");
}
if ((HardwareVersion & 0xF0) != 0x10)
{
throw new IOException($"CCS811 does not have a valid version: {HardwareVersion}, should be 0x1X where any X is valid.");
}
// Read status
if (!Status.HasFlag(Status.APP_VALID))
{
throw new IOException($"CCS811 has no application firmware loaded.");
}
// Switch to app mode and wait 1 millisecond according to doc
WriteRegister(Register.APP_START);
Thread.Sleep(1);
if (!Status.HasFlag(Status.FW_MODE))
{
throw new IOException($"CCS811 is not in application mode.");
}
// Set interrupt if the interruption pin is valid
if (_controller is object && _pinInterruption >= 0)
{
var interruptPin = _controller.OpenPin(_pinInterruption, PinMode.Input);
byte mode = 0b0000_1000;
WriteRegister(Register.MEAS_MODE, mode);
_running = true;
// Start a new thread to monitor the events
new Thread(() =>
{
_isRunning = true;
while (_running)
{
var currentState = interruptPin.Read();
if (currentState == PinValue.High)
{
// We know we won't get any new measurement in next 250 milliseconds at least
// Waiting to make sure the sensor will have time to remove the interrupt pin
Thread.Sleep(50);
}
else
{
// new measurement available
InterruptReady();
}
}
_isRunning = false;
}).Start();
}
}
private void InterruptReady()
{
MeasurementArgs measurement = new MeasurementArgs();
var success = TryReadGasData(out VolumeConcentration eCo2, out VolumeConcentration eTvoc, out ElectricCurrent current, out int adc);
measurement.MeasurementSuccess = success;
measurement.EquivalentCO2 = eCo2;
measurement.EquivalentTotalVolatileOrganicCompound = eTvoc;
measurement.RawCurrentSelected = current;
measurement.RawAdcReading = adc;
MeasurementReady?.Invoke(this, measurement);
}
private Status Status => (Status)ReadRegister(Register.STATUS);
/// <summary>
/// Gets or sets operation mode.
/// </summary>
public OperationMode OperationMode
{
get
{
var mode = ReadRegister(Register.MEAS_MODE);
mode = (byte)((mode >> 4) & 0b0000_0111);
return (OperationMode)mode;
}
set
{
var mode = ReadRegister(Register.MEAS_MODE);
// Clear previous mode
mode = (byte)(mode & 0b1000_1111);
mode = (byte)(mode | (((byte)value) << 4));
WriteRegister(Register.MEAS_MODE, mode);
}
}
/// <summary>
/// Get the error.
/// </summary>
/// <returns></returns>
public Error Error => (Error)ReadRegister(Register.ERROR_ID);
/// <summary>
/// Get the hardware identification, it has to be 0x81.
/// </summary>
public byte HardwareIdentification => ReadRegister(Register.HW_ID);
/// <summary>
/// Is the hardware interrupt enabled.
/// </summary>
public bool InterruptEnable => _pinInterruption >= 0;
/// <summary>
/// Hardware version should be 0x1X, any X seems valid.
/// </summary>
public byte HardwareVersion => ReadRegister(Register.HW_Version);
/// <summary>
/// Gets the application version.
/// </summary>
public Version ApplicationVersion
{
get
{
SpanByte version = new byte[2];
ReadRegister(Register.FW_App_Version, version);
return new Version(version[0] >> 4, version[0] & 0b0000_1111, version[1], 0);
}
}
/// <summary>
/// Gets the boot loader version.
/// </summary>
public Version BootloaderVersion
{
get
{
SpanByte version = new byte[2];
ReadRegister(Register.FW_Boot_Version, version);
return new Version(version[0] >> 4, version[0] & 0b0000_1111, version[1], 0);
}
}
/// <summary>
/// Is the wake feature enabled.
/// </summary>
public bool WakeEnable => _pinWake >= 0;
/// <summary>
/// Gets a value indicating whether we have data ready to read.
/// </summary>
public bool IsDataReady
{
get
{
var status = (Status)ReadRegister(Register.STATUS);
return status.HasFlag(Status.DATA_READY);
}
}
/// <summary>
/// Read the equivalent CO2 in ppm and equivalent Total Volatile Compound in ppb.
/// </summary>
/// <param name="equivalentCO2">The equivalent CO2 (eCO2) output range for CCS811 is from
/// 400ppm up to 29206ppm.</param>
/// <param name="equivalentTotalVolatileOrganicCompound">The equivalent Total Volatile Organic Compound (eTVOC)
/// output range for CCS811 is from 0ppb up to 32768ppb.</param>
/// <param name="rawCurrentSelected">Raw data containing the value of the
/// current through the sensor(0μA to 63μA).</param>
/// <param name="rawAdcReading">Raw data containing the
/// readings of the voltage across the sensor with the selected
/// current(1023 = 1.65V) where 1023 is the maximum value.</param>
/// <returns>True if success.</returns>
public bool TryReadGasData(out VolumeConcentration equivalentCO2, out VolumeConcentration equivalentTotalVolatileOrganicCompound, out ElectricCurrent rawCurrentSelected, out int rawAdcReading)
{
int equivalentCO2InPpm = -1;
int equivalentTotalVolatileOrganicCompoundInPpb = -1;
int rawCurrent = -1;
rawAdcReading = -1;
SpanByte toRead = new byte[8];
ReadRegister(Register.ALG_RESULT_DATA, toRead);
if (toRead[5] != (byte)Error.NoError)
{
equivalentCO2 = VolumeConcentration.Zero;
equivalentTotalVolatileOrganicCompound = VolumeConcentration.Zero;
rawCurrentSelected = ElectricCurrent.Zero;
return false;
}
equivalentCO2InPpm = BinaryPrimitives.ReadInt16BigEndian(toRead.Slice(0, 2));
equivalentTotalVolatileOrganicCompoundInPpb = BinaryPrimitives.ReadInt16BigEndian(toRead.Slice(2, 2));
rawCurrent = toRead[6] >> 2;
rawAdcReading = ((toRead[6] & 0b0000_0011) << 2) + toRead[7];
equivalentCO2 = VolumeConcentration.FromPartsPerMillion(equivalentCO2InPpm);
equivalentTotalVolatileOrganicCompound = VolumeConcentration.FromPartsPerBillion(equivalentTotalVolatileOrganicCompoundInPpb);
rawCurrentSelected = ElectricCurrent.FromMicroamperes(rawCurrent);
return (equivalentCO2InPpm >= 400) && (equivalentCO2InPpm <= 29206) && (equivalentTotalVolatileOrganicCompoundInPpb >= 0) && (equivalentTotalVolatileOrganicCompoundInPpb <= 32768);
}
/// <summary>
/// Read the equivalent CO2 in ppm and equivalent Total Volatile Compound in ppb.
/// </summary>
/// <param name="equivalentCO2">The equivalent CO2 (eCO2) output range for CCS811 is from
/// 400ppm up to 29206ppm.</param>
/// <param name="equivalentTotalVolatileOrganicCompound">The equivalent Total Volatile Organic Compound (eTVOC)
/// output range for CCS811 is from 0ppb up to 32768ppb.</param>
/// <returns>True if success.</returns>
public bool TryReadGasData(out VolumeConcentration equivalentCO2, out VolumeConcentration equivalentTotalVolatileOrganicCompound)
{
return TryReadGasData(out equivalentCO2, out equivalentTotalVolatileOrganicCompound, out ElectricCurrent curr, out int adc);
}
/// <summary>
/// Gets or sets the encoded version of the current baseline used in Algorithm Calculations.
/// </summary>
/// <remarks>A previously stored value may be written back to this two byte
/// register and the Algorithms will use the new value in its
/// calculations(until it adjusts it as part of its internal Automatic
/// Baseline Correction). Please refer to documentation to understand when to restore a
/// previous baseline: https://www.sciosense.com/wp-content/uploads/2020/01/Application-Note-Baseline-Save-and-Restore-on-CCS811.pdf.</remarks>
public ushort BaselineAlgorithmCalculation
{
get
{
SpanByte baseline = new byte[2];
ReadRegister(Register.BASELINE, baseline);
return BinaryPrimitives.ReadUInt16BigEndian(baseline);
}
set
{
SpanByte baseline = new byte[2];
BinaryPrimitives.WriteUInt16BigEndian(baseline, value);
WriteRegister(Register.BASELINE, baseline);
}
}
/// <summary>
/// Set the environmental data, this is impacting the equivalent calculation
/// of the gas.
/// </summary>
/// <param name="temperature">The temperature.</param>
/// <param name="humidity">The relative humidity, best to use Percent from 0 to 100.</param>
public void SetEnvironmentData(Temperature temperature, RelativeHumidity humidity)
{
if ((humidity.Percent < 0) || (humidity.Percent > 100))
{
throw new ArgumentException(nameof(humidity), "Humidity can only be between 0 and 100.");
}
SpanByte environment = new byte[4];
// Convert the humidity first
ConvertForEnvironement(humidity.Percent, environment.Slice(0, 2));
// Cap the temperature to the minimum or maximum according to documentation
var temp = temperature.DegreesCelsius;
temp += 25;
temp = Math.Max(temp, 0);
temp = Math.Min(temp, 127);
ConvertForEnvironement(temp, environment.Slice(2, 2));
WriteRegister(Register.ENV_DATA, environment);
}
private void ConvertForEnvironement(double toConvert, SpanByte converted)
{
// Format is 7 bits for the integer part and 9 bits for the decimal one
byte integerPart = (byte)toConvert;
double decimalPart = toConvert - integerPart;
converted[0] = (byte)(integerPart << 1);
// There a 9 bits with fractions so we have to sample in 1/512 = 0.001953125
uint decimalPartUint = ((uint)(decimalPart / 0.001953125)) & 0x1FF;
converted[0] = (byte)(converted[0] | (decimalPartUint >> 8));
converted[1] = (byte)(decimalPartUint & 0xFF);
}
/// <summary>
/// Set the threshold for the equivalent CO2. The pinInterrupt should be existing so
/// interruptions are activated. If not, then the function will return false.
/// </summary>
/// <param name="lowEquivalentCO2">The low value for the threshold.</param>
/// <param name="highEquivalentCO2">The high value for the threshold.</param>
/// <returns>True if success.</returns>
/// <remarks>Difference between the low and high value should be more than 50. This is called
/// the hysteresis value.</remarks>
public bool SetThreshold(VolumeConcentration lowEquivalentCO2, VolumeConcentration highEquivalentCO2)
{
if (_pinInterruption < 0)
{
return false;
}
if (!IsPpmValidThreshold(lowEquivalentCO2))
{
throw new ArgumentException(nameof(lowEquivalentCO2), $"Value can only be between 0 and {ushort.MaxValue}.");
}
if (!IsPpmValidThreshold(highEquivalentCO2))
{
throw new ArgumentException(nameof(highEquivalentCO2), $"Value can only be between 0 and {ushort.MaxValue}.");
}
if (lowEquivalentCO2.Value > highEquivalentCO2.Value)
{
var temp = highEquivalentCO2;
highEquivalentCO2 = lowEquivalentCO2;
lowEquivalentCO2 = temp;
}
if (highEquivalentCO2.Value - lowEquivalentCO2.Value < VolumeConcentration.FromPartsPerMillion(50).Value)
{
throw new ArgumentException(nameof(lowEquivalentCO2), $"value of {nameof(highEquivalentCO2)}-{nameof(lowEquivalentCO2)} must be more than 50.");
}
SpanByte toSend = new byte[4];
BinaryPrimitives.WriteUInt16BigEndian(toSend.Slice(0, 2), (ushort)lowEquivalentCO2.PartsPerMillion);
BinaryPrimitives.WriteUInt16BigEndian(toSend.Slice(2, 2), (ushort)highEquivalentCO2.PartsPerMillion);
WriteRegister(Register.THRESHOLDS, toSend);
// Activate the interrupt threshold as well
byte mode = ReadRegister(Register.MEAS_MODE);
mode |= 0b0000_0100;
WriteRegister(Register.MEAS_MODE, mode);
return !Status.HasFlag(Status.ERROR);
}
private bool IsPpmValidThreshold(VolumeConcentration ppm)
{
if ((ppm.Value < VolumeConcentration.Zero.Value) || (ppm.Value > VolumeConcentration.FromPartsPerMillion(ushort.MaxValue).Value))
{
return false;
}
return true;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
_running = false;
while (_isRunning)
{
Thread.Sleep(1);
}
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null;
}
else if (_controller is object)
{
if (_pinInterruption >= 0)
{
_controller.ClosePin(_pinInterruption);
}
if (_pinReset >= 0)
{
_controller.ClosePin(_pinReset);
}
if (_pinWake >= 0)
{
_controller.ClosePin(_pinWake);
}
}
}
#region I2C operations
private void WakeUpDevice()
{
if (_controller is object && _pinWake >= 0)
{
_controller.Write(_pinWake, PinValue.Low);
// Doc says wait 50 micro seconds
DelayHelper.DelayMicroseconds(50, true);
}
}
private void SleepDownDevice()
{
if (_controller is object && _pinWake >= 0)
{
_controller.Write(_pinWake, PinValue.High);
// Doc says wait 20 micro seconds
DelayHelper.DelayMicroseconds(50, true);
}
}
private void WriteRegister(Register register)
{
WakeUpDevice();
_i2cDevice.WriteByte((byte)register);
SleepDownDevice();
}
private void WriteRegister(Register register, byte data)
{
SpanByte toSend = new byte[2]
{
(byte)register,
data
};
WakeUpDevice();
_i2cDevice.Write(toSend);
SleepDownDevice();
}
private void WriteRegister(Register register, SpanByte data)
{
SpanByte toSend = new byte[data.Length + 1];
toSend[0] = (byte)register;
WakeUpDevice();
data.CopyTo(toSend.Slice(1));
_i2cDevice.Write(toSend);
SleepDownDevice();
}
private byte ReadRegister(Register register)
{
WakeUpDevice();
_i2cDevice.WriteByte((byte)register);
var ret = _i2cDevice.ReadByte();
SleepDownDevice();
return ret;
}
private void ReadRegister(Register register, SpanByte dataRead)
{
WakeUpDevice();
_i2cDevice.WriteByte((byte)register);
_i2cDevice.Read(dataRead);
SleepDownDevice();
}
#endregion
}
}