diff --git a/src/ISensor.h b/src/ISensor.h index a4e6f4e..a77c5d4 100644 --- a/src/ISensor.h +++ b/src/ISensor.h @@ -22,22 +22,10 @@ class ISensor { virtual uint16_t start() = 0; /** - * @brief Query sensor whether an initialization step is required or not. - * - * @param[out] bool true if either a conditioning or initialization command - * must be called before measurements are available + * @brief Perform extended sensor initialization (if applicable) and + * determines sensor metadata. */ - virtual bool requiresInitializationStep() const { - return false; - }; - - /** - * @brief Perform extended sensor initialization. - * As most sensors don't require this, overriding this method is optional. - */ - virtual uint16_t initializationStep() { - return 0; - }; + virtual uint16_t initializationStep() = 0; /** * @brief Get the duration of the conditioning period @@ -104,9 +92,9 @@ class ISensor { /** * @brief Get the MetaData of the ISensor realization - * + * * @return MetaData - */ + */ virtual MetaData getMetaData() const = 0; /** diff --git a/src/Sensirion_upt_i2c_auto_detection.h b/src/Sensirion_upt_i2c_auto_detection.h index 1875371..4537b2a 100644 --- a/src/Sensirion_upt_i2c_auto_detection.h +++ b/src/Sensirion_upt_i2c_auto_detection.h @@ -1,8 +1,8 @@ #ifndef _SENSIRION_UPT_I2C_AUTO_DETECTION_H_ #define _SENSIRION_UPT_I2C_AUTO_DETECTION_H_ -#include "Sensirion_UPT_Core.h" #include "I2CAutoDetector.h" +#include "Sensirion_UPT_Core.h" #include "SensorManager.h" #include diff --git a/src/SensorStateMachine.cpp b/src/SensorStateMachine.cpp index 5da0104..5a2c0c4 100644 --- a/src/SensorStateMachine.cpp +++ b/src/SensorStateMachine.cpp @@ -18,33 +18,30 @@ SensorStateMachine::SensorStateMachine(ISensor* pSensor) }; AutoDetectorError SensorStateMachine::_initialize() { - if (_sensor->requiresInitializationStep()) { - uint16_t error = _sensor->initializationStep(); - if (error) { - char errorMsg[256]; - errorToString(error, errorMsg, 256); - Serial.printf("Failed to perform initialization step: %s\n", - errorMsg); - return I2C_ERROR; - } - - _lastMeasurementTimeStampMs = millis(); - _sensorState = SensorStatus::INITIALIZING; - - _sensorSignals.init(_sensor->getNumberOfDataPoints()); + uint16_t error = _sensor->initializationStep(); + if (error) { + char errorMsg[256]; + errorToString(error, errorMsg, 256); + Serial.printf("Failed to perform initialization step: %s\n", errorMsg); + return I2C_ERROR; + } - for (size_t i = 0; i < _sensor->getNumberOfDataPoints(); ++i) { - Measurement measurement; - measurement.metaData = _sensor->getMetaData(); - _sensorSignals.addMeasurement(measurement); - } + _sensorSignals.init(_sensor->getNumberOfDataPoints()); + _measurementIntervalMs = _sensor->getMinimumMeasurementIntervalMs(); + _lastMeasurementTimeStampMs = millis(); + if (_sensor->getInitializationIntervalMs() > 0) { + // SGP4X, SCD4X, + _sensorState = SensorStatus::INITIALIZING; } else { _sensorState = SensorStatus::RUNNING; - _sensorSignals.init(_sensor->getNumberOfDataPoints()); } - _measurementIntervalMs = _sensor->getMinimumMeasurementIntervalMs(); + for (size_t i = 0; i < _sensor->getNumberOfDataPoints(); ++i) { + Measurement measurement; + measurement.metaData = _sensor->getMetaData(); + _sensorSignals.addMeasurement(measurement); + } return NO_ERROR; } diff --git a/src/SensorWrappers/Scd30.cpp b/src/SensorWrappers/Scd30.cpp index adeab0a..cdf1b4d 100644 --- a/src/SensorWrappers/Scd30.cpp +++ b/src/SensorWrappers/Scd30.cpp @@ -100,10 +100,6 @@ unsigned long Scd30::getMinimumMeasurementIntervalMs() const { return 2000; } -bool Scd30::requiresInitializationStep() const { - return true; -} - void* Scd30::getDriver() { return reinterpret_cast(&_driver); } diff --git a/src/SensorWrappers/Scd30.h b/src/SensorWrappers/Scd30.h index 79b5494..cde1eda 100644 --- a/src/SensorWrappers/Scd30.h +++ b/src/SensorWrappers/Scd30.h @@ -17,7 +17,6 @@ class Scd30 : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; void* getDriver() override; private: diff --git a/src/SensorWrappers/Scd4x.cpp b/src/SensorWrappers/Scd4x.cpp index 5a55d42..89f080f 100644 --- a/src/SensorWrappers/Scd4x.cpp +++ b/src/SensorWrappers/Scd4x.cpp @@ -79,8 +79,9 @@ unsigned long Scd4x::getMinimumMeasurementIntervalMs() const { return 5000; } -bool Scd4x::requiresInitializationStep() const { - return true; +unsigned long Scd4x::getInitializationIntervalMs() const { + // Sensor does not produce measurements for ~12s after startPeriodicMeasurement() is called + return 12 * 1000; } void* Scd4x::getDriver() { diff --git a/src/SensorWrappers/Scd4x.h b/src/SensorWrappers/Scd4x.h index cad1bc0..660a6b5 100644 --- a/src/SensorWrappers/Scd4x.h +++ b/src/SensorWrappers/Scd4x.h @@ -17,7 +17,8 @@ class Scd4x : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; + // Same as measurement interval + unsigned long getInitializationIntervalMs() const override; void* getDriver() override; private: diff --git a/src/SensorWrappers/Sen5x.cpp b/src/SensorWrappers/Sen5x.cpp index c9fe704..c98361b 100644 --- a/src/SensorWrappers/Sen5x.cpp +++ b/src/SensorWrappers/Sen5x.cpp @@ -151,10 +151,6 @@ unsigned long Sen5x::getMinimumMeasurementIntervalMs() const { return 1000; } -bool Sen5x::requiresInitializationStep() const { - return true; -} - void* Sen5x::getDriver() { return reinterpret_cast(&_driver); } diff --git a/src/SensorWrappers/Sen5x.h b/src/SensorWrappers/Sen5x.h index 5e823fe..974e80e 100644 --- a/src/SensorWrappers/Sen5x.h +++ b/src/SensorWrappers/Sen5x.h @@ -17,7 +17,6 @@ class Sen5x : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; void* getDriver() override; private: diff --git a/src/SensorWrappers/Sfa3x.cpp b/src/SensorWrappers/Sfa3x.cpp index 930b5db..e4cac2e 100644 --- a/src/SensorWrappers/Sfa3x.cpp +++ b/src/SensorWrappers/Sfa3x.cpp @@ -84,10 +84,6 @@ unsigned long Sfa3x::getMinimumMeasurementIntervalMs() const { return 5000; } -bool Sfa3x::requiresInitializationStep() const { - return true; -} - void* Sfa3x::getDriver() { return reinterpret_cast(&_driver); } diff --git a/src/SensorWrappers/Sfa3x.h b/src/SensorWrappers/Sfa3x.h index 2ade069..eed8bd8 100644 --- a/src/SensorWrappers/Sfa3x.h +++ b/src/SensorWrappers/Sfa3x.h @@ -17,7 +17,6 @@ class Sfa3x : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; void* getDriver() override; private: diff --git a/src/SensorWrappers/Sgp4x.cpp b/src/SensorWrappers/Sgp4x.cpp index 9314257..4268740 100644 --- a/src/SensorWrappers/Sgp4x.cpp +++ b/src/SensorWrappers/Sgp4x.cpp @@ -69,10 +69,6 @@ unsigned long Sgp41::getMinimumMeasurementIntervalMs() const { return 1000; } -bool Sgp41::requiresInitializationStep() const { - return true; -} - unsigned long Sgp41::getInitializationIntervalMs() const { return 8000; } diff --git a/src/SensorWrappers/Sgp4x.h b/src/SensorWrappers/Sgp4x.h index 0f78628..f21a6d7 100644 --- a/src/SensorWrappers/Sgp4x.h +++ b/src/SensorWrappers/Sgp4x.h @@ -17,7 +17,6 @@ class Sgp41 : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; // Typical: 10s unsigned long getInitializationIntervalMs() const override; diff --git a/src/SensorWrappers/Sht4x.cpp b/src/SensorWrappers/Sht4x.cpp index a100267..6589615 100644 --- a/src/SensorWrappers/Sht4x.cpp +++ b/src/SensorWrappers/Sht4x.cpp @@ -14,13 +14,6 @@ uint16_t Sht4x::start() { uint16_t Sht4x::measureAndWrite(Measurement measurements[], const unsigned long timeStamp) { - if (_metaData.deviceID == 0) { - // Read sensor identifier at first measurement retrieval - uint32_t serialNo; - _driver.serialNumber(serialNo); - _metaData.deviceID = static_cast(serialNo); - } - float temp; float humi; uint16_t error = _driver.measureHighPrecision(temp, humi); @@ -41,6 +34,16 @@ uint16_t Sht4x::measureAndWrite(Measurement measurements[], return HighLevelError::NoError; } +uint16_t Sht4x::initializationStep() { + uint32_t serialNo; + uint16_t error = _driver.serialNumber(serialNo); + if (error) { + return error; + } + _metaData.deviceID = static_cast(serialNo); + return error; +} + SensorType Sht4x::getSensorType() const { return _metaData.deviceType.sensorType; } diff --git a/src/SensorWrappers/Sht4x.h b/src/SensorWrappers/Sht4x.h index 2da5d6d..3bc6f05 100644 --- a/src/SensorWrappers/Sht4x.h +++ b/src/SensorWrappers/Sht4x.h @@ -12,6 +12,7 @@ class Sht4x : public ISensor { uint16_t start() override; uint16_t measureAndWrite(Measurement measurements[], const unsigned long timeStamp) override; + uint16_t initializationStep() override; SensorType getSensorType() const override; MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; diff --git a/src/SensorWrappers/Stc3x.cpp b/src/SensorWrappers/Stc3x.cpp index 01bb0db..50a8df9 100644 --- a/src/SensorWrappers/Stc3x.cpp +++ b/src/SensorWrappers/Stc3x.cpp @@ -118,10 +118,6 @@ unsigned long Stc3x::getMinimumMeasurementIntervalMs() const { return 1000; } -bool Stc3x::requiresInitializationStep() const { - return 1; -} - void* Stc3x::getDriver() { return reinterpret_cast(&_driver); } diff --git a/src/SensorWrappers/Stc3x.h b/src/SensorWrappers/Stc3x.h index 4ea64b4..e87423e 100644 --- a/src/SensorWrappers/Stc3x.h +++ b/src/SensorWrappers/Stc3x.h @@ -17,7 +17,6 @@ class Stc3x : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; void* getDriver() override; private: diff --git a/src/SensorWrappers/Svm4x.cpp b/src/SensorWrappers/Svm4x.cpp index 40dd881..359e8af 100644 --- a/src/SensorWrappers/Svm4x.cpp +++ b/src/SensorWrappers/Svm4x.cpp @@ -91,10 +91,6 @@ unsigned long Svm4x::getMinimumMeasurementIntervalMs() const { return 1000; } -bool Svm4x::requiresInitializationStep() const { - return true; -} - void* Svm4x::getDriver() { return reinterpret_cast(&_driver); } diff --git a/src/SensorWrappers/Svm4x.h b/src/SensorWrappers/Svm4x.h index 8621632..2601371 100644 --- a/src/SensorWrappers/Svm4x.h +++ b/src/SensorWrappers/Svm4x.h @@ -17,7 +17,6 @@ class Svm4x : public ISensor { MetaData getMetaData() const override; size_t getNumberOfDataPoints() const override; unsigned long getMinimumMeasurementIntervalMs() const override; - bool requiresInitializationStep() const override; void* getDriver() override; private: