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-23
Revision:
2:a694440145e9
Child:
5:a37e3a15444b

File content as of revision 2:a694440145e9:

/** --- 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);

/* Event Hankder */
CmdParseRule rules[] = {
    /* サンプルコマンド */
    {"CMD", sampleHanlder, 0},
    /* 時刻取得 */
    {"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;
}

/* 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;
}