Commit 316d13bc authored by Eric Duminil's avatar Eric Duminil
Browse files

Refactor co2_sensor. Smaller methods

parent e10bc6a4
...@@ -116,7 +116,7 @@ void loop() { ...@@ -116,7 +116,7 @@ void loop() {
// Short press for night mode, Long press for calibration. // Short press for night mode, Long press for calibration.
checkFlashButton(); checkFlashButton();
sensor::getAndSendData(); sensor::processData();
uint32_t duration = millis() - t0; uint32_t duration = millis() - t0;
if (duration > max_loop_duration) { if (duration > max_loop_duration) {
......
...@@ -97,6 +97,7 @@ namespace sensor { ...@@ -97,6 +97,7 @@ namespace sensor {
bool updateDataIfAvailable() { bool updateDataIfAvailable() {
if (scd30.dataAvailable()) { if (scd30.dataAvailable()) {
// checkTimerDeviation(); // checkTimerDeviation();
timestamp = ntp::getLocalTime();
co2 = scd30.getCO2(); co2 = scd30.getCO2();
temperature = scd30.getTemperature(); temperature = scd30.getTemperature();
humidity = scd30.getHumidity(); humidity = scd30.getHumidity();
...@@ -130,7 +131,36 @@ namespace sensor { ...@@ -130,7 +131,36 @@ namespace sensor {
ESP.restart(); ESP.restart();
} }
void getAndSendData() { void logToSerial() {
Serial.println(timestamp);
Serial.print(F("co2(ppm): "));
Serial.print(co2);
Serial.print(F(" temp(C): "));
Serial.print(temperature);
Serial.print(F(" humidity(%): "));
Serial.println(humidity);
}
void displayCO2OnLedRing() {
if (co2 < 250) {
// Sensor should be calibrated.
LedEffects::showWaitingLED(color::magenta);
return;
}
/**
* Display data, even if it's "old" (with breathing).
* Those effects include a short delay.
*/
if (co2 < 2000) {
LedEffects::displayCO2color(co2);
LedEffects::breathe(co2);
} else {
// >= 2000: entire ring blinks red
LedEffects::redAlert();
}
}
void processData() {
bool freshData = updateDataIfAvailable(); bool freshData = updateDataIfAvailable();
//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.
...@@ -141,22 +171,14 @@ namespace sensor { ...@@ -141,22 +171,14 @@ namespace sensor {
} }
/** /**
* Fresh data. Show it and send it if needed. * Fresh data. Log it and send it if needed.
*/ */
if (freshData) { if (freshData) {
if (should_calibrate) { if (should_calibrate) {
countStableMeasurements(); countStableMeasurements();
} }
timestamp = ntp::getLocalTime();
Serial.println(timestamp);
Serial.print(F("co2(ppm): "));
Serial.print(co2);
Serial.print(F(" temp(C): "));
Serial.print(temperature);
Serial.print(F(" humidity(%): "));
Serial.println(humidity);
logToSerial();
csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity); csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity);
#ifdef MQTT #ifdef MQTT
...@@ -172,22 +194,6 @@ namespace sensor { ...@@ -172,22 +194,6 @@ namespace sensor {
return; return;
} }
if (co2 < 250) { displayCO2OnLedRing();
// Sensor should be calibrated.
LedEffects::showWaitingLED(color::magenta);
return;
}
/**
* Display data, even if it's "old" (with breathing).
* Those effects include a short delay.
*/
if (co2 < 2000) {
LedEffects::displayCO2color(co2);
LedEffects::breathe(co2);
} else { // >= 2000: entire ring blinks red
LedEffects::redAlert();
}
} }
} }
...@@ -28,7 +28,7 @@ namespace sensor { ...@@ -28,7 +28,7 @@ namespace sensor {
extern String timestamp; extern String timestamp;
void initialize(); void initialize();
void getAndSendData(); void processData();
void startCalibrationProcess(); void startCalibrationProcess();
} }
#endif #endif
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