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

Dependencies:   mbed SDFileSystem

main.cpp

Committer:
Inscape_ao
Date:
2019-04-24
Revision:
4:bec3f80dc49c
Parent:
2:a694440145e9
Child:
5:a37e3a15444b

File content as of revision 4:bec3f80dc49c:

/** --- Includes --- */
#include "mbed.h"
#include "SDFileSystem.h"
#include "TimeManager.h"
#include "UartReceiver.h"
#include "CommandParser.h"
#include "global.h"
#include "string.h"

/** --- Global Variables --- */
Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
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()
{
    /** UART Initalizer */
    /* setup UART 115200, 8bit, Parity=None, stopbit:1bit  */
    /* https://os.mbed.com/users/okini3939/notebook/Serial_jp/ */
    pc.baud(115200);
    pc.format(8, Serial::None, 1);
    
    /* new timer manager */
    pTM = new TimeManager();
    /* Receive Buffer Control */
    pUR = new UartReceiver(&pc);
    /* Generate Command parser as DeviceID = 0, ParsingRule = rules */
    pCP = new CommandParser(pUR, 0, rules, getNumOfRules);
    pCP->run();
    
    pc.printf("Hello World !\n");
    sdcard_main();
    
    while(1) {
        wait(1);
    }
}