forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ds1621.cs
355 lines (310 loc) · 12.9 KB
/
Ds1621.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// 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.Device.Gpio;
using System.Device.I2c;
using Iot.Device.Common;
using UnitsNet;
using UnitsNet.Units;
namespace Iot.Device.Ds1621
{
/// <summary>
/// I2C digital thermometer with temperature range of -55°C to 125°C and 0.5°C resolution.
/// </summary>
public class Ds1621 : IDisposable
{
/// <summary>
/// Default I2C address for Ds1621.
/// </summary>
public const byte DefaultI2cAddress = 0x48;
/// <summary>
/// The underlying I2C device used for communication.
/// </summary>
private readonly I2cDevice _i2cDevice;
/// <summary>
/// Converts the devices interal two-byte temperature format into a floating point representation.
/// </summary>
/// <param name="temperatureMSB">The most significant bit of the temperature register.</param>
/// <param name="temperatureLSB">The least significant bit of the temperature register.</param>
/// <returns>The temperature in degrees Celsius.</returns>
internal static Temperature UnpackTemperature(byte temperatureMSB, byte temperatureLSB)
{
double unpackedTemperature = (sbyte)temperatureMSB;
// Check if 0.5°C bit is set.
if (temperatureLSB == 0x80)
{
unpackedTemperature += 0.5f;
}
return Temperature.FromDegreesCelsius(unpackedTemperature);
}
/// <summary>
/// Converts a temperature into the devices interal two-byte temperature format.
/// </summary>
/// <param name="temperature">The temperature to convert.</param>
/// <returns>
/// A byte array holding the temperature.
/// </returns>
/// <remarks>
/// The temperature will be clamped to the the range -55°C through 125°C and rounded to the nearest 0.5°C.
/// </remarks>
internal static byte[] PackTemperature(Temperature temperature)
{
byte[] packedTemperature = new byte[2];
// Clamp temperature to the valid range for a Ds1621.
double clampedTemperature = Math.Clamp(temperature.DegreesCelsius, -55, 125);
// Get temperature fraction for use in rounding temperature to nearest 0.5°C.
double fractionalPortion = clampedTemperature - (int)clampedTemperature;
if (clampedTemperature >= 0)
{
if (fractionalPortion >= 0.75)
{
// Round temperature up and store in two's complement.
packedTemperature[0] = (byte)(sbyte)((int)clampedTemperature + 1);
// No half degree flag.
packedTemperature[1] = 0;
}
else
{
// Truncate temperature and store in two's complement.
packedTemperature[0] = (byte)(sbyte)clampedTemperature;
if (fractionalPortion <= 0.25)
{
// No half degree flag.
packedTemperature[1] = 0;
}
else
{
// Round to 0.5°C (Set half degree flag).
packedTemperature[1] = 0x80;
}
}
}
else
{
if (fractionalPortion >= -0.25)
{
// Round temperature down and store in two's complement.
packedTemperature[0] = (byte)~(-(sbyte)clampedTemperature - 1);
// No half degree flag.
packedTemperature[1] = 0;
}
else
{
// Truncate temperature and store in two's complement.
packedTemperature[0] = (byte)~(-(sbyte)clampedTemperature);
if (fractionalPortion <= -0.75)
{
// No half degree flag.
packedTemperature[1] = 0;
}
else
{
// Round to 0.5°C (Set half degree flag).
packedTemperature[1] = 0x80;
}
}
}
return packedTemperature;
}
/// <summary>
/// Initializes a new instance of the <see cref="Ds1621" /> class.
/// </summary>
/// <param name="i2cDevice">The I2C device to use for communication.</param>
/// <param name="mode">The measurement mode to use when the device receives a temperature measurement command.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="i2cDevice"/> is <c>null</c>.</exception>
public Ds1621(I2cDevice i2cDevice, MeasurementMode mode = MeasurementMode.Single)
{
if (i2cDevice == null)
{
throw new ArgumentNullException();
}
_i2cDevice = i2cDevice;
MeasurementMode = mode;
}
#region Temperature
/// <summary>
/// Requests the device perform a temperature measurement.
/// </summary>
/// <remarks>
/// A temperature measurement can take up to 750ms.
/// </remarks>
public void MeasureTemperature()
{
_i2cDevice.WriteByte((byte)Command.StartTemperatureConversion);
}
/// <summary>
/// Requests the device stop performing temperature measurement.
/// </summary>
public void StopTemperatureMeasurement()
{
_i2cDevice.WriteByte((byte)Command.StopTemperatureConversion);
}
/// <summary>
/// Gets a value indicating whether the device is currently performing a temperature measurement.
/// </summary>
public bool IsMeasuringTemperature
{
get
{
return RegisterHelper.RegisterBitIsSet(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.TemperatureConversionDoneMask);
}
}
/// <summary>
/// Returns the last temperature measurement taken by the device in the range -55°C through 125°C and rounded to the nearest 0.5°C.
/// </summary>
/// <returns>
/// A <see cref = "Temperature" /> object whose value is a 9-bit representation of the last temperature measurement taken by the device.
/// </returns>
public Temperature GetTemperature()
{
byte[] temperature = RegisterHelper.ReadRegisterBlock(_i2cDevice, (byte)Register.Temperature, 2);
return UnpackTemperature(temperature[0], temperature[1]);
}
/// <summary>
/// Returns the last temperature measurement taken by the device in the range -55°C through 125°C.
/// </summary>
/// <returns>
/// A <see cref = "Temperature" /> object whose value is a 12-bit representation of the last temperature measurement taken by the device.
/// </returns>
public Temperature GetHighResolutionTemperature()
{
// Register reads must be done in the following order.
byte[] temperature = RegisterHelper.ReadRegisterBlock(_i2cDevice, (byte)Register.Temperature, 2);
byte countsRemaining = RegisterHelper.ReadRegister(_i2cDevice, (byte)Register.CountsRemaining);
byte countsPerDegree = RegisterHelper.ReadRegister(_i2cDevice, (byte)Register.CountsPerDegree);
double highResolutionTemperature = temperature[0] - 0.25d + ((countsPerDegree - countsRemaining) / (double)countsPerDegree);
return Temperature.FromDegreesCelsius(highResolutionTemperature);
}
#endregion
#region High Temperature Alarm
/// <summary>
/// Gets or sets the current high temperature alarm threshold.
/// </summary>
/// <remarks>
/// The temperature will be clamped to the the range -55°C through 125°C and rounded to the nearest 0.5°C.
/// </remarks>
public Temperature HighTemperatureAlarm
{
get
{
byte[] highTemperature = RegisterHelper.ReadRegisterBlock(_i2cDevice, (byte)Register.HighTemperature, 2);
return UnpackTemperature(highTemperature[0], highTemperature[1]);
}
set
{
byte[] highTemperature = PackTemperature(value);
RegisterHelper.WriteRegisterBlock(_i2cDevice, (byte)Register.HighTemperature, highTemperature);
}
}
/// <summary>
/// Gets a value indicating whether the device has triggered a high temperature alarm.
/// </summary>
public bool HasHighTemperatureAlarm
{
get
{
return RegisterHelper.RegisterBitIsSet(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.HighTemperatureAlarmMask);
}
}
/// <summary>
/// Clears the high temperature alarm flag.
/// </summary>
public void ResetHighTemperatureAlarm()
{
RegisterHelper.ClearRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.HighTemperatureAlarmMask);
}
#endregion
#region Low Temperature Alarm
/// <summary>
/// Gets or sets the current low temperature alarm threshold.
/// </summary>
/// <remarks>
/// The temperature will be clamped to the the range -55°C through 125°C and rounded to the nearest 0.5°C.
/// </remarks>
public Temperature LowTemperatureAlarm
{
get
{
byte[] lowTemperature = RegisterHelper.ReadRegisterBlock(_i2cDevice, (byte)Register.LowTemperature, 2);
return UnpackTemperature(lowTemperature[0], lowTemperature[1]);
}
set
{
byte[] lowTemperature = PackTemperature(value);
RegisterHelper.WriteRegisterBlock(_i2cDevice, (byte)Register.LowTemperature, lowTemperature);
}
}
/// <summary>
/// Gets a value indicating whether the device has triggered a low temperature alarm.
/// </summary>
public bool HasLowTemperatureAlarm
{
get
{
return RegisterHelper.RegisterBitIsSet(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.LowTemperatureAlarmMask);
}
}
/// <summary>
/// Clears the low temperature alarm flag.
/// </summary>
public void ResetLowTemperatureAlarm()
{
RegisterHelper.ClearRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.LowTemperatureAlarmMask);
}
#endregion
/// <summary>
/// Gets or sets the polarity of the thermostat output pin when a temperature alarm is asserted.
/// </summary>
public PinValue OutputPolarity
{
get
{
if (RegisterHelper.RegisterBitIsSet(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OutputPolarityMask))
{
return PinValue.High;
}
return PinValue.Low;
}
set
{
if (value == PinValue.High)
{
RegisterHelper.SetRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OutputPolarityMask);
}
else
{
RegisterHelper.ClearRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OutputPolarityMask);
}
}
}
/// <summary>
/// Gets or sets the mode used when the device receives a temperature measurement command.
/// </summary>
public MeasurementMode MeasurementMode
{
get
{
if (RegisterHelper.RegisterBitIsSet(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OneShotConversionModeMask))
{
return MeasurementMode.Single;
}
return MeasurementMode.Continuous;
}
set
{
if (value == MeasurementMode.Single)
{
RegisterHelper.SetRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OneShotConversionModeMask);
}
else
{
RegisterHelper.ClearRegisterBit(_i2cDevice, (byte)Register.Configuration, (byte)RegisterMask.OneShotConversionModeMask);
}
}
}
/// <inheritdoc/>
public void Dispose()
{
}
}
}