UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
Diff: main.cpp
- Revision:
- 4:bec3f80dc49c
- Parent:
- 2:a694440145e9
- Child:
- 5:a37e3a15444b
--- a/main.cpp Tue Apr 23 08:49:40 2019 +0000 +++ b/main.cpp Wed Apr 24 00:40:42 2019 +0000 @@ -10,7 +10,152 @@ /** --- Global Variables --- */ Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut myled(LED1); -SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, D4, "sd"); +SDFileSystem sd(D11, D12, D13, D4, "sd"); // MOSI, MISO, SCK, CS +FILE *fp; + +class SDDataStore +{ +public: + const static int MaxPathLength = 256; +private: + SDFileSystem *pS; + TimeManager *pT; + FILE *fpCurrent; + char fnameCurrent[MaxPathLength]; +private: + /* (INVALIDED) constructor */ + SDDataStore(void); +public: + /* constructor, pSD, TimeManager */ + SDDataStore(SDFileSystem *pSetSD, TimeManager *pSetTM) + { + pS = pSetSD; + pT = pSetTM; + fpCurrent = NULL; + fnameCurrent[0] = '\0'; + } + + /* destructor */ + ~SDDataStore() + { + closeFile(); + } + + bool startFileWithTimeStamp(void) + { + char timeStamp[TimeManager::TimeStampLength + 1] = {0}; + char fname[MaxPathLength] = {0}; + pT->getTimeStamp(timeStamp); + sprintf(fname, "%s.txt", timeStamp); + if (this->checkFileExist(fname) == true) { + this->removeFile(fname); + } + if (this->openFile(fname) != true) { + return false; + } + return true; + } + + /* checkFileExist, false=NOT-EXIST, true=EXIST */ + bool checkFileExist(char *fname) + { + FILE *fp; + char filePath[MaxPathLength]; + sprintf(filePath, "/sd/%s", fname); + pathTermination(filePath); + fp = fopen(filePath, "r"); + if (fp == NULL) { + return false; + } + fclose(fp); + return true; + } + + /* removeFile, false=NOT-EXIST, true=EXIST */ + void removeFile(char *fname) + { + char filePath[MaxPathLength]; + sprintf(filePath, "/sd/%s", fname); + pathTermination(filePath); + remove(filePath); + } + + /* open file at WriteMode FALSE=ERR, TRUE=OK */ + bool openFile(char *fname) + { + FILE *fp; + char filePath[MaxPathLength]; + sprintf(filePath, "/sd/%s", fname); + pathTermination(filePath); + + fp = fopen(filePath, "w"); + if (fp == NULL) { + return false; + } + fpCurrent = fp; + sprintf(fnameCurrent, "%s", fname); + return true; + } + + /* get FileName of open file */ + char *getFileName(void) + { + return fnameCurrent; + } + + /* get current file pointer */ + FILE *getFilePointer(void) { + return fpCurrent; + } + + /* close file with this Class */ + void syncFile(void) + { + if (fpCurrent != NULL) { + fflush(fpCurrent); + } + } + + /* close file with this Class */ + void closeFile(void) + { + if (fpCurrent != NULL) { + fclose(fpCurrent); + fpCurrent = NULL; + fnameCurrent[0] = '\0'; + } + } + +private: + void pathTermination(char *pathArray) + { + pathArray[MaxPathLength-1] = '\0'; + } +}; + + + +/** --- static --- */ +static void sdcard_main() +{ + FILE *fp; + SDDataStore *pSds; + pc.printf("Initializing...\r\n"); + pSds = new SDDataStore(&sd, pTM); + + if (pSds->startFileWithTimeStamp() != true) { + pc.printf("Unable to write the file\n"); + return; + } + if ((fp = pSds->getFilePointer()) == NULL) { + pc.printf("Unable to write the file\r\n"); + return; + } + fprintf(fp, "mbed SDCard application!(in %s)", pSds->getFileName()); + pSds->syncFile(); + pSds->closeFile(); + pc.printf("File successfully written!\r\n"); +} /** --- main --- */ int main() @@ -30,6 +175,8 @@ pCP->run(); pc.printf("Hello World !\n"); + sdcard_main(); + while(1) { wait(1); }