|
| 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.I2c; |
| 7 | +using System.Threading; |
| 8 | +using UnitsNet; |
| 9 | + |
| 10 | +namespace Iot.Device.Scd4x |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Humidity and Temperature Sensor Scd4x. |
| 14 | + /// </summary> |
| 15 | + public class Scd4x : IDisposable |
| 16 | + { |
| 17 | + private const byte Crc8PolyNominal = 0x31; |
| 18 | + private const byte Crc8 = 0xFF; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Default I2C address of the Scd4x sensor. |
| 22 | + /// </summary> |
| 23 | + public const byte I2cDefaultAddress = 0x62; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Represents an I2C device used for communication with the Scd4x sensor. |
| 27 | + /// </summary> |
| 28 | + private I2cDevice _i2CDevice; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="Scd4x" /> class. |
| 32 | + /// </summary> |
| 33 | + /// <param name="i2CDevice">The I2C device used for communication.</param> |
| 34 | + public Scd4x(I2cDevice i2CDevice) |
| 35 | + { |
| 36 | + _i2CDevice = i2CDevice ?? throw new ArgumentNullException(); |
| 37 | + } |
| 38 | + |
| 39 | + /// <inheritdoc/> |
| 40 | + public void Dispose() |
| 41 | + { |
| 42 | + _i2CDevice?.Dispose(); |
| 43 | + _i2CDevice = null; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Retrieves the serial number from the SDC device. |
| 48 | + /// </summary> |
| 49 | + /// <returns>Serial number.</returns> |
| 50 | + public string GetSerialNumber() |
| 51 | + { |
| 52 | + var result = _i2CDevice.Write(new byte[] { 0x36, 0x82 }); |
| 53 | + CheckI2cResultOrThrow(result); |
| 54 | + |
| 55 | + var buffer = new byte[9]; |
| 56 | + result = _i2CDevice.Read(buffer); |
| 57 | + CheckI2cResultOrThrow(result); |
| 58 | + |
| 59 | + var serial1 = ReadUInt16BigEndian(buffer, 0); |
| 60 | + var serial2 = ReadUInt16BigEndian(buffer, 2); |
| 61 | + var serial3 = ReadUInt16BigEndian(buffer, 4); |
| 62 | + return $"0x{serial1:X4}{serial2:X4}{serial3:X4}"; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Starts the periodic measurment. |
| 67 | + /// </summary> |
| 68 | + public void StartPeriodicMeasurement() |
| 69 | + { |
| 70 | + var result = _i2CDevice.Write(new byte[] { 0x21, 0xB1 }); |
| 71 | + CheckI2cResultOrThrow(result); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Reads the data. |
| 76 | + /// </summary> |
| 77 | + /// <returns>Sensor data.</returns> |
| 78 | + public Scd4xSensorData ReadData() |
| 79 | + { |
| 80 | + var result = _i2CDevice.Write(new byte[] { 0xEC, 0x05 }); |
| 81 | + CheckI2cResultOrThrow(result); |
| 82 | + |
| 83 | + Thread.Sleep(1); |
| 84 | + var buffer = new byte[9]; |
| 85 | + result = _i2CDevice.Read(buffer); |
| 86 | + CheckI2cResultOrThrow(result); |
| 87 | + |
| 88 | + var co2 = ReadUInt16BigEndian(buffer, 0); |
| 89 | + var tempRaw = ReadUInt16BigEndian(buffer, 3); |
| 90 | + var humRaw = ReadUInt16BigEndian(buffer, 6); |
| 91 | + |
| 92 | + var temp = (tempRaw * 175.0 / 65535.0) - 45.0; |
| 93 | + var hum = humRaw * 100 / 65535.0; |
| 94 | + return new Scd4xSensorData(Temperature.FromDegreesCelsius(temp), RelativeHumidity.FromPercent(hum), VolumeConcentration.FromPartsPerMillion(co2)); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Stops the periodic measurment. |
| 99 | + /// </summary> |
| 100 | + public void StopPeriodicMeasurement() |
| 101 | + { |
| 102 | + var result = _i2CDevice.Write(new byte[] { 0x3F, 0x86 }); |
| 103 | + CheckI2cResultOrThrow(result); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Gets the temperature offset of sensor. |
| 108 | + /// </summary> |
| 109 | + /// <returns>Temperature offset.</returns> |
| 110 | + public Temperature GetTemperatureOffset() |
| 111 | + { |
| 112 | + var result = _i2CDevice.Write(new byte[] { 0x23, 0x18 }); |
| 113 | + CheckI2cResultOrThrow(result); |
| 114 | + |
| 115 | + Thread.Sleep(1); |
| 116 | + var buffer = new byte[3]; |
| 117 | + result = _i2CDevice.Read(buffer); |
| 118 | + CheckI2cResultOrThrow(result); |
| 119 | + |
| 120 | + var tempRaw = ReadUInt16BigEndian(buffer, 0); |
| 121 | + var temp = tempRaw * 175.0 / 65535.0; |
| 122 | + return Temperature.FromDegreesCelsius(temp); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Sets the temperature offset. Doesn't affects CO2 measurments. |
| 127 | + /// </summary> |
| 128 | + /// <param name="offset">New offset.</param> |
| 129 | + public void SetTemperatureOffset(Temperature offset) |
| 130 | + { |
| 131 | + var asTicks = (offset.DegreesCelsius * 65536.0 / 175.0) + 0.5f; |
| 132 | + var tOffset = (ushort)asTicks; |
| 133 | + var crc = GenerateCRC(tOffset); |
| 134 | + var buffer = new byte[5]; |
| 135 | + buffer[0] = 0x24; |
| 136 | + buffer[1] = 0x1D; |
| 137 | + buffer[2] = (byte)(tOffset >> 8); |
| 138 | + buffer[3] = (byte)(tOffset & 0x00FF); |
| 139 | + buffer[4] = crc; |
| 140 | + |
| 141 | + var result = _i2CDevice.Write(buffer); |
| 142 | + CheckI2cResultOrThrow(result); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Gets the status of data. |
| 147 | + /// </summary> |
| 148 | + /// <returns>True if data is ready to read.</returns> |
| 149 | + public bool IsDataReady() |
| 150 | + { |
| 151 | + var result = _i2CDevice.Write(new byte[] { 0xE4, 0xB8 }); |
| 152 | + CheckI2cResultOrThrow(result); |
| 153 | + |
| 154 | + Thread.Sleep(1); |
| 155 | + var buffer = new byte[3]; |
| 156 | + result = _i2CDevice.Read(buffer); |
| 157 | + CheckI2cResultOrThrow(result); |
| 158 | + |
| 159 | + var word = ReadUInt16BigEndian(buffer, 0); |
| 160 | + return (word & 0x07FF) != 0; |
| 161 | + } |
| 162 | + |
| 163 | + private static byte GenerateCRC(byte[] data) |
| 164 | + { |
| 165 | + byte crc = Crc8; |
| 166 | + |
| 167 | + foreach (var currentByte in data) |
| 168 | + { |
| 169 | + crc ^= currentByte; |
| 170 | + for (int crcBit = 0; crcBit < 8; crcBit++) |
| 171 | + { |
| 172 | + if ((crc & 0x80) != 0) |
| 173 | + { |
| 174 | + crc = (byte)((crc << 1) ^ Crc8PolyNominal); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + crc = (byte)(crc << 1); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return crc; |
| 184 | + } |
| 185 | + |
| 186 | + private static byte GenerateCRC(ushort data) |
| 187 | + { |
| 188 | + var highByte = (byte)(data >> 8); |
| 189 | + var lowByte = (byte)(data & 0xFF); |
| 190 | + return GenerateCRC(new byte[] { highByte, lowByte }); |
| 191 | + } |
| 192 | + |
| 193 | + private void CheckI2cResultOrThrow(I2cTransferResult status) |
| 194 | + { |
| 195 | + if (status.Status != I2cTransferStatus.FullTransfer) |
| 196 | + { |
| 197 | + throw new Exception(); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private ushort ReadUInt16BigEndian(byte[] buffer, int startIndex) |
| 202 | + { |
| 203 | + return (ushort)((buffer[startIndex] << 8) | buffer[startIndex + 1]); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments