Skip to content

Commit

Permalink
Merge pull request #709 from tyeth/scd30-scd4x-others-poll-min-1sec
Browse files Browse the repository at this point in the history
Scd30 scd4x others poll min 1sec
  • Loading branch information
tyeth authored Mar 7, 2025
2 parents 712e434 + 16c6723 commit 0bf00bc
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 273 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
},
"C_Cpp.dimInactiveRegions": true,
"dotnet.defaultSolution": "disable",
"cmake.configureOnOpen": false
"cmake.configureOnOpen": false,
"C_Cpp.clang_format_fallbackStyle": "Google",
"C_Cpp.clang_format_style": "file"
}
6 changes: 3 additions & 3 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
@returns True if the data was read successfully, False otherwise.
*/
/*******************************************************************************/
bool readSensorData() {
bool ReadSensorData() {
uint16_t status = _hdc302x->readStatus();
if (status & 0x0010) {
WS_DEBUG_PRINTLN(F("Device Reset Detected"));
Expand Down Expand Up @@ -99,7 +99,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
*/
/*******************************************************************************/
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
if (readSensorData() == false)
if (ReadSensorData() == false)
return false;
tempEvent->temperature = _temp;
return true;
Expand All @@ -115,7 +115,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
*/
/*******************************************************************************/
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
if (readSensorData() == false)
if (ReadSensorData() == false)
return false;
humidEvent->relative_humidity = _humidity;
return true;
Expand Down
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 @@ -85,13 +85,13 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
@returns True if the sensor was read successfully, False otherwise.
*/
/*******************************************************************************/
bool readSensorData() {
bool ReadSensorData() {
// dont read sensor more than once per second
if (alreadyRecentlyRead()) {
if (HasBeenReadInLastSecond()) {
return true;
}

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

Expand All @@ -114,7 +114,7 @@ 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 (!readSensorData()) {
if (!ReadSensorData()) {
return false;
}

Expand All @@ -133,7 +133,7 @@ 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 (!readSensorData()) {
if (!ReadSensorData()) {
return false;
}

Expand All @@ -152,7 +152,7 @@ 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 (!readSensorData()) {
if (!ReadSensorData()) {
return false;
}

Expand All @@ -162,7 +162,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {

protected:
Adafruit_SCD30 *_scd = nullptr; ///< SCD30 driver object
ulong _lastRead = 0ul; ///< Last time the sensor was read
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
Expand Down
94 changes: 67 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,74 @@ 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);
for (int i = 0; i < 2; i++) {
uint16_t error = _scd->getDataReadyStatus(isDataReady);
if (error == 0 && isDataReady) {
return true;
}
delay(100);
}
return false;
}

// 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 +137,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 +156,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 +175,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

0 comments on commit 0bf00bc

Please sign in to comment.