|
| 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.Buffers.Binary; |
| 6 | +using System.Device.Spi; |
| 7 | +using System.IO; |
| 8 | +using UnitsNet; |
| 9 | + |
| 10 | +namespace Iot.Device.Max31856 |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Max31856 - cold-junction compensated thermocouple to digital converter |
| 14 | + /// </summary> |
| 15 | + public class Max31856 : IDisposable |
| 16 | + { |
| 17 | + private readonly byte _thermocoupleType; |
| 18 | + private SpiDevice _spiDevice; |
| 19 | + |
| 20 | + #region SpiSettings |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Spi Clock Frequency |
| 24 | + /// </summary> |
| 25 | + public const int SpiClockFrequency = 5_000_000; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// SPI data flow |
| 29 | + /// </summary> |
| 30 | + public const DataFlow SpiDataFlow = DataFlow.MsbFirst; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// SPI Mode |
| 34 | + /// </summary> |
| 35 | + public const SpiMode SpiModeSetup = SpiMode.Mode1; |
| 36 | + |
| 37 | + #endregion |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Command to Get Temperature from the device |
| 41 | + /// </summary> |
| 42 | + public Temperature GetTemperature() |
| 43 | + { |
| 44 | + return ReadTemperature(out byte[] spiOutputData); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Reads the temperature from the Cold-Junction sensor |
| 49 | + /// </summary> |
| 50 | + /// <returns> |
| 51 | + /// Temperature, precision +- 0.7 Celsius range from -20 Celsius to +85 Celsius |
| 52 | + /// </returns> |
| 53 | + public Temperature GetColdJunctionTemperature() => Temperature.FromDegreesCelsius(ReadCJTemperature()); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Creates a new instance of the Max31856. |
| 57 | + /// </summary> |
| 58 | + /// <param name="spiDevice">The communications channel to a device on a SPI bus</param> |
| 59 | + /// <param name="thermocoupleType">Thermocouple type. It Defaults to T.</param> |
| 60 | + public Max31856(SpiDevice spiDevice, ThermocoupleType thermocoupleType = ThermocoupleType.T) |
| 61 | + { |
| 62 | + _spiDevice = spiDevice ?? throw new ArgumentNullException(nameof(spiDevice)); |
| 63 | + _thermocoupleType = (byte)thermocoupleType; |
| 64 | + Initialize(); |
| 65 | + } |
| 66 | + |
| 67 | + #region private and internal |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Standard initialization routine. |
| 71 | + /// </summary> |
| 72 | + /// /// <remarks> |
| 73 | + /// You can add new write lines if you want to alter the settings of the device. Settings can be found in the Technical Manual |
| 74 | + /// </remarks> |
| 75 | + private void Initialize() |
| 76 | + { |
| 77 | + SpanByte configurationSetting0 = new byte[] |
| 78 | + { |
| 79 | + (byte)Register.WRITE_CR0, |
| 80 | + (byte)Register.ONESHOT_FAULT_SETTING |
| 81 | + }; |
| 82 | + SpanByte configurationSetting1 = new byte[] |
| 83 | + { |
| 84 | + (byte)Register.WRITE_CR1, |
| 85 | + _thermocoupleType |
| 86 | + }; |
| 87 | + |
| 88 | + Write(configurationSetting0); |
| 89 | + Write(configurationSetting1); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Command to Get Temperature from the Device |
| 95 | + /// </summary> |
| 96 | + /// <remarks> |
| 97 | + /// Initializes the device and then reads the data for the cold junction temperature also checks for errors to throw |
| 98 | + /// </remarks> |
| 99 | + private double ReadCJTemperature() |
| 100 | + { |
| 101 | + var spiOutputData = WriteRead(Register.READ_CR0, 16); |
| 102 | + var coldJunctionTemperature = ConvertspiOutputDataTempColdJunction(spiOutputData); |
| 103 | + return coldJunctionTemperature; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Converts the Thermocouple Temperature Reading |
| 108 | + /// </summary> |
| 109 | + /// <remarks> |
| 110 | + /// Takes the spi data as an input and outputs the Thermocouple Temperature Reading |
| 111 | + /// </remarks> |
| 112 | + private Temperature ReadTemperature(out byte[] spiOutputData) |
| 113 | + { |
| 114 | + spiOutputData = WriteRead(Register.READ_CR0, 16); |
| 115 | + double tempRaw = (((spiOutputData[13] & 0x7F) << 16) + (spiOutputData[14] << 8) + (spiOutputData[15])); |
| 116 | + double temperature = tempRaw / 4096; // temperature in degrees C |
| 117 | + if ((spiOutputData[13] & 0x80) == 0x80) // checks if the temp is negative |
| 118 | + { |
| 119 | + temperature = -temperature; |
| 120 | + } |
| 121 | + |
| 122 | + var temperatureOut = Temperature.FromDegreesCelsius(temperature); // Converts temperature to type Temperature struct and establishes it as Degrees C for its initial units |
| 123 | + |
| 124 | + return temperatureOut; |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Converts Cold Junction Temperature Reading |
| 129 | + /// </summary> |
| 130 | + /// <remarks> |
| 131 | + /// Takes the spi data as an input and outputs the Cold Junction Temperature Reading |
| 132 | + /// </remarks> |
| 133 | + /// <param name="spiOutputData">Spidata read from the device as 16 bytes</param> |
| 134 | + private double ConvertspiOutputDataTempColdJunction(byte[] spiOutputData) |
| 135 | + { |
| 136 | + SpanByte reading = new SpanByte(spiOutputData, 11, 2); |
| 137 | + reading[0] = (byte)(spiOutputData[11] & 0x7F); |
| 138 | + short tempRaw = BinaryPrimitives.ReadInt16BigEndian(reading); |
| 139 | + if ((spiOutputData[11] & 0x80) == 0x80) // checks if the temp is negative |
| 140 | + { |
| 141 | + return -tempRaw / 256.0; |
| 142 | + } |
| 143 | + |
| 144 | + return tempRaw / 256.0; |
| 145 | + } |
| 146 | + |
| 147 | + #endregion |
| 148 | + |
| 149 | + #region read and write operations |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Writes the Data to the Spi Device |
| 153 | + /// </summary> |
| 154 | + /// <remarks> |
| 155 | + /// Takes the data input byte and writes it to the spi device |
| 156 | + /// </remarks> |
| 157 | + /// <param name="data">Data to write to the device</param> |
| 158 | + private void Write(SpanByte data) => _spiDevice.Write(data); |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Full Duplex Read of the Data on the Device |
| 162 | + /// </summary> |
| 163 | + /// <remarks> |
| 164 | + /// Writes the read address of the register and outputs a byte list of the length provided |
| 165 | + /// </remarks> |
| 166 | + /// /// <param name="register">Register location to write to which starts the device reading</param> |
| 167 | + /// /// <param name="readbytesize">Number of bytes being read</param> |
| 168 | + private byte[] WriteRead(Register register, int readbytesize) |
| 169 | + { |
| 170 | + SpanByte readBuf = new byte[readbytesize + 1]; |
| 171 | + SpanByte regAddrBuf = new byte[1 + readbytesize]; |
| 172 | + |
| 173 | + regAddrBuf[0] = (byte)(register); |
| 174 | + _spiDevice.TransferFullDuplex(regAddrBuf, readBuf); |
| 175 | + var rawData = readBuf.ToArray(); |
| 176 | + return rawData; |
| 177 | + } |
| 178 | + |
| 179 | + #endregion |
| 180 | + |
| 181 | + /// <inheritdoc/> |
| 182 | + public void Dispose() |
| 183 | + { |
| 184 | + _spiDevice?.Dispose(); |
| 185 | + _spiDevice = null!; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments