AGH UST MBED part 2
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
decoder.cpp@4:e48aee3e8d09, 2020-05-21 (annotated)
- Committer:
- matis755
- Date:
- Thu May 21 11:03:32 2020 +0000
- Revision:
- 4:e48aee3e8d09
- Child:
- 5:3c19c3ae6286
Ready without GUI;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matis755 | 4:e48aee3e8d09 | 1 | #include "decoder.h" |
matis755 | 4:e48aee3e8d09 | 2 | |
matis755 | 4:e48aee3e8d09 | 3 | |
matis755 | 4:e48aee3e8d09 | 4 | struct Keyword asKeywordList[MAX_KEYWORD_NR] = { |
matis755 | 4:e48aee3e8d09 | 5 | {ID, "id"}, |
matis755 | 4:e48aee3e8d09 | 6 | {CLB, "callib"}, |
matis755 | 4:e48aee3e8d09 | 7 | {GT, "goto"}, |
matis755 | 4:e48aee3e8d09 | 8 | {ST, "step"}, |
matis755 | 4:e48aee3e8d09 | 9 | }; |
matis755 | 4:e48aee3e8d09 | 10 | |
matis755 | 4:e48aee3e8d09 | 11 | enum Result { OK, NOK }; |
matis755 | 4:e48aee3e8d09 | 12 | |
matis755 | 4:e48aee3e8d09 | 13 | |
matis755 | 4:e48aee3e8d09 | 14 | void Decoder::ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){ |
matis755 | 4:e48aee3e8d09 | 15 | |
matis755 | 4:e48aee3e8d09 | 16 | unsigned char ucCharCounter; |
matis755 | 4:e48aee3e8d09 | 17 | |
matis755 | 4:e48aee3e8d09 | 18 | for(ucCharCounter=0;pcString[ucCharCounter]!='\0';ucCharCounter++){ |
matis755 | 4:e48aee3e8d09 | 19 | if(pcString[ucCharCounter] == cOldChar) pcString[ucCharCounter] = cNewChar; |
matis755 | 4:e48aee3e8d09 | 20 | } |
matis755 | 4:e48aee3e8d09 | 21 | } |
matis755 | 4:e48aee3e8d09 | 22 | |
matis755 | 4:e48aee3e8d09 | 23 | |
matis755 | 4:e48aee3e8d09 | 24 | |
matis755 | 4:e48aee3e8d09 | 25 | enum Result Decoder::eHexStringToUInt(char pcStr[],unsigned int *puiValue){ |
matis755 | 4:e48aee3e8d09 | 26 | |
matis755 | 4:e48aee3e8d09 | 27 | unsigned char ucCharCounter; |
matis755 | 4:e48aee3e8d09 | 28 | |
matis755 | 4:e48aee3e8d09 | 29 | if((pcStr[0] != '0') | (pcStr[1] != 'x') | (pcStr[2] == '\0')) |
matis755 | 4:e48aee3e8d09 | 30 | return NOK; |
matis755 | 4:e48aee3e8d09 | 31 | *puiValue = 0; |
matis755 | 4:e48aee3e8d09 | 32 | for(ucCharCounter=2;ucCharCounter<7;ucCharCounter++){ |
matis755 | 4:e48aee3e8d09 | 33 | if(pcStr[ucCharCounter] == '\0') |
matis755 | 4:e48aee3e8d09 | 34 | return OK; |
matis755 | 4:e48aee3e8d09 | 35 | *puiValue = *puiValue << 4; |
matis755 | 4:e48aee3e8d09 | 36 | if(pcStr[ucCharCounter] >= 'A') |
matis755 | 4:e48aee3e8d09 | 37 | *puiValue = *puiValue | (pcStr[ucCharCounter] - 'A' + 10); |
matis755 | 4:e48aee3e8d09 | 38 | else |
matis755 | 4:e48aee3e8d09 | 39 | *puiValue = *puiValue | (pcStr[ucCharCounter] - '0'); |
matis755 | 4:e48aee3e8d09 | 40 | } |
matis755 | 4:e48aee3e8d09 | 41 | return OK; |
matis755 | 4:e48aee3e8d09 | 42 | } |
matis755 | 4:e48aee3e8d09 | 43 | |
matis755 | 4:e48aee3e8d09 | 44 | |
matis755 | 4:e48aee3e8d09 | 45 | unsigned char Decoder::ucFindTokensInString(char *pcString){ |
matis755 | 4:e48aee3e8d09 | 46 | |
matis755 | 4:e48aee3e8d09 | 47 | unsigned char ucTokenPointer; |
matis755 | 4:e48aee3e8d09 | 48 | unsigned char ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 49 | char cCurrentChar; |
matis755 | 4:e48aee3e8d09 | 50 | enum State {TOKEN, DELIMITER}; |
matis755 | 4:e48aee3e8d09 | 51 | enum State eState = DELIMITER; |
matis755 | 4:e48aee3e8d09 | 52 | ucDelimiterCounter = 0; |
matis755 | 4:e48aee3e8d09 | 53 | |
matis755 | 4:e48aee3e8d09 | 54 | for(ucTokenPointer=0;;ucTokenPointer++){ |
matis755 | 4:e48aee3e8d09 | 55 | cCurrentChar = pcString[ucTokenPointer]; |
matis755 | 4:e48aee3e8d09 | 56 | switch(eState){ |
matis755 | 4:e48aee3e8d09 | 57 | case DELIMITER: |
matis755 | 4:e48aee3e8d09 | 58 | if(cCurrentChar == '\0') |
matis755 | 4:e48aee3e8d09 | 59 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 60 | else if(cCurrentChar == ' ') {} |
matis755 | 4:e48aee3e8d09 | 61 | else { |
matis755 | 4:e48aee3e8d09 | 62 | eState = TOKEN; |
matis755 | 4:e48aee3e8d09 | 63 | asToken[ucDelimiterCounter].uValue.pcString = pcString+ucTokenPointer; |
matis755 | 4:e48aee3e8d09 | 64 | ucDelimiterCounter++; |
matis755 | 4:e48aee3e8d09 | 65 | } |
matis755 | 4:e48aee3e8d09 | 66 | break; |
matis755 | 4:e48aee3e8d09 | 67 | case TOKEN: |
matis755 | 4:e48aee3e8d09 | 68 | if(cCurrentChar == '\0') |
matis755 | 4:e48aee3e8d09 | 69 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 70 | else if(ucDelimiterCounter == MAX_TOKEN_NR) |
matis755 | 4:e48aee3e8d09 | 71 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 72 | else if(cCurrentChar != ' ') {} |
matis755 | 4:e48aee3e8d09 | 73 | else |
matis755 | 4:e48aee3e8d09 | 74 | eState = DELIMITER; |
matis755 | 4:e48aee3e8d09 | 75 | break; |
matis755 | 4:e48aee3e8d09 | 76 | } |
matis755 | 4:e48aee3e8d09 | 77 | } |
matis755 | 4:e48aee3e8d09 | 78 | } |
matis755 | 4:e48aee3e8d09 | 79 | |
matis755 | 4:e48aee3e8d09 | 80 | enum Result Decoder::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){ |
matis755 | 4:e48aee3e8d09 | 81 | |
matis755 | 4:e48aee3e8d09 | 82 | unsigned char ucTokenCounter; |
matis755 | 4:e48aee3e8d09 | 83 | |
matis755 | 4:e48aee3e8d09 | 84 | for(ucTokenCounter=0;ucTokenCounter<MAX_KEYWORD_NR;ucTokenCounter++){ |
matis755 | 4:e48aee3e8d09 | 85 | if (!strcmp(pcStr, asKeywordList[ucTokenCounter].cString)) { |
matis755 | 4:e48aee3e8d09 | 86 | *peKeywordCode = asKeywordList[ucTokenCounter].eCode; |
matis755 | 4:e48aee3e8d09 | 87 | return OK; |
matis755 | 4:e48aee3e8d09 | 88 | } |
matis755 | 4:e48aee3e8d09 | 89 | } |
matis755 | 4:e48aee3e8d09 | 90 | return NOK; |
matis755 | 4:e48aee3e8d09 | 91 | } |
matis755 | 4:e48aee3e8d09 | 92 | |
matis755 | 4:e48aee3e8d09 | 93 | void Decoder::DecodeTokens(unsigned char ucTokenCnt){ |
matis755 | 4:e48aee3e8d09 | 94 | |
matis755 | 4:e48aee3e8d09 | 95 | unsigned char ucTokenCounter; |
matis755 | 4:e48aee3e8d09 | 96 | Token* tValue; |
matis755 | 4:e48aee3e8d09 | 97 | |
matis755 | 4:e48aee3e8d09 | 98 | for(ucTokenCounter=0;ucTokenCounter<ucTokenCnt;ucTokenCounter++){ |
matis755 | 4:e48aee3e8d09 | 99 | tValue = &asToken[ucTokenCounter]; |
matis755 | 4:e48aee3e8d09 | 100 | if (eStringToKeyword(tValue->uValue.pcString,&tValue->uValue.eKeyword) == OK) tValue->eType = KEYWORD; |
matis755 | 4:e48aee3e8d09 | 101 | else if (eHexStringToUInt(tValue->uValue.pcString,&tValue->uValue.uiNumber) == OK) tValue->eType = NUMBER; |
matis755 | 4:e48aee3e8d09 | 102 | else tValue->eType = STRING; |
matis755 | 4:e48aee3e8d09 | 103 | } |
matis755 | 4:e48aee3e8d09 | 104 | } |
matis755 | 4:e48aee3e8d09 | 105 | |
matis755 | 4:e48aee3e8d09 | 106 | void Decoder::DecodeMsg(char *pcString){ |
matis755 | 4:e48aee3e8d09 | 107 | |
matis755 | 4:e48aee3e8d09 | 108 | ucTokenCnt = ucFindTokensInString(pcString); |
matis755 | 4:e48aee3e8d09 | 109 | ReplaceCharactersInString(pcString,' ','\0'); |
matis755 | 4:e48aee3e8d09 | 110 | DecodeTokens(ucTokenCnt); |
matis755 | 4:e48aee3e8d09 | 111 | } |
matis755 | 4:e48aee3e8d09 | 112 | |
matis755 | 4:e48aee3e8d09 | 113 | enum TokenType Decoder::eGetTokenType(unsigned char ucIdx) { |
matis755 | 4:e48aee3e8d09 | 114 | return asToken[ucIdx].eType; |
matis755 | 4:e48aee3e8d09 | 115 | } |
matis755 | 4:e48aee3e8d09 | 116 | |
matis755 | 4:e48aee3e8d09 | 117 | unsigned char Decoder::GetTokenCnt(void) { |
matis755 | 4:e48aee3e8d09 | 118 | return ucTokenCnt; |
matis755 | 4:e48aee3e8d09 | 119 | } |
matis755 | 4:e48aee3e8d09 | 120 | |
matis755 | 4:e48aee3e8d09 | 121 | enum KeywordCode Decoder::eGetKeyword(unsigned char ucIdx) { |
matis755 | 4:e48aee3e8d09 | 122 | return asToken[ucIdx].uValue.eKeyword; |
matis755 | 4:e48aee3e8d09 | 123 | } |