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

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Thu May 09 00:06:21 2019 +0000
Revision:
6:81a3d517fd56
Parent:
5:a37e3a15444b
Child:
7:9ab8809f9693
2019-05-08_small-changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 5:a37e3a15444b 1 #include "SDDataStore.h"
Inscape_ao 5:a37e3a15444b 2
Inscape_ao 5:a37e3a15444b 3 /* constructor, pSD, TimeManager */
Inscape_ao 5:a37e3a15444b 4 SDDataStore::SDDataStore(TimeManager *pSetTM)
Inscape_ao 5:a37e3a15444b 5 {
Inscape_ao 6:81a3d517fd56 6 //pS = new SDFileSystem(D11, D12, D13, D4, "sd"); // MOSI, MISO, SCK, CS;
Inscape_ao 6:81a3d517fd56 7 pS = new SDFileSystem(PC_12, PC_11, PC_10, D4, "sd"); // MOSI, MISO, SCK, CS;
Inscape_ao 5:a37e3a15444b 8 pT = pSetTM;
Inscape_ao 5:a37e3a15444b 9 fpCurrent = NULL;
Inscape_ao 5:a37e3a15444b 10 fnameCurrent[0] = '\0';
Inscape_ao 5:a37e3a15444b 11 }
Inscape_ao 5:a37e3a15444b 12
Inscape_ao 5:a37e3a15444b 13 /* destructor */
Inscape_ao 5:a37e3a15444b 14 SDDataStore::~SDDataStore()
Inscape_ao 5:a37e3a15444b 15 {
Inscape_ao 5:a37e3a15444b 16 closeFile();
Inscape_ao 5:a37e3a15444b 17 }
Inscape_ao 5:a37e3a15444b 18
Inscape_ao 5:a37e3a15444b 19 /* for Sensing and Logging generate (prefix)TimeStampFile.(ext)*/
Inscape_ao 5:a37e3a15444b 20 bool SDDataStore::startFileWithTimeStamp(char *prefix, char *ext)
Inscape_ao 5:a37e3a15444b 21 {
Inscape_ao 5:a37e3a15444b 22 char timeStamp[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 5:a37e3a15444b 23 char fname[MaxPathLength] = {0};
Inscape_ao 5:a37e3a15444b 24 pT->getTimeStamp(timeStamp);
Inscape_ao 5:a37e3a15444b 25 sprintf(fname, "%s%s.%s", prefix, timeStamp, ext);
Inscape_ao 5:a37e3a15444b 26 if (this->checkFileExist(fname) == true) {
Inscape_ao 5:a37e3a15444b 27 this->removeFile(fname);
Inscape_ao 5:a37e3a15444b 28 }
Inscape_ao 5:a37e3a15444b 29 if (this->openFile(fname) != true) {
Inscape_ao 5:a37e3a15444b 30 return false;
Inscape_ao 5:a37e3a15444b 31 }
Inscape_ao 5:a37e3a15444b 32 return true;
Inscape_ao 5:a37e3a15444b 33 }
Inscape_ao 5:a37e3a15444b 34
Inscape_ao 5:a37e3a15444b 35 /* checkFileExist, false=NOT-EXIST, true=EXIST */
Inscape_ao 5:a37e3a15444b 36 bool SDDataStore::checkFileExist(char *fname)
Inscape_ao 5:a37e3a15444b 37 {
Inscape_ao 5:a37e3a15444b 38 FILE *fp;
Inscape_ao 5:a37e3a15444b 39 char filePath[MaxPathLength];
Inscape_ao 5:a37e3a15444b 40 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 5:a37e3a15444b 41 pathTermination(filePath);
Inscape_ao 5:a37e3a15444b 42 fp = fopen(filePath, "r");
Inscape_ao 5:a37e3a15444b 43 if (fp == NULL) {
Inscape_ao 5:a37e3a15444b 44 return false;
Inscape_ao 5:a37e3a15444b 45 }
Inscape_ao 5:a37e3a15444b 46 fclose(fp);
Inscape_ao 5:a37e3a15444b 47 return true;
Inscape_ao 5:a37e3a15444b 48 }
Inscape_ao 5:a37e3a15444b 49
Inscape_ao 5:a37e3a15444b 50 /* removeFile, false=NOT-EXIST, true=EXIST */
Inscape_ao 5:a37e3a15444b 51 void SDDataStore::removeFile(char *fname)
Inscape_ao 5:a37e3a15444b 52 {
Inscape_ao 5:a37e3a15444b 53 char filePath[MaxPathLength];
Inscape_ao 5:a37e3a15444b 54 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 5:a37e3a15444b 55 pathTermination(filePath);
Inscape_ao 5:a37e3a15444b 56 remove(filePath);
Inscape_ao 5:a37e3a15444b 57 }
Inscape_ao 5:a37e3a15444b 58
Inscape_ao 5:a37e3a15444b 59 /* open file at WriteMode FALSE=ERR, TRUE=OK */
Inscape_ao 5:a37e3a15444b 60 bool SDDataStore::openFile(char *fname)
Inscape_ao 5:a37e3a15444b 61 {
Inscape_ao 5:a37e3a15444b 62 FILE *fp;
Inscape_ao 5:a37e3a15444b 63 char filePath[MaxPathLength];
Inscape_ao 5:a37e3a15444b 64 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 5:a37e3a15444b 65 pathTermination(filePath);
Inscape_ao 5:a37e3a15444b 66
Inscape_ao 5:a37e3a15444b 67 fp = fopen(filePath, "w");
Inscape_ao 5:a37e3a15444b 68 if (fp == NULL) {
Inscape_ao 5:a37e3a15444b 69 return false;
Inscape_ao 5:a37e3a15444b 70 }
Inscape_ao 5:a37e3a15444b 71 fpCurrent = fp;
Inscape_ao 5:a37e3a15444b 72 sprintf(fnameCurrent, "%s", fname);
Inscape_ao 5:a37e3a15444b 73 return true;
Inscape_ao 5:a37e3a15444b 74 }
Inscape_ao 5:a37e3a15444b 75
Inscape_ao 5:a37e3a15444b 76 /* get FileName of open file */
Inscape_ao 5:a37e3a15444b 77 char *SDDataStore::getFileName(void)
Inscape_ao 5:a37e3a15444b 78 {
Inscape_ao 5:a37e3a15444b 79 return fnameCurrent;
Inscape_ao 5:a37e3a15444b 80 }
Inscape_ao 5:a37e3a15444b 81
Inscape_ao 5:a37e3a15444b 82 /* get current file pointer */
Inscape_ao 5:a37e3a15444b 83 FILE *SDDataStore::getFilePointer(void)
Inscape_ao 5:a37e3a15444b 84 {
Inscape_ao 5:a37e3a15444b 85 return fpCurrent;
Inscape_ao 5:a37e3a15444b 86 }
Inscape_ao 5:a37e3a15444b 87
Inscape_ao 5:a37e3a15444b 88 /* close file with this Class */
Inscape_ao 5:a37e3a15444b 89 void SDDataStore::syncFile(void)
Inscape_ao 5:a37e3a15444b 90 {
Inscape_ao 5:a37e3a15444b 91 if (fpCurrent != NULL) {
Inscape_ao 5:a37e3a15444b 92 fflush(fpCurrent);
Inscape_ao 5:a37e3a15444b 93 }
Inscape_ao 5:a37e3a15444b 94 }
Inscape_ao 5:a37e3a15444b 95
Inscape_ao 5:a37e3a15444b 96 /* close file with this Class */
Inscape_ao 5:a37e3a15444b 97 void SDDataStore::closeFile(void)
Inscape_ao 5:a37e3a15444b 98 {
Inscape_ao 5:a37e3a15444b 99 if (fpCurrent != NULL) {
Inscape_ao 5:a37e3a15444b 100 fclose(fpCurrent);
Inscape_ao 5:a37e3a15444b 101 fpCurrent = NULL;
Inscape_ao 5:a37e3a15444b 102 fnameCurrent[0] = '\0';
Inscape_ao 5:a37e3a15444b 103 }
Inscape_ao 5:a37e3a15444b 104 }
Inscape_ao 5:a37e3a15444b 105
Inscape_ao 5:a37e3a15444b 106 void SDDataStore::pathTermination(char *pathArray)
Inscape_ao 5:a37e3a15444b 107 {
Inscape_ao 5:a37e3a15444b 108 pathArray[MaxPathLength-1] = '\0';
Inscape_ao 5:a37e3a15444b 109 }