AGH UST MBED part 2
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
decoder.cpp@6:06ff453bb16e, 2020-05-22 (annotated)
- Committer:
- matis755
- Date:
- Fri May 22 07:30:59 2020 +0000
- Revision:
- 6:06ff453bb16e
- Parent:
- 5:3c19c3ae6286
- Child:
- 7:34a9bd9d3093
Use standard C functions
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 | 6:06ff453bb16e | 22 | |
matis755 | 4:e48aee3e8d09 | 23 | unsigned char Decoder::ucFindTokensInString(char *pcString){ |
matis755 | 4:e48aee3e8d09 | 24 | |
matis755 | 4:e48aee3e8d09 | 25 | unsigned char ucTokenPointer; |
matis755 | 4:e48aee3e8d09 | 26 | unsigned char ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 27 | char cCurrentChar; |
matis755 | 4:e48aee3e8d09 | 28 | enum State {TOKEN, DELIMITER}; |
matis755 | 4:e48aee3e8d09 | 29 | enum State eState = DELIMITER; |
matis755 | 4:e48aee3e8d09 | 30 | ucDelimiterCounter = 0; |
matis755 | 4:e48aee3e8d09 | 31 | |
matis755 | 4:e48aee3e8d09 | 32 | for(ucTokenPointer=0;;ucTokenPointer++){ |
matis755 | 4:e48aee3e8d09 | 33 | cCurrentChar = pcString[ucTokenPointer]; |
matis755 | 4:e48aee3e8d09 | 34 | switch(eState){ |
matis755 | 4:e48aee3e8d09 | 35 | case DELIMITER: |
matis755 | 4:e48aee3e8d09 | 36 | if(cCurrentChar == '\0') |
matis755 | 4:e48aee3e8d09 | 37 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 38 | else if(cCurrentChar == ' ') {} |
matis755 | 4:e48aee3e8d09 | 39 | else { |
matis755 | 4:e48aee3e8d09 | 40 | eState = TOKEN; |
matis755 | 4:e48aee3e8d09 | 41 | asToken[ucDelimiterCounter].uValue.pcString = pcString+ucTokenPointer; |
matis755 | 4:e48aee3e8d09 | 42 | ucDelimiterCounter++; |
matis755 | 4:e48aee3e8d09 | 43 | } |
matis755 | 4:e48aee3e8d09 | 44 | break; |
matis755 | 4:e48aee3e8d09 | 45 | case TOKEN: |
matis755 | 4:e48aee3e8d09 | 46 | if(cCurrentChar == '\0') |
matis755 | 4:e48aee3e8d09 | 47 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 48 | else if(ucDelimiterCounter == MAX_TOKEN_NR) |
matis755 | 4:e48aee3e8d09 | 49 | return ucDelimiterCounter; |
matis755 | 4:e48aee3e8d09 | 50 | else if(cCurrentChar != ' ') {} |
matis755 | 4:e48aee3e8d09 | 51 | else |
matis755 | 4:e48aee3e8d09 | 52 | eState = DELIMITER; |
matis755 | 4:e48aee3e8d09 | 53 | break; |
matis755 | 4:e48aee3e8d09 | 54 | } |
matis755 | 4:e48aee3e8d09 | 55 | } |
matis755 | 4:e48aee3e8d09 | 56 | } |
matis755 | 4:e48aee3e8d09 | 57 | |
matis755 | 4:e48aee3e8d09 | 58 | enum Result Decoder::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){ |
matis755 | 4:e48aee3e8d09 | 59 | |
matis755 | 4:e48aee3e8d09 | 60 | unsigned char ucTokenCounter; |
matis755 | 4:e48aee3e8d09 | 61 | |
matis755 | 4:e48aee3e8d09 | 62 | for(ucTokenCounter=0;ucTokenCounter<MAX_KEYWORD_NR;ucTokenCounter++){ |
matis755 | 4:e48aee3e8d09 | 63 | if (!strcmp(pcStr, asKeywordList[ucTokenCounter].cString)) { |
matis755 | 4:e48aee3e8d09 | 64 | *peKeywordCode = asKeywordList[ucTokenCounter].eCode; |
matis755 | 4:e48aee3e8d09 | 65 | return OK; |
matis755 | 4:e48aee3e8d09 | 66 | } |
matis755 | 4:e48aee3e8d09 | 67 | } |
matis755 | 4:e48aee3e8d09 | 68 | return NOK; |
matis755 | 4:e48aee3e8d09 | 69 | } |
matis755 | 4:e48aee3e8d09 | 70 | |
matis755 | 4:e48aee3e8d09 | 71 | void Decoder::DecodeTokens(unsigned char ucTokenCnt){ |
matis755 | 4:e48aee3e8d09 | 72 | |
matis755 | 4:e48aee3e8d09 | 73 | unsigned char ucTokenCounter; |
matis755 | 4:e48aee3e8d09 | 74 | Token* tValue; |
matis755 | 4:e48aee3e8d09 | 75 | |
matis755 | 4:e48aee3e8d09 | 76 | for(ucTokenCounter=0;ucTokenCounter<ucTokenCnt;ucTokenCounter++){ |
matis755 | 4:e48aee3e8d09 | 77 | tValue = &asToken[ucTokenCounter]; |
matis755 | 4:e48aee3e8d09 | 78 | if (eStringToKeyword(tValue->uValue.pcString,&tValue->uValue.eKeyword) == OK) tValue->eType = KEYWORD; |
matis755 | 6:06ff453bb16e | 79 | else if ((int)strtol(tValue->uValue.pcString, NULL, 0)) { |
matis755 | 6:06ff453bb16e | 80 | tValue->uValue.uiNumber = (int)strtol(tValue->uValue.pcString, NULL, 0); |
matis755 | 6:06ff453bb16e | 81 | tValue->eType = NUMBER; |
matis755 | 6:06ff453bb16e | 82 | } |
matis755 | 4:e48aee3e8d09 | 83 | else tValue->eType = STRING; |
matis755 | 4:e48aee3e8d09 | 84 | } |
matis755 | 4:e48aee3e8d09 | 85 | } |
matis755 | 4:e48aee3e8d09 | 86 | |
matis755 | 4:e48aee3e8d09 | 87 | void Decoder::DecodeMsg(char *pcString){ |
matis755 | 4:e48aee3e8d09 | 88 | |
matis755 | 4:e48aee3e8d09 | 89 | ucTokenCnt = ucFindTokensInString(pcString); |
matis755 | 4:e48aee3e8d09 | 90 | ReplaceCharactersInString(pcString,' ','\0'); |
matis755 | 4:e48aee3e8d09 | 91 | DecodeTokens(ucTokenCnt); |
matis755 | 4:e48aee3e8d09 | 92 | } |
matis755 | 4:e48aee3e8d09 | 93 | |
matis755 | 4:e48aee3e8d09 | 94 | enum TokenType Decoder::eGetTokenType(unsigned char ucIdx) { |
matis755 | 4:e48aee3e8d09 | 95 | return asToken[ucIdx].eType; |
matis755 | 4:e48aee3e8d09 | 96 | } |
matis755 | 4:e48aee3e8d09 | 97 | |
matis755 | 4:e48aee3e8d09 | 98 | unsigned char Decoder::GetTokenCnt(void) { |
matis755 | 4:e48aee3e8d09 | 99 | return ucTokenCnt; |
matis755 | 4:e48aee3e8d09 | 100 | } |
matis755 | 4:e48aee3e8d09 | 101 | |
matis755 | 4:e48aee3e8d09 | 102 | enum KeywordCode Decoder::eGetKeyword(unsigned char ucIdx) { |
matis755 | 4:e48aee3e8d09 | 103 | return asToken[ucIdx].uValue.eKeyword; |
matis755 | 4:e48aee3e8d09 | 104 | } |
matis755 | 5:3c19c3ae6286 | 105 | |
matis755 | 5:3c19c3ae6286 | 106 | unsigned int Decoder::uiGetNumber(unsigned char ucIdx) { |
matis755 | 5:3c19c3ae6286 | 107 | return asToken[ucIdx].uValue.uiNumber; |
matis755 | 5:3c19c3ae6286 | 108 | } |