Mateusz Garwol / Mbed 2 deprecated 2_5

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

Files at this revision

API Documentation at this revision

Comitter:
matis755
Date:
Thu May 21 11:03:32 2020 +0000
Parent:
3:6fc7976cc5bf
Child:
5:3c19c3ae6286
Commit message:
Ready without GUI;

Changed in this revision

decoder.cpp Show annotated file Show diff for this revision Revisions of this file
decoder.h Show annotated file Show diff for this revision Revisions of this file
led.cpp Show annotated file Show diff for this revision Revisions of this file
led.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
stepper.cpp Show annotated file Show diff for this revision Revisions of this file
stepper.h Show annotated file Show diff for this revision Revisions of this file
string.cpp Show diff for this revision Revisions of this file
string.h Show diff for this revision Revisions of this file
uart.cpp Show annotated file Show diff for this revision Revisions of this file
uart.h Show annotated file Show diff for this revision Revisions of this file
--- /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;    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/decoder.h	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,62 @@
+#ifndef DECODER
+#define DECODER
+
+#include "mbed.h"
+
+#define NULL 0
+#define TERMINATOR '\r'
+#define DELIMITER_CHAR 0x20
+
+#define HEX_bm 0x000F
+#define MAX_TOKEN_NR 2
+#define MAX_KEYWORD_STRING_LTH 6
+#define MAX_KEYWORD_NR 4
+
+
+typedef enum TokenType 
+{KEYWORD, NUMBER, STRING} TokenType;
+
+typedef enum KeywordCode 
+{ ID, CLB, GT, ST } KeywordCode;
+
+typedef union TokenValue
+{
+enum KeywordCode eKeyword;
+unsigned int uiNumber;
+char *pcString; 
+} TokenValue;
+
+typedef struct Token
+{
+enum TokenType eType; 
+union TokenValue uValue; 
+} Token;
+
+
+typedef struct Keyword
+{
+enum KeywordCode eCode;
+char cString[MAX_KEYWORD_STRING_LTH + 1];
+} Keyword;
+
+
+
+
+class Decoder {
+    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);
+};
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/led.cpp	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,31 @@
+#include "led.h"
+
+Led::Led() : led0(PG_4), led1(PG_5), led2(PG_6), led3(PG_7) {
+
+}
+
+
+void Led::On(unsigned char ucLedIndex)
+{
+    led0 = 0;  
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    
+    switch(ucLedIndex){
+        case 0:
+            led0 = 1;    
+            break;
+        case 1:
+            led1 = 1;      
+            break;
+        case 2:
+            led2 = 1;      
+            break;
+        case 3:
+            led3 = 1;  
+            break;
+        default:
+            break;      
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/led.h	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,18 @@
+#ifndef LED
+#define LED
+
+#include "mbed.h"
+
+class Led {
+    private:
+        DigitalOut led0;
+        DigitalOut led1;
+        DigitalOut led2;
+        DigitalOut led3;
+    public:
+        void On(unsigned char);
+        Led();  
+};
+
+
+#endif
\ No newline at end of file
--- a/main.cpp	Wed May 20 16:37:25 2020 +0000
+++ b/main.cpp	Thu May 21 11:03:32 2020 +0000
@@ -1,65 +1,52 @@
 #include "mbed.h"
-#include "string.h"
+#include "decoder.h"
+#include "uart.h"
+#include "stepper.h"
 
-#define BUFFER_SIZE 100
 
-Serial MySerial(USBTX, USBRX);
 DigitalOut led_green(LED1);
 DigitalOut led_red(LED2);
 
-char gets(char *pStr, unsigned char ucSize){
-    unsigned char ucStrPtr = 0;
-    while(ucStrPtr < ucSize) {
-        pStr[ucStrPtr] = MySerial.getc();
-        if(pStr[ucStrPtr] == '\r') {
-            pStr[ucStrPtr] = NULL;
-            return 0;
-        }
-        ucStrPtr ++;    
-    }
-    return 1;
-}
-
-char puts(char *pStr, unsigned char ucSize){   
-    unsigned char ucNullPtr;
-    for(ucNullPtr = 0; ucNullPtr <= ucSize; ucNullPtr++){
-        if(pStr[ucNullPtr] == NULL) {
-            break;
-        }
-        else {}
-    }
-    if (ucNullPtr == ucSize) {
-        return 1;
-    }
-
-    for(unsigned char ucIdx = 0; ucIdx < ucNullPtr; ucIdx ++) {
-        MySerial.putc(pStr[ucIdx]);
-    }
-    MySerial.putc('\r');
-    return 0;
-}
-
 
 int main() {
-    Tokenizer MyTokenizer;
+    Decoder MyDecoder;
+    Uart MyUart;
+    Stepper MyStepper;
+    
     char cTablica[BUFFER_SIZE];
-
+    char cCorrectResponse[] = "ok\n";
+    char cUnknownKeyword[] = "unknowncommand\n";
+    char cIdentifier[] = "id anyidentyfier\n";
    
     while(1){
-        if(!gets(cTablica, BUFFER_SIZE)){        
-            MyTokenizer.DecodeMsg(cTablica);
-            if ((MyTokenizer.eGetTokenType(0) == KEYWORD) & (MyTokenizer.GetTokenCnt() != 0)){
-                switch (MyTokenizer.eGetKeyword(0)){
+        if(!MyUart.gets(cTablica, BUFFER_SIZE)){        
+            MyDecoder.DecodeMsg(cTablica);
+            if ((MyDecoder.eGetTokenType(0) == KEYWORD) & (MyDecoder.GetTokenCnt() != 0)){
+                switch (MyDecoder.eGetKeyword(0)){
+                    case (ID):
+                        MyUart.puts(cIdentifier, BUFFER_SIZE);
+                        break;
                     case (CLB):
                         led_green = !led_green;
+                        MyStepper.Callib();
+                        MyUart.puts(cCorrectResponse, BUFFER_SIZE);
                         break;
                     case (GT):
                         led_red = !led_red;
+                        MyStepper.Goto(4);
+                        MyUart.puts(cCorrectResponse, BUFFER_SIZE);
+                        break;
+                    case (ST):
+                        MyStepper.Step(5);
+                        MyUart.puts(cCorrectResponse, BUFFER_SIZE);
                         break;
                     default:
                         break;
                 }
             }
+            else {
+                MyUart.puts(cUnknownKeyword, BUFFER_SIZE);
+            }
         }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepper.cpp	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,57 @@
+#include "stepper.h"
+
+enum Step{LEFT,RIGHT};
+
+Stepper::Stepper() : Button(USER_BUTTON){
+}
+
+void Stepper::Step(enum Step eStep){
+    if(eStep == LEFT){
+        ucLedIdx++;
+    }
+    else if(eStep == RIGHT){
+        ucLedIdx--;
+    }
+    else{}
+    ucLedIdx = ucLedIdx % 4;
+    MyLed.On(ucLedIdx);
+}
+
+void Stepper::StepLeft(void){
+    Step(LEFT);
+}
+
+void Stepper::StepRight(void){
+    Step(RIGHT);
+}
+
+void Stepper::Callib(void) {
+    while(!Button) {
+        StepRight();
+        wait(0.1);
+    }
+}
+
+void Stepper::Goto(unsigned char ucDestination) {
+    ucDesiredPos = ucDestination;
+    while (ucCurrentPos > ucDesiredPos) {
+        StepLeft();
+        ucCurrentPos --;
+        wait(0.2);
+    }
+    while (ucCurrentPos < ucDesiredPos) {
+        StepRight();
+        ucCurrentPos ++;
+        wait(0.2);
+    } 
+}
+
+void Stepper::Step(unsigned char ucSteps) {
+    ucDesiredPos = ucCurrentPos + ucSteps;
+    while (ucCurrentPos < ucDesiredPos) {
+        StepRight();
+        ucCurrentPos ++;
+        wait(0.2);
+    } 
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepper.h	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,21 @@
+#ifndef STEPPER
+#define STEPPER
+
+#include "led.h"
+
+class Stepper {
+    private:
+        DigitalIn Button;
+        Led MyLed;
+        unsigned char ucLedIdx, ucCurrentPos, ucDesiredPos;
+        void Step(enum Step);
+        void StepLeft(void);
+        void StepRight(void);
+    public:
+        Stepper();
+        void Callib(void);
+        void Goto(unsigned char);
+        void Step(unsigned char);
+};
+
+#endif
\ No newline at end of file
--- a/string.cpp	Wed May 20 16:37:25 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-#include "string.h"
-
-
-struct Keyword asKeywordList[MAX_KEYWORD_NR] = {
-    {CLB,"callib"},
-    {GT, "goto" },
-};
-
-enum Result { OK, NOK };
-
-
-void Tokenizer::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 Tokenizer::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 Tokenizer::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 Tokenizer::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){
-  
-    unsigned char ucTokenCounter;
-  
-    for(ucTokenCounter=0;ucTokenCounter<MAX_TOKEN_NR;ucTokenCounter++){
-        if (!strcmp(pcStr, asKeywordList[ucTokenCounter].cString)) {
-            *peKeywordCode = asKeywordList[ucTokenCounter].eCode;
-            return OK;
-        }
-    }
-    return NOK;
-}
-
-void Tokenizer::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 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 16:37:25 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-#include "mbed.h"
-
-#define NULL 0
-#define TERMINATOR '\r'
-#define DELIMITER_CHAR 0x20
-
-#define HEX_bm 0x000F
-#define MAX_TOKEN_NR 2
-#define MAX_KEYWORD_STRING_LTH 6
-#define MAX_KEYWORD_NR 2
-
-
-typedef enum TokenType 
-{KEYWORD, NUMBER, STRING} TokenType;
-
-typedef enum KeywordCode 
-{ CLB, GT} KeywordCode;
-
-typedef union TokenValue
-{
-enum KeywordCode eKeyword;
-unsigned int uiNumber;
-char *pcString; 
-} TokenValue;
-
-typedef struct Token
-{
-enum TokenType eType; 
-union TokenValue uValue; 
-} Token;
-
-
-typedef struct Keyword
-{
-enum KeywordCode eCode;
-char cString[MAX_KEYWORD_STRING_LTH + 1];
-} Keyword;
-
-
-
-
-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);
-};
-
-
-
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart.cpp	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,37 @@
+#include "uart.h"
+
+Uart::Uart() : MySerial(USBTX, USBRX){
+
+};
+
+char Uart::gets(char *pStr, unsigned char ucSize){
+    unsigned char ucStrPtr = 0;
+    while(ucStrPtr < ucSize) {
+        pStr[ucStrPtr] = MySerial.getc();
+        if(pStr[ucStrPtr] == '\r') {
+            pStr[ucStrPtr] = NULL;
+            return 0;
+        }
+        ucStrPtr ++;    
+    }
+    return 1;
+}
+
+char Uart::puts(char *pStr, unsigned char ucSize){   
+    unsigned char ucNullPtr;
+    for(ucNullPtr = 0; ucNullPtr <= ucSize; ucNullPtr++){
+        if(pStr[ucNullPtr] == NULL) {
+            break;
+        }
+        else {}
+    }
+    if (ucNullPtr == ucSize) {
+        return 1;
+    }
+
+    for(unsigned char ucIdx = 0; ucIdx < ucNullPtr; ucIdx ++) {
+        MySerial.putc(pStr[ucIdx]);
+    }
+    MySerial.putc('\r');
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart.h	Thu May 21 11:03:32 2020 +0000
@@ -0,0 +1,17 @@
+#ifndef UART
+#define UART
+
+#include "mbed.h"
+
+#define BUFFER_SIZE 100
+
+class Uart {
+    private:
+        Serial MySerial;
+    public:
+        Uart();
+        char gets(char *pStr, unsigned char ucSize);
+        char puts(char *pStr, unsigned char ucSize);
+};
+
+#endif
\ No newline at end of file