a

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

decoder.cpp

Committer:
matis755
Date:
2020-05-22
Revision:
5:3c19c3ae6286
Parent:
4:e48aee3e8d09
Child:
6:06ff453bb16e

File content as of revision 5:3c19c3ae6286:

#include "decoder.h"


struct Keyword asKeywordList[MAX_KEYWORD_NR] = {
    {ID, "id"},
    {CLB, "callib"},
    {GT, "goto"},
    {ST, "step"},
};

enum Result { OK, NOK };


void Decoder::ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){
  
    unsigned char ucCharCounter;
  
    for(ucCharCounter=0;pcString[ucCharCounter]!='\0';ucCharCounter++){
        if(pcString[ucCharCounter] == cOldChar) pcString[ucCharCounter] = cNewChar;
    }
}



enum Result Decoder::eHexStringToUInt(char pcStr[],unsigned int *puiValue){
  
    unsigned char ucCharCounter;
  
    if((pcStr[0] != '0') | (pcStr[1] != 'x') | (pcStr[2] == '\0'))
        return NOK;
    *puiValue = 0;
    for(ucCharCounter=2;ucCharCounter<7;ucCharCounter++){
        if(pcStr[ucCharCounter] == '\0') 
            return OK;
      *puiValue = *puiValue << 4;
        if(pcStr[ucCharCounter] >= 'A') 
            *puiValue = *puiValue | (pcStr[ucCharCounter] - 'A' + 10); 
        else 
            *puiValue = *puiValue | (pcStr[ucCharCounter] - '0');
    }
    return OK;
}

    
unsigned char Decoder::ucFindTokensInString(char *pcString){
  
    unsigned char ucTokenPointer;
    unsigned char ucDelimiterCounter;
    char cCurrentChar;
    enum State {TOKEN, DELIMITER};
    enum State eState = DELIMITER;
    ucDelimiterCounter = 0;
    
    for(ucTokenPointer=0;;ucTokenPointer++){
        cCurrentChar = pcString[ucTokenPointer];
        switch(eState){
            case DELIMITER:
                if(cCurrentChar == '\0') 
                    return ucDelimiterCounter;
                else if(cCurrentChar == ' ') {}
                else {
                    eState = TOKEN;
                    asToken[ucDelimiterCounter].uValue.pcString = pcString+ucTokenPointer;
                    ucDelimiterCounter++;
                }
                break;
            case TOKEN:
                if(cCurrentChar == '\0') 
                    return ucDelimiterCounter;
                else if(ucDelimiterCounter == MAX_TOKEN_NR) 
                    return ucDelimiterCounter;
                else if(cCurrentChar != ' ') {}
                else 
                    eState = DELIMITER;
                break;
        }
    }
}

enum Result Decoder::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){
  
    unsigned char ucTokenCounter;
  
    for(ucTokenCounter=0;ucTokenCounter<MAX_KEYWORD_NR;ucTokenCounter++){
        if (!strcmp(pcStr, asKeywordList[ucTokenCounter].cString)) {
            *peKeywordCode = asKeywordList[ucTokenCounter].eCode;
            return OK;
        }
    }
    return NOK;
}

void Decoder::DecodeTokens(unsigned char ucTokenCnt){
  
    unsigned char ucTokenCounter;
    Token* tValue;
  
    for(ucTokenCounter=0;ucTokenCounter<ucTokenCnt;ucTokenCounter++){
        tValue = &asToken[ucTokenCounter];
        if (eStringToKeyword(tValue->uValue.pcString,&tValue->uValue.eKeyword) == OK) tValue->eType = KEYWORD;
        else if (eHexStringToUInt(tValue->uValue.pcString,&tValue->uValue.uiNumber) == OK) tValue->eType = NUMBER;
        else tValue->eType = STRING;
    }
}

void Decoder::DecodeMsg(char *pcString){
  
    ucTokenCnt = ucFindTokensInString(pcString);
    ReplaceCharactersInString(pcString,' ','\0');
    DecodeTokens(ucTokenCnt);
}

enum TokenType Decoder::eGetTokenType(unsigned char ucIdx) {
    return asToken[ucIdx].eType;    
}

unsigned char Decoder::GetTokenCnt(void) {
    return ucTokenCnt;
}

enum KeywordCode Decoder::eGetKeyword(unsigned char ucIdx) {
    return asToken[ucIdx].uValue.eKeyword;    
}

unsigned int Decoder::uiGetNumber(unsigned char ucIdx) {
    return asToken[ucIdx].uValue.uiNumber;    
}