Paweł Woźniak / Mbed 2 deprecated mbedcz2_5

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers command_decoder.cpp Source File

command_decoder.cpp

00001 #include "command_decoder.h"
00002 #include <string.h>
00003 #include <algorithm>
00004 #include <stdio.h>
00005 
00006 #define NULL 0
00007 
00008 struct Keyword asKeywordList[MAX_KEYWORD_NR]=
00009 {
00010     {ID, "id"},
00011     {CLB, "callib"},
00012     {GOTO, "goto"},
00013     {STEP, "step"}
00014 };
00015 
00016 enum Result{OK, ERROR};
00017 
00018 unsigned char Decoder::ucFindTokensInString(char *pcString)
00019 {
00020     unsigned char ucTokenPointer;
00021     unsigned char ucTokenCounter = 0;
00022     char cCurrentChar;
00023     enum State {TOKEN, DELIMITER};
00024     enum State eState = DELIMITER;
00025 
00026     for(ucTokenPointer=0 ;; ucTokenPointer++) {
00027         cCurrentChar = pcString[ucTokenPointer];
00028         switch(eState) {
00029             case DELIMITER:
00030                 if(cCurrentChar == NULL) {
00031                     return ucTokenCounter;
00032                 } else if(cCurrentChar != ' ') {
00033                     eState = TOKEN;
00034                     asToken[ucTokenCounter].uValue.pcString = pcString + ucTokenPointer;
00035                     ucTokenCounter++;
00036                 }
00037                 break;
00038             case TOKEN:
00039                 if(cCurrentChar == NULL) {
00040                     return ucTokenCounter;
00041                 } else if(ucTokenCounter == MAX_TOKEN_NR) {
00042                     return ucTokenCounter;
00043                 } else if(cCurrentChar == ' ') {
00044                     eState = DELIMITER;
00045                 }
00046                 break;
00047         }
00048     }
00049 }
00050 
00051 enum Result Decoder::eStringToKeyword(char pcStr[],enum KeywordCode *peKeywordCode)
00052 {
00053     unsigned char ucKeywordIndex;
00054 
00055     for(ucKeywordIndex=0 ; ucKeywordIndex < MAX_KEYWORD_NR ; ucKeywordIndex++) {
00056         if(strcmp(pcStr, asKeywordList[ucKeywordIndex].cString) == 0) {
00057             *peKeywordCode = asKeywordList[ucKeywordIndex].eCode;
00058             return OK;
00059         }
00060     }
00061     return ERROR;
00062 }
00063 
00064 void Decoder::DecodeTokens(void)
00065 {
00066     unsigned char ucCurrentToken;
00067     unsigned int uiHexValue;
00068     enum KeywordCode Keyword;
00069     struct Token* pasValue;
00070 
00071     for(ucCurrentToken=0 ; ucCurrentToken < ucTokenNr ; ucCurrentToken++) {
00072         pasValue = &asToken[ucCurrentToken];
00073         if(eStringToKeyword(pasValue->uValue.pcString, &Keyword) == OK) {
00074             pasValue->eType = KEYWORD;
00075             pasValue->uValue.eKeyword = Keyword;
00076         } else if(sscanf(pasValue->uValue.pcString, "%4x", &uiHexValue) == 1) {
00077             pasValue->eType = NUMBER;
00078             pasValue->uValue.uiNumber = uiHexValue;
00079         } else {
00080             pasValue->eType = STRING;
00081         }
00082     }
00083 }
00084 
00085 void Decoder::DecodeMsg(char *pcString)
00086 {
00087     ucTokenNr=ucFindTokensInString(pcString);
00088     std::replace(pcString, pcString + strlen(pcString), ' ', '\0');
00089     DecodeTokens();
00090 }