forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHdc1080.cs
204 lines (180 loc) · 7.27 KB
/
Hdc1080.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
// 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.Threading;
using System.Device.I2c;
using System.Device.Model;
using UnitsNet;
namespace Iot.Device.Hdc1080
{
/// <summary>
/// Temperature and Humidity sensor Hdc1080
/// </summary>
[Interface("Hdc1080 - temperature and humidity sensor")]
public class Hdc1080 : IDisposable
{
private I2cDevice _i2cDevice;
private int _wakeUpTimeMilliseconds = 20;
/// <summary>
/// Default I2C address
/// </summary>
public const byte DefaultI2cAddress = 0x40;
/// <summary>
/// Constructs Hdc1080 instance with high temperature and humidity sampling. Measurement mode set by default to humidity and temperature.
/// </summary>
/// <param name="i2cDevice">I2C device used to communicate with the device</param>
/// <param name="humidityResolution">Humidity sampling resolution</param>
/// <param name="temperatureResolution">Temperature sampling resolution</param>
public Hdc1080(I2cDevice i2cDevice, HumidityResolution humidityResolution = HumidityResolution.High, TemperatureResolution temperatureResolution = TemperatureResolution.High)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
// According to data sheet sensor needs at least 15ms after wake up to be ready.
Thread.Sleep(_wakeUpTimeMilliseconds);
TemperatureResolution = temperatureResolution;
HumidityResolution = humidityResolution;
}
/// <summary>
/// Reads temperature, check data sheet, page 14, 8.6.1 section
/// </summary>
/// <returns>
/// Temperature
/// </returns>
[Telemetry("Temperature")]
public Temperature ReadTemperature() => Temperature.FromDegreesCelsius((ReadData(RegisterAddress.TemperatureMeasurement) * 165 / 65536f) - 40);
/// <summary>
/// Reads relative humidity, check data sheet, page 14, 8.6.2 section
/// </summary>
/// <returns>
/// Relative humidity
/// </returns>
[Telemetry("Humidity")]
public RelativeHumidity ReadHumidity() => RelativeHumidity.FromPercent(ReadData(RegisterAddress.HumidityMeasurement) * 100 / 65536);
/// <summary>
/// Reads device id, check data sheet, page 14, 8.6.6 section
/// </summary>
/// <returns>
/// Device id
/// </returns>
[Property("DeviceId")]
public string DeviceId => ReadData(RegisterAddress.DeviceId).ToString();
/// <summary>
/// Reads manufacturer id, check data sheet, page 14, 8.6.5 section
/// </summary>
/// <returns>
/// Manufacturer id
/// </returns>
[Property("ManufacturerId")]
public string ManufacturerId => ReadData(RegisterAddress.ManufacturerId).ToString();
/// <summary>
/// Reads serial number, check data sheet, page 14, 8.6.4 section
/// </summary>
/// <returns>
/// Serial number
/// </returns>
[Property("SerialNumber")]
public string SerialNumber => $"{ReadData(RegisterAddress.SerialIdFirstByte)}{ReadData(RegisterAddress.SerialIdSecondByte)}{ReadData(RegisterAddress.SerialIdThirdByte)}";
/// <summary>
/// Sets or gets humidity resolution
/// </summary>
[Property("HumidityResolution")]
public HumidityResolution HumidityResolution
{
get
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
return (HumidityResolution)register.HumidityMeasurementResolution;
}
set
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
register.HumidityMeasurementResolution = (byte)value;
WriteRegister(register);
}
}
/// <summary>
/// Sets or gets temperature resolution
/// </summary>
[Property("TemperatureResolution")]
public TemperatureResolution TemperatureResolution
{
get
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
return (TemperatureResolution)register.TemperatureMeasurementResolution;
}
set
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
register.TemperatureMeasurementResolution = (byte)value;
WriteRegister(register);
}
}
/// <summary>
/// Runs heater for given time, check data sheet, page 9, 8.3.3 section
/// </summary>
/// <param name="heatingTime">Heating time. Must be greater than 1 second</param>
public void HeatUp(TimeSpan heatingTime)
{
if (heatingTime.Seconds < 1)
{
throw new ArgumentOutOfRangeException(nameof(heatingTime), "Heating time can't be lower than 1 second.");
}
SetHeater(true);
for (int i = 0; i < heatingTime.Seconds; i++)
{
_i2cDevice.Write(new byte[] { (byte)RegisterAddress.TemperatureMeasurement });
// wait SCL free
Thread.Sleep(_wakeUpTimeMilliseconds);
_i2cDevice.Read(new byte[4]);
Thread.Sleep(1000 - _wakeUpTimeMilliseconds);
}
SetHeater(false);
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null!;
}
private long ReadData(RegisterAddress address)
{
byte[] resultData = new byte[2];
_i2cDevice.Write(new byte[] { (byte)address });
// wait SCL free
Thread.Sleep(_wakeUpTimeMilliseconds);
_i2cDevice.Read(resultData);
return resultData[0] << 8 | resultData[1];
}
private void WriteRegister(ConfigurationRegister register)
{
_i2cDevice.Write(new byte[] { (byte)RegisterAddress.Configuration, register.GetData(), (byte)RegisterAddress.TemperatureMeasurement });
// wait SCL free
Thread.Sleep(_wakeUpTimeMilliseconds);
}
private ConfigurationRegister ReadRegister()
{
ConfigurationRegister register = new ConfigurationRegister();
register.SetData((byte)(ReadData(RegisterAddress.Configuration) >> 8));
return register;
}
private void SetHeater(bool isOn)
{
if (isOn)
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
register.Heater = true;
register.SeparateReadings = false;
WriteRegister(register);
}
else
{
ConfigurationRegister register = (ConfigurationRegister)ReadRegister();
register.Heater = false;
register.SeparateReadings = true;
WriteRegister(register);
}
}
}
}