-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Ags01db.cs
168 lines (144 loc) · 4.74 KB
/
Ags01db.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
// 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.Device.Model;
using System.Threading;
using UnitsNet;
namespace Iot.Device.Ags01db
{
/// <summary>
/// MEMS VOC Gas Sensor ASG01DB.
/// </summary>
[Interface("MEMS VOC Gas Sensor ASG01DB")]
public class Ags01db : IDisposable
{
/// <summary>
/// ASG01DB Default I2C Address.
/// </summary>
public const byte DefaultI2cAddress = 0x11;
// CRC const
private const byte CrcPolynomial = 0x31;
private const byte CrcInit = 0xFF;
private I2cDevice _i2cDevice;
private int _lastMeasurement = 0;
/// <summary>
/// ASG01DB VOC (Volatile Organic Compounds) Gas Concentration (ppm).
/// </summary>
[Telemetry]
public Ratio Concentration => Ratio.FromPartsPerMillion(GetConcentration());
/// <summary>
/// ASG01DB Version.
/// </summary>
[Property]
public byte Version => GetVersion();
/// <summary>
/// Initializes a new instance of the <see cref="Ags01db" /> class.
/// </summary>
/// <param name="i2cDevice">The I2C device used for communication.</param>
public Ags01db(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
}
/// <summary>
/// Get ASG01DB VOC Gas Concentration.
/// </summary>
/// <returns>Concentration (ppm).</returns>
private double GetConcentration()
{
// The time of two measurements should be more than 2s.
while (DateTime.UtcNow.Ticks - _lastMeasurement < 2000)
{
Thread.Sleep((int)DateTime.UtcNow.Ticks - _lastMeasurement);
}
// Details in the Datasheet P5
// Write command MSB, LSB
SpanByte writeBuff = new byte[2]
{
(byte)Register.ASG_DATA_MSB, (byte)Register.ASG_DATA_LSB
};
// Return data MSB, LSB, CRC checksum
SpanByte readBuff = new byte[3];
_i2cDevice.Write(writeBuff);
_i2cDevice.Read(readBuff);
_lastMeasurement = (int)DateTime.UtcNow.Ticks;
// CRC check error
if (!CheckCrc8(readBuff.Slice(0, 2), 2, readBuff[2]))
{
return -1;
}
ushort res = BinaryPrimitives.ReadUInt16BigEndian(readBuff.Slice(0, 2));
return res / 10.0;
}
/// <summary>
/// Get ASG01DB Version.
/// </summary>
/// <returns>ASG01DB Version.</returns>
private byte GetVersion()
{
// Details in the Datasheet P5
// Write command MSB, LSB
SpanByte writeBuff = new byte[2]
{
(byte)Register.ASG_VERSION_MSB, (byte)Register.ASG_VERSION_LSB
};
// Return version, CRC checksum
SpanByte readBuff = new byte[2];
_i2cDevice.Write(writeBuff);
_i2cDevice.Read(readBuff);
// CRC check error
if (!CheckCrc8(readBuff.Slice(0, 1), 1, readBuff[1]))
{
return unchecked((byte)-1);
}
return readBuff[0];
}
/// <summary>
/// 8-bit CRC Checksum Calculation.
/// </summary>
/// <param name="data">Raw Data.</param>
/// <param name="length">Data Length.</param>
/// <param name="crc8">Raw CRC8.</param>
/// <returns>Checksum is true or false.</returns>
private bool CheckCrc8(SpanByte data, int length, byte crc8)
{
// Details in the Datasheet P6
byte crc = CrcInit;
for (int i = 0; i < length; i++)
{
crc ^= data[i];
for (int j = 8; j > 0; j--)
{
if ((crc & 0x80) != 0)
{
crc = (byte)((crc << 1) ^ CrcPolynomial);
}
else
{
crc = (byte)(crc << 1);
}
}
}
if (crc == crc8)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
if (_i2cDevice != null)
{
_i2cDevice?.Dispose();
_i2cDevice = null;
}
}
}
}