Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scd30 scd4x others poll min 1sec #709

Merged
merged 9 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
@brief Checks if sensor was read within last 1s, or is the first read.
@returns True if the sensor was recently read, False otherwise.
*/
bool alreadyRecentlyRead() {
return (_lastRead != 0 && millis() - _lastRead) < 1000;
bool hasBeenReadInLastSecond() {
return _lastRead != 0 && millis() - _lastRead < 1000;
}

/*******************************************************************************/
Expand All @@ -68,7 +68,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
@returns True if the sensor is ready, False otherwise.
*/
/*******************************************************************************/
bool sensorReady() {
bool isSensorReady() {
if (!_scd->dataReady()) {
// failed, one more quick attempt
delay(100);
Expand All @@ -87,11 +87,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool readSensorData() {
// dont read sensor more than once per second
if (alreadyRecentlyRead()) {
if (hasBeenReadInLastSecond()) {
return true;
}

if (!sensorReady()) {
if (!isSensorReady()) {
return false;
}

Expand Down Expand Up @@ -161,11 +161,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
}

protected:
Adafruit_SCD30 *_scd = nullptr; ///< SCD30 driver object
ulong _lastRead = 0; ///< Last time the sensor was read
sensors_event_t _temperature; ///< Temperature
sensors_event_t _humidity; ///< Relative Humidity
sensors_event_t _CO2; ///< CO2
Adafruit_SCD30 *_scd = nullptr; ///< SCD30 driver object
ulong _lastRead = 0uL; ///< Last time the sensor was read
sensors_event_t _temperature = {0}; ///< Temperature
sensors_event_t _humidity = {0}; ///< Relative Humidity
sensors_event_t _CO2 = {0}; ///< CO2
};

#endif // WipperSnapper_I2C_Driver_SCD30
95 changes: 68 additions & 27 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,75 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
_scd->begin(*_i2c, _sensorAddress);

// stop previously started measurement
if (_scd->stopPeriodicMeasurement())
if (_scd->stopPeriodicMeasurement() != 0) {
return false;
}

// start measurements
if (_scd->startPeriodicMeasurement())
if (_scd->startPeriodicMeasurement() != 0) {
return false;
}

return true;
}

/********************************************************************************/
/*******************************************************************************/
/*!
@brief Checks if sensor was read within last 1s, or is the first read.
@returns True if the sensor was recently read, False otherwise.
*/
bool hasBeenReadInLastSecond() {
return _lastRead != 0 && millis() - _lastRead < 1000;
}

/*******************************************************************************/
/*!
@brief Attempts to read the SCD4x's sensor measurements
@returns True if the measurements were read without errors, False
if read errors occured or if sensor did not have data ready.
@brief Checks if the sensor is ready to be read
@returns True if the sensor is ready, False otherwise.
*/
/********************************************************************************/
bool readSensorMeasurements() {
uint16_t error;
/*******************************************************************************/
bool isSensorReady() {
bool isDataReady = false;
delay(100);
uint16_t error = _scd->getDataReadyStatus(isDataReady);
if (error != 0 || !isDataReady) {
// failed, one more quick attempt
delay(100);
error = _scd->getDataReadyStatus(isDataReady);
if (error != 0 || !isDataReady) {
return false;
}
}
return true;
}

// Check if data is ready
error = _scd->getDataReadyStatus(isDataReady);
if (error || !isDataReady)
/*******************************************************************************/
/*!
@brief Reads the sensor.
@returns True if the sensor was read successfully, False otherwise.
*/
/*******************************************************************************/
bool readSensorData() {
// dont read sensor more than once per second
if (hasBeenReadInLastSecond()) {
return true;
}

if (!isSensorReady()) {
return false;
}

// Read SCD4x measurement
error = _scd->readMeasurement(_co2, _temperature, _humidity);
if (error || _co2 == 0)
uint16_t co2 = 0;
float temperature = 0;
float humidity = 0;
int16_t error = _scd->readMeasurement(co2, temperature, humidity);
if (error != 0 || co2 == 0) {
return false;

}
_CO2.CO2 = co2;
_temperature.temperature = temperature;
_humidity.relative_humidity = humidity;
_lastRead = millis();
return true;
}

Expand All @@ -101,10 +138,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
// read all sensor measurements
if (!readSensorMeasurements())
if (!readSensorData()) {
return false;
}

tempEvent->temperature = _temperature;
tempEvent = &_temperature;
return true;
}

Expand All @@ -119,10 +157,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
// read all sensor measurements
if (!readSensorMeasurements())
if (!readSensorData()) {
return false;
}

humidEvent->relative_humidity = _humidity;
humidEvent = &_humidity;
return true;
}

Expand All @@ -137,18 +176,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventCO2(sensors_event_t *co2Event) {
// read all sensor measurements
if (!readSensorMeasurements())
if (!readSensorData()) {
return false;
}

co2Event->CO2 = (float)_co2;
co2Event = &_CO2;
return true;
}

protected:
SensirionI2cScd4x *_scd; ///< SCD4x driver object
uint16_t _co2; ///< SCD4x co2 reading
float _temperature; ///< SCD4x temperature reading
float _humidity; ///< SCD4x humidity reading
SensirionI2cScd4x *_scd = nullptr; ///< SCD4x driver object
sensors_event_t _temperature = {0}; ///< Temperature
sensors_event_t _humidity = {0}; ///< Relative Humidity
sensors_event_t _CO2 = {0}; ///< CO2
ulong _lastRead = 0uL; ///< Last time the sensor was read
};

#endif // WipperSnapper_I2C_Driver_SCD4X
#endif // WipperSnapper_I2C_Driver_SCD4X_H
Loading
Loading