Skip to content

Commit

Permalink
Merge pull request #632 from adafruit/scd30-read-data-at-once
Browse files Browse the repository at this point in the history
Add retry if data not ready for SCD30
  • Loading branch information
tyeth authored Sep 24, 2024
2 parents 2da7105 + d1898c9 commit 784a166
Showing 1 changed file with 64 additions and 15 deletions.
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

0 comments on commit 784a166

Please sign in to comment.