UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Revision:
5:a37e3a15444b
Child:
6:81a3d517fd56
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/SDDataStore.cpp	Wed Apr 24 02:34:51 2019 +0000
@@ -0,0 +1,108 @@
+#include "SDDataStore.h"
+
+/* constructor, pSD, TimeManager */
+SDDataStore::SDDataStore(TimeManager *pSetTM)
+{
+    pS = new SDFileSystem(D11, D12, D13, D4, "sd"); // MOSI, MISO, SCK, CS;
+    pT = pSetTM;
+    fpCurrent = NULL;
+    fnameCurrent[0] = '\0';
+}
+
+/* destructor */
+SDDataStore::~SDDataStore()
+{
+    closeFile();
+}
+    
+/* for Sensing and Logging generate (prefix)TimeStampFile.(ext)*/
+bool SDDataStore::startFileWithTimeStamp(char *prefix, char *ext)
+{
+    char timeStamp[TimeManager::TimeStampLength + 1] = {0};
+    char fname[MaxPathLength] = {0};
+    pT->getTimeStamp(timeStamp);
+    sprintf(fname, "%s%s.%s", prefix, timeStamp, ext);
+    if (this->checkFileExist(fname) == true) {
+        this->removeFile(fname);
+    }
+    if (this->openFile(fname) != true) {
+        return false;
+    }
+    return true;
+}
+    
+/* checkFileExist, false=NOT-EXIST, true=EXIST */
+bool SDDataStore::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 SDDataStore::removeFile(char *fname)
+{
+    char filePath[MaxPathLength];
+    sprintf(filePath, "/sd/%s", fname);
+    pathTermination(filePath);
+    remove(filePath);
+}
+    
+/* open file at WriteMode FALSE=ERR, TRUE=OK */
+bool SDDataStore::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 *SDDataStore::getFileName(void)
+{
+    return fnameCurrent;
+}
+
+/* get current file pointer  */
+FILE *SDDataStore::getFilePointer(void)
+{
+    return fpCurrent;
+}
+    
+/* close file with this Class  */
+void SDDataStore::syncFile(void)
+{
+    if (fpCurrent != NULL) {
+        fflush(fpCurrent);
+    }
+}
+
+/* close file with this Class  */
+void SDDataStore::closeFile(void)
+{
+    if (fpCurrent != NULL) {
+        fclose(fpCurrent);
+        fpCurrent = NULL;
+        fnameCurrent[0] = '\0';
+    }
+}
+    
+void SDDataStore::pathTermination(char *pathArray)
+{
+    pathArray[MaxPathLength-1] = '\0';
+}