added sensirion

This commit is contained in:
grove5_admin committed 2026-04-29 20:55:55 +00:00
1 parent 1956d68bcc
commit 75804f2191
2 files changed
+1647

No files matched your search

+773
View File
@@ -0,0 +1,773 @@
/*
* THIS FILE IS AUTOMATICALLY GENERATED
*
* Generator: sensirion-driver-generator 1.1.2
* Product: scd4x
* Model-Version: 2.0
*/
/*
* Copyright (c) 2025, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "SensirionI2cScd4x.h"
#include <Arduino.h>
// make sure that we use the proper definition of NO_ERROR
#ifdef NO_ERROR
#undef NO_ERROR
#endif
#define NO_ERROR 0
#define ROUND(x) ((int32_t)((x) + 0.5))
static uint8_t communication_buffer[9] = {0};
SensirionI2cScd4x::SensirionI2cScd4x() {
}
float SensirionI2cScd4x::signalTemperature(uint16_t rawTemperature) {
float temperature = 0.0;
temperature = -45.0 + ((175.0 * rawTemperature) / 65535.0);
return temperature;
}
float SensirionI2cScd4x::signalRelativeHumidity(uint16_t rawRelativeHumidity) {
float relativeHumidity = 0.0;
relativeHumidity = (100.0 * rawRelativeHumidity) / 65535.0;
return relativeHumidity;
}
uint16_t
SensirionI2cScd4x::signalCo2Concentration(uint16_t rawCo2Concentration) {
uint16_t co2Concentration = 0;
co2Concentration = rawCo2Concentration;
return co2Concentration;
}
float SensirionI2cScd4x::signalTemperatureOffset(
uint16_t rawTemperatureOffset) {
float temperatureOffset = 0.0;
temperatureOffset = (175 * rawTemperatureOffset) / 65535.0;
return temperatureOffset;
}
uint32_t SensirionI2cScd4x::signalAmbientPressure(uint16_t rawAmbientPressure) {
uint32_t ambientPressure = 0;
ambientPressure = (uint32_t)rawAmbientPressure * 100;
return ambientPressure;
}
int16_t SensirionI2cScd4x::readMeasurement(uint16_t& aCo2Concentration,
float& aTemperature,
float& aRelativeHumidity) {
uint16_t rawCo2Concentration = 0;
uint16_t rawTemperature = 0;
uint16_t rawRelativeHumidity = 0;
int16_t localError = 0;
localError = readMeasurementRaw(rawCo2Concentration, rawTemperature,
rawRelativeHumidity);
if (localError != NO_ERROR) {
return localError;
}
aCo2Concentration =
SensirionI2cScd4x::signalCo2Concentration(rawCo2Concentration);
aTemperature = SensirionI2cScd4x::signalTemperature(rawTemperature);
aRelativeHumidity =
SensirionI2cScd4x::signalRelativeHumidity(rawRelativeHumidity);
return localError;
}
int16_t SensirionI2cScd4x::setTemperatureOffset(float temperatureOffset) {
int16_t localError = 0;
uint16_t rawTemperatureOffset =
(uint16_t)ROUND((temperatureOffset * 65535.0) / 175.0);
localError = setTemperatureOffsetRaw(rawTemperatureOffset);
if (localError != NO_ERROR) {
return localError;
}
return localError;
}
int16_t SensirionI2cScd4x::getTemperatureOffset(float& aTemperatureOffset) {
uint16_t rawTemperatureOffset = 0;
int16_t localError = 0;
localError = getTemperatureOffsetRaw(rawTemperatureOffset);
if (localError != NO_ERROR) {
return localError;
}
aTemperatureOffset =
SensirionI2cScd4x::signalTemperatureOffset(rawTemperatureOffset);
return localError;
}
int16_t SensirionI2cScd4x::setAmbientPressure(uint32_t ambientPressure) {
int16_t localError = 0;
uint16_t rawAmbientPressure = (uint16_t)ROUND(ambientPressure / 100.0);
localError = setAmbientPressureRaw(rawAmbientPressure);
if (localError != NO_ERROR) {
return localError;
}
return localError;
}
int16_t SensirionI2cScd4x::getAmbientPressure(uint32_t& aAmbientPressure) {
uint16_t rawAmbientPressure = 0;
int16_t localError = 0;
localError = getAmbientPressureRaw(rawAmbientPressure);
if (localError != NO_ERROR) {
return localError;
}
aAmbientPressure =
SensirionI2cScd4x::signalAmbientPressure(rawAmbientPressure);
return localError;
}
int16_t SensirionI2cScd4x::getDataReadyStatus(bool& arg0) {
uint16_t dataReadyStatus = 0;
int16_t localError = 0;
localError = getDataReadyStatusRaw(dataReadyStatus);
if (localError != NO_ERROR) {
return localError;
}
arg0 = (dataReadyStatus & 2047) != 0;
;
return localError;
}
int16_t
SensirionI2cScd4x::getSensorVariant(SCD4xSensorVariant& aSensorVariant) {
SCD4xSensorVariant retVal = SCD4X_SENSOR_VARIANT_MASK;
uint16_t rawSensorVariant = 0;
uint16_t mySensorVariant = 0;
int16_t localError = 0;
retVal = SCD4X_SENSOR_VARIANT_MASK;
uint16_t mask = (uint16_t)(retVal);
localError = getSensorVariantRaw(rawSensorVariant);
if (localError != NO_ERROR) {
return localError;
}
mySensorVariant = (uint16_t)(rawSensorVariant & mask);
if (mySensorVariant == (uint16_t)(SCD4X_SENSOR_VARIANT_SCD40)) {
retVal = SCD4X_SENSOR_VARIANT_SCD40;
} else if (mySensorVariant == (uint16_t)(SCD4X_SENSOR_VARIANT_SCD41)) {
retVal = SCD4X_SENSOR_VARIANT_SCD41;
} else if (mySensorVariant == (uint16_t)(SCD4X_SENSOR_VARIANT_SCD42)) {
retVal = SCD4X_SENSOR_VARIANT_SCD42;
} else if (mySensorVariant == (uint16_t)(SCD4X_SENSOR_VARIANT_SCD43)) {
retVal = SCD4X_SENSOR_VARIANT_SCD43;
}
aSensorVariant = retVal;
return localError;
}
int16_t SensirionI2cScd4x::measureAndReadSingleShot(uint16_t& aCo2Concentration,
float& aTemperature,
float& aRelativeHumidity) {
bool dataReady = false;
int16_t localError = 0;
localError = measureSingleShot();
if (localError != NO_ERROR) {
return localError;
}
localError = getDataReadyStatus(dataReady);
if (localError != NO_ERROR) {
return localError;
}
while (!dataReady) {
delay(100);
localError = getDataReadyStatus(dataReady);
if (localError != NO_ERROR) {
return localError;
}
}
localError =
readMeasurement(aCo2Concentration, aTemperature, aRelativeHumidity);
return localError;
}
int16_t SensirionI2cScd4x::startPeriodicMeasurement() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x21b1, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
return localError;
}
int16_t SensirionI2cScd4x::readMeasurementRaw(uint16_t& co2Concentration,
uint16_t& temperature,
uint16_t& relativeHumidity) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0xec05, buffer_ptr, 9);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 9);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 9,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(co2Concentration);
localError |= rxFrame.getUInt16(temperature);
localError |= rxFrame.getUInt16(relativeHumidity);
return localError;
}
int16_t SensirionI2cScd4x::stopPeriodicMeasurement() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3f86, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(500);
return localError;
}
int16_t SensirionI2cScd4x::setTemperatureOffsetRaw(uint16_t offsetTemperature) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x241d, buffer_ptr, 5);
localError |= txFrame.addUInt16(offsetTemperature);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t
SensirionI2cScd4x::getTemperatureOffsetRaw(uint16_t& offsetTemperature) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2318, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(offsetTemperature);
return localError;
}
int16_t SensirionI2cScd4x::setSensorAltitude(uint16_t sensorAltitude) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2427, buffer_ptr, 5);
localError |= txFrame.addUInt16(sensorAltitude);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t SensirionI2cScd4x::getSensorAltitude(uint16_t& sensorAltitude) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2322, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(sensorAltitude);
return localError;
}
int16_t SensirionI2cScd4x::setAmbientPressureRaw(uint16_t ambientPressure) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0xe000, buffer_ptr, 5);
localError |= txFrame.addUInt16(ambientPressure);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t SensirionI2cScd4x::getAmbientPressureRaw(uint16_t& ambientPressure) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0xe000, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(ambientPressure);
return localError;
}
int16_t
SensirionI2cScd4x::performForcedRecalibration(uint16_t targetCO2Concentration,
uint16_t& frcCorrection) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x362f, buffer_ptr, 5);
localError |= txFrame.addUInt16(targetCO2Concentration);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(400);
SensirionI2CRxFrame rxFrame(buffer_ptr, 5);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(frcCorrection);
return localError;
}
int16_t
SensirionI2cScd4x::setAutomaticSelfCalibrationEnabled(uint16_t ascEnabled) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2416, buffer_ptr, 5);
localError |= txFrame.addUInt16(ascEnabled);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t
SensirionI2cScd4x::getAutomaticSelfCalibrationEnabled(uint16_t& ascEnabled) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2313, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(ascEnabled);
return localError;
}
int16_t
SensirionI2cScd4x::setAutomaticSelfCalibrationTarget(uint16_t ascTarget) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x243a, buffer_ptr, 5);
localError |= txFrame.addUInt16(ascTarget);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t
SensirionI2cScd4x::getAutomaticSelfCalibrationTarget(uint16_t& ascTarget) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x233f, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(ascTarget);
return localError;
}
int16_t SensirionI2cScd4x::startLowPowerPeriodicMeasurement() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x21ac, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
return localError;
}
int16_t SensirionI2cScd4x::getDataReadyStatusRaw(uint16_t& dataReadyStatus) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0xe4b8, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(dataReadyStatus);
return localError;
}
int16_t SensirionI2cScd4x::persistSettings() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3615, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(800);
return localError;
}
int16_t SensirionI2cScd4x::getSerialNumber(uint64_t& serialNumber) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3682, buffer_ptr, 9);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 9);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 9,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getInteger(reinterpret_cast<uint8_t*>(&serialNumber),
LongInteger, 6);
return localError;
}
int16_t SensirionI2cScd4x::performSelfTest(uint16_t& sensorStatus) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3639, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(10000);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(sensorStatus);
return localError;
}
int16_t SensirionI2cScd4x::performFactoryReset() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3632, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1200);
return localError;
}
int16_t SensirionI2cScd4x::reinit() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x3646, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(30);
return localError;
}
int16_t SensirionI2cScd4x::getSensorVariantRaw(uint16_t& sensorVariant) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x202f, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(sensorVariant);
return localError;
}
int16_t SensirionI2cScd4x::measureSingleShot() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x219d, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(5000);
return localError;
}
int16_t SensirionI2cScd4x::measureSingleShotRhtOnly() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2196, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(50);
return localError;
}
int16_t SensirionI2cScd4x::powerDown() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x36e0, buffer_ptr, 2);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t SensirionI2cScd4x::wakeUp() {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x36f6, buffer_ptr, 2);
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
delay(30);
return localError;
}
int16_t SensirionI2cScd4x::setAutomaticSelfCalibrationInitialPeriod(
uint16_t ascInitialPeriod) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2445, buffer_ptr, 5);
localError |= txFrame.addUInt16(ascInitialPeriod);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t SensirionI2cScd4x::getAutomaticSelfCalibrationInitialPeriod(
uint16_t& ascInitialPeriod) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x2340, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(ascInitialPeriod);
return localError;
}
int16_t SensirionI2cScd4x::setAutomaticSelfCalibrationStandardPeriod(
uint16_t ascStandardPeriod) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x244e, buffer_ptr, 5);
localError |= txFrame.addUInt16(ascStandardPeriod);
if (localError != NO_ERROR) {
return localError;
}
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
return localError;
}
int16_t SensirionI2cScd4x::getAutomaticSelfCalibrationStandardPeriod(
uint16_t& ascStandardPeriod) {
int16_t localError = NO_ERROR;
uint8_t* buffer_ptr = communication_buffer;
SensirionI2CTxFrame txFrame =
SensirionI2CTxFrame::createWithUInt16Command(0x234b, buffer_ptr, 3);
localError =
SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
delay(1);
SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
rxFrame, *_i2cBus);
if (localError != NO_ERROR) {
return localError;
}
localError |= rxFrame.getUInt16(ascStandardPeriod);
return localError;
}
void SensirionI2cScd4x::begin(TwoWire& i2cBus, uint8_t i2cAddress) {
_i2cBus = &i2cBus;
_i2cAddress = i2cAddress;
}
+874
View File
@@ -0,0 +1,874 @@
/*
* THIS FILE IS AUTOMATICALLY GENERATED
*
* Generator: sensirion-driver-generator 1.1.2
* Product: scd4x
* Model-Version: 2.0
*/
/*
* Copyright (c) 2025, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SENSIRIONI2CSCD4X_H
#define SENSIRIONI2CSCD4X_H
#include <SensirionCore.h>
#include <Wire.h>
#define SCD40_I2C_ADDR_62 0x62
#define SCD41_I2C_ADDR_62 0x62
#define SCD43_I2C_ADDR_62 0x62
typedef enum {
SCD4X_START_PERIODIC_MEASUREMENT_CMD_ID = 0x21b1,
SCD4X_READ_MEASUREMENT_RAW_CMD_ID = 0xec05,
SCD4X_STOP_PERIODIC_MEASUREMENT_CMD_ID = 0x3f86,
SCD4X_SET_TEMPERATURE_OFFSET_RAW_CMD_ID = 0x241d,
SCD4X_GET_TEMPERATURE_OFFSET_RAW_CMD_ID = 0x2318,
SCD4X_SET_SENSOR_ALTITUDE_CMD_ID = 0x2427,
SCD4X_GET_SENSOR_ALTITUDE_CMD_ID = 0x2322,
SCD4X_SET_AMBIENT_PRESSURE_RAW_CMD_ID = 0xe000,
SCD4X_GET_AMBIENT_PRESSURE_RAW_CMD_ID = 0xe000,
SCD4X_PERFORM_FORCED_RECALIBRATION_CMD_ID = 0x362f,
SCD4X_SET_AUTOMATIC_SELF_CALIBRATION_ENABLED_CMD_ID = 0x2416,
SCD4X_GET_AUTOMATIC_SELF_CALIBRATION_ENABLED_CMD_ID = 0x2313,
SCD4X_SET_AUTOMATIC_SELF_CALIBRATION_TARGET_CMD_ID = 0x243a,
SCD4X_GET_AUTOMATIC_SELF_CALIBRATION_TARGET_CMD_ID = 0x233f,
SCD4X_START_LOW_POWER_PERIODIC_MEASUREMENT_CMD_ID = 0x21ac,
SCD4X_GET_DATA_READY_STATUS_RAW_CMD_ID = 0xe4b8,
SCD4X_PERSIST_SETTINGS_CMD_ID = 0x3615,
SCD4X_GET_SERIAL_NUMBER_CMD_ID = 0x3682,
SCD4X_PERFORM_SELF_TEST_CMD_ID = 0x3639,
SCD4X_PERFORM_FACTORY_RESET_CMD_ID = 0x3632,
SCD4X_REINIT_CMD_ID = 0x3646,
SCD4X_GET_SENSOR_VARIANT_RAW_CMD_ID = 0x202f,
SCD4X_MEASURE_SINGLE_SHOT_CMD_ID = 0x219d,
SCD4X_MEASURE_SINGLE_SHOT_RHT_ONLY_CMD_ID = 0x2196,
SCD4X_POWER_DOWN_CMD_ID = 0x36e0,
SCD4X_WAKE_UP_CMD_ID = 0x36f6,
SCD4X_SET_AUTOMATIC_SELF_CALIBRATION_INITIAL_PERIOD_CMD_ID = 0x2445,
SCD4X_GET_AUTOMATIC_SELF_CALIBRATION_INITIAL_PERIOD_CMD_ID = 0x2340,
SCD4X_SET_AUTOMATIC_SELF_CALIBRATION_STANDARD_PERIOD_CMD_ID = 0x244e,
SCD4X_GET_AUTOMATIC_SELF_CALIBRATION_STANDARD_PERIOD_CMD_ID = 0x234b,
} SCD4xCmdId;
typedef enum {
SCD4X_SENSOR_VARIANT_MASK = 0x00F000,
SCD4X_SENSOR_VARIANT_SCD40 = 0x000000,
SCD4X_SENSOR_VARIANT_SCD41 = 0x001000,
SCD4X_SENSOR_VARIANT_SCD42 = 0x002000,
SCD4X_SENSOR_VARIANT_SCD43 = 0x005000,
} SCD4xSensorVariant;
class SensirionI2cScd4x {
public:
SensirionI2cScd4x();
/**
* @brief Initializes the SCD4x class.
*
* @param i2cBus Arduino stream object to be used for communication.
*/
void begin(TwoWire& i2cBus, uint8_t i2cAddress);
/**
* @brief Read CO₂, temperature, and humidity measurements in physical
* units.
*
* Reads the sensor output. The measurement data can only be read out once
* per signal update interval as the buffer is emptied upon read-out. If no
* data is available in the buffer, the sensor returns a NACK. To avoid a
* NACK response, the get_data_ready_status can be issued to check data
* status. The I2C master can abort the read transfer with a NACK followed
* by a STOP condition after any data byte if the user is not interested in
* subsequent data.
*
* @param[out] aCo2Concentration CO₂ concentration in ppm
* @param[out] aTemperature Temperature in °C
* @param[out] aRelativeHumidity Relative humidity in %RH
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t readMeasurement(uint16_t& aCo2Concentration, float& aTemperature,
float& aRelativeHumidity);
/**
* @brief Set the temperature compensation offset.
*
* Setting the temperature offset of the SCD4x inside the customer device
* allows the user to optimize the RH and T output signal. The temperature
* offset can depend on several factors such as the SCD4x measurement mode,
* self-heating of close components, the ambient temperature and air flow.
* Thus, the SCD4x temperature offset should be determined after integration
* into the final device and under its typical operating conditions
* (including the operation mode to be used in the application) in thermal
* equilibrium. By default, the temperature offset is set to 4 °C. To save
* the setting to the EEPROM, the persist_settings command may be issued.
* Equation (1) details how the characteristic temperature offset can be
* calculated using the current temperature output of the sensor (TSCD4x), a
* reference temperature value (TReference), and the previous temperature
* offset (Toffset_pervious) obtained using the get_temperature_offset_raw
* command:
*
* Toffset_actual = TSCD4x - TReference + Toffset_pervious.
*
* Recommended temperature offset values are between 0 °C and 20 °C. The
* temperature offset does not impact the accuracy of the CO2 output.
*
* @param[in] temperatureOffset Temperature offset value in °C
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t setTemperatureOffset(float temperatureOffset);
/**
* @brief Get the temperature compensation offset used by the sensor in °C.
*
* @param[out] aTemperatureOffset Temperature in °C
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getTemperatureOffset(float& aTemperatureOffset);
/**
* @brief Set the ambient pressure around the sensor.
*
* The set_ambient_pressure command can be sent during periodic measurements
* to enable continuous pressure compensation. Note that setting an ambient
* pressure overrides any pressure compensation based on a previously set
* sensor altitude. Use of this command is highly recommended for
* applications experiencing significant ambient pressure changes to ensure
* sensor accuracy. Valid input values are between 70000 - 120000 Pa. The
* default value is 101300 Pa.
*
* @param[in] ambientPressure Ambient pressure around the sensor in Pa
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t setAmbientPressure(uint32_t ambientPressure);
/**
* @brief Get the ambient pressure around the sensor.
*
* @param[out] aAmbientPressure Pressure in Pa
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getAmbientPressure(uint32_t& aAmbientPressure);
/**
* @brief Read if data is ready.
*
* Polls the sensor for whether data from a periodic or single shot
* measurement is ready to be read out.
*
* @param[out] arg0
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getDataReadyStatus(bool& arg0);
/**
* @brief Reads out the SCD4x sensor variant.
*
* @param[out] aSensorVariant
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getSensorVariant(SCD4xSensorVariant& aSensorVariant);
/**
* @brief Start a single shot measurement and read out the data when ready
*
* @param[out] aCo2Concentration CO₂ concentration in ppm
* @param[out] aTemperature Temperature in °C
* @param[out] aRelativeHumidity Relative humidity in %RH
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t measureAndReadSingleShot(uint16_t& aCo2Concentration,
float& aTemperature,
float& aRelativeHumidity);
/**
* @brief Start periodic measurement mode.
*
* Starts the periodic measurement mode. The signal update interval is 5
* seconds.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t startPeriodicMeasurement();
/**
* @brief Read CO₂, temperature, and humidity measurements raw values.
*
* Reads the sensor output. The measurement data can only be read out once
* per signal update interval as the buffer is emptied upon read-out. If no
* data is available in the buffer, the sensor returns a NACK. To avoid a
* NACK response, the get_data_ready_status can be issued to check data
* status. The I2C master can abort the read transfer with a NACK followed
* by a STOP condition after any data byte if the user is not interested in
* subsequent data.
*
* @param[out] co2Concentration CO₂ concentration in ppm
* @param[out] temperature Convert to degrees celsius by (175 * value /
* 65535) - 45
* @param[out] relativeHumidity Convert to relative humidity in % by (100 *
* value / 65535)
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t readMeasurementRaw(uint16_t& co2Concentration,
uint16_t& temperature,
uint16_t& relativeHumidity);
/**
* @brief Stop periodic measurement to change the sensor configuration or to
* save power.
*
* Command returns a sensor running in periodic measurement mode or low
* power periodic measurement mode back to the idle state, e.g. to then
* allow changing the sensor configuration or to save power.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t stopPeriodicMeasurement();
/**
* @brief Set the temperature compensation offset (raw value).
*
* Setting the temperature offset of the SCD4x inside the customer device
* allows the user to optimize the RH and T output signal. The temperature
* offset can depend on several factors such as the SCD4x measurement mode,
* self-heating of close components, the ambient temperature and air flow.
* Thus, the SCD4x temperature offset should be determined after integration
* into the final device and under its typical operating conditions
* (including the operation mode to be used in the application) in thermal
* equilibrium. By default, the temperature offset is set to 4 °C. To save
* the setting to the EEPROM, the persist_settings command may be issued.
* Equation (1) details how the characteristic temperature offset can be
* calculated using the current temperature output of the sensor (TSCD4x), a
* reference temperature value (TReference), and the previous temperature
* offset (Toffset_pervious) obtained using the get_temperature_offset_raw
* command:
*
* Toffset_actual = TSCD4x - TReference + Toffset_pervious.
*
* Recommended temperature offset values are between 0 °C and 20 °C. The
* temperature offset does not impact the accuracy of the CO2 output.
*
* @param[in] offsetTemperature Temperature offset. Convert Toffset in °C to
* value by: (Toffset * 65535 / 175)
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setTemperatureOffsetRaw(1498);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setTemperatureOffsetRaw(uint16_t offsetTemperature);
/**
* @brief Get the raw temperature compensation offset used by the sensor.
*
* @param[out] offsetTemperature Convert to °C by (175 * value / 65535)
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getTemperatureOffsetRaw(uint16_t& offsetTemperature);
/**
* @brief Set the altitude of the sensor (in meters above sea level).
*
* Typically, the sensor altitude is set once after device installation. To
* save the setting to the EEPROM, the persist_settings command must be
* issued. The default sensor altitude value is set to 0 meters above sea
* level. Note that setting a sensor altitude to the sensor overrides any
* pressure compensation based on a previously set ambient pressure.
*
* @param[in] sensorAltitude Sensor altitude in meters above sea level.
* Valid input values are between 0 - 3000 m.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setSensorAltitude(0);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setSensorAltitude(uint16_t sensorAltitude);
/**
* @brief Get the sensor altitude used by the sensor.
*
* @param[out] sensorAltitude Sensor altitude used by the sensor in meters
* above sea level.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getSensorAltitude(uint16_t& sensorAltitude);
/**
* @brief Set the raw ambient pressure value.
*
* The set_ambient_pressure command can be sent during periodic measurements
* to enable continuous pressure compensation. Note that setting an ambient
* pressure overrides any pressure compensation based on a previously set
* sensor altitude. Use of this command is highly recommended for
* applications experiencing significant ambient pressure changes to ensure
* sensor accuracy. Valid input values are between 70000 - 120000 Pa. The
* default value is 101300 Pa.
*
* @param[in] ambientPressure Convert ambient_pressure in hPa to Pa by
* ambient_pressure / 100.
*
* @note Available during measurements.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setAmbientPressureRaw(1013);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setAmbientPressureRaw(uint16_t ambientPressure);
/**
* @brief Get the ambient pressure around the sensor.
*
* @param[out] ambientPressure Convert to Pa by value = ambient_pressure *
* 100.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getAmbientPressureRaw(uint16_t& ambientPressure);
/**
* @brief Perform a forced recalibration (FRC) of the CO₂ concentration.
*
* To successfully conduct an accurate FRC, the following steps need to be
* carried out:
*
* 1. Operate the SCD4x in the operation mode later used for normal sensor
* operation (e.g. periodic measurement) for at least 3 minutes in an
* environment with a homogenous and constant CO2 concentration. The sensor
* must be operated at the voltage desired for the application when
* performing the FRC sequence. 2. Issue the stop_periodic_measurement
* command. 3. Issue the perform_forced_recalibration command.
*
* A return value of 0xffff indicates that the FRC has failed because the
* sensor was not operated before sending the command.
*
* @param[in] targetCO2Concentration Target CO₂ concentration in ppm CO₂.
* @param[out] frcCorrection Convert to FRC correction in ppm CO₂ by
* frc_correction - 0x8000. A return value of 0xFFFF indicates that the FRC
* has failed because the sensor was not operated before sending the
* command.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t performForcedRecalibration(uint16_t targetCO2Concentration,
uint16_t& frcCorrection);
/**
* @brief Enable or disable automatic self calibration (ASC).
*
* Sets the current state (enabled / disabled) of the ASC. By default, ASC
* is enabled. To save the setting to the EEPROM, the persist_settings
* command must be issued. The ASC enables excellent long-term stability of
* SCD4x without the need for regular user intervention. The algorithm
* leverages the sensor's measurement history and the assumption of exposure
* of the sensor to a known minimum background CO₂ concentration at least
* once over a period of cumulative operation. By default, the ASC algorithm
* assumes that the sensor is exposed to outdoor fresh air at 400 ppm CO₂
* concentration at least once per week of accumulated operation using one
* of the following measurement modes for at least 4 hours without
* interruption at a time: periodic measurement mode, low power periodic
* measurement mode or single shot mode with a measurement interval of 5
* minutes (SCD41 only).
*
* @param[in] ascEnabled 1 enables ASC, 0 disables ASC.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setAutomaticSelfCalibrationEnabled(1);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setAutomaticSelfCalibrationEnabled(uint16_t ascEnabled);
/**
* @brief Check if automatic self calibration (ASC) is enabled.
*
* @param[out] ascEnabled 1 if ASC is enabled, 0 if ASC is disabled.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getAutomaticSelfCalibrationEnabled(uint16_t& ascEnabled);
/**
* @brief Set the value of ASC baseline target in ppm.
*
* Sets the value of the ASC baseline target, i.e. the CO₂ concentration in
* ppm which the ASC algorithm will assume as lower-bound background to
* which the SCD4x is exposed to regularly within one ASC period of
* operation. To save the setting to the EEPROM, the persist_settings
* command must be issued subsequently. The factory default value is 400
* ppm.
*
* @param[in] ascTarget ASC baseline value in ppm CO₂
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setAutomaticSelfCalibrationTarget(400);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setAutomaticSelfCalibrationTarget(uint16_t ascTarget);
/**
* @brief Reads out the ASC baseline target concentration parameter.
*
* @param[out] ascTarget ASC baseline target concentration parameter in ppm
* CO₂.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getAutomaticSelfCalibrationTarget(uint16_t& ascTarget);
/**
* @brief Start a low-power periodic measurement (interval 30 s).
*
* To enable use-cases with a constrained power budget, the SCD4x features a
* low power periodic measurement mode with a signal update interval of
* approximately 30 seconds. The low power periodic measurement mode is
* initiated using the start_low_power_periodic_measurement command and
* read-out in a similar manner as the periodic measurement mode using the
* read_measurement command. To periodically check whether a new measurement
* result is available for read out, the get_data_ready_status command can
* be used to synchronize to the sensor's internal measurement interval as
* an alternative to relying on the ACK/NACK status of the
* read_measurement_command.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t startLowPowerPeriodicMeasurement();
/**
* @brief Read if data is ready.
*
* Polls the sensor for whether data from a periodic or single shot
* measurement is ready to be read out.
*
* @param[out] dataReadyStatus If one or more of the 11 least significant
* bits are 1, then the data is ready.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getDataReadyStatusRaw(uint16_t& dataReadyStatus);
/**
* @brief Store volatile sensor settings in the EEPROM.
*
* Configuration settings such as the temperature offset, sensor altitude
* and the ASC enabled/disabled parameters are by default stored in the
* volatile memory (RAM) only. The persist_settings command stores the
* current configuration in the EEPROM of the SCD4x, ensuring the current
* settings persist after power-cycling. To avoid unnecessary wear of the
* EEPROM, the persist_settings command should only be sent following
* configuration changes whose persistence is required. The EEPROM is
* guaranteed to withstand at least 2000 write cycles. Note that field
* calibration history (i.e. FRC and ASC) is automatically stored in a
* separate EEPROM dimensioned for the specified sensor lifetime when
* operated continuously in either periodic measurement mode, low power
* periodic measurement mode or single shot mode with 5 minute measurement
* interval (SCD41 only).
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t persistSettings();
/**
* @brief Read the sensor's unique serial number.
*
* Reading out the serial number can be used to identify the chip and to
* verify the presence of the sensor. The get_serial_number command returns
* 3 words, and every word is followed by an 8-bit CRC checksum. Together,
* the 3 words constitute a unique serial number with a length of 48 bits
* (in big endian format). This method takes care of converting the serial
* number from the 3 words to an 64 bit unsigned integer.
*
* @param[out] serialNumber of the sensor as integer.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getSerialNumber(uint64_t& serialNumber);
/**
* @brief Perform self test to assess sensor functionality and power supply.
*
* Can be used as an end-of-line test to check the sensor functionality.
*
* @param[out] sensorStatus If sensor status is equal to 0, no malfunction
* has been detected.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t performSelfTest(uint16_t& sensorStatus);
/**
* @brief Perform factory reset to erase the settings stored in the EEPROM.
*
* The perform_factory_reset command resets all configuration settings
* stored in the EEPROM and erases the FRC and ASC algorithm history.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t performFactoryReset();
/**
* @brief Reinitialize the sensor by reloading the settings from the EEPROM.
*
* The reinit command reinitialize the sensor by reloading user settings
* from EEPROM. The sensor must be in the idle state before sending the
* reinit command. If the reinit command does not trigger the desired
* re-initialization, a power-cycle should be applied to the SCD4x.
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t reinit();
/**
* @brief Reads out the SCD4x sensor variant.
*
* @param[out] sensorVariant Bits[15…12] = 0000 → SCD40 Bits[15…12] = 0001 →
* SCD41
*
* @note This command is only available in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t getSensorVariantRaw(uint16_t& sensorVariant);
/**
* @brief On-demand measurement of the CO₂ concentration, temperature, and
* humidity.
*
* The sensor output is read out by using the read_measurement command. The
* fastest possible sampling interval for single shot measurements is 5
* seconds. The ASC is enabled by default in single shot operation and
* optimized for single shot measurements performed every 5 minutes. For
* more details about single shot measurements and optimization of power
* consumption please refer to the datasheet.
*
* @note This command is only available for SCD41.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t measureSingleShot();
/**
* @brief On-demand measurement of the temperature and humidity only.
*
* For more details about single shot measurements and optimization of power
* consumption please refer to the datasheet.
*
* @note This command is only available for SCD41.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t measureSingleShotRhtOnly();
/**
* @brief Put the sensor into sleep mode from idle mode.
*
* Put the sensor from idle to sleep to reduce power consumption. Can be
* used to power down when operating the sensor in power-cycled single shot
* mode.
*
* @note This command is only available in idle mode. Only for SCD41.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t powerDown();
/**
* @brief Wake up sensor from sleep mode to idle mode.
*
* Wake up the sensor from sleep mode into idle mode. Note that the SCD4x
* does not acknowledge the wake_up command. The sensor's idle state after
* wake up can be verified by reading out the serial number.
*
* @note This command is only available for SCD41.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t wakeUp();
/**
* @brief Sets the initial period for ASC correction
*
* Sets the duration of the initial period for ASC correction (in hours). By
* default, the initial period for ASC correction is 44 hours. Allowed
* values are integer multiples of 4 hours. A value of 0 results in an
* immediate correction. To save the setting to the EEPROM, the
* persist_settings command must be issued.
*
* For single shot operation, this parameter always assumes a measurement
* interval of 5 minutes, counting the number of single shots to calculate
* elapsed time. If single shot measurements are taken more / less
* frequently than once every 5 minutes, this parameter must be scaled
* accordingly to achieve the intended period in hours (e.g. for a 10-minute
* measurement interval, the scaled parameter value is obtained by
* multiplying the intended period in hours by 0.5).
*
* @param[in] ascInitialPeriod ASC initial period in hours
*
* @note This command is available for SCD41 and only in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setAutomaticSelfCalibrationInitialPeriod(44);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t setAutomaticSelfCalibrationInitialPeriod(uint16_t ascInitialPeriod);
/**
* @brief Read out the initial period for ASC correction
*
* @param[out] ascInitialPeriod ASC initial period in hours
*
* @note This command is only available for SCD41 and only in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t
getAutomaticSelfCalibrationInitialPeriod(uint16_t& ascInitialPeriod);
/**
* @brief Sets the standard period for ASC correction.
*
* Sets the standard period for ASC correction (in hours). By default, the
* standard period for ASC correction is 156 hours. Allowed values are
* integer multiples of 4 hours. Note: a value of 0 results in an immediate
* correction. To save the setting to the EEPROM, the persist_settings (see
* Section 3.10.1) command must be issued.
*
* For single shot operation, this parameter always assumes a measurement
* interval of 5 minutes, counting the number of single shots to calculate
* elapsed time. If single shot measurements are taken more / less
* frequently than once every 5 minutes, this parameter must be scaled
* accordingly to achieve the intended period in hours (e.g. for a 10-minute
* measurement interval, the scaled parameter value is obtained by
* multiplying the intended period in hours by 0.5).
*
* @param[in] ascStandardPeriod ASC standard period in hours
*
* @note This command is only available for SCD41 and only in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*
* Example:
* --------
*
* @code{.cpp}
*
* int16_t localError = 0;
* localError = sensor.setAutomaticSelfCalibrationStandardPeriod(156);
* if (localError != NO_ERROR) {
* return;
* }
*
* @endcode
*
*/
int16_t
setAutomaticSelfCalibrationStandardPeriod(uint16_t ascStandardPeriod);
/**
* @brief Get the standard period for ASC correction.
*
* @param[out] ascStandardPeriod ASC standard period in hours
*
* @note This command is only available for SCD41 and only in idle mode.
*
* @return error_code 0 on success, an error code otherwise.
*/
int16_t
getAutomaticSelfCalibrationStandardPeriod(uint16_t& ascStandardPeriod);
/**
* @brief signalTemperature
*
* @param[in] rawTemperature
*
* @return Temperature in °C
*/
static float signalTemperature(uint16_t rawTemperature);
/**
* @brief signalRelativeHumidity
*
* @param[in] rawRelativeHumidity
*
* @return Relative humidity in %RH
*/
static float signalRelativeHumidity(uint16_t rawRelativeHumidity);
/**
* @brief signalCo2Concentration
*
* @param[in] rawCo2Concentration
*
* @return CO₂ concentration in ppm
*/
static uint16_t signalCo2Concentration(uint16_t rawCo2Concentration);
/**
* @brief signalTemperatureOffset
*
* @param[in] rawTemperatureOffset
*
* @return Temperature in °C
*/
static float signalTemperatureOffset(uint16_t rawTemperatureOffset);
/**
* @brief signalAmbientPressure
*
* @param[in] rawAmbientPressure
*
* @return Pressure in Pa
*/
static uint32_t signalAmbientPressure(uint16_t rawAmbientPressure);
private:
TwoWire* _i2cBus = nullptr;
uint8_t _i2cAddress = 0;
};
#endif // SENSIRIONI2CSCD4X_H