diff --git a/csv_writer.cpp b/csv_writer.cpp index b900fc5a2c41b630018ea9a129cbd7a81d3a2a3c..84bdc646940cf7e6ac582c239113033fd040e06a 100644 --- a/csv_writer.cpp +++ b/csv_writer.cpp @@ -134,32 +134,36 @@ namespace csv_writer { return csv_file; } - void logIfTimeHasCome(const String &timeStamp, int16_t co2, float temp, float hum) { + void log(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity) { + LedEffects::onBoardLEDOn(); + File csv_file = openOrCreate(); + char csv_line[42]; + snprintf(csv_line, sizeof(csv_line), "%s;%d;%.1f;%.1f\r\n", timeStamp.c_str(), co2, temperature, humidity); + if (csv_file) { + size_t written_bytes = csv_file.print(csv_line); + csv_file.close(); + if (written_bytes == 0) { + Serial.println(F("Nothing written. Disk full?")); + } else { + Serial.println(F("Wrote file content:")); + Serial.print(csv_line); + last_successful_write = ntp::getLocalTime(); + } + updateFsInfo(); + delay(50); + } else { + //NOTE: Can it ever happen that outfile is false? + Serial.println(F("Problem on create file!")); + } + LedEffects::onBoardLEDOff(); + } + + void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity) { unsigned long now = seconds(); //TODO: Write average since last CSV write? if (now - last_written_at > config::csv_interval) { last_written_at = now; - LedEffects::onBoardLEDOn(); - File csv_file = openOrCreate(); - char csv_line[42]; - snprintf(csv_line, sizeof(csv_line), "%s;%d;%.1f;%.1f\r\n", timeStamp.c_str(), co2, temp, hum); - if (csv_file) { - size_t written_bytes = csv_file.print(csv_line); - csv_file.close(); - if (written_bytes == 0) { - Serial.println(F("Nothing written. Disk full?")); - } else { - Serial.println(F("Wrote file content:")); - Serial.print(csv_line); - last_successful_write = ntp::getLocalTime(); - } - updateFsInfo(); - delay(50); - } else { - //NOTE: Can it ever happen that outfile is false? - Serial.println(F("Problem on create file!")); - } - LedEffects::onBoardLEDOff(); + log(timeStamp, co2, temperature, humidity); } } } diff --git a/csv_writer.h b/csv_writer.h index 6478d88dd47e3985807701f8dfba09c3e824ea85..c6b71a1ed980fbd5ceca472056000af72b50ae8c 100644 --- a/csv_writer.h +++ b/csv_writer.h @@ -14,12 +14,12 @@ #include "led_effects.h" #include "config.h" namespace config { - extern uint16_t csv_interval; // [s] + extern uint16_t csv_interval; // [s] } namespace csv_writer { extern String last_successful_write; void initialize(); - void logIfTimeHasCome(const String &timeStamp, int16_t co2, float temp, float hum); + void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity); int getAvailableSpace(); extern const String filename; }