|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Device.I2c; |
| 6 | +using System.Device.Model; |
| 7 | +using System.Threading; |
| 8 | +using UnitsNet; |
| 9 | + |
| 10 | +namespace Iot.Device.Mcp9808 |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Microchip's MCP9808 I2C Temp sensor |
| 14 | + /// </summary> |
| 15 | + [Interface("Microchip's MCP9808 I2C Temp sensor")] |
| 16 | + public class Mcp9808 : IDisposable |
| 17 | + { |
| 18 | + private I2cDevice _i2cDevice; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// MCP9808 I2C Address |
| 22 | + /// </summary> |
| 23 | + public const byte DefaultI2cAddress = 0x18; |
| 24 | + |
| 25 | + #region prop |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// MCP9808 Temperature |
| 29 | + /// </summary> |
| 30 | + [Telemetry] |
| 31 | + public Temperature Temperature { get => Temperature.FromDegreesCelsius(GetTemperature()); } |
| 32 | + |
| 33 | + private bool _disable; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Disable MCP9808 |
| 37 | + /// </summary> |
| 38 | + [Property] |
| 39 | + public bool Disabled |
| 40 | + { |
| 41 | + get => _disable; |
| 42 | + set |
| 43 | + { |
| 44 | + SetShutdown(value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + #endregion |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Creates a new instance of the MCP9808 |
| 52 | + /// </summary> |
| 53 | + /// <param name="i2cDevice">The I2C device used for communication.</param> |
| 54 | + public Mcp9808(I2cDevice i2cDevice) |
| 55 | + { |
| 56 | + _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice)); |
| 57 | + |
| 58 | + Disabled = false; |
| 59 | + |
| 60 | + if (!Init()) |
| 61 | + { |
| 62 | + throw new Exception("Unable to identify manufacturer/device id"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Checks if the device is a MCP9808 |
| 68 | + /// </summary> |
| 69 | + /// <returns>True if device has been correctly detected</returns> |
| 70 | + private bool Init() |
| 71 | + { |
| 72 | + if (Read16(Register8.MCP_MANUF_ID) != 0x0054) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + if (Read16(Register8.MCP_DEVICE_ID) != 0x0400) |
| 78 | + { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Return the internal resolution register |
| 87 | + /// </summary> |
| 88 | + /// <returns>Resolution setting</returns> |
| 89 | + [Property("Resolution")] |
| 90 | + public byte GetResolution() => Read8(Register8.MCP_RESOLUTION); |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Wakes-up the device |
| 94 | + /// </summary> |
| 95 | + [Command] |
| 96 | + public void Wake() |
| 97 | + { |
| 98 | + SetShutdown(false); |
| 99 | + |
| 100 | + // Sleep 250 ms, which is the typical temperature conversion time for the highest resolution (page 3 in datasheet, tCONV) |
| 101 | + Thread.Sleep(250); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Shuts down the device |
| 106 | + /// </summary> |
| 107 | + [Command] |
| 108 | + public void Shutdown() => SetShutdown(true); |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Read MCP9808 Temperature (℃) |
| 112 | + /// </summary> |
| 113 | + /// <returns>Temperature</returns> |
| 114 | + private double GetTemperature() |
| 115 | + { |
| 116 | + ushort value = Read16(Register8.MCP_AMBIENT_TEMP); |
| 117 | + |
| 118 | + if (value == 0xFFFF) |
| 119 | + { |
| 120 | + // Return NaN if the MCP9808 doesn't have a measurement ready at the time of reading |
| 121 | + return double.NaN; |
| 122 | + } |
| 123 | + |
| 124 | + double temp = value & 0x0FFF; |
| 125 | + temp /= 16.0; |
| 126 | + if ((value & 0x1000) != 0) |
| 127 | + { |
| 128 | + temp -= 256; |
| 129 | + } |
| 130 | + |
| 131 | + return Math.Round(temp * 100000) / 100000; |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Set MCP9808 Shutdown |
| 136 | + /// </summary> |
| 137 | + /// <param name="isShutdown">Shutdown when value is true.</param> |
| 138 | + private void SetShutdown(bool isShutdown) |
| 139 | + { |
| 140 | + Register16 curVal = ReadRegister16(Register8.MCP_CONFIG); |
| 141 | + |
| 142 | + if (isShutdown) |
| 143 | + { |
| 144 | + curVal |= Register16.MCP_CONFIG_SHUTDOWN; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + curVal &= ~Register16.MCP_CONFIG_SHUTDOWN; |
| 149 | + } |
| 150 | + |
| 151 | + Write16(Register8.MCP_CONFIG, curVal); |
| 152 | + |
| 153 | + _disable = isShutdown; |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Cleanup |
| 158 | + /// </summary> |
| 159 | + public void Dispose() |
| 160 | + { |
| 161 | + _i2cDevice?.Dispose(); |
| 162 | + _i2cDevice = null!; |
| 163 | + } |
| 164 | + |
| 165 | + internal Register16 ReadRegister16(Register8 reg) => (Register16)Read16(reg); |
| 166 | + |
| 167 | + internal ushort Read16(Register8 reg) |
| 168 | + { |
| 169 | + _i2cDevice.WriteByte((byte)reg); |
| 170 | + SpanByte buf = new byte[2]; |
| 171 | + _i2cDevice.Read(buf); |
| 172 | + |
| 173 | + return (ushort)(buf[0] << 8 | buf[1]); |
| 174 | + } |
| 175 | + |
| 176 | + internal void Write16(Register8 reg, Register16 value) |
| 177 | + { |
| 178 | + _i2cDevice.Write(new byte[] |
| 179 | + { |
| 180 | + (byte)reg, |
| 181 | + (byte)((ushort)value >> 8), |
| 182 | + (byte)((ushort)value & 0xFF) |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + internal byte Read8(Register8 reg) |
| 187 | + { |
| 188 | + _i2cDevice.WriteByte((byte)reg); |
| 189 | + return _i2cDevice.ReadByte(); |
| 190 | + } |
| 191 | + |
| 192 | + internal void Write8(Register8 reg, byte value) |
| 193 | + { |
| 194 | + _i2cDevice.Write(new byte[] |
| 195 | + { |
| 196 | + (byte)reg, |
| 197 | + value |
| 198 | + }); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments