-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathMcp9808.cs
201 lines (168 loc) · 5.15 KB
/
Mcp9808.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
// 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.I2c;
using System.Device.Model;
using System.Threading;
using UnitsNet;
namespace Iot.Device.Mcp9808
{
/// <summary>
/// Microchip's MCP9808 I2C Temp sensor
/// </summary>
[Interface("Microchip's MCP9808 I2C Temp sensor")]
public class Mcp9808 : IDisposable
{
private I2cDevice _i2cDevice;
/// <summary>
/// MCP9808 I2C Address
/// </summary>
public const byte DefaultI2cAddress = 0x18;
#region prop
/// <summary>
/// MCP9808 Temperature
/// </summary>
[Telemetry]
public Temperature Temperature { get => Temperature.FromDegreesCelsius(GetTemperature()); }
private bool _disable;
/// <summary>
/// Disable MCP9808
/// </summary>
[Property]
public bool Disabled
{
get => _disable;
set
{
SetShutdown(value);
}
}
#endregion
/// <summary>
/// Creates a new instance of the MCP9808
/// </summary>
/// <param name="i2cDevice">The I2C device used for communication.</param>
public Mcp9808(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
Disabled = false;
if (!Init())
{
throw new Exception("Unable to identify manufacturer/device id");
}
}
/// <summary>
/// Checks if the device is a MCP9808
/// </summary>
/// <returns>True if device has been correctly detected</returns>
private bool Init()
{
if (Read16(Register8.MCP_MANUF_ID) != 0x0054)
{
return false;
}
if (Read16(Register8.MCP_DEVICE_ID) != 0x0400)
{
return false;
}
return true;
}
/// <summary>
/// Return the internal resolution register
/// </summary>
/// <returns>Resolution setting</returns>
[Property("Resolution")]
public byte GetResolution() => Read8(Register8.MCP_RESOLUTION);
/// <summary>
/// Wakes-up the device
/// </summary>
[Command]
public void Wake()
{
SetShutdown(false);
// Sleep 250 ms, which is the typical temperature conversion time for the highest resolution (page 3 in datasheet, tCONV)
Thread.Sleep(250);
}
/// <summary>
/// Shuts down the device
/// </summary>
[Command]
public void Shutdown() => SetShutdown(true);
/// <summary>
/// Read MCP9808 Temperature (℃)
/// </summary>
/// <returns>Temperature</returns>
private double GetTemperature()
{
ushort value = Read16(Register8.MCP_AMBIENT_TEMP);
if (value == 0xFFFF)
{
// Return NaN if the MCP9808 doesn't have a measurement ready at the time of reading
return double.NaN;
}
double temp = value & 0x0FFF;
temp /= 16.0;
if ((value & 0x1000) != 0)
{
temp -= 256;
}
return Math.Round(temp * 100000) / 100000;
}
/// <summary>
/// Set MCP9808 Shutdown
/// </summary>
/// <param name="isShutdown">Shutdown when value is true.</param>
private void SetShutdown(bool isShutdown)
{
Register16 curVal = ReadRegister16(Register8.MCP_CONFIG);
if (isShutdown)
{
curVal |= Register16.MCP_CONFIG_SHUTDOWN;
}
else
{
curVal &= ~Register16.MCP_CONFIG_SHUTDOWN;
}
Write16(Register8.MCP_CONFIG, curVal);
_disable = isShutdown;
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null!;
}
internal Register16 ReadRegister16(Register8 reg) => (Register16)Read16(reg);
internal ushort Read16(Register8 reg)
{
_i2cDevice.WriteByte((byte)reg);
SpanByte buf = new byte[2];
_i2cDevice.Read(buf);
return (ushort)(buf[0] << 8 | buf[1]);
}
internal void Write16(Register8 reg, Register16 value)
{
_i2cDevice.Write(new byte[]
{
(byte)reg,
(byte)((ushort)value >> 8),
(byte)((ushort)value & 0xFF)
});
}
internal byte Read8(Register8 reg)
{
_i2cDevice.WriteByte((byte)reg);
return _i2cDevice.ReadByte();
}
internal void Write8(Register8 reg, byte value)
{
_i2cDevice.Write(new byte[]
{
(byte)reg,
value
});
}
}
}