1st release (without comment)

RMS_UART.cpp

Committer:
nsrwsurasak
Date:
2016-09-15
Revision:
0:bc10ea82aec3
Child:
1:5e6c5fbd48d6

File content as of revision 0:bc10ea82aec3:

#include "mbed.h"
#include "RMS_UART.h"

#ifdef EXT_SERIAL
    Serial serial_device(SERIAL_TX, SERIAL_RX);
#else
    Serial serial_device(SERIAL_TX, SERIAL_RX);
#endif

char Msg_RxBuf[MSG_BUF_SIZE + 1];            // Reading Cmd Buffer 
int  Msg_index;                              // An Cmd index

char Content_RxBuf[CONTENT_BUF_SIZE + 1];    // Reading Cmd Buffer 
int  Content_index;                          // An Cmd index

char FileName[MSG2FILENAME];

bool MsgContentManagement = false;

/**
 * @brief   
 * @note 
 * @retval 
 */
void Init_SerialDevice()
{
    serial_device.baud(BPS_9600);
    serial_device.attach(&RxMsgInterruptCallback);
}

/**
 * @brief   
 * @note 
 * @retval 
 */
void RxMsgInterruptCallback()
{
    /* Start Rx interrupt  */
    if(MsgContentManagement == false)
    {
        DoTheMsgCmd();
    }
    else
    {
        DoTheContent();
    }
}

/**
 * @brief 
 * @note 
 * @retval
 */
void DoTheContent()
{   
    // Note: you need to actually read from the serial to clear the RX interrupt
    Content_RxBuf[Content_index] = serial_device.getc();
    Content_index++;
    
    if(strstr(Content_RxBuf,"*end*"))
    {
        SendContentToFile();
        ClearContentIndexAndBuf();
        MsgContentManagement = false;
    }
    
    if(Content_index == CONTENT_BUF_SIZE)
    {
        SendContentToFile();
        ClearContentIndexAndBuf();
    }
}

 /**
 * @brief 
 * @note 
 * @retval
 */
void DoTheMsgCmd()
{
        
    // Note: you need to actually read from the serial to clear the RX interrupt
    Msg_RxBuf[Msg_index] = serial_device.getc();
    Msg_index++;
    
    if(strstr(Msg_RxBuf,"ls"))
    {
        GetListFileCmd();
    }
    
    if(strstr(Msg_RxBuf,".csv"))
    {
        CheckReadEditCmd();
    }
        
    if(strstr(Msg_RxBuf,".xml"))
    {
        CheckReadEditCmd();
    }
    
    if(strstr(Msg_RxBuf,".log"))
    {
        CheckReadEditCmd();
    }
    
    if(Msg_index >= 2)
    {
        if( !((strstr(Msg_RxBuf,"ls")) || (strstr(Msg_RxBuf,"rd")) || (strstr(Msg_RxBuf,"ed"))))
        {
            GetCmdError();
        }
    }
    
    if(Msg_index == MSG_BUF_SIZE)
    {
       ClearCmdIndexAndBuf();
    }
}

/**
 * @brief 
 * @note 
 * @retval
 */
void GetListFileCmd()
{   
    ClearCmdIndexAndBuf();
}

/**
 * @brief 
 * @note 
 * @retval
 */
void CheckReadEditCmd()
{
    if(strstr(Msg_RxBuf,"rd"))
    {   
        GetFileName();
    }
    else if(strstr(Msg_RxBuf,"ed"))
    {
        GetFileName();
        MsgContentManagement = true;
    }
    else
    {
        GetCmdError();
    }
}

/**
 * @brief 
 * @note 
 * @retval
 */
void GetCmdError()
{   
    serial_device.printf("$?? -- Command Error\n",Msg_RxBuf + 1);
    ClearCmdIndexAndBuf();
}

/**
 * @brief 
 * @note 
 * @retval
 */
void GetFileName()
{   
    memcpy(FileName,&Msg_RxBuf[3],Msg_index - 3);
    ClearCmdIndexAndBuf();
}

/**
 * @brief 
 * @note 
 * @retval
 */
void SendContentToFile()
{   
    MsgContentManagement = true;
    serial_device.printf("Content -- %s \n",Content_RxBuf);
    ClearContentIndexAndBuf();
}

/**
 * @brief 
 * @note 
 * @retval
 */
void ClearCmdIndexAndBuf()
{   
    Msg_index = 0;
    memset(Msg_RxBuf,' ',MSG_BUF_SIZE);
}

/**
 * @brief 
 * @note 
 * @retval
 */
void ClearContentIndexAndBuf()
{   
    Content_index = 0;
    memset(Content_RxBuf,' ',CONTENT_BUF_SIZE);
}