From d5eeba119fdc3e7b8769ed66cd599a05fcb62ab9 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sat, 26 Dec 2020 22:31:49 +0100
Subject: [PATCH] Refactor : fewer dependencies

---
 ampel-firmware.ino | 14 +++++++++++++-
 co2_sensor.cpp     | 47 ++++++++++++++++------------------------------
 co2_sensor.h       | 10 +---------
 3 files changed, 30 insertions(+), 41 deletions(-)

diff --git a/ampel-firmware.ino b/ampel-firmware.ino
index bdce462..a903956 100644
--- a/ampel-firmware.ino
+++ b/ampel-firmware.ino
@@ -132,7 +132,19 @@ void loop() {
   // Short press for night mode, Long press for calibration.
   checkFlashButton();
 
-  sensor::processData();
+  if (sensor::processData()) {
+#ifdef CSV_WRITER
+    csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
+#endif
+
+#ifdef MQTT
+    mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
+#endif
+
+#if defined(LORAWAN) && defined(ESP32)
+    lorawan::preparePayloadIfTimehasCome();
+#endif
+  }
 
   uint32_t duration = millis() - t0;
   if (duration > max_loop_duration) {
diff --git a/co2_sensor.cpp b/co2_sensor.cpp
index 6e3dec3..59d7446 100644
--- a/co2_sensor.cpp
+++ b/co2_sensor.cpp
@@ -95,18 +95,6 @@ namespace sensor {
     previous_co2 = co2;
   }
 
-  bool updateDataIfAvailable() {
-    if (scd30.dataAvailable()) {
-      // checkTimerDeviation();
-      timestamp = ntp::getLocalTime();
-      co2 = scd30.getCO2();
-      temperature = scd30.getTemperature();
-      humidity = scd30.getHumidity();
-      return true;
-    }
-    return false;
-  }
-
   void startCalibrationProcess() {
     /** From the sensor documentation:
      * For best results, the sensor has to be run in a stable environment in continuous mode at
@@ -157,14 +145,25 @@ namespace sensor {
     }
   }
 
-  void processData() {
-    bool freshData = updateDataIfAvailable();
+  /** Gets fresh data if available, checks calibration status, displays CO2 levels.
+   * Returns true if fresh data is available, for further processing (MQTT)
+   */
+  bool processData() {
+    bool freshData = scd30.dataAvailable();
+
+    if (freshData) {
+      // checkTimerDeviation();
+      timestamp = ntp::getLocalTime();
+      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.
       LedEffects::showWaitingLED(color::blue);
-      return;
+      return false;
     }
 
     /**
@@ -174,22 +173,7 @@ namespace sensor {
       if (should_calibrate) {
         countStableMeasurements();
       }
-
       logToSerial();
-
-      //TODO: Move the 3 back to ampel-firmware.ino and remove headers from co2_sensor.h
-
-#ifdef CSV_WRITER
-      csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity);
-#endif
-
-#ifdef MQTT
-      mqtt::publishIfTimeHasCome(timestamp, co2, temperature, humidity);
-#endif
-
-#if defined(LORAWAN) && defined(ESP32)
-      lorawan::preparePayloadIfTimehasCome();
-#endif
     }
 
     if (should_calibrate) {
@@ -197,9 +181,10 @@ namespace sensor {
         calibrateAndRestart();
       }
       LedEffects::showWaitingLED(waiting_color);
-      return;
+      return false;
     }
 
     displayCO2OnLedRing();
+    return freshData;
   }
 }
diff --git a/co2_sensor.h b/co2_sensor.h
index fc5c051..f11eb81 100644
--- a/co2_sensor.h
+++ b/co2_sensor.h
@@ -7,16 +7,8 @@
 #include "config.h"
 #include "led_effects.h"
 #include "util.h"
-#include "csv_writer.h" // To close filesystem before restart.
 #include <Wire.h>
 
-#ifdef MQTT
-#  include "mqtt.h"
-#endif
-#ifdef LORAWAN
-#  include "lorawan.h"
-#endif
-
 namespace config {
   extern uint16_t measurement_timestep; // [s] Value between 2 and 1800 (range for SCD30 sensor)
   extern const bool auto_calibrate_sensor; // [true / false]
@@ -32,7 +24,7 @@ namespace sensor {
   extern String timestamp;
 
   void initialize();
-  void processData();
+  bool processData();
   void startCalibrationProcess();
 }
 #endif
-- 
GitLab