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:
Fri Jul 05 01:26:19 2019 +0000
Revision:
19:36072b9b79f3
Parent:
16:602bc04e3cb5
add LICENSE.txt

Who changed what in which revision?

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