diff --git a/.vscode/settings.json b/.vscode/settings.json index 65076780b..12d7d847e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h index ec4323fd3..bf67c778c 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h @@ -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")); @@ -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; @@ -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; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h index 0dc9f4109..57b83ea2a 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h @@ -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; } /*******************************************************************************/ @@ -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); @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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 diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index d5490568e..28885ac12 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -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; } @@ -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; } @@ -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; } @@ -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 \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN5X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN5X.h index 5b4e91636..f7156a5fa 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN5X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN5X.h @@ -43,6 +43,14 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { : WipperSnapper_I2C_Driver(i2c, sensorAddress) { _i2c = i2c; _sensorAddress = sensorAddress; + _massConcentrationPm1p0 = NAN; + _massConcentrationPm2p5 = NAN; + _massConcentrationPm4p0 = NAN; + _massConcentrationPm10p0 = NAN; + _ambientHumidity = NAN; + _ambientTemperature = NAN; + _vocIndex = NAN; + _noxIndex = NAN; } /*******************************************************************************/ @@ -68,6 +76,60 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { 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 Checks if the sensor is ready to be read + @returns True if the sensor is ready, False otherwise. + */ + /*******************************************************************************/ + bool IsSensorReady() { + bool isDataReady = false; + for (int i = 0; i < 2; i++) { + uint16_t error = _sen->readDataReady(isDataReady); + if (error == 0 && isDataReady) { + return true; + } + delay(100); + } + return false; + } + + /*******************************************************************************/ + /*! + @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; + } + + uint16_t error = _sen->readMeasuredValues( + _massConcentrationPm1p0, _massConcentrationPm2p5, + _massConcentrationPm4p0, _massConcentrationPm10p0, _ambientHumidity, + _ambientTemperature, _vocIndex, _noxIndex); + if (error != 0) { + return false; + } + _lastRead = millis(); + return true; + } + /*******************************************************************************/ /*! @brief Gets the SEN5X's current temperature. @@ -78,20 +140,10 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventAmbientTemp(sensors_event_t *tempEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_tempSensorPeriod != 0 && error != 0) || ambientTemperature == NAN) { + if (!ReadSensorData() || _ambientTemperature == NAN) { return false; } - - tempEvent->temperature = ambientTemperature; + tempEvent->temperature = _ambientTemperature; return true; } @@ -105,20 +157,10 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventRelativeHumidity(sensors_event_t *humidEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_humidSensorPeriod != 0 && error != 0) || ambientHumidity == NAN) { + if (!ReadSensorData() || _ambientHumidity == NAN) { return false; } - - humidEvent->relative_humidity = ambientHumidity; + humidEvent->relative_humidity = _ambientHumidity; return true; } @@ -135,20 +177,10 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventNOxIndex(sensors_event_t *noxIndexEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_NOxIndexPeriod != 0 && error != 0) || noxIndex == NAN) { + if (!ReadSensorData() || _noxIndex == NAN) { return false; } - - noxIndexEvent->nox_index = noxIndex; + noxIndexEvent->nox_index = _noxIndex; return true; } @@ -162,20 +194,10 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventVOCIndex(sensors_event_t *vocIndexEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_VOCIndexPeriod != 0 && error != 0) || vocIndex == NAN) { + if (!ReadSensorData() || _vocIndex == NAN) { return false; } - - vocIndexEvent->voc_index = vocIndex; + vocIndexEvent->voc_index = _vocIndex; return true; } @@ -189,22 +211,11 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM10_STD(sensors_event_t *pm10StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_PM10SensorPeriod != 0 && error != 0) || - massConcentrationPm1p0 == NAN || - massConcentrationPm1p0 == OVERFLOW_SEN55) { + if (!ReadSensorData() || _massConcentrationPm1p0 == NAN || + _massConcentrationPm1p0 == OVERFLOW_SEN55) { return false; } - - pm10StdEvent->pm10_std = massConcentrationPm1p0; + pm10StdEvent->pm10_std = _massConcentrationPm1p0; return true; } @@ -218,22 +229,11 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM25_STD(sensors_event_t *pm25StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_PM25SensorPeriod != 0 && error != 0) || - massConcentrationPm2p5 == NAN || - massConcentrationPm2p5 == OVERFLOW_SEN55) { + if (!ReadSensorData() || _massConcentrationPm2p5 == NAN || + _massConcentrationPm2p5 == OVERFLOW_SEN55) { return false; } - - pm25StdEvent->pm25_std = massConcentrationPm2p5; + pm25StdEvent->pm25_std = _massConcentrationPm2p5; return true; } @@ -247,22 +247,11 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM40_STD(sensors_event_t *pm40StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_PM25SensorPeriod != 0 && error != 0) || - massConcentrationPm4p0 == NAN || - massConcentrationPm4p0 == OVERFLOW_SEN55) { + if (!ReadSensorData() || _massConcentrationPm4p0 == NAN || + _massConcentrationPm4p0 == OVERFLOW_SEN55) { return false; } - - pm40StdEvent->data[0] = massConcentrationPm4p0; + pm40StdEvent->data[0] = _massConcentrationPm4p0; return true; } @@ -276,27 +265,25 @@ class WipperSnapper_I2C_Driver_SEN5X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM100_STD(sensors_event_t *pm100StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex); - if ((_PM100SensorPeriod != 0 && error != 0) || - massConcentrationPm10p0 == NAN || - massConcentrationPm10p0 == OVERFLOW_SEN55) { + if (!ReadSensorData() || _massConcentrationPm10p0 == NAN || + _massConcentrationPm10p0 == OVERFLOW_SEN55) { return false; } - - pm100StdEvent->pm100_std = massConcentrationPm10p0; + pm100StdEvent->pm100_std = _massConcentrationPm10p0; return true; } protected: - SensirionI2CSen5x *_sen; ///< SEN5X driver object + SensirionI2CSen5x *_sen = nullptr; ///< SEN5X driver object + float _massConcentrationPm1p0; ///< PM1.0 mass concentration + float _massConcentrationPm2p5; ///< PM2.5 mass concentration + float _massConcentrationPm4p0; ///< PM4.0 mass concentration + float _massConcentrationPm10p0; ///< PM10.0 mass concentration + float _ambientHumidity; ///< Ambient humidity + float _ambientTemperature; ///< Ambient temperature + float _vocIndex; ///< VOC index + float _noxIndex; ///< NOx index + ulong _lastRead = 0uL; ///< Last time the sensor was read }; #endif // WipperSnapper_I2C_Driver_SEN5X diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN6X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN6X.h index 8b4b8d5ad..a56945db0 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN6X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SEN6X.h @@ -45,6 +45,15 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { : WipperSnapper_I2C_Driver(i2c, sensorAddress) { _i2c = i2c; _sensorAddress = sensorAddress; + _massConcentrationPm1p0 = NAN; + _massConcentrationPm2p5 = NAN; + _massConcentrationPm4p0 = NAN; + _massConcentrationPm10p0 = NAN; + _ambientHumidity = NAN; + _ambientTemperature = NAN; + _vocIndex = NAN; + _noxIndex = NAN; + _co2 = 0uL; } /*******************************************************************************/ @@ -70,6 +79,61 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { 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 Checks if the sensor is ready to be read + @returns True if the sensor is ready, False otherwise. + */ + /*******************************************************************************/ + bool IsSensorReady() { + bool isDataReady = false; + uint8_t padding = 0x0; + for (int i = 0; i < 2; i++) { + uint16_t error = _sen->getDataReady(padding, isDataReady); + if (error == 0 && isDataReady) { + return true; + } + delay(100); + } + return false; + } + + /*******************************************************************************/ + /*! + @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; + } + + uint16_t error = _sen->readMeasuredValues( + _massConcentrationPm1p0, _massConcentrationPm2p5, + _massConcentrationPm4p0, _massConcentrationPm10p0, _ambientHumidity, + _ambientTemperature, _vocIndex, _noxIndex, _co2); + if (error != 0) { + return false; + } + _lastRead = millis(); + return true; + } + /*******************************************************************************/ /*! @brief Gets the SEN6X's current temperature. @@ -80,21 +144,10 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventAmbientTemp(sensors_event_t *tempEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_tempSensorPeriod != 0 && error != 0) || ambientTemperature == NAN) { + if (!ReadSensorData() || _ambientTemperature == NAN) { return false; } - - tempEvent->temperature = ambientTemperature; + tempEvent->temperature = _ambientTemperature; return true; } @@ -108,21 +161,10 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventRelativeHumidity(sensors_event_t *humidEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_humidSensorPeriod != 0 && error != 0) || ambientHumidity == NAN) { + if (!ReadSensorData() || _ambientHumidity == NAN) { return false; } - - humidEvent->relative_humidity = ambientHumidity; + humidEvent->relative_humidity = _ambientHumidity; return true; } @@ -139,21 +181,10 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventNOxIndex(sensors_event_t *noxIndexEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_NOxIndexPeriod != 0 && error != 0) || noxIndex == NAN) { + if (!ReadSensorData() || _noxIndex == NAN) { return false; } - - noxIndexEvent->nox_index = noxIndex; + noxIndexEvent->nox_index = _noxIndex; return true; } @@ -167,21 +198,10 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventVOCIndex(sensors_event_t *vocIndexEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_VOCIndexPeriod != 0 && error != 0) || vocIndex == NAN) { + if (!ReadSensorData() || _vocIndex == NAN) { return false; } - - vocIndexEvent->voc_index = vocIndex; + vocIndexEvent->voc_index = _vocIndex; return true; } @@ -195,23 +215,11 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM10_STD(sensors_event_t *pm10StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_PM10SensorPeriod != 0 && error != 0) || - massConcentrationPm1p0 == NAN || - massConcentrationPm1p0 == OVERFLOW_SEN6X) { + if (!ReadSensorData() || _massConcentrationPm1p0 == NAN || + _massConcentrationPm1p0 == OVERFLOW_SEN6X) { return false; } - - pm10StdEvent->pm10_std = massConcentrationPm1p0; + pm10StdEvent->pm10_std = _massConcentrationPm1p0; return true; } @@ -225,23 +233,11 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM25_STD(sensors_event_t *pm25StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_PM25SensorPeriod != 0 && error != 0) || - massConcentrationPm2p5 == NAN || - massConcentrationPm2p5 == OVERFLOW_SEN6X) { + if (!ReadSensorData() || _massConcentrationPm2p5 == NAN || + _massConcentrationPm2p5 == OVERFLOW_SEN6X) { return false; } - - pm25StdEvent->pm25_std = massConcentrationPm2p5; + pm25StdEvent->pm25_std = _massConcentrationPm2p5; return true; } @@ -255,23 +251,11 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM40_STD(sensors_event_t *pm40StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_PM25SensorPeriod != 0 && error != 0) || - massConcentrationPm4p0 == NAN || - massConcentrationPm4p0 == OVERFLOW_SEN6X) { + if (!ReadSensorData() || _massConcentrationPm4p0 == NAN || + _massConcentrationPm4p0 == OVERFLOW_SEN6X) { return false; } - - pm40StdEvent->data[0] = massConcentrationPm4p0; + pm40StdEvent->data[0] = _massConcentrationPm4p0; return true; } @@ -285,23 +269,11 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventPM100_STD(sensors_event_t *pm100StdEvent) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - if ((_PM100SensorPeriod != 0 && error != 0) || - massConcentrationPm10p0 == NAN || - massConcentrationPm10p0 == OVERFLOW_SEN6X) { + if (!ReadSensorData() || _massConcentrationPm10p0 == NAN || + _massConcentrationPm10p0 == OVERFLOW_SEN6X) { return false; } - - pm100StdEvent->pm100_std = massConcentrationPm10p0; + pm100StdEvent->pm100_std = _massConcentrationPm10p0; return true; } @@ -315,27 +287,25 @@ class WipperSnapper_I2C_Driver_SEN6X : public WipperSnapper_I2C_Driver { */ /*******************************************************************************/ bool getEventCO2(sensors_event_t *co2Event) { - float massConcentrationPm1p0, massConcentrationPm2p5, - massConcentrationPm4p0, massConcentrationPm10p0, ambientHumidity, - ambientTemperature, vocIndex, noxIndex; - uint16_t co2; - uint16_t error; - - error = _sen->readMeasuredValues( - massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0, - massConcentrationPm10p0, ambientHumidity, ambientTemperature, vocIndex, - noxIndex, co2); - - if ((_CO2SensorPeriod != 0 && error != 0) || co2 == 0xFFFF) { + if (!ReadSensorData() || _co2 == 0xFFFF) { return false; } - - co2Event->CO2 = co2; + co2Event->CO2 = _co2; return true; } protected: - SensirionI2cSen66 *_sen; ///< SEN6X driver object + SensirionI2cSen66 *_sen = nullptr; ///< SEN6X driver object + float _massConcentrationPm1p0; ///< PM1.0 mass concentration + float _massConcentrationPm2p5; ///< PM2.5 mass concentration + float _massConcentrationPm4p0; ///< PM4.0 mass concentration + float _massConcentrationPm10p0; ///< PM10.0 mass concentration + float _ambientHumidity; ///< Ambient humidity + float _ambientTemperature; ///< Ambient temperature + float _vocIndex; ///< VOC index + float _noxIndex; ///< NOx index + uint16_t _co2; ///< CO2 value + ulong _lastRead; ///< Last time the sensor was read }; #endif // WipperSnapper_I2C_Driver_SEN6X