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

Dependencies:   mbed SDFileSystem

common/SDDataStore.cpp

Committer:
Inscape_ao
Date:
2019-05-18
Revision:
10:db2be22bc2f9
Parent:
7:9ab8809f9693
Child:
16:602bc04e3cb5

File content as of revision 10:db2be22bc2f9:

#include "SDDataStore.h"

/* constructor, pSD, TimeManager */
SDDataStore::SDDataStore(TimeManager *pSetTM)
{
    //pS = new SDFileSystem(PC_12, PC_11, PC_10, D4, "sd"); // MOSI, MISO, SCK, CS;
    pS = new SDFileSystem(PC_12, PC_11, PC_10, PD_2, "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';
}