Commit 295800aa authored by Käppler's avatar Käppler
Browse files

co2_sensor: cleanup handling for special states

Previously, the control flow was determined by a mixture of
`if-else`-constructes and premature `return` statements,
which made it hard to understand which case occurs when.
parent 7224beff
...@@ -188,6 +188,21 @@ namespace sensor { ...@@ -188,6 +188,21 @@ namespace sensor {
} }
} }
void showState() {
switch(current_state) {
case BOOTUP: led_effects::showWaitingLED(color::blue); break;
// No special signaling, we want to show the CO2 value
case READY: break;
case NEEDSCALIBRATION: led_effects::showWaitingLED(color::magenta); break;
case PREPARECALIBRATION: led_effects::showWaitingLED(waiting_color); break;
// No special signaling here, too.
case CALIBRATION: break;
// This should not happen.
default:
Serial.println(F("Encountered unknown sensor state"));
}
}
/** Gets fresh data if available, checks calibration status, displays CO2 levels. /** Gets fresh data if available, checks calibration status, displays CO2 levels.
* Returns true if fresh data is available, for further processing (e.g. MQTT, CSV or LoRa) * Returns true if fresh data is available, for further processing (e.g. MQTT, CSV or LoRa)
*/ */
...@@ -200,44 +215,34 @@ namespace sensor { ...@@ -200,44 +215,34 @@ namespace sensor {
co2 = scd30.getCO2(); co2 = scd30.getCO2();
temperature = scd30.getTemperature(); temperature = scd30.getTemperature();
humidity = scd30.getHumidity(); humidity = scd30.getHumidity();
}
//NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity. //NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity.
if (co2 <= 0) { if (co2 <= 0) {
// No measurement yet. Waiting. // No measurement yet. Waiting.
switchState(BOOTUP); switchState(BOOTUP);
led_effects::showWaitingLED(color::blue); } else if (co2 < 250) {
return false; // Sensor should be calibrated.
} else if (current_state != PREPARECALIBRATION) { switchState(NEEDSCALIBRATION);
switchState(READY); } else if (should_calibrate) {
}
/**
* Fresh data. Log it and send it if needed.
*/
if (freshData) {
if (should_calibrate) {
countStableMeasurements(); countStableMeasurements();
if (stable_measurements == config::enough_stable_measurements) {
calibrateAndRestart();
}
} else {
switchState(READY);
} }
logToSerial();
}
if (should_calibrate) { // Log every time fresh data is available.
if (stable_measurements == config::enough_stable_measurements) { logToSerial();
calibrateAndRestart();
}
led_effects::showWaitingLED(waiting_color);
return false;
} }
if (co2 < 250) { // We need to show LED effects for "old" data, too, as long as we get new data.
// Sensor should be calibrated. if (current_state == READY) {
led_effects::showWaitingLED(color::magenta); displayCO2OnLedRing();
switchState(NEEDSCALIBRATION); } else {
return false; showState();
} }
displayCO2OnLedRing();
return freshData; return freshData;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment