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 {
}
}
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.
* Returns true if fresh data is available, for further processing (e.g. MQTT, CSV or LoRa)
*/
......@@ -200,44 +215,34 @@ namespace sensor {
co2 = scd30.getCO2();
temperature = scd30.getTemperature();
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.
if (co2 <= 0) {
// No measurement yet. Waiting.
switchState(BOOTUP);
led_effects::showWaitingLED(color::blue);
return false;
} else if (current_state != PREPARECALIBRATION) {
switchState(READY);
}
/**
* Fresh data. Log it and send it if needed.
*/
if (freshData) {
if (should_calibrate) {
//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) {
// No measurement yet. Waiting.
switchState(BOOTUP);
} else if (co2 < 250) {
// Sensor should be calibrated.
switchState(NEEDSCALIBRATION);
} else if (should_calibrate) {
countStableMeasurements();
if (stable_measurements == config::enough_stable_measurements) {
calibrateAndRestart();
}
} else {
switchState(READY);
}
logToSerial();
}
if (should_calibrate) {
if (stable_measurements == config::enough_stable_measurements) {
calibrateAndRestart();
}
led_effects::showWaitingLED(waiting_color);
return false;
// Log every time fresh data is available.
logToSerial();
}
if (co2 < 250) {
// Sensor should be calibrated.
led_effects::showWaitingLED(color::magenta);
switchState(NEEDSCALIBRATION);
return false;
// We need to show LED effects for "old" data, too, as long as we get new data.
if (current_state == READY) {
displayCO2OnLedRing();
} else {
showState();
}
displayCO2OnLedRing();
return freshData;
}
......
Supports Markdown
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