Skip to content

Commit

Permalink
Simplify State Machine Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianpaulsen committed Feb 28, 2024
1 parent ff1589b commit 05a4555
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 79 deletions.
22 changes: 5 additions & 17 deletions src/ISensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,9 +92,9 @@ class ISensor {

/**
* @brief Get the MetaData of the ISensor realization
*
*
* @return MetaData
*/
*/
virtual MetaData getMetaData() const = 0;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sensirion_upt_i2c_auto_detection.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>

Expand Down
39 changes: 18 additions & 21 deletions src/SensorStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Scd30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ unsigned long Scd30::getMinimumMeasurementIntervalMs() const {
return 2000;
}

bool Scd30::requiresInitializationStep() const {
return true;
}

void* Scd30::getDriver() {
return reinterpret_cast<void*>(&_driver);
}
1 change: 0 additions & 1 deletion src/SensorWrappers/Scd30.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/SensorWrappers/Scd4x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/SensorWrappers/Scd4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Sen5x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ unsigned long Sen5x::getMinimumMeasurementIntervalMs() const {
return 1000;
}

bool Sen5x::requiresInitializationStep() const {
return true;
}

void* Sen5x::getDriver() {
return reinterpret_cast<void*>(&_driver);
}
Expand Down
1 change: 0 additions & 1 deletion src/SensorWrappers/Sen5x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Sfa3x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ unsigned long Sfa3x::getMinimumMeasurementIntervalMs() const {
return 5000;
}

bool Sfa3x::requiresInitializationStep() const {
return true;
}

void* Sfa3x::getDriver() {
return reinterpret_cast<void*>(&_driver);
}
1 change: 0 additions & 1 deletion src/SensorWrappers/Sfa3x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Sgp4x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ unsigned long Sgp41::getMinimumMeasurementIntervalMs() const {
return 1000;
}

bool Sgp41::requiresInitializationStep() const {
return true;
}

unsigned long Sgp41::getInitializationIntervalMs() const {
return 8000;
}
Expand Down
1 change: 0 additions & 1 deletion src/SensorWrappers/Sgp4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/SensorWrappers/Sht4x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(serialNo);
}

float temp;
float humi;
uint16_t error = _driver.measureHighPrecision(temp, humi);
Expand All @@ -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<uint64_t>(serialNo);
return error;
}

SensorType Sht4x::getSensorType() const {
return _metaData.deviceType.sensorType;
}
Expand Down
1 change: 1 addition & 0 deletions src/SensorWrappers/Sht4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Stc3x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ unsigned long Stc3x::getMinimumMeasurementIntervalMs() const {
return 1000;
}

bool Stc3x::requiresInitializationStep() const {
return 1;
}

void* Stc3x::getDriver() {
return reinterpret_cast<void*>(&_driver);
}
1 change: 0 additions & 1 deletion src/SensorWrappers/Stc3x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions src/SensorWrappers/Svm4x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ unsigned long Svm4x::getMinimumMeasurementIntervalMs() const {
return 1000;
}

bool Svm4x::requiresInitializationStep() const {
return true;
}

void* Svm4x::getDriver() {
return reinterpret_cast<void*>(&_driver);
}
1 change: 0 additions & 1 deletion src/SensorWrappers/Svm4x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 05a4555

Please sign in to comment.