callib

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

Files at this revision

API Documentation at this revision

Comitter:
filipksiezyc
Date:
Tue Jun 09 10:21:57 2020 +0000
Commit message:
1331;

Changed in this revision

BSP_DISCO_F429ZI.lib Show annotated file Show diff for this revision Revisions of this file
LCD_DISCO_F429ZI.lib Show annotated file Show diff for this revision Revisions of this file
ServoGUI.cpp Show annotated file Show diff for this revision Revisions of this file
ServoGUI.h Show annotated file Show diff for this revision Revisions of this file
TokensHandling.cpp Show annotated file Show diff for this revision Revisions of this file
TokensHandling.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BSP_DISCO_F429ZI.lib	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/BSP_DISCO_F429ZI/#53d9067a4feb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD_DISCO_F429ZI.lib	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/filipksiezyc/code/LCD_DISCO_F429ZI/#a35e1053b0fa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoGUI.cpp	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,117 @@
+#include "ServoGUI.h"
+#include "mbed.h"
+#include "mbed.h"
+
+DigitalIn UserButton(USER_BUTTON);
+
+enum eStep{RIGHT, LEFT};
+enum eDetectorState{ACTIVE, OFF};
+
+ServoGUI::ServoGUI(){
+    LCD_Display.Clear(LCD_COLOR_WHITE);
+    sPosition.X=120;
+    sPosition.Y=210;
+    ucCurrentPosition=0;
+    ucStepPosition=0;
+    ucCallibPosition=12;
+    
+    DrawServo();
+    LedOn(0);
+    Callib();
+}
+
+void ServoGUI::DrawServo(){
+    LCD_Display.SetTextColor(LCD_COLOR_BLACK);
+    LCD_Display.FillCircle(120,110,102);
+    LCD_Display.SetTextColor(LCD_COLOR_BLUE);
+    LCD_Display.FillCircle(120, 110, 100);
+    LCD_Display.SetTextColor(LCD_COLOR_YELLOW);
+    LCD_Display.DrawLine(120, 110, sPosition.X, sPosition.Y);
+    LCD_Display.SetTextColor(LCD_COLOR_WHITE);
+    LCD_Display.FillCircle(120, 110, 20);
+    }  
+
+void ServoGUI::DrawLed(unsigned char ucCurrentLed, uint32_t uiLedColor){
+            LCD_Display.SetTextColor(LCD_COLOR_BLACK);
+            LCD_Display.FillCircle(30+60*ucCurrentLed,270,20);
+            LCD_Display.SetTextColor(uiLedColor);
+            LCD_Display.FillCircle(30+60*ucCurrentLed,270,18);
+    }
+    
+void ServoGUI::LedOn(unsigned char ucLed){
+    unsigned char ucCurrentLed;
+    for(ucCurrentLed=0; ucCurrentLed<NUMBER_OF_LEDS; ucCurrentLed++){
+       if(ucCurrentLed==ucLed){
+            DrawLed(ucCurrentLed,LCD_COLOR_BLUE);
+            }
+       else{
+           DrawLed(ucCurrentLed, LCD_COLOR_WHITE);
+           }
+       }
+    }  
+    
+void ServoGUI::Step(void){
+    double dStep;
+    dStep=((((ucCurrentPosition+ucCallibPosition)%48))%48*(3.1415/24));    
+    sPosition.X=(120+100*cos(dStep));
+    sPosition.Y=(110+100*sin(dStep));
+    DrawServo();
+    LedOn(ucCurrentPosition%4);
+    wait(0.1);  
+    }
+
+void ServoGUI::MovePos(enum eStep eDirection){
+     while(ucCurrentPosition!=ucStepPosition){
+            if(eDirection==LEFT){    
+                ucCurrentPosition--;      
+                }
+            else if(eDirection==RIGHT){
+                ucCurrentPosition++;
+                }
+            else;
+            ucCurrentPosition=ucCurrentPosition%48;
+            Step();
+            }
+        }
+   
+  
+    
+void ServoGUI::GoTo(unsigned char ucPosition){
+    ucStepPosition=ucPosition;
+    
+    if(ucCurrentPosition!=ucStepPosition){
+        enum eStep eDirection= ucPosition < ucCurrentPosition ? LEFT : RIGHT;
+        MovePos(eDirection);
+        }
+    else;
+    }        
+      
+    
+void ServoGUI::Shift(unsigned char ucLed){
+    ucStepPosition+=ucLed;
+    if(ucCurrentPosition!=ucStepPosition){
+        enum eStep eDirection= ucStepPosition < ucCurrentPosition ? LEFT : RIGHT;
+        ucStepPosition=ucStepPosition%48;
+        MovePos(eDirection);
+        }
+    else;
+    }   
+    
+enum eDetectorState ServoGUI::GetDetector(){  
+    if(UserButton==1)
+        return ACTIVE;
+    else
+        return OFF;
+}  
+    
+void ServoGUI::Callib(){
+    unsigned char ucCallibCounter=12;
+    while(GetDetector()!=ACTIVE){
+        Shift(1);
+        ucCallibCounter++;
+        }
+    ucCallibPosition=ucCallibCounter%48;
+    ucCurrentPosition=0;
+    ucStepPosition=0;
+    }  
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoGUI.h	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,32 @@
+#ifndef SERVO_GUI
+#define SERVO_GUI
+
+#include "LCD_DISCO_F429ZI.h"
+
+#define NUMBER_OF_LEDS 4
+
+
+struct sPoint{double X; double Y;};
+
+class ServoGUI{
+    private:
+        LCD_DISCO_F429ZI LCD_Display; 
+        sPoint sPosition;
+        unsigned char ucCurrentPosition;
+        unsigned char ucStepPosition; 
+        unsigned char ucCallibPosition;
+        
+        enum eDetectorState GetDetector();
+        void MovePos(enum eStep eDirection);
+        void DrawServo();
+        void DrawLed(unsigned char ucCurrentLed=0,uint32_t uiLedColor=LCD_COLOR_WHITE);     
+        void Step();
+    public:
+        ServoGUI();
+        void LedOn(unsigned char ucLed=0);
+        void GoTo(unsigned char ucPosition);
+        void Shift(unsigned char ucLed=0);
+        void Callib();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TokensHandling.cpp	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,88 @@
+#include "TokensHandling.h"
+#include "mbed.h"
+
+#define MAX_KEYWORD_NR 4
+
+enum Result {OK, ERR};
+
+struct Keyword{
+    enum KeywordCode eCode;
+    char cString[MAX_KEYWORD_STRING_LTH];
+};
+
+struct Keyword asKeywordList[MAX_KEYWORD_NR]={
+    {CALLIB, "callib"},
+    {GOTO,  "goto"},
+    {ID,  "id"},
+    {STEP,  "step"},
+};
+
+void TokensHandler::ReplaceCharactersInString(char *pcString,char cOldChar,char cNewChar){
+        char *CurrentChar = strchr(pcString,cOldChar);
+        while(*CurrentChar){
+            *CurrentChar = cNewChar;
+            CurrentChar = strchr(CurrentChar,cOldChar);
+        }
+}  
+
+
+unsigned char TokensHandler::ucFindTokensInString(char *pcString){
+    char *pcTokenString = pcString;
+    pcTokenString = strtok(pcTokenString, " ");
+    
+    for(ucTokenNr=0;((pcTokenString != NULL) && (ucTokenNr < MAX_TOKEN_NR));ucTokenNr++)
+    {      
+            asToken[ucTokenNr].uValue.pcString = pcTokenString;
+            pcTokenString = strtok(NULL, " ");
+    }
+    return ucTokenNr;
+}
+
+
+
+enum Result TokensHandler::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){
+    unsigned char ucKeywordCounter;
+
+    for(ucKeywordCounter=0; ucKeywordCounter< MAX_KEYWORD_NR; ucKeywordCounter++){
+        
+        if( (strcmp(pcStr, asKeywordList[ucKeywordCounter].cString))==0){
+            *peKeywordCode = asKeywordList[ucKeywordCounter].eCode;
+            return OK;
+        }
+    }
+    return ERR;
+}
+
+void TokensHandler::DecodeTokens(void){
+    unsigned char ucTokenCounter;
+    struct Token *psCurrentToken;
+    unsigned int uiTokenValue;
+    enum KeywordCode eTokenCode;
+
+    for(ucTokenCounter= 0; ucTokenCounter< ucTokenNr; ucTokenCounter++){
+        psCurrentToken= &asToken[ucTokenCounter];
+    
+        if(sscanf(asToken[ucTokenCounter].uValue.pcString, "%x", &uiTokenValue) == 1 ){
+            psCurrentToken->eType= NUMBER;
+            psCurrentToken->uValue.uiNumber= uiTokenValue;
+        }
+        else if(OK== eStringToKeyword(psCurrentToken->uValue.pcString, &eTokenCode)){
+            psCurrentToken->eType=KEYWORD;
+            psCurrentToken->uValue.eKeyword=eTokenCode;
+        }
+        else{
+            psCurrentToken->eType=STRING;
+        }
+    }
+}
+
+void TokensHandler::DecodeMsg(char *pcString){
+    ucTokenNr= ucFindTokensInString(pcString);
+    ReplaceCharactersInString(pcString, ' ', NULL);
+    DecodeTokens();
+}
+
+TokensHandler::TokensHandler(){
+    ucTokenNr=0;    
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TokensHandling.h	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,34 @@
+#ifndef TOKENS_HANDLING_H
+#define TOKENS_HANDLING_H
+
+#define MAX_KEYWORD_STRING_LTH 15
+#define MAX_TOKEN_NR 2
+
+enum KeywordCode {CALLIB, GOTO, ID, STEP};
+enum TokenType {KEYWORD, NUMBER, STRING};
+
+union TokenValue{
+    enum KeywordCode eKeyword;
+    unsigned int uiNumber;
+    char* pcString;
+};
+
+struct Token{
+    enum TokenType eType;
+    union TokenValue uValue;
+};
+
+class TokensHandler{
+    private: 
+        unsigned char ucFindTokensInString(char *pcString);
+        enum Result eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode);
+        void DecodeTokens(void);
+        void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar);
+    public:
+        TokensHandler();
+        unsigned char ucTokenNr;
+        struct Token asToken[MAX_TOKEN_NR];
+        void DecodeMsg(char *pcString);
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,101 @@
+#include "mbed.h"
+#include "ServoGUI.h"
+#include "TokensHandling.h"
+
+#define CR '\r'
+
+Serial MySerialConnection(USBTX, USBRX, 9600);
+
+int puts(char *pcCharsToSend, unsigned char ucNumberOfCharsToSend){
+    unsigned char ucCurrentChar;
+    
+    for(ucCurrentChar=0; ucCurrentChar<ucNumberOfCharsToSend; ucCurrentChar++){
+        if(pcCharsToSend[ucCurrentChar]==NULL) 
+            break; 
+        else if((ucCurrentChar==ucNumberOfCharsToSend)&&(pcCharsToSend[ucCurrentChar]!=NULL))
+            return 1; 
+        }
+    
+    for(ucCurrentChar=0; pcCharsToSend[ucCurrentChar]!=NULL; ucCurrentChar++){
+        MySerialConnection.putc(pcCharsToSend[ucCurrentChar]);
+        }
+    
+    MySerialConnection.putc(CR);
+    return 0;
+    }
+
+int gets(char *pcGotCharDestination, unsigned char ucInputBufferSize){
+    unsigned char ucCurrentRecivedChar=0;
+
+        for(ucCurrentRecivedChar=0; ucCurrentRecivedChar<ucInputBufferSize; ucCurrentRecivedChar++){
+            char cInputChar=MySerialConnection.getc();
+            
+            if(cInputChar == CR){
+                pcGotCharDestination[ucCurrentRecivedChar]=NULL;
+                return 0;
+            }
+            else {
+                pcGotCharDestination[ucCurrentRecivedChar]=cInputChar;
+            }
+        } 
+    pcGotCharDestination[--ucCurrentRecivedChar]=NULL;   
+    return 1;
+    }
+
+
+int main(){
+    ServoGUI MyServo;
+    TokensHandler TokenDecoder;
+    
+    char cInputString[MAX_KEYWORD_STRING_LTH];
+    char cErrorMessage[]="unknowncommand\n";
+    char cBoardId[]="DISCO_F429ZI\n";
+    char cOkMessage[]="OK\n";
+
+    while(1){
+        
+        if(gets(cInputString, MAX_KEYWORD_STRING_LTH)==0){
+            TokenDecoder.DecodeMsg(cInputString);
+            
+            if((TokenDecoder.asToken[0].eType==KEYWORD)&&(TokenDecoder.ucTokenNr>0)){
+                
+                switch(TokenDecoder.asToken[0].uValue.eKeyword){            
+                        case GOTO:    
+                            if(TokenDecoder.asToken[1].eType==NUMBER){  
+                                MyServo.GoTo(TokenDecoder.asToken[1].uValue.uiNumber);
+                                puts(cOkMessage, (strlen(cOkMessage)+1));
+                            }
+                            break;
+                        
+                        case CALLIB:   
+                            MyServo.Callib();
+                            puts(cOkMessage, (strlen(cOkMessage)+1));
+                            break;     
+                        
+                        case STEP:
+                            if(TokenDecoder.asToken[1].eType==NUMBER){
+                                MyServo.Shift(TokenDecoder.asToken[1].uValue.uiNumber);
+                                puts(cOkMessage, (strlen(cOkMessage)+1));   
+                            }
+                            break;
+                        
+                        case ID:
+                            puts(cBoardId, (strlen(cBoardId)+1));
+                            break;
+                        
+                        default:
+                            puts(cErrorMessage, (strlen(cErrorMessage)+1)); 
+                            break;
+                        }
+                }
+        else {
+            puts(cErrorMessage, (strlen(cErrorMessage)+1));
+        }
+    }
+} 
+}
+    
+  
+ 
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jun 09 10:21:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file