forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Si7021.cs
270 lines (217 loc) · 7.75 KB
/
Si7021.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
// 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.Si7021
{
/// <summary>
/// Temperature and Humidity Sensor Si7021.
/// </summary>
[Interface("Temperature and Humidity Sensor Si7021")]
public class Si7021 : IDisposable
{
private const byte SerialNumberLenght = 8;
private const byte FwRevisionV20 = 0x20;
private const byte FwRevisionV10 = 0xFF;
private I2cDevice _i2cDevice;
/// <summary>
/// Si7021 Default I2C Address.
/// </summary>
public const byte DefaultI2cAddress = 0x40;
/// <summary>
/// Si7021 Temperature [°C].
/// </summary>
[Telemetry]
public Temperature Temperature => Temperature.FromDegreesCelsius(GetTemperature());
/// <summary>
/// Relative Humidity.
/// </summary>
[Telemetry]
public RelativeHumidity Humidity => GetHumidity();
/// <summary>
/// Si7021 Firmware Revision.
/// </summary>
[Property]
public Version Revision => GetRevision();
/// <summary>
/// Gets or sets measurement resolution.
/// </summary>
[Property]
public Resolution Resolution { get => GetResolution(); set => SetResolution(value); }
private bool _heater;
/// <summary>
/// Gets or sets a value indicating whether heater is on.
/// </summary>
[Property]
public bool Heater
{
get => _heater;
set
{
SetHeater(value);
_heater = value;
}
}
/// <summary>
/// Gets individualized serial number of the Si7021.
/// </summary>
public byte[] SerialNumber { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="Si7021" /> class.
/// </summary>
/// <param name="i2cDevice"><see cref="I2cDevice"/> to communicate with Si7021 device.</param>
/// <param name="resolution">Si7021 Read Resolution.</param>
public Si7021(I2cDevice i2cDevice, Resolution resolution = Resolution.Resolution1)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
// read electronic serial number
ReadElectronicSerialNumber();
SetResolution(resolution);
}
/// <summary>
/// Read electronic serial number.
/// </summary>
private void ReadElectronicSerialNumber()
{
SerialNumber = new byte[SerialNumberLenght];
// setup reading of 1st byte
SpanByte writeBuff = new byte[2]
{
(byte)Command.SI_READ_Electronic_ID_1_1, (byte)Command.SI_READ_Electronic_ID_1_2
};
_i2cDevice.Write(writeBuff);
// read 1st half and store in the initial half of the array
_ = _i2cDevice.Read(new SpanByte(SerialNumber, 0, 4));
writeBuff = new byte[2]
{
(byte)Command.SI_READ_Electronic_ID_2_1, (byte)Command.SI_READ_Electronic_ID_2_2
};
_i2cDevice.Write(writeBuff);
// read 2nd half and store in the respective half of the array
_ = _i2cDevice.Read(new SpanByte(SerialNumber, 3, 4));
}
/// <summary>
/// Get Si7021 Temperature [°C].
/// </summary>
/// <returns>Temperature [°C].</returns>
private double GetTemperature()
{
SpanByte readbuff = new byte[2];
// Send temperature command, read back two bytes
_ = _i2cDevice.WriteByte((byte)Command.SI_TEMP);
// wait for conversion to complete: tCONV(T) max 11ms)
Thread.Sleep(10);
_ = _i2cDevice.Read(readbuff);
// Calculate temperature
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readbuff);
double temp = ((175.72 * raw) / 65536.0) - 46.85;
return Math.Round(temp * 10) / 10.0;
}
/// <summary>
/// Get Si7021 Relative Humidity (%).
/// </summary>
/// <returns>Relative Humidity (%).</returns>
private RelativeHumidity GetHumidity()
{
SpanByte readbuff = new byte[2];
// Send humidity read command, read back two bytes
_ = _i2cDevice.WriteByte((byte)Command.SI_HUMI);
// wait for conversion to complete: tCONV(RH) + tCONV(T) max 20ms
Thread.Sleep(20);
_ = _i2cDevice.Read(readbuff);
// Calculate humidity
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readbuff);
double humidity = ((125 * raw) / 65536.0) - 6;
return RelativeHumidity.FromPercent(humidity);
}
/// <summary>
/// Get Si7021 firmware revision.
/// </summary>
/// <returns>The FirmwareRevision.</returns>
private Version GetRevision()
{
SpanByte writeBuff = new byte[2]
{
(byte)Command.SI_REVISION_MSB, (byte)Command.SI_REVISION_LSB
};
_i2cDevice.Write(writeBuff);
var fwRevision = _i2cDevice.ReadByte();
if (fwRevision == FwRevisionV20)
{
return new Version(2, 0);
}
else if (fwRevision == FwRevisionV10)
{
return new Version(1, 0);
}
return new Version(0, 0);
}
/// <summary>
/// Set Si7021 Measurement Resolution.
/// </summary>
/// <param name="resolution">Measurement Resolution.</param>
private void SetResolution(Resolution resolution)
{
byte reg1 = GetUserRegister1();
reg1 &= 0b0111_1110;
// Details in the Datasheet P25
reg1 = (byte)(reg1 | ((byte)resolution & 0b01) | (((byte)resolution & 0b10) >> 1 << 7));
SpanByte writeBuff = new byte[2]
{
(byte)Command.SI_USER_REG1_WRITE, reg1
};
_i2cDevice.Write(writeBuff);
}
/// <summary>
/// Get Si7021 Measurement Resolution.
/// </summary>
/// <returns>Measurement Resolution.</returns>
private Resolution GetResolution()
{
byte reg1 = GetUserRegister1();
byte bit0 = (byte)(reg1 & 0b0000_0001);
byte bit1 = (byte)((reg1 & 0b1000_0000) >> 7);
return (Resolution)(bit1 << 1 | bit0);
}
/// <summary>
/// Set Si7021 Heater.
/// </summary>
/// <param name="isOn">Heater on when value is true.</param>
private void SetHeater(bool isOn)
{
byte reg1 = GetUserRegister1();
if (isOn)
{
reg1 |= 0b0100;
}
else
{
reg1 &= 0b1111_1011;
}
SpanByte writeBuff = new byte[2]
{
(byte)Command.SI_USER_REG1_WRITE, reg1
};
_i2cDevice.Write(writeBuff);
}
/// <summary>
/// Get User Register 1.
/// </summary>
/// <returns>Content of User Register 1.</returns>
private byte GetUserRegister1()
{
_i2cDevice.WriteByte((byte)Command.SI_USER_REG1_READ);
return _i2cDevice.ReadByte();
}
/// <inheritdoc/>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null;
}
}
}