Skip to content

Commit 78dfbda

Browse files
authored
Improvements in Si7021 (#452)
1 parent 7eb7075 commit 78dfbda

File tree

13 files changed

+231
-106
lines changed

13 files changed

+231
-106
lines changed

devices/Si7021/Command.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Si7021
5+
{
6+
/// <summary>
7+
/// Si7021 Commands.
8+
/// </summary>
9+
internal enum Command : byte
10+
{
11+
SI_TEMP = 0xF3,
12+
SI_HUMI = 0xF5,
13+
SI_RESET = 0xFE,
14+
SI_REVISION_MSB = 0x84,
15+
SI_REVISION_LSB = 0xB8,
16+
SI_USER_REG1_WRITE = 0xE6,
17+
SI_USER_REG1_READ = 0xE7,
18+
19+
/// <summary>
20+
/// Read Electronic ID 1st Byte. 1 of 2 command sequence.
21+
/// </summary>
22+
SI_READ_Electronic_ID_1_1 = 0xFA,
23+
24+
/// <summary>
25+
/// Read Electronic ID 1st Byte. 2 of 2 command sequence.
26+
/// </summary>
27+
SI_READ_Electronic_ID_1_2 = 0x0F,
28+
29+
/// <summary>
30+
/// Read Electronic ID 2nd Byte. 1 of 2 command sequence.
31+
/// </summary>
32+
SI_READ_Electronic_ID_2_1 = 0xFA,
33+
34+
/// <summary>
35+
/// Read Electronic ID 2nd Byte. 2 of 2 command sequence.
36+
/// </summary>
37+
SI_READ_Electronic_ID_2_2 = 0x0F,
38+
}
39+
}

devices/Si7021/Extensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Si7021
5+
{
6+
/// <summary>
7+
/// Extensions for <see cref="Si7021"/> class.
8+
/// </summary>
9+
static public class Extensions
10+
{
11+
/// <summary>
12+
/// Returns a string representation of a byte array. Hexadecimal formated.
13+
/// </summary>
14+
/// <param name="array">The <see cref="byte"/>[] to convert to a <see cref="string"/>.</param>
15+
/// <returns>A <see cref="string"/> with the representation of <paramref name="array"/>.</returns>
16+
public static string AsText(this byte[] array)
17+
{
18+
string output = "";
19+
20+
foreach(var item in array)
21+
{
22+
output += item.ToString("X2");
23+
}
24+
25+
return output;
26+
}
27+
}
28+
}

devices/Si7021/FirmwareRevision.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Si7021
5+
{
6+
/// <summary>
7+
/// Si7021 firmware revision.
8+
/// </summary>
9+
public enum FirmwareRevision : byte
10+
{
11+
/// <summary>
12+
/// Unknown firmware revision.
13+
/// </summary>
14+
Unknow = 00,
15+
16+
/// <summary>
17+
/// Firmware version 2.0
18+
/// </summary>
19+
V2_0 = 0x20,
20+
21+
/// <summary>
22+
/// Firmware version 1.0
23+
/// </summary>
24+
V1_0 = 0xFF,
25+
}
26+
}

devices/Si7021/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ using (Si7021 sensor = new Si7021(device, Resolution.Resolution1))
5050
var tempValue = sensor.Temperature;
5151
var humValue = sensor.Humidity;
5252

53-
Debug.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C");
54-
Debug.WriteLine($"Relative humidity: {humValue:0.#}%");
53+
Debug.WriteLine($"Temperature: {tempValue.Celsius:N2}\u00B0C");
54+
Debug.WriteLine($"Relative humidity: {humValue:N2}%");
5555

5656
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
57-
Debug.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).Celsius:0.#}\u00B0C");
58-
Debug.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:0.#}\u00B0C");
57+
Debug.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).Celsius:N2}\u00B0C");
58+
Debug.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:N2}\u00B0C");
5959
Debug.WriteLine();
6060

6161
Thread.Sleep(1000);

devices/Si7021/Register.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

devices/Si7021/Si7021.cs

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,45 @@ namespace Iot.Device.Si7021
1616
[Interface("Temperature and Humidity Sensor Si7021")]
1717
public class Si7021 : IDisposable
1818
{
19+
private const byte SerialNumberLenght = 8;
20+
private const byte FwRevisionV2_0 = 0x20;
21+
private const byte FwRevisionV1_0 = 0xFF;
22+
1923
private I2cDevice _i2cDevice;
2024

2125
/// <summary>
22-
/// Si7021 Default I2C Address
26+
/// Si7021 Default I2C Address.
2327
/// </summary>
2428
public const byte DefaultI2cAddress = 0x40;
2529

2630
/// <summary>
27-
/// Si7021 Temperature
31+
/// Si7021 Temperature [°C].
2832
/// </summary>
2933
[Telemetry]
3034
public Temperature Temperature => Temperature.FromDegreesCelsius(GetTemperature());
3135

3236
/// <summary>
33-
/// Relative Humidity
37+
/// Relative Humidity.
3438
/// </summary>
3539
[Telemetry]
3640
public RelativeHumidity Humidity => GetHumidity();
3741

3842
/// <summary>
39-
/// Si7021 Firmware Revision
43+
/// Si7021 Firmware Revision.
4044
/// </summary>
4145
[Property]
42-
public byte Revision => GetRevision();
46+
public Version Revision => GetRevision();
4347

4448
/// <summary>
45-
/// Si7021 Measurement Resolution
49+
/// Si7021 Measurement Resolution.
4650
/// </summary>
4751
[Property]
4852
public Resolution Resolution { get => GetResolution(); set => SetResolution(value); }
4953

5054
private bool _heater;
5155

5256
/// <summary>
53-
/// Si7021 Heater
57+
/// Si7021 Heater.
5458
/// </summary>
5559
[Property]
5660
public bool Heater
@@ -64,30 +68,69 @@ public bool Heater
6468
}
6569

6670
/// <summary>
67-
/// Creates a new instance of the Si7021
71+
/// Individualized serial number of the Si7021.
6872
/// </summary>
69-
/// <param name="i2cDevice">I2C Device, like UnixI2cDevice or Windows10I2cDevice</param>
73+
public byte[] SerialNumber { get; private set; }
74+
75+
/// <summary>
76+
/// Creates a new instance of the Si7021.
77+
/// </summary>
78+
/// <param name="i2cDevice"><see cref="I2cDevice"/> to communicate with Si7021 device.</param>
7079
/// <param name="resolution">Si7021 Read Resolution</param>
7180
public Si7021(I2cDevice i2cDevice, Resolution resolution = Resolution.Resolution1)
7281
{
7382
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
7483

84+
// read electronic serial number
85+
ReadElectronicSerialNumber();
86+
7587
SetResolution(resolution);
7688
}
7789

7890
/// <summary>
79-
/// Get Si7021 Temperature (℃)
91+
/// Read electronic serial number.
92+
/// </summary>
93+
private void ReadElectronicSerialNumber()
94+
{
95+
SerialNumber = new byte[SerialNumberLenght];
96+
97+
// setup reading of 1st byte
98+
SpanByte writeBuff = new byte[2]
99+
{
100+
(byte)Command.SI_READ_Electronic_ID_1_1, (byte)Command.SI_READ_Electronic_ID_1_2
101+
};
102+
103+
_i2cDevice.Write(writeBuff);
104+
105+
// read 1st half and store in the initial half of the array
106+
_ = _i2cDevice.Read(new SpanByte(SerialNumber, 0, 4));
107+
108+
writeBuff = new byte[2]
109+
{
110+
(byte)Command.SI_READ_Electronic_ID_2_1, (byte)Command.SI_READ_Electronic_ID_2_2
111+
};
112+
113+
_i2cDevice.Write(writeBuff);
114+
115+
// read 2nd half and store in the respective half of the array
116+
_ = _i2cDevice.Read(new SpanByte(SerialNumber, 3, 4));
117+
}
118+
119+
/// <summary>
120+
/// Get Si7021 Temperature [°C].
80121
/// </summary>
81-
/// <returns>Temperature (℃)</returns>
122+
/// <returns>Temperature [°C].</returns>
82123
private double GetTemperature()
83124
{
84125
SpanByte readbuff = new byte[2];
85126

86127
// Send temperature command, read back two bytes
87-
_i2cDevice.WriteByte((byte)Register.SI_TEMP);
88-
// wait SCL free
89-
Thread.Sleep(20);
90-
_i2cDevice.Read(readbuff);
128+
_ = _i2cDevice.WriteByte((byte)Command.SI_TEMP);
129+
130+
// wait for conversion to complete: tCONV(T) max 11ms)
131+
Thread.Sleep(10);
132+
133+
_ = _i2cDevice.Read(readbuff);
91134

92135
// Calculate temperature
93136
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readbuff);
@@ -97,18 +140,20 @@ private double GetTemperature()
97140
}
98141

99142
/// <summary>
100-
/// Get Si7021 Relative Humidity (%)
143+
/// Get Si7021 Relative Humidity (%).
101144
/// </summary>
102-
/// <returns>Relative Humidity (%)</returns>
145+
/// <returns>Relative Humidity (%).</returns>
103146
private RelativeHumidity GetHumidity()
104147
{
105148
SpanByte readbuff = new byte[2];
106149

107150
// Send humidity read command, read back two bytes
108-
_i2cDevice.WriteByte((byte)Register.SI_HUMI);
109-
// wait SCL free
151+
_ = _i2cDevice.WriteByte((byte)Command.SI_HUMI);
152+
153+
// wait for conversion to complete: tCONV(RH) + tCONV(T) max 20ms
110154
Thread.Sleep(20);
111-
_i2cDevice.Read(readbuff);
155+
156+
_ = _i2cDevice.Read(readbuff);
112157

113158
// Calculate humidity
114159
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readbuff);
@@ -118,21 +163,30 @@ private RelativeHumidity GetHumidity()
118163
}
119164

120165
/// <summary>
121-
/// Get Si7021 Firmware Revision
166+
/// Get Si7021 firmware revision.
122167
/// </summary>
123-
/// <returns>Firmware Revision</returns>
124-
private byte GetRevision()
168+
/// <returns>The FirmwareRevision.</returns>
169+
private Version GetRevision()
125170
{
126171
SpanByte writeBuff = new byte[2]
127172
{
128-
(byte)Register.SI_REVISION_MSB, (byte)Register.SI_REVISION_LSB
173+
(byte)Command.SI_REVISION_MSB, (byte)Command.SI_REVISION_LSB
129174
};
130175

131176
_i2cDevice.Write(writeBuff);
132-
// wait SCL free
133-
Thread.Sleep(20);
134177

135-
return _i2cDevice.ReadByte();
178+
var fwRevision = _i2cDevice.ReadByte();
179+
180+
if (fwRevision == FwRevisionV2_0)
181+
{
182+
return new Version(2, 0);
183+
}
184+
else if (fwRevision == FwRevisionV1_0)
185+
{
186+
return new Version(1, 0);
187+
}
188+
189+
return new Version(0,0);
136190
}
137191

138192
/// <summary>
@@ -150,8 +204,9 @@ private void SetResolution(Resolution resolution)
150204

151205
SpanByte writeBuff = new byte[2]
152206
{
153-
(byte)Register.SI_USER_REG1_WRITE, reg1
207+
(byte)Command.SI_USER_REG1_WRITE, reg1
154208
};
209+
155210
_i2cDevice.Write(writeBuff);
156211
}
157212

@@ -188,21 +243,19 @@ private void SetHeater(bool isOn)
188243

189244
SpanByte writeBuff = new byte[2]
190245
{
191-
(byte)Register.SI_USER_REG1_WRITE, reg1
246+
(byte)Command.SI_USER_REG1_WRITE, reg1
192247
};
193248

194249
_i2cDevice.Write(writeBuff);
195250
}
196251

197252
/// <summary>
198-
/// Get User Register1
253+
/// Get User Register 1.
199254
/// </summary>
200-
/// <returns>User Register1 Byte</returns>
255+
/// <returns>Content of User Register 1.</returns>
201256
private byte GetUserRegister1()
202257
{
203-
_i2cDevice.WriteByte((byte)Register.SI_USER_REG1_READ);
204-
// wait SCL free
205-
Thread.Sleep(20);
258+
_i2cDevice.WriteByte((byte)Command.SI_USER_REG1_READ);
206259

207260
return _i2cDevice.ReadByte();
208261
}

devices/Si7021/Si7021.nfproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@
6060
</ItemGroup>
6161
<ItemGroup>
6262
<None Include="packages.config" />
63+
<Compile Include="Extensions.cs" />
64+
<Compile Include="FirmwareRevision.cs" />
6365
<Compile Include="Si7021.cs" />
64-
<Compile Include="Register.cs" />
66+
<Compile Include="Command.cs" />
6567
<Compile Include="Resolution.cs" />
6668
</ItemGroup>
6769
<ItemGroup>
@@ -74,11 +76,11 @@
7476
<ProjectConfigurationsDeclaredAsItems />
7577
</ProjectCapabilities>
7678
</ProjectExtensions>
77-
<Import Project="packages\Nerdbank.GitVersioning.3.5.103\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.5.103\build\Nerdbank.GitVersioning.targets')" />
79+
<Import Project="packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" />
7880
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
7981
<PropertyGroup>
8082
<ErrorText> This project references NuGet package(s) that are missing on this computer.Enable NuGet Package Restore to download them.For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
8183
</PropertyGroup>
82-
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.5.103\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.5.103\build\Nerdbank.GitVersioning.targets'))" />
84+
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets'))" />
8385
</Target>
8486
</Project>

0 commit comments

Comments
 (0)