forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDs18b20.cs
545 lines (467 loc) · 18.8 KB
/
Ds18b20.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// 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.Collections;
using System.Device.Model;
using System.Threading;
using nanoFramework.Device.OneWire;
using UnitsNet;
namespace Iot.Device.Ds18b20
{
/// <summary>
/// DS18B20 - Temperature sensor.
/// </summary>
[Interface("DS18B20 - Temperature sensor")]
public class Ds18b20
{
private const float ErrorTemperature = -999.99F;
private const byte ParasitePoweringModeValue = 0x00;
private OneWireHost _oneWireHost = null;
private bool _isTrackingChanges = false;
private double _tempLowAlarm = 0;
private double _tempHighAlarm = 0;
private Thread _changeTracker = null;
private float _temperatureInCelsius = 0;
private bool _manyDevicesConnected = false;
/// <summary>
/// Initializes a new instance of the <see cref="Ds18b20"/> class.
/// </summary>
/// <param name="oneWireHost">One wire host (logical bus) to use.</param>
/// <param name="deviceAddress">The device address (if null, then this device will search for one on the bus and latch on to the first one found).</param>
/// <param name="manyDevicesConnected">True for more than one sensor connected to bus.</param>
/// <param name="temperatureResolution">Sensor resolution.</param>
/// <exception cref="ArgumentException">When device address length is not equal to 8 characters.</exception>
/// <exception cref="ArgumentException">When device address is not Ds18b20 sensors family address.</exception>
public Ds18b20(OneWireHost oneWireHost, byte[] deviceAddress = null, bool manyDevicesConnected = false, TemperatureResolution temperatureResolution = TemperatureResolution.VeryHigh)
{
_oneWireHost = oneWireHost;
TemperatureResolution = temperatureResolution;
_manyDevicesConnected = manyDevicesConnected;
if (deviceAddress != null)
{
if (deviceAddress.Length != 8)
{
throw new ArgumentException();
}
if (deviceAddress[0] != (byte)RomCommands.FamilyCode)
{
throw new ArgumentException();
}
Address = deviceAddress;
}
_temperatureInCelsius = ErrorTemperature;
TemperatureHighAlarm = Temperature.FromDegreesCelsius(30);
TemperatureLowAlarm = Temperature.FromDegreesCelsius(20);
}
/// <summary>
/// Gets or sets temperature resolution.
/// </summary>
[Property("TemperatureResolution")]
public TemperatureResolution TemperatureResolution
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether alarm search mode is enabled.
/// </summary>
[Property("IsAlarmSearchCommandEnabled")]
public bool IsAlarmSearchCommandEnabled
{
get;
set;
}
/// <summary>
/// Gets or sets high temperature alarm. Min -55C, Max 125C.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">When high alarm temperature is not between -55 and 125 degrees.</exception>
[Property("TemperatureHighAlarm")]
public Temperature TemperatureHighAlarm
{
get
{
return Temperature.FromDegreesCelsius(_tempHighAlarm);
}
set
{
if (value.DegreesCelsius < -55 || value.DegreesCelsius > 125)
{
throw new ArgumentOutOfRangeException();
}
_tempHighAlarm = value.DegreesCelsius;
}
}
/// <summary>
/// Gets or sets low temperature alarm. Min -55C, Max 125C.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">When low alarm temperature is not between -55 and 125 degrees.</exception>
[Property("TemperatureLowAlarm")]
public Temperature TemperatureLowAlarm
{
get
{
return Temperature.FromDegreesCelsius(_tempLowAlarm);
}
set
{
if (value.DegreesCelsius < -55 || value.DegreesCelsius > 125)
{
throw new ArgumentOutOfRangeException();
}
_tempLowAlarm = value.DegreesCelsius;
}
}
/// <summary>
/// Gets a value indicating whether it's powered as parasite of the board.
/// </summary>
[Property("IsParasitePowered")]
public bool IsParasitePowered
{
get
{
SelectDevice();
var verify = _oneWireHost.WriteByte((byte)FunctionCommands.ReadPowerSupply);
return _oneWireHost.ReadByte() == ParasitePoweringModeValue;
}
}
/// <summary>
/// Delegate that defines method signature that will be called
/// when sensor value change event happens.
/// </summary>
/// <param name="temperature">
/// Contains the measured temperature if the sensor reacted on request.
/// Contains -999.99 otherwise.
/// </param>
public delegate void OnSensorChanged(Temperature temperature);
/// <summary>
/// Event that is called when the sensor value changes.
/// </summary>
public event OnSensorChanged SensorValueChanged;
/// <summary>
/// Gets or sets the 8-byte address of selected device
/// (since there could be more than one such devices on the bus).
/// </summary>
public byte[] Address { get; set; }
/// <summary>
/// Gets an array of addresses of all Ds18B20 devices on network or only
/// devices in alarm if alarm mode is set on.
/// </summary>
public byte[][] AddressNet { get; private set; }
/// <summary>
/// Initializes the sensor. This step will perform a reset of the 1-wire bus.
/// It will check for existence of a 1-wire device. If no address was provided, then the
/// 1-wire bus will be searched and the first device that matches the family code will be latched on to.
/// Developer should check for successful initialization by checking the value returned.
/// If ManyDevicesConnected is set it will keep searching until find last device, saving all in AddressNet array.
/// </summary>
/// <returns><b>true</b> on success, else <b>false</b></returns>
public bool Initialize()
{
int foundDevices = 0;
ArrayList allDevices = new ArrayList();
_oneWireHost.TouchReset();
if (Address == null)
{
if (_manyDevicesConnected)
{
if (_oneWireHost.FindFirstDevice(false, IsAlarmSearchCommandEnabled))
{
do
{
if (_oneWireHost.SerialNumber[0] == (byte)RomCommands.FamilyCode)
{
_oneWireHost.TouchReset();
Address = new byte[_oneWireHost.SerialNumber.Length];
Array.Copy(_oneWireHost.SerialNumber, Address, _oneWireHost.SerialNumber.Length);
foundDevices++;
allDevices.Add(Address);
}
}
while (_oneWireHost.FindNextDevice(false, IsAlarmSearchCommandEnabled));
}
if (foundDevices > 0)
{
AddressNet = new byte[foundDevices][];
int i = 0;
foreach (byte[] device in allDevices)
{
AddressNet[i] = new byte[device.Length];
Array.Copy(device, AddressNet[i], device.Length);
i++;
}
}
}
else
{
if (_oneWireHost.FindFirstDevice(true, IsAlarmSearchCommandEnabled))
{
do
{
if (_oneWireHost.SerialNumber[0] == (byte)RomCommands.FamilyCode)
{
Address = new byte[_oneWireHost.SerialNumber.Length];
Array.Copy(_oneWireHost.SerialNumber, Address, _oneWireHost.SerialNumber.Length);
foundDevices = 1;
break;
}
}
while (_oneWireHost.FindNextDevice(true, IsAlarmSearchCommandEnabled));
}
}
}
return foundDevices > 0;
}
/// <summary>
/// Reads the temperature. A return value indicates whether the reading succeeded.
/// </summary>
/// <param name="temperature">
/// Contains the measured temperature if the sensor reacted on request.
/// Contains -999.99 otherwise.
/// </param>
/// <returns><code>true</code> if measurement was not skipped, otherwise <code>false</code>.</returns>
[Telemetry("Temperature")]
public bool TryReadTemperature(out Temperature temperature) => Read(out temperature);
private bool Read(out Temperature temperature)
{
PrepareToRead();
SelectDevice();
var verify = _oneWireHost.WriteByte((byte)FunctionCommands.ReadScratchpad);
if (verify == 0)
{
temperature = Temperature.FromDegreesCelsius(ErrorTemperature);
return false;
}
var tempLo = _oneWireHost.ReadByte();
var tempHi = _oneWireHost.ReadByte();
if (_oneWireHost.TouchReset())
{
var temp = (tempHi << 8) | tempLo;
// Bits manipulation to represent negative values correctly.
if ((tempHi >> 7) == 1)
{
temp = temp | unchecked((int)0xffff0000);
}
_temperatureInCelsius = ((float)temp) / 16;
temperature = Temperature.FromDegreesCelsius(_temperatureInCelsius);
return true;
}
else
{
_temperatureInCelsius = ErrorTemperature;
temperature = Temperature.FromDegreesCelsius(_temperatureInCelsius);
return false;
}
}
/// <summary>
/// Reset the sensor.
/// </summary>
public void Reset()
{
_oneWireHost.TouchReset();
_temperatureInCelsius = ErrorTemperature;
}
/// <summary>
/// Search for alarm condition.
/// </summary>
/// <returns><b>true</b> on success, else <b>false</b></returns>
public bool SearchForAlarmCondition()
{
Address = null;
ConvertTemperature();
return Initialize();
}
/// <summary>
/// Reads sensor configuration.
/// </summary>
/// <param name="restoreRegisterFromEEPROM">Flag indicating if configuration should be restored from EEPROM.</param>
/// <returns><b>true</b> on success, else <b>false</b></returns>
public bool ConfigurationRead(bool restoreRegisterFromEEPROM = false)
{
var verificationValue = 0;
if (restoreRegisterFromEEPROM)
{
SelectDevice();
verificationValue = _oneWireHost.WriteByte((byte)FunctionCommands.RecallAlarmTriggerValues);
if (verificationValue == 0)
{
return false;
}
while (_oneWireHost.ReadByte() == 0)
{
Thread.Sleep(10);
}
}
SelectDevice();
verificationValue = _oneWireHost.WriteByte((byte)FunctionCommands.ReadScratchpad);
if (verificationValue == 0)
{
return false;
}
// Discard temperature bytes
_oneWireHost.ReadByte();
_oneWireHost.ReadByte();
_tempHighAlarm = (sbyte)_oneWireHost.ReadByte();
TemperatureHighAlarm = Temperature.FromDegreesCelsius(_tempHighAlarm);
_tempLowAlarm = (sbyte)_oneWireHost.ReadByte();
TemperatureLowAlarm = Temperature.FromDegreesCelsius(_tempLowAlarm);
int configReg = _oneWireHost.ReadByte();
if (_oneWireHost.TouchReset())
{
TemperatureResolution = (TemperatureResolution)(configReg >> 5);
}
else
{
TemperatureResolution = TemperatureResolution.VeryHigh;
return false;
}
return true;
}
/// <summary>
/// Writes sensor configuration. The unchanged registers will be overwritten.
/// </summary>
/// <param name="writeToEEPROM">Flag indicating if configuration should be written also to EEPROM.</param>
public void ConfigurationWrite(bool writeToEEPROM = false)
{
SelectDevice();
_oneWireHost.WriteByte((byte)FunctionCommands.WriteScratchpad);
_oneWireHost.WriteByte((byte)_tempHighAlarm);
_oneWireHost.WriteByte((byte)_tempLowAlarm);
_oneWireHost.WriteByte((byte)((byte)TemperatureResolution << 5));
if (writeToEEPROM)
{
SelectDevice();
_oneWireHost.WriteByte((byte)FunctionCommands.CopyScratchpad);
Thread.Sleep(10);
}
}
/// <summary>
/// Starts tracking the changes of temperature.
/// </summary>
/// <exception cref="InvalidOperationException">When tracking is already enabled.</exception>
/// <exception cref="ArgumentOutOfRangeException">When tracking interval is shorter than 50 milliseconds.</exception>
/// <exception cref="NotSupportedException">When event handler is not assigned.</exception>
/// <param name="trackingInterval">Interval to track the changes to sensor values.</param>
public void BeginTrackChanges(TimeSpan trackingInterval)
{
if (_isTrackingChanges)
{
throw new InvalidOperationException();
}
if (trackingInterval.TotalMilliseconds < 50)
{
throw new ArgumentOutOfRangeException();
}
if (SensorValueChanged == null)
{
throw new NotSupportedException();
}
_changeTracker = new Thread(() =>
{
int divs = (int)(trackingInterval.TotalMilliseconds / 1000);
while (_isTrackingChanges)
{
if (trackingInterval.TotalMilliseconds > 1000)
{
divs = (int)(trackingInterval.TotalMilliseconds / 1000);
while (_isTrackingChanges && divs > 0)
{
Thread.Sleep(1000);
divs--;
}
}
else
{
Thread.Sleep(trackingInterval);
}
if (HasSensorValueChanged() && SensorValueChanged != null)
{
SensorValueChanged(Temperature.FromDegreesCelsius(_temperatureInCelsius));
}
}
});
_isTrackingChanges = true;
_changeTracker.Start();
}
/// <summary>
/// Stops tracking changes.
/// </summary>
public void EndTrackChanges()
{
_isTrackingChanges = false;
// see BeginChangeTracker to know why 3000 is chosen - 3x of lowest wait time
Thread.Sleep(3000);
if (_changeTracker.IsAlive)
{
try
{
_changeTracker.Abort();
}
finally
{
_changeTracker = null;
}
}
}
private void SelectDevice()
{
if (Address != null && Address.Length == 8 && Address[0] == (byte)RomCommands.FamilyCode)
{
// do not convert to a for..loop
byte[] cmdAndData = new byte[9]
{
(byte)RomCommands.Match,
Address[0], Address[1], Address[2], Address[3], Address[4], Address[5], Address[6],
Address[7]
};
_oneWireHost.TouchReset();
foreach (var b in cmdAndData)
{
_oneWireHost.WriteByte(b);
}
}
}
private void ConvertTemperature()
{
_oneWireHost.TouchReset();
_oneWireHost.WriteByte((byte)RomCommands.Skip);
_oneWireHost.WriteByte((byte)FunctionCommands.ConvertTemperature);
int waitConversion;
switch (TemperatureResolution)
{
case TemperatureResolution.VeryLow:
waitConversion = 125;
break;
case TemperatureResolution.Low:
waitConversion = 250;
break;
case TemperatureResolution.High:
waitConversion = 500;
break;
case TemperatureResolution.VeryHigh:
default:
waitConversion = 1000;
break;
}
Thread.Sleep(waitConversion);
}
private void PrepareToRead()
{
if (Address != null && Address.Length == 8 && Address[0] == (byte)RomCommands.FamilyCode)
{
ConvertTemperature();
}
}
private bool HasSensorValueChanged()
{
float previousTemperature = _temperatureInCelsius;
var readStatus = Read(out _);
if (!readStatus)
{
return false;
}
float currentTemperature = _temperatureInCelsius;
bool valuesChanged = previousTemperature != currentTemperature;
return valuesChanged;
}
}
}