From 48ba035aa179c61368e4b0f7e9f87ba69984b2e7 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 2 Apr 2022 13:15:55 +0100 Subject: =?UTF-8?q?more=20serial=20commands:=20logwipe,=20log=E2=86=92logc?= =?UTF-8?q?at?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datalog.h | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/datalog.h b/datalog.h index 73299b5..d68f1b0 100644 --- a/datalog.h +++ b/datalog.h @@ -27,26 +27,41 @@ const char csvHeader[]=" secs, co2, temp, humid, pm1, pm2.5, pm4, pm const char csvFormat[]="% 6lu, % 4u, % 5.1f, %5.1f, %6.1f, %6.1f, %6.1f, %6.1f, %4.2f\n"; const size_t lineLength=65; +const char defaultFilename[] = "quality.csv"; + class DataLog { private: File logfile; + const char *filename; char line[100]; uint8_t linesSinceLastFlush; uint8_t flushThreshold; -public: - DataLog(uint8_t ft=4) : flushThreshold(ft) {} - void start() { - bool sdok=SD.begin(mySD_CS, mySD_MOSI, mySD_MISO, mySD_SCLK); - Serial.print("# sdok: ");Serial.println(sdok); - logfile = SD.open("quality.csv",FILE_WRITE); + void openFile() { + logfile = SD.open(filename,FILE_WRITE); Serial.print("# file: ");Serial.println(logfile.name()); + uint32_t fileSize = logfile.size(); logfile.seek(fileSize); + if (fileSize == 0) { logfile.write((uint8_t*)csvHeader, lineLength); - } - linesSinceLastFlush=1; + linesSinceLastFlush=1; + } + else { + linesSinceLastFlush=0; + } + } +public: + DataLog(uint8_t ft=4, const char *fn=defaultFilename) : + filename(fn), + flushThreshold(ft) + {} + + void start() { + bool sdok=SD.begin(mySD_CS, mySD_MOSI, mySD_MISO, mySD_SCLK); + Serial.print("# sdok: ");Serial.println(sdok); + openFile(); } void show(const SensorData *data) { @@ -78,7 +93,7 @@ public: } bool serialCommand(const String &tag, const String &command) { - if (command == "log") { + if (command == "logcat") { Serial.print(tag);Serial.println(" begin"); uint8_t buffer[1024]; @@ -95,6 +110,13 @@ public: return true; } + else if (command == "logwipe") { + logfile.close(); + // ugh, why is it `remove(char*)`?? + SD.remove(const_cast(filename)); + openFile(); + Serial.print(tag);Serial.println(" wiped"); + } return false; } -- cgit v1.2.3