@@ -16,41 +16,45 @@ namespace Iot.Device.Si7021
16
16
[ Interface ( "Temperature and Humidity Sensor Si7021" ) ]
17
17
public class Si7021 : IDisposable
18
18
{
19
+ private const byte SerialNumberLenght = 8 ;
20
+ private const byte FwRevisionV2_0 = 0x20 ;
21
+ private const byte FwRevisionV1_0 = 0xFF ;
22
+
19
23
private I2cDevice _i2cDevice ;
20
24
21
25
/// <summary>
22
- /// Si7021 Default I2C Address
26
+ /// Si7021 Default I2C Address.
23
27
/// </summary>
24
28
public const byte DefaultI2cAddress = 0x40 ;
25
29
26
30
/// <summary>
27
- /// Si7021 Temperature
31
+ /// Si7021 Temperature [°C].
28
32
/// </summary>
29
33
[ Telemetry ]
30
34
public Temperature Temperature => Temperature . FromDegreesCelsius ( GetTemperature ( ) ) ;
31
35
32
36
/// <summary>
33
- /// Relative Humidity
37
+ /// Relative Humidity.
34
38
/// </summary>
35
39
[ Telemetry ]
36
40
public RelativeHumidity Humidity => GetHumidity ( ) ;
37
41
38
42
/// <summary>
39
- /// Si7021 Firmware Revision
43
+ /// Si7021 Firmware Revision.
40
44
/// </summary>
41
45
[ Property ]
42
- public byte Revision => GetRevision ( ) ;
46
+ public Version Revision => GetRevision ( ) ;
43
47
44
48
/// <summary>
45
- /// Si7021 Measurement Resolution
49
+ /// Si7021 Measurement Resolution.
46
50
/// </summary>
47
51
[ Property ]
48
52
public Resolution Resolution { get => GetResolution ( ) ; set => SetResolution ( value ) ; }
49
53
50
54
private bool _heater ;
51
55
52
56
/// <summary>
53
- /// Si7021 Heater
57
+ /// Si7021 Heater.
54
58
/// </summary>
55
59
[ Property ]
56
60
public bool Heater
@@ -64,30 +68,69 @@ public bool Heater
64
68
}
65
69
66
70
/// <summary>
67
- /// Creates a new instance of the Si7021
71
+ /// Individualized serial number of the Si7021.
68
72
/// </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>
70
79
/// <param name="resolution">Si7021 Read Resolution</param>
71
80
public Si7021 ( I2cDevice i2cDevice , Resolution resolution = Resolution . Resolution1 )
72
81
{
73
82
_i2cDevice = i2cDevice ?? throw new ArgumentNullException ( nameof ( i2cDevice ) ) ;
74
83
84
+ // read electronic serial number
85
+ ReadElectronicSerialNumber ( ) ;
86
+
75
87
SetResolution ( resolution ) ;
76
88
}
77
89
78
90
/// <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].
80
121
/// </summary>
81
- /// <returns>Temperature (℃) </returns>
122
+ /// <returns>Temperature [°C]. </returns>
82
123
private double GetTemperature ( )
83
124
{
84
125
SpanByte readbuff = new byte [ 2 ] ;
85
126
86
127
// 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 ) ;
91
134
92
135
// Calculate temperature
93
136
ushort raw = BinaryPrimitives . ReadUInt16BigEndian ( readbuff ) ;
@@ -97,18 +140,20 @@ private double GetTemperature()
97
140
}
98
141
99
142
/// <summary>
100
- /// Get Si7021 Relative Humidity (%)
143
+ /// Get Si7021 Relative Humidity (%).
101
144
/// </summary>
102
- /// <returns>Relative Humidity (%)</returns>
145
+ /// <returns>Relative Humidity (%). </returns>
103
146
private RelativeHumidity GetHumidity ( )
104
147
{
105
148
SpanByte readbuff = new byte [ 2 ] ;
106
149
107
150
// 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
110
154
Thread . Sleep ( 20 ) ;
111
- _i2cDevice . Read ( readbuff ) ;
155
+
156
+ _ = _i2cDevice . Read ( readbuff ) ;
112
157
113
158
// Calculate humidity
114
159
ushort raw = BinaryPrimitives . ReadUInt16BigEndian ( readbuff ) ;
@@ -118,21 +163,30 @@ private RelativeHumidity GetHumidity()
118
163
}
119
164
120
165
/// <summary>
121
- /// Get Si7021 Firmware Revision
166
+ /// Get Si7021 firmware revision.
122
167
/// </summary>
123
- /// <returns>Firmware Revision </returns>
124
- private byte GetRevision ( )
168
+ /// <returns>The FirmwareRevision. </returns>
169
+ private Version GetRevision ( )
125
170
{
126
171
SpanByte writeBuff = new byte [ 2 ]
127
172
{
128
- ( byte ) Register . SI_REVISION_MSB , ( byte ) Register . SI_REVISION_LSB
173
+ ( byte ) Command . SI_REVISION_MSB , ( byte ) Command . SI_REVISION_LSB
129
174
} ;
130
175
131
176
_i2cDevice . Write ( writeBuff ) ;
132
- // wait SCL free
133
- Thread . Sleep ( 20 ) ;
134
177
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 ) ;
136
190
}
137
191
138
192
/// <summary>
@@ -150,8 +204,9 @@ private void SetResolution(Resolution resolution)
150
204
151
205
SpanByte writeBuff = new byte [ 2 ]
152
206
{
153
- ( byte ) Register . SI_USER_REG1_WRITE , reg1
207
+ ( byte ) Command . SI_USER_REG1_WRITE , reg1
154
208
} ;
209
+
155
210
_i2cDevice . Write ( writeBuff ) ;
156
211
}
157
212
@@ -188,21 +243,19 @@ private void SetHeater(bool isOn)
188
243
189
244
SpanByte writeBuff = new byte [ 2 ]
190
245
{
191
- ( byte ) Register . SI_USER_REG1_WRITE , reg1
246
+ ( byte ) Command . SI_USER_REG1_WRITE , reg1
192
247
} ;
193
248
194
249
_i2cDevice . Write ( writeBuff ) ;
195
250
}
196
251
197
252
/// <summary>
198
- /// Get User Register1
253
+ /// Get User Register 1.
199
254
/// </summary>
200
- /// <returns>User Register1 Byte </returns>
255
+ /// <returns>Content of User Register 1. </returns>
201
256
private byte GetUserRegister1 ( )
202
257
{
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 ) ;
206
259
207
260
return _i2cDevice . ReadByte ( ) ;
208
261
}
0 commit comments