private ushort[] runCommand(SGPCommand cmd, ushort[] parameters = null) { bus.SetDevice(this.address); var bytesToWrite = new byte[cmd.OpCode.Length + (cmd.ParameterLength * (WORD_LENGTH + CRC_LENGTH))]; if (cmd.ParameterLength > 0 && parameters?.Length != cmd.ParameterLength) { throw new ArgumentException(nameof(parameters)); } Array.Copy(cmd.OpCode, 0, bytesToWrite, 0, cmd.OpCode.Length); if (parameters != null) { foreach (var i in Enumerable.Range(0, parameters.Length)) { Span <byte> span = bytesToWrite.AsSpan().Slice(i * (WORD_LENGTH + CRC_LENGTH) + cmd.OpCode.Length, (WORD_LENGTH + CRC_LENGTH)); span[0] = (byte)(parameters[i] >> 8); span[1] = (byte)(parameters[i] & 0xFF); span[2] = CRC(span.Slice(0, WORD_LENGTH)); } } var argBytes = bytesToWrite.AsSpan(1); bus.WriteI2cBlockData(bytesToWrite[0], argBytes.ToArray()); Thread.Sleep(cmd.ResponseTimeMs); if (cmd.ResponseLength > 0) { var data = new byte[cmd.ResponseLength * (WORD_LENGTH + CRC_LENGTH)]; bus.ReadI2cBlockData(0, data); ushort[] result = ReadCheckSummedWords(data); return(result); } return(Array.Empty <ushort>()); }
public void InitAirQuality() { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x20, 0x03 }, ResponseLength = 0, ResponseTimeMs = 10, ParameterLength = 0 }; var result = this.runCommand(cmd); }
public void SetBaseline(ushort a, ushort b) { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x20, 0x1e }, ResponseLength = 0, ResponseTimeMs = 10, ParameterLength = 2 }; this.runCommand(cmd, new ushort[] { a, b }); }
public (ushort Raw_CO2, ushort Raw_VOC) GetBaseline() { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x20, 0x15 }, ResponseLength = 2, ResponseTimeMs = 10, ParameterLength = 0 }; var result = this.runCommand(cmd); return(Raw_CO2 : result[0], Raw_VOC : result[1]); }
public (ushort CO2_PartsPerMillion, ushort VOC_PartsPerMillion) MeasureAirQuality() { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x20, 0x08 }, ResponseLength = 2, ResponseTimeMs = 12, ParameterLength = 0 }; var result = this.runCommand(cmd); return(CO2_PartsPerMillion : result[0], VOC_PartsPerMillion : result[1]); }
public int GetFeatureSetVersion() { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x20, 0x2f }, ResponseLength = 1, ResponseTimeMs = 2, ParameterLength = 0 }; var result = this.runCommand(cmd); return(result[0]); }
public string GetSerialNumber() { SGPCommand cmd = new SGPCommand() { OpCode = new byte[] { 0x36, 0x82 }, ResponseLength = 3, ResponseTimeMs = 1, ParameterLength = 0 }; var result = this.runCommand(cmd); return(string.Join(" ", result.Select(x => string.Format("0x{0:X4}", x)))); }