change CD logging to single file

This commit is contained in:
cyberman54 2022-01-30 15:25:46 +01:00
parent dffb5dee75
commit bce80542a8
2 changed files with 77 additions and 46 deletions

View File

@ -1,15 +1,17 @@
#ifndef _SDCARD_H
#define _SDCARD_H
#include <globals.h>
#include "globals.h"
#include <stdio.h>
#include <SPI.h>
#if (HAS_SDCARD)
#if HAS_SDCARD == 1
#include <mySD.h>
#define MYSD SD
#elif HAS_SDCARD == 2
#include <SD_MMC.h>
#define MYSD SD_MMC
#else
#error HAS_SDCARD unknown card reader value, must be either 1 or 2
#endif
@ -54,7 +56,7 @@
#define SDCARD_DATA3 13
#endif
#define SDCARD_FILE_NAME "/paxcount.%02d"
#define SDCARD_FILE_NAME clientId
#define SDCARD_FILE_HEADER "timestamp,wifi,ble"
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
@ -62,7 +64,7 @@
#endif
bool sdcard_init(bool create = true);
bool sdcard_close(void);
void sdcard_close(void);
void sdcardWriteData(uint16_t, uint16_t, uint16_t = 0);
#endif // _SDCARD_H

View File

@ -1,4 +1,7 @@
// routines for writing data to an SD-card, if present
// use FAT32 formatted card
// check whether your card reader supports SPI oder SDMMC and select appropriate
// SD low level driver in board hal file
// Local logging tag
static const char TAG[] = __FILE__;
@ -7,19 +10,16 @@ static const char TAG[] = __FILE__;
#ifdef HAS_SDCARD
static bool useSDCard;
static void createFile(void);
static bool useSDCard = false;
static void openFile(void);
File fileSDCard;
bool sdcard_close(void) {
ESP_LOGD(TAG, "unmounting SD-card");
fileSDCard.flush();
fileSDCard.end();
}
bool sdcard_init(bool create) {
uint8_t cardType;
uint64_t cardSize;
ESP_LOGI(TAG, "looking for SD-card...");
// for usage of SD drivers on ESP32 platform see
@ -27,22 +27,51 @@ bool sdcard_init(bool create) {
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html
#if HAS_SDCARD == 1 // use SD SPI host driver
useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
useSDCard = MYSD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
#elif HAS_SDCARD == 2 // use SD MMC host driver
// enable internal pullups of sd-data lines
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA0), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA1), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA2), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA3), GPIO_PULLUP_ONLY);
useSDCard = SD_MMC.begin();
useSDCard = MYSD.begin();
#endif
if (useSDCard) {
ESP_LOGI(TAG, "SD-card found");
createFile();
} else
cardType = MYSD.cardType();
cardSize = MYSD.cardSize() / (1024 * 1024);
} else {
ESP_LOGI(TAG, "SD-card not found");
return useSDCard;
return false;
}
if (cardType == CARD_NONE) {
ESP_LOGI(TAG, "No SD card attached");
return false;
}
if (cardType == CARD_MMC) {
ESP_LOGI(TAG, "SD Card type: MMC");
} else if (cardType == CARD_SD) {
ESP_LOGI(TAG, "SD Card type: SDSC");
} else if (cardType == CARD_SDHC) {
ESP_LOGI(TAG, "SD Card type: SDHC");
} else {
ESP_LOGI(TAG, "SD Card type: UNKNOWN");
}
ESP_LOGI(TAG, "SD Card Size: %lluMB\n", cardSize);
openFile();
return true;
}
void sdcard_close(void) {
ESP_LOGI(TAG, "closing SD-card");
fileSDCard.flush();
fileSDCard.close();
}
void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
@ -60,7 +89,7 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
if (!useSDCard)
return;
ESP_LOGD(TAG, "writing to SD-card");
ESP_LOGI(TAG, "SD: writing data");
strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt);
fileSDCard.print(tempBuffer);
snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle);
@ -78,38 +107,39 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
if (++counterWrites > 2) {
// force writing to SD-card
ESP_LOGD(TAG, "flushing data to card");
ESP_LOGI(TAG, "SD: flushing data");
fileSDCard.flush();
counterWrites = 0;
}
}
void createFile(void) {
char bufferFilename[8 + 1 + 3 + 1];
void openFile(void) {
char bufferFilename[30];
useSDCard = false;
for (int i = 0; i < 100; i++) {
sprintf(bufferFilename, SDCARD_FILE_NAME, i);
ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
snprintf(bufferFilename, sizeof(bufferFilename), "/%s.csv", SDCARD_FILE_NAME);
ESP_LOGI(TAG, "SD: looking for file <%s>", bufferFilename);
#if HAS_SDCARD == 1
bool fileExists = SD.exists(bufferFilename);
#elif HAS_SDCARD == 2
bool fileExists = SD_MMC.exists(bufferFilename);
#endif
/*
if (MYSD.exists(bufferFilename)) {
if (MYSD.open(bufferFilename, FILE_APPEND))
useSDCard = true;
} else {
ESP_LOGI(TAG, "SD: file does not exist, creating it");
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
}
*/
if (!fileExists) {
ESP_LOGD(TAG, "SD: file does not exist: creating");
if (!MYSD.exists(bufferFilename))
ESP_LOGI(TAG, "SD: file does not exist, creating it");
#if HAS_SDCARD == 1
fileSDCard = SD.open(bufferFilename, FILE_WRITE);
#elif HAS_SDCARD == 2
fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE);
#endif
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
if (fileSDCard) {
ESP_LOGD(TAG, "SD: file opened: <%s>", bufferFilename);
if (useSDCard) {
ESP_LOGI(TAG, "SD: file opened: <%s>", bufferFilename);
fileSDCard.print(SDCARD_FILE_HEADER);
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data
@ -118,11 +148,10 @@ void createFile(void) {
fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
#endif
fileSDCard.println();
useSDCard = true;
break;
}
}
} else {
ESP_LOGE(TAG, "SD: file not opened error");
}
return;
}