RadioSonde/libraries/SondeLib/sd.h

180 lines
5.4 KiB
C
Raw Permalink Normal View History

2022-08-24 08:28:06 +02:00
#include "FS.h"
#include "SD_MMC.h"
//Write data RadioSonde on SD
//Fonction de gestion SD
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
}
void sdwrite()
{
Serial.print("Detect SD\n");
if(SD_MMC.begin()){
uint8_t cardType = SD_MMC.cardType();
if(cardType != CARD_NONE){
Serial.print("SD_MMC Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
/*
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
*/
if (sonde.si()->validPos) {
char buff[20];
char chemin[40];
//String repertoire="/radiosonde/", name=sonde.si()->ser;
//repertoire.toCharArray(chemin, 40);
//if (!SD_MMC.exists(chemin)) {
createDir(SD_MMC, "/radiosonde/");
Serial.printf("\nWrite Rep\n");
//listDir(SD_MMC, chemin, 0);
//}
//if (name.length()>8) {
//name.concat(".csv");
//repertoire.concat(name);
//repertoire.toCharArray(chemin, 40);
writeFile(SD_MMC, "/radiosonde/sonde.csv", "");
Serial.printf("\nWrite file\n");
//if (!SD_MMC.exists(chemin)) {
appendFile(SD_MMC, "/radiosonde/sonde.csv", "RadioSonde N; Ser ; Type ; Freq ; Alt ; VSpeed ; HSpeed ; Dir ; LaunchSite ; RSSI\n");
Serial.printf("\nWrite titre\n");
//}
Serial.printf("\nWrite Data\n");
snprintf(buff, 16, "%10c", sonde.si()->id);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%12c", sonde.si()->ser);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
appendFile(SD_MMC, "/radiosonde/sonde.csv", sondeTypeStr[sonde.si()->type]);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%3.3fMhz", sonde.si()->freq);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, sonde.si()->alt>=1000?" %5.0fm":" %3.1fm", sonde.si()->alt);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%2.2fm/s", sonde.si()->vs);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%2.2fm/s", sonde.si()->hs);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%3.0f°", sonde.si()->dir);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%18c", sonde.si()->launchsite);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
snprintf(buff, 16, "%d", sonde.si()->rssi);
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
appendFile(SD_MMC, "/radiosonde/sonde.csv", "\n");
}
}
}
}