Class Scd4x
A Sensirion SCD40 or SCD41 carbon dioxide sensor, and a driver for one on an I2C bus.
public sealed class Scd4x : IDisposable
- Inheritance
-
Scd4x
- Implements
- Inherited Members
Remarks
The static members are the part's datasheet, decoded exactly as the manufacturer specifies, and Scd4x.Sim, a part that is not there.
An instance drives the part over an I2cBus in periodic measurement: the part produces a result every five seconds, and Measure() waits for the next one. The part has one address, Address. Nothing is sent until Init() or the first Measure().
Constructors
Scd4x(I2cBus)
Creates a driver for the part on bus.
public Scd4x(I2cBus bus)
Parameters
busI2cBusThe bus the part is on.
Exceptions
- PamojaException
The native driver could not be created.
Fields
Address
The one address the part answers to.
public const byte Address = 98
Field Value
Properties
Serial
The 48-bit serial number read at initialization, or null before it.
public ulong? Serial { get; }
Property Value
Methods
AllowedDuringMeasurement(ushort)
Reports whether an SCD4x accepts a command while it is measuring.
public static bool AllowedDuringMeasurement(ushort command)
Parameters
commandushortThe command.
Returns
- bool
The value the C ABI computes.
AmbientPressurePascals(ushort)
Reads an SCD4x ambient-pressure word back as a pressure.
public static uint AmbientPressurePascals(ushort word)
Parameters
wordushortThe word.
Returns
- uint
The value the C ABI computes.
AmbientPressureWord(uint)
Builds the SCD4x ambient-pressure word for a pressure.
public static ushort AmbientPressureWord(uint pascals)
Parameters
pascalsuintThe pascals.
Returns
- ushort
The value the C ABI computes.
AutomaticSelfCalibrationEnabled(ushort)
Reports whether an SCD4x word says automatic self-calibration is on.
public static bool AutomaticSelfCalibrationEnabled(ushort word)
Parameters
wordushortThe word.
Returns
- bool
The value the C ABI computes.
AutomaticSelfCalibrationWord(bool)
Builds the SCD4x word that turns automatic self-calibration on or off.
public static ushort AutomaticSelfCalibrationWord(bool enabled)
Parameters
enabledboolThe enabled.
Returns
- ushort
The value the C ABI computes.
Celsius(ushort)
Converts a raw SCD4x temperature word to degrees Celsius.
public static float Celsius(ushort raw)
Parameters
rawushortThe raw.
Returns
- float
The value the C ABI computes.
CommandFrame(ushort)
Builds the two bytes that address an SCD4x command, most significant first.
public static byte[] CommandFrame(ushort command)
Parameters
commandushortThe command.
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
Crc(ReadOnlySpan<byte>)
Computes the CRC-8 an SCD4x appends to every data word.
public static byte Crc(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The data.
Returns
- byte
The value the C ABI computes.
DataReady()
Asks the part whether a periodic result is waiting.
public bool DataReady()
Returns
Exceptions
- PamojaException
The transfer failed, or the status word failed its checksum.
DataReady(ushort)
Reports whether an SCD4x data-ready word says a fresh result is waiting.
public static bool DataReady(ushort word)
Parameters
wordushortThe word.
Returns
- bool
The value the C ABI computes.
Dispose()
Releases the driver and its share of the bus.
public void Dispose()
ForcedRecalibrationCorrectionPpm(ushort)
Reads the correction a forced recalibration applied.
public static int? ForcedRecalibrationCorrectionPpm(ushort word)
Parameters
wordushortThe word.
Returns
- int?
The value, or null where the part defines none.
ForcedRecalibrationWord(bool, int)
Builds the word an SCD4x returns for a forced-recalibration outcome.
public static ushort ForcedRecalibrationWord(bool succeeded, int correctionPpm)
Parameters
Returns
- ushort
The value the C ABI computes.
HumidityMilliPercent(ushort)
Converts a raw SCD4x humidity word to milli-percent.
public static uint HumidityMilliPercent(ushort raw)
Parameters
rawushortThe raw.
Returns
- uint
The value the C ABI computes.
HumidityRaw(uint)
Builds the SCD4x humidity word that decodes to a relative humidity.
public static ushort HumidityRaw(uint milliPercent)
Parameters
milliPercentuintThe milli percent.
Returns
- ushort
The value the C ABI computes.
Init()
Stops any running measurement, reads the serial number, and starts periodic measurement.
public void Init()
Exceptions
- PamojaException
Nothing answered, or the serial number failed its checksum.
MaxDurationMs(ushort)
Returns how long an SCD4x command may take before its result can be read.
public static ushort? MaxDurationMs(ushort command)
Parameters
commandushortThe command.
Returns
- ushort?
The value, or null where the part defines none.
Measure()
Waits for the next periodic result and reads it, initializing the part first if needed.
public PamojaScd4xMeasurement Measure()
Returns
- PamojaScd4xMeasurement
The carbon dioxide, temperature, and humidity.
Exceptions
- PamojaException
As Init(), when no result becomes ready, and when a word fails its checksum.
MeasureSingleShot()
Runs one on-demand measurement on an SCD41, which takes five seconds.
public PamojaScd4xMeasurement MeasureSingleShot()
Returns
- PamojaScd4xMeasurement
The measurement.
Remarks
Exceptions
MeasurementBytes(ushort, ushort, ushort)
Builds the nine bytes an SCD4x sends for a set of raw words, each CRC included.
public static byte[] MeasurementBytes(ushort co2Ppm, ushort temperatureRaw, ushort humidityRaw)
Parameters
co2PpmushortThe co2 ppm.
temperatureRawushortThe temperature raw.
humidityRawushortThe humidity raw.
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
MeasurementFromPhysical(ushort, int, uint)
Builds the SCD4x measurement a sensor reporting these physical values would send.
public static PamojaScd4xMeasurement MeasurementFromPhysical(ushort co2Ppm, int milliCelsius, uint humidityMilliPercent)
Parameters
co2PpmushortThe co2 ppm.
milliCelsiusintThe milli celsius.
humidityMilliPercentuintThe humidity milli percent.
Returns
- PamojaScd4xMeasurement
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
MilliCelsius(ushort)
Converts a raw SCD4x temperature word to milli-degrees Celsius.
public static int MilliCelsius(ushort raw)
Parameters
rawushortThe raw.
Returns
- int
The value the C ABI computes.
ParseMeasurement(ReadOnlySpan<byte>)
Parses and CRC-checks a nine-byte SCD4x measurement frame.
public static PamojaScd4xMeasurement ParseMeasurement(ReadOnlySpan<byte> frame)
Parameters
frameReadOnlySpan<byte>The frame.
Returns
- PamojaScd4xMeasurement
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
RelativeHumidityPercent(ushort)
Converts a raw SCD4x humidity word to a relative humidity percentage.
public static float RelativeHumidityPercent(ushort raw)
Parameters
rawushortThe raw.
Returns
- float
The value the C ABI computes.
SelfTestPassed(ushort)
Reports whether an SCD4x self-test word says the part is healthy.
public static bool SelfTestPassed(ushort word)
Parameters
wordushortThe word.
Returns
- bool
The value the C ABI computes.
SerialNumber(ReadOnlySpan<byte>)
Reads the 48-bit serial number out of a nine-byte SCD4x frame.
public static ulong SerialNumber(ReadOnlySpan<byte> frame)
Parameters
frameReadOnlySpan<byte>The frame.
Returns
- ulong
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
SerialNumberFrame(ulong)
Builds the nine bytes an SCD4x sends for a serial number, each CRC included.
public static byte[] SerialNumberFrame(ulong serial)
Parameters
serialulongThe serial.
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
SetSensorAltitude(ushort)
Sets the altitude the part corrects its carbon dioxide reading for.
public void SetSensorAltitude(ushort meters)
Parameters
metersushortThe altitude above sea level.
Exceptions
- PamojaException
The transfer failed.
SetTemperatureOffset(uint)
Sets the temperature offset that compensates the part's own warmth, until power is lost.
public void SetTemperatureOffset(uint milliCelsius)
Parameters
milliCelsiusuintThe offset to subtract, in millidegrees.
Exceptions
- PamojaException
The transfer failed.
Start()
Starts periodic measurement.
public void Start()
Exceptions
- PamojaException
The transfer failed.
Stop()
Stops periodic measurement, after which the part takes its settings commands.
public void Stop()
Exceptions
- PamojaException
The transfer failed.
TemperatureOffsetMilliCelsius(ushort)
Reads an SCD4x temperature-offset word back as an offset.
public static uint TemperatureOffsetMilliCelsius(ushort word)
Parameters
wordushortThe word.
Returns
- uint
The value the C ABI computes.
TemperatureOffsetWord(uint)
Builds the SCD4x temperature-offset word for an offset.
public static ushort TemperatureOffsetWord(uint milliCelsius)
Parameters
milliCelsiusuintThe milli celsius.
Returns
- ushort
The value the C ABI computes.
TemperatureRaw(int)
Builds the SCD4x temperature word that decodes to a temperature.
public static ushort TemperatureRaw(int milliCelsius)
Parameters
milliCelsiusintThe milli celsius.
Returns
- ushort
The value the C ABI computes.
Word(ReadOnlySpan<byte>)
Reads a CRC-checked three-byte SCD4x word frame.
public static ushort Word(ReadOnlySpan<byte> frame)
Parameters
frameReadOnlySpan<byte>The frame.
Returns
- ushort
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
WordFrame(ushort)
Builds the three bytes an SCD4x sends for a word: the word then its CRC.
public static byte[] WordFrame(ushort value)
Parameters
valueushortThe value.
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.
WriteFrame(ushort, ushort)
Builds the five bytes that write a word to an SCD4x: command, word, CRC.
public static byte[] WriteFrame(ushort command, ushort value)
Parameters
Returns
- byte[]
The decoded value.
Exceptions
- PamojaException
The device data is not valid.