From 14baacc4bf1bc3b39a7cafb82373366e9e67b359 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Mon, 3 Feb 2020 15:27:49 +0100 Subject: [PATCH] Update sdcard.cpp --- src/sdcard.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 8e45ea7c..1c1256a4 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -19,7 +19,7 @@ static void createFile(void); File fileSDCard; -bool sdcardInit() { +bool sdcard_init() { ESP_LOGD(TAG, "looking for SD-card..."); useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK); if (useSDCard) @@ -43,7 +43,6 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) { sprintf(tempBuffer, "%d,%d", noWifi, noBle); fileSDCard.print( tempBuffer); #if (HAS_SDS011) - ESP_LOGD(TAG, "fine-dust-values: %5.1f,%4.1f", pm10, pm25); sprintf(tempBuffer, ",%5.1f,%4.1f", pm10, pm25); fileSDCard.print( tempBuffer); #endif @@ -84,4 +83,41 @@ void createFile(void) { return; } +#endif // (HAS_SDCARD) + + if (++counterWrites > 2) { + // force writing to SD-card + ESP_LOGD(TAG, "flushing data to card"); + fileSDCard.flush(); + counterWrites = 0; + } +} + +void createFile(void) { + char bufferFilename[8 + 1 + 3 + 1]; + + useSDCard = false; + + for (int i = 0; i < 100; i++) { + sprintf(bufferFilename, SDCARD_FILE_NAME, i); + ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename); + bool fileExists = SD.exists(bufferFilename); + if (!fileExists) { + ESP_LOGD(TAG, "SD: file does not exist: opening"); + fileSDCard = SD.open(bufferFilename, FILE_WRITE); + if (fileSDCard) { + ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); + fileSDCard.print( SDCARD_FILE_HEADER ); +#if (HAS_SDS011) + fileSDCard.print( SDCARD_FILE_HEADER_SDS011 ); +#endif + fileSDCard.println(); + useSDCard = true; + break; + } + } + } + return; +} + #endif // (HAS_SDCARD)