Commit f530136b 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 9809f41c
......@@ -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) {
} else if (co2 < 250) {
// Sensor should be calibrated.
switchState(NEEDSCALIBRATION);
} else if (should_calibrate) {
countStableMeasurements();
}
logToSerial();
}
if (should_calibrate) {
if (stable_measurements == config::enough_stable_measurements) {
if (stable_measurements == 60) {
calibrateAndRestart();
}
led_effects::showWaitingLED(waiting_color);
return false;
} else {
switchState(READY);
}
if (co2 < 250) {
// Sensor should be calibrated.
led_effects::showWaitingLED(color::magenta);
switchState(NEEDSCALIBRATION);
return false;
// Log every time fresh data is available.
logToSerial();
}
// We need to show LED effects for "old" data, too, as long as we get new data.
if (current_state == READY) {
displayCO2OnLedRing();
} else {
showState();
}
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