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

Add retry if data not ready for SCD30 #632

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Changes from all 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
79 changes: 64 additions & 15 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,56 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
return _scd->begin((uint8_t)_sensorAddress, _i2c);
}

/*******************************************************************************/
/*!
@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;
}

/*******************************************************************************/
/*!
@brief Checks if the sensor is ready to be read
@returns True if the sensor is ready, False otherwise.
*/
/*******************************************************************************/
bool sensorReady() {
if (!_scd->dataReady()) {
// failed, one more quick attempt
delay(100);
if (!_scd->dataReady()) {
return false;
}
}
return true;
}

/*******************************************************************************/
/*!
@brief Reads the SCD30 sensor.
@returns True if the sensor was read successfully, False otherwise.
*/
/*******************************************************************************/
bool readSensorData() {
// dont read sensor more than once per second
if (alreadyRecentlyRead()) {
return true;
}

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

if (_scd->getEvent(&_humidity, &_temperature)) {
return false;
}
_CO2.CO2 = _scd->CO2;
_lastRead = millis();
return true;
}

/*******************************************************************************/
/*!
@brief Gets the SCD30's current temperature.
Expand All @@ -64,14 +114,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
// check if sensor is enabled and data is available
if (_tempSensorPeriod != 0 && (!_scd->dataReady()))
return false;

// attempt to get temperature data
sensors_event_t humidEvent;
if (!_scd->getEvent(&humidEvent, tempEvent))
if (!readSensorData()) {
return false;
}

tempEvent = &_temperature;
return true;
}

Expand All @@ -86,14 +133,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
// check if sensor is enabled and data is available
if (_humidSensorPeriod != 0 && (!_scd->dataReady()))
return false;

// attempt to get temperature data
sensors_event_t tempEvent;
if (!_scd->getEvent(humidEvent, &tempEvent))
if (!readSensorData()) {
return false;
}

humidEvent = &_humidity;
return true;
}

Expand All @@ -108,15 +152,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
/*******************************************************************************/
bool getEventCO2(sensors_event_t *co2Event) {
// check if sensor is enabled and data is available
if (_CO2SensorPeriod != 0 && (!_scd->dataReady()))
if (!readSensorData()) {
return false;
}

co2Event->CO2 = _scd->CO2;
co2Event = &_CO2;
return true;
}

protected:
Adafruit_SCD30 *_scd; ///< SCD30 driver object
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
};

#endif // WipperSnapper_I2C_Driver_SCD30
Loading