AGH UST MBED part 2

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

Revision:
4:e48aee3e8d09
Child:
5:3c19c3ae6286
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/decoder.cpp	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,123 @@
+#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;    
+}