Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
Revision 3:6fc7976cc5bf, committed 2020-05-20
- Comitter:
- matis755
- Date:
- Wed May 20 16:37:25 2020 +0000
- Parent:
- 2:8788d711db7e
- Child:
- 4:e48aee3e8d09
- Commit message:
- Create class for decoding strings
Changed in this revision
--- a/main.cpp Wed May 20 15:42:07 2020 +0000
+++ b/main.cpp Wed May 20 16:37:25 2020 +0000
@@ -41,25 +41,25 @@
int main() {
+ Tokenizer MyTokenizer;
char cTablica[BUFFER_SIZE];
- extern struct Token asToken[];
- extern unsigned char ucTokenCnt;
+
while(1){
if(!gets(cTablica, BUFFER_SIZE)){
- DecodeMsg(cTablica);
- if ((asToken[0].eType == KEYWORD) & (ucTokenCnt != 0)){
- switch (asToken[0].uValue.eKeyword){
- case (CLB):
- led_green = !led_green;
- break;
- case (GT):
- led_red = !led_red;
- break;
- default:
- break;
+ MyTokenizer.DecodeMsg(cTablica);
+ if ((MyTokenizer.eGetTokenType(0) == KEYWORD) & (MyTokenizer.GetTokenCnt() != 0)){
+ switch (MyTokenizer.eGetKeyword(0)){
+ case (CLB):
+ led_green = !led_green;
+ break;
+ case (GT):
+ led_red = !led_red;
+ break;
+ default:
+ break;
+ }
}
- }
}
}
}
--- a/string.cpp Wed May 20 15:42:07 2020 +0000
+++ b/string.cpp Wed May 20 16:37:25 2020 +0000
@@ -1,49 +1,15 @@
#include "string.h"
-struct Token asToken[MAX_TOKEN_NR];
-
-struct Keyword asKeywordList[MAX_KEYWORD_NR]=
-{
-{CLB,"callib"},
-{GT, "goto" },
+struct Keyword asKeywordList[MAX_KEYWORD_NR] = {
+ {CLB,"callib"},
+ {GT, "goto" },
};
-
-unsigned char ucTokenCnt;
-
-void CopyString(char pcSource[], char pcDestination[]){
-
- unsigned char ucCharCounter;
-
- for(ucCharCounter=0;pcSource[ucCharCounter]!='\0';ucCharCounter++){
- pcDestination[ucCharCounter] = pcSource[ucCharCounter];
- }
- pcDestination[ucCharCounter] = '\0';
-}
+enum Result { OK, NOK };
-enum CompResult eCompareString(char pcStr1[], char pcStr2[]){
-
- unsigned char ucCharCounter;
-
- for(ucCharCounter=0;pcStr1[ucCharCounter] != '\0';ucCharCounter++){
- if (pcStr1[ucCharCounter] != pcStr2[ucCharCounter]) return DIFFERENT;
- }
- return EQUAL;
-}
-
-
-void AppendString (char pcSourceStr[],char pcDestinationStr[]){
-
- unsigned char ucEndPointer;
-
- for(ucEndPointer=0;pcDestinationStr[ucEndPointer]!='\0';ucEndPointer++) {}
- CopyString(pcSourceStr,pcDestinationStr+ucEndPointer);
-}
-
-
-void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){
+void Tokenizer::ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){
unsigned char ucCharCounter;
@@ -53,25 +19,8 @@
}
-void UIntToHexStr(unsigned int uiValue, char pcStr[]){
-
- unsigned char ucNibbleCounter;
- unsigned char ucCurrentNibble;
-
- pcStr[0] = '0';
- pcStr[1] = 'x';
- for(ucNibbleCounter=0;ucNibbleCounter<4;ucNibbleCounter++){
- ucCurrentNibble = ((uiValue >> ucNibbleCounter*4) & HEX_bm);
- if(ucCurrentNibble>9)
- pcStr[5-ucNibbleCounter] = ucCurrentNibble - 10 + 'A';
- else
- pcStr[5-ucNibbleCounter] = ucCurrentNibble + '0';
- }
- pcStr[6] = '\0';
-}
-
-enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue){
+enum Result Tokenizer::eHexStringToUInt(char pcStr[],unsigned int *puiValue){
unsigned char ucCharCounter;
@@ -90,18 +39,8 @@
return OK;
}
-void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]){
-
- unsigned char ucEndPointer;
-
- for(ucEndPointer=0;pcDestinationStr[ucEndPointer]!='\0';ucEndPointer++) {}
- UIntToHexStr(uiValue,pcDestinationStr+ucEndPointer);
-}
-
-
-
-unsigned char ucFindTokensInString(char *pcString){
+unsigned char Tokenizer::ucFindTokensInString(char *pcString){
unsigned char ucTokenPointer;
unsigned char ucDelimiterCounter;
@@ -136,12 +75,12 @@
}
}
-enum Result eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){
+enum Result Tokenizer::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){
unsigned char ucTokenCounter;
for(ucTokenCounter=0;ucTokenCounter<MAX_TOKEN_NR;ucTokenCounter++){
- if (eCompareString(pcStr,asKeywordList[ucTokenCounter].cString) == EQUAL) {
+ if (!strcmp(pcStr, asKeywordList[ucTokenCounter].cString)) {
*peKeywordCode = asKeywordList[ucTokenCounter].eCode;
return OK;
}
@@ -149,7 +88,7 @@
return NOK;
}
-void DecodeTokens(unsigned char ucTokenCnt){
+void Tokenizer::DecodeTokens(unsigned char ucTokenCnt){
unsigned char ucTokenCounter;
Token* tValue;
@@ -162,9 +101,21 @@
}
}
-void DecodeMsg(char *pcString){
+void Tokenizer::DecodeMsg(char *pcString){
ucTokenCnt = ucFindTokensInString(pcString);
ReplaceCharactersInString(pcString,' ','\0');
DecodeTokens(ucTokenCnt);
}
+
+enum TokenType Tokenizer::eGetTokenType(unsigned char ucIdx) {
+ return asToken[ucIdx].eType;
+}
+
+unsigned char Tokenizer::GetTokenCnt(void) {
+ return ucTokenCnt;
+}
+
+enum KeywordCode Tokenizer::eGetKeyword(unsigned char ucIdx) {
+ return asToken[ucIdx].uValue.eKeyword;
+}
--- a/string.h Wed May 20 15:42:07 2020 +0000
+++ b/string.h Wed May 20 16:37:25 2020 +0000
@@ -1,3 +1,5 @@
+#include "mbed.h"
+
#define NULL 0
#define TERMINATOR '\r'
#define DELIMITER_CHAR 0x20
@@ -8,12 +10,6 @@
#define MAX_KEYWORD_NR 2
-typedef enum CompResult
-{ DIFFERENT , EQUAL } CompResult;
-
-typedef enum Result
-{ OK, NOK } Result;
-
typedef enum TokenType
{KEYWORD, NUMBER, STRING} TokenType;
@@ -41,14 +37,24 @@
} Keyword;
-void CopyString(char pcSource[], char pcDestination[]);
-enum CompResult eCompareString(char pcStr1[], char pcStr2[]);
-void AppendString (char pcSourceStr[],char pcDestinationStr[]);
-void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar);
-void UIntToHexStr(unsigned int uiValue, char pcStr[]);
-enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue);
-void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]);
-unsigned char ucFindTokensInString(char *pcString);
-enum Result eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode);
-void DecodeTokens(unsigned char ucTokenCnt);
-void DecodeMsg(char *pcString);
+
+
+class Tokenizer {
+ private:
+ struct Token asToken[MAX_TOKEN_NR];
+ unsigned char ucTokenCnt;
+ void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar);
+ enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue);
+ unsigned char ucFindTokensInString(char *pcString);
+ enum Result eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode);
+ void DecodeTokens(unsigned char ucTokenCnt);
+ public:
+ void DecodeMsg(char *pcString);
+ enum TokenType eGetTokenType(unsigned char ucIdx);
+ enum KeywordCode eGetKeyword(unsigned char ucIdx);
+ unsigned char GetTokenCnt(void);
+};
+
+
+
+