
last version
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
command_decoder.cpp@2:e23243b26a23, 2020-06-09 (annotated)
- Committer:
- bolko
- Date:
- Tue Jun 09 11:23:48 2020 +0000
- Revision:
- 2:e23243b26a23
look;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bolko | 2:e23243b26a23 | 1 | #include "command_decoder.h" |
bolko | 2:e23243b26a23 | 2 | #include <string.h> |
bolko | 2:e23243b26a23 | 3 | #include <iostream> |
bolko | 2:e23243b26a23 | 4 | #include "mbed.h" |
bolko | 2:e23243b26a23 | 5 | |
bolko | 2:e23243b26a23 | 6 | struct Keyword asKeywordList[MAX_KEYWORD_NR]= |
bolko | 2:e23243b26a23 | 7 | { |
bolko | 2:e23243b26a23 | 8 | {ID, "id"}, |
bolko | 2:e23243b26a23 | 9 | {CALLIB, "callib"}, |
bolko | 2:e23243b26a23 | 10 | {GOTO, "goto"}, |
bolko | 2:e23243b26a23 | 11 | {STEP, "step"} |
bolko | 2:e23243b26a23 | 12 | }; |
bolko | 2:e23243b26a23 | 13 | struct Token asToken[MAX_TOKEN_NR]; |
bolko | 2:e23243b26a23 | 14 | enum State {TOKEN,DELIMITER}; |
bolko | 2:e23243b26a23 | 15 | unsigned char ucTokenNr = 0; |
bolko | 2:e23243b26a23 | 16 | |
bolko | 2:e23243b26a23 | 17 | unsigned char CommandDecoder::ucFindTokensInString(char *pcString){ |
bolko | 2:e23243b26a23 | 18 | char * pCurrentToken; |
bolko | 2:e23243b26a23 | 19 | unsigned char ucTokenCounter = 0; |
bolko | 2:e23243b26a23 | 20 | |
bolko | 2:e23243b26a23 | 21 | pCurrentToken = strtok(pcString, " "); |
bolko | 2:e23243b26a23 | 22 | while(NULL != pCurrentToken && ucTokenCounter < MAX_TOKEN_NR){ |
bolko | 2:e23243b26a23 | 23 | asToken[ucTokenCounter].uValue.pcString = pCurrentToken; |
bolko | 2:e23243b26a23 | 24 | ucTokenCounter++; |
bolko | 2:e23243b26a23 | 25 | pCurrentToken = strtok(NULL, " "); |
bolko | 2:e23243b26a23 | 26 | } |
bolko | 2:e23243b26a23 | 27 | return ucTokenCounter; |
bolko | 2:e23243b26a23 | 28 | } |
bolko | 2:e23243b26a23 | 29 | |
bolko | 2:e23243b26a23 | 30 | |
bolko | 2:e23243b26a23 | 31 | enum Result CommandDecoder::eStringToKeyword(char pcStr[],enum KeywordCode *peKeywordCode){ |
bolko | 2:e23243b26a23 | 32 | unsigned char ucKeywordCounter; |
bolko | 2:e23243b26a23 | 33 | for(ucKeywordCounter=0;ucKeywordCounter<MAX_KEYWORD_NR;ucKeywordCounter++) |
bolko | 2:e23243b26a23 | 34 | { |
bolko | 2:e23243b26a23 | 35 | if (strcmp(pcStr,asKeywordList[ucKeywordCounter].cString) == 0) |
bolko | 2:e23243b26a23 | 36 | { |
bolko | 2:e23243b26a23 | 37 | *peKeywordCode = asKeywordList[ucKeywordCounter].eCode; |
bolko | 2:e23243b26a23 | 38 | return OK; |
bolko | 2:e23243b26a23 | 39 | } |
bolko | 2:e23243b26a23 | 40 | } |
bolko | 2:e23243b26a23 | 41 | return FAIL; |
bolko | 2:e23243b26a23 | 42 | }; |
bolko | 2:e23243b26a23 | 43 | |
bolko | 2:e23243b26a23 | 44 | |
bolko | 2:e23243b26a23 | 45 | enum Result CommandDecoder::DecodeTokens(){ |
bolko | 2:e23243b26a23 | 46 | |
bolko | 2:e23243b26a23 | 47 | unsigned int uiNumber; |
bolko | 2:e23243b26a23 | 48 | unsigned char ucTokenIndex; |
bolko | 2:e23243b26a23 | 49 | enum KeywordCode eKeyword; |
bolko | 2:e23243b26a23 | 50 | |
bolko | 2:e23243b26a23 | 51 | for(ucTokenIndex=0; ucTokenIndex < ucTokenNr; ucTokenIndex++){ |
bolko | 2:e23243b26a23 | 52 | if((eStringToKeyword (asToken[ucTokenIndex].uValue.pcString, &eKeyword))== OK){ |
bolko | 2:e23243b26a23 | 53 | asToken[ucTokenIndex].eType = KEYWORD; |
bolko | 2:e23243b26a23 | 54 | asToken[ucTokenIndex].uValue.eKeyword=eKeyword; |
bolko | 2:e23243b26a23 | 55 | } |
bolko | 2:e23243b26a23 | 56 | else if(sscanf(asToken[ucTokenIndex].uValue.pcString,"%x",&uiNumber) == 1){ |
bolko | 2:e23243b26a23 | 57 | asToken[ucTokenIndex].eType = NUMBER; |
bolko | 2:e23243b26a23 | 58 | asToken[ucTokenIndex].uValue.uiNumber = uiNumber; |
bolko | 2:e23243b26a23 | 59 | } |
bolko | 2:e23243b26a23 | 60 | else{ |
bolko | 2:e23243b26a23 | 61 | asToken[ucTokenIndex].eType = STRING; |
bolko | 2:e23243b26a23 | 62 | } |
bolko | 2:e23243b26a23 | 63 | } |
bolko | 2:e23243b26a23 | 64 | return OK; |
bolko | 2:e23243b26a23 | 65 | } |
bolko | 2:e23243b26a23 | 66 | |
bolko | 2:e23243b26a23 | 67 | |
bolko | 2:e23243b26a23 | 68 | void CommandDecoder::ReplaceCharactersInString(char pcString[], char cOldChar,char cNewChar){ |
bolko | 2:e23243b26a23 | 69 | char *pString = pcString; |
bolko | 2:e23243b26a23 | 70 | while((pString = strchr(pString,cOldChar)) != NULL){ |
bolko | 2:e23243b26a23 | 71 | *pString++ = cNewChar; |
bolko | 2:e23243b26a23 | 72 | } |
bolko | 2:e23243b26a23 | 73 | } |
bolko | 2:e23243b26a23 | 74 | |
bolko | 2:e23243b26a23 | 75 | |
bolko | 2:e23243b26a23 | 76 | void CommandDecoder::DecodeMsg(char *pcString){ |
bolko | 2:e23243b26a23 | 77 | ucTokenNr = ucFindTokensInString(pcString); |
bolko | 2:e23243b26a23 | 78 | ReplaceCharactersInString(pcString,' ', NULL); |
bolko | 2:e23243b26a23 | 79 | DecodeTokens(); |
bolko | 2:e23243b26a23 | 80 | } |