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

Dependencies:   mbed SDFileSystem

event.cpp

Committer:
Inscape_ao
Date:
2019-04-24
Revision:
5:a37e3a15444b
Parent:
2:a694440145e9
Child:
7:9ab8809f9693

File content as of revision 5:a37e3a15444b:

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

static int sampleHanlder(CommandParser *pC, char *arg, int exarg);
static int setTimeHanlder(CommandParser *pC, char *arg, int exarg);
static int getTimeHanlder(CommandParser *pC, char *arg, int exarg);
static int startSDStore(CommandParser *pC, char *arg, int exarg);
static int endSDStore(CommandParser *pC, char *arg, int exarg);
static int writeSDStore(CommandParser *pC, char *arg, int exarg);

/* Event Hankder */
CmdParseRule rules[] = {
    /* サンプルコマンド */
    {"CMD", sampleHanlder, 0},
    /* SDカード記録 */
    {"SDS", startSDStore, 0},
    {"SDE", endSDStore, 0},
    {"SDW", writeSDStore, 0}, /* for TEST */
    /* 時刻取得 */
    {"GTS", getTimeHanlder, 0},
    /* 時刻設定 */
    {"TYR", setTimeHanlder, TimeManager::SetTimeMethod::Year},
    {"TMO", setTimeHanlder, TimeManager::SetTimeMethod::Month},
    {"TDA", setTimeHanlder, TimeManager::SetTimeMethod::Day},
    {"THR", setTimeHanlder, TimeManager::SetTimeMethod::Hour},
    {"TMI", setTimeHanlder, TimeManager::SetTimeMethod::Min},
    {"TSE", setTimeHanlder, TimeManager::SetTimeMethod::Sec},
    };

int getNumOfRules = sizeof(rules)/sizeof(CmdParseRule);


/****************************************************/
/* Event Handlers */
/****************************************************/
/* Sample Command */
static int sampleHanlder(CommandParser *pC, char *arg, int exarg)
{   
    pC->reply();
    return 0;
}

/****************************************************/
/* Event Handlers (SD Control) */
/****************************************************/
static int startSDStore(CommandParser *pC, char *arg, int exarg)
{
    if (pSds->startFileWithTimeStamp("","txt") != true) {
        pC->reply(false, 1);
        return 1;
    }
    pC->reply();
    return 0;
}

static int endSDStore(CommandParser *pC, char *arg, int exarg)
{
    FILE *fp;
    if ((fp = pSds->getFilePointer()) == NULL) {
        /* NOP */
        pC->reply(false, 2);
        return 2;
    }
    fprintf(fp, "call endSDStore(%s)\n", pSds->getFileName());    
    pSds->syncFile();
    pSds->closeFile();
    pC->reply();
    return 0;
}

static int writeSDStore(CommandParser *pC, char *arg, int exarg)
{
    FILE *fp;
    char curr[TimeManager::TimeStampLength + 1] = {0};
    
    if ((fp = pSds->getFilePointer()) == NULL) {
        /* NOP */
        pC->reply(false, 2);
        return 2;
    }
    pTM->getTimeStamp(curr);
    fprintf(fp, "call writeSDStore at %s\n", curr);    
    pSds->syncFile();
    pC->reply();
    return 0;
}

/****************************************************/
/* Event Handlers (Time Control) */
/****************************************************/
/* Time Set Command */
static int setTimeHanlder(CommandParser *pC, char *arg, int exarg)
{   
    bool success = false;
    int setvalue = atoi(arg);
    
    switch (exarg) {
    case TimeManager::SetTimeMethod::Year:
    case TimeManager::SetTimeMethod::Month:
    case TimeManager::SetTimeMethod::Day:
    case TimeManager::SetTimeMethod::Hour:
    case TimeManager::SetTimeMethod::Min:
    case TimeManager::SetTimeMethod::Sec:
        success = pTM->setCurrentTime(exarg,setvalue);
        break;
    }
    pC->reply(success, (success)? 0 : setvalue );
    return 0;
}

/* Time Get Command */
static int getTimeHanlder(CommandParser *pC, char *arg, int exarg)
{
    int len;
    char timestamp[TimeManager::TimeStampLength + 1] = {0};
    Serial *pUart = pC->getCurrentUart();
    len = pTM->getTimeStamp(timestamp);
    pUart->printf(":%d RTS %04d %s\n", pC->getDeviceID(), len, timestamp);
    return 0;
}