Robert Labuz
/
mbed5a_testy
mbed5a_testy
Revision 1:b8d65b5745d1, committed 2017-05-05
- Comitter:
- Robsonik16
- Date:
- Fri May 05 20:01:41 2017 +0000
- Parent:
- 0:e69a0b7f4b41
- Commit message:
- a
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.cpp Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,87 @@ +#include "string.h" +#include "command_decoder.h" + +struct Keyword asKeywordList[MAX_KEYWORD_NR]= + { + {CALLIB, "callib"}, + {GOTO, "goto"}, + {STEP, "step"}, + {ID, "id"} + }; +struct Token asToken[MAX_TOKEN_NR]; + +unsigned char ucTokenNr; + +unsigned char ucFindTokensInString(char *pcString){ + + enum LedState { TOKEN, DELIMITER}; + enum LedState eState = DELIMITER; + + unsigned char ucLicznikTokenu = 0; + unsigned char ucLicznikZnaku = 0; + char cAktualnyZnak; + + for(ucLicznikZnaku=0; pcString[ucLicznikZnaku] != NULL; ucLicznikZnaku++){ + cAktualnyZnak = pcString[ucLicznikZnaku]; + switch(eState){ + case DELIMITER: + if(cAktualnyZnak == DELIMITER_CHAR){ + eState = DELIMITER; + }else if(ucLicznikTokenu < MAX_TOKEN_NR){ + eState = TOKEN; + asToken[ucLicznikTokenu].uValue.pcString = pcString + ucLicznikZnaku; + ucLicznikTokenu++; + }else{ + return(ucLicznikTokenu); + } + break; + case TOKEN: + if(cAktualnyZnak!= DELIMITER_CHAR){ + eState = TOKEN; + }else{ + eState = DELIMITER; + } + break; + } + } + return(ucLicznikTokenu); +} + +enum Result eStringToKeyword(char pcStr[], enum KeywordCode *peKeywordCode){ + + unsigned char ucKeywordIndex; + + for(ucKeywordIndex=0; ucKeywordIndex < MAX_KEYWORD_NR; ucKeywordIndex++){ + if(eCompareString(pcStr, asKeywordList[ucKeywordIndex].cString) == EQUAL){ + *peKeywordCode = asKeywordList[ucKeywordIndex].eCode; + return(OK); + } + } + return(ERROR); +} + +void DecodeTokens(void){ + + unsigned int uiNumber; + unsigned char ucTokenIndex; + enum KeywordCode eKeyword; + + for(ucTokenIndex=0; ucTokenIndex < ucTokenNr; ucTokenIndex++){ + if((eStringToKeyword (asToken[ucTokenIndex].uValue.pcString, &eKeyword))== OK){ + asToken[ucTokenIndex].eType = KEYWORD; + asToken[ucTokenIndex].uValue.eKeyword=eKeyword; + }else if((eHexStringToUInt(asToken[ucTokenIndex].uValue.pcString,&uiNumber))== OK){ + asToken[ucTokenIndex].eType = NUMBER; + asToken[ucTokenIndex].uValue.uiNumber = uiNumber; + }else{ + asToken[ucTokenIndex].eType = STRING; + } + } +} + +void DecodeMsg(char *pcString){ + + ucTokenNr = ucFindTokensInString (pcString); + ReplaceCharactersInString(pcString,' ',NULL); + DecodeTokens(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.h Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,33 @@ + +enum KeywordCode {CALLIB,GOTO,STEP,ID}; +enum TokenType { KEYWORD, NUMBER, STRING}; + +#define MAX_TOKEN_NR 2 +#define MAX_KEYWORD_STRING_LTH 15 +#define MAX_KEYWORD_NR 4 + +union TokenValue +{ + enum KeywordCode eKeyword; + unsigned int uiNumber; + char *pcString; +}; + +struct Token +{ + enum TokenType eType; + union TokenValue uValue; +}; + +struct Keyword +{ + enum KeywordCode eCode; + char cString[MAX_KEYWORD_STRING_LTH + 1]; +}; + + +extern struct Keyword asKeywordList[MAX_KEYWORD_NR]; +extern struct Token asToken[MAX_TOKEN_NR]; +extern unsigned char ucTokenNr; + +void DecodeMsg(char *pcString);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.cpp Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,61 @@ +#include "led.h" +#include "mbed.h" + +DigitalOut led_0(PA_4); +DigitalOut led_1(PA_5); +DigitalOut led_2(PA_6); +DigitalOut led_3(PA_7); + +enum Step{LEFT, RIGHT}; + +void LedInt(void){ + //IO1DIR=IO1DIR|(LED0_bm|LED1_bm|LED2_bm|LED3_bm); + //IO1SET= LED0_bm; +} + +void LedOn(unsigned char ucLedIndeks){ + + + //IO1CLR = LED0_bm|LED1_bm|LED2_bm|LED3_bm; + led_0=0; + led_1=0; + led_2=0; + led_3=0; + switch (ucLedIndeks) { + case 0: + led_0=1; + break; + case 1: + led_1=1; + break; + case 2: + led_2=1; + break; + case 3: + led_3=1; + break; + default: + break; + } + + +} +void Led_Step(enum Step Direction){ + + static unsigned int uiDioda; + + if (Direction == RIGHT ){ + uiDioda++; + }else{ + uiDioda--; + } + LedOn(uiDioda%4); +} + +void Led_StepLeft(void){ + Led_Step(LEFT); +} + +void Led_StepRight(void){ + Led_Step(RIGHT); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.h Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,4 @@ +void LedInt(void); +void Led_StepLeft(void); +void Led_StepRight(void); +void LedOn(unsigned char ucLedIndeks);
--- a/led.lib Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/Robsonik16/code/mbed5a/#f9da71c488d7
--- a/other/command_decoder.cpp Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,87 +0,0 @@ -#include "string.h" -#include "command_decoder.h" - -struct Keyword asKeywordList[MAX_KEYWORD_NR]= - { - {CALLIB, "callib"}, - {GOTO, "goto"}, - {STEP, "step"}, - {ID, "id"} - }; -struct Token asToken[MAX_TOKEN_NR]; - -unsigned char ucTokenNr; - -unsigned char ucFindTokensInString(char *pcString){ - - enum LedState { TOKEN, DELIMITER}; - enum LedState eState = DELIMITER; - - unsigned char ucLicznikTokenu = 0; - unsigned char ucLicznikZnaku = 0; - char cAktualnyZnak; - - for(ucLicznikZnaku=0; pcString[ucLicznikZnaku] != NULL; ucLicznikZnaku++){ - cAktualnyZnak = pcString[ucLicznikZnaku]; - switch(eState){ - case DELIMITER: - if(cAktualnyZnak == DELIMITER_CHAR){ - eState = DELIMITER; - }else if(ucLicznikTokenu < MAX_TOKEN_NR){ - eState = TOKEN; - asToken[ucLicznikTokenu].uValue.pcString = pcString + ucLicznikZnaku; - ucLicznikTokenu++; - }else{ - return(ucLicznikTokenu); - } - break; - case TOKEN: - if(cAktualnyZnak!= DELIMITER_CHAR){ - eState = TOKEN; - }else{ - eState = DELIMITER; - } - break; - } - } - return(ucLicznikTokenu); -} - -enum Result eStringToKeyword(char pcStr[], enum KeywordCode *peKeywordCode){ - - unsigned char ucKeywordIndex; - - for(ucKeywordIndex=0; ucKeywordIndex < MAX_KEYWORD_NR; ucKeywordIndex++){ - if(eCompareString(pcStr, asKeywordList[ucKeywordIndex].cString) == EQUAL){ - *peKeywordCode = asKeywordList[ucKeywordIndex].eCode; - return(OK); - } - } - return(ERROR); -} - -void DecodeTokens(void){ - - unsigned int uiNumber; - unsigned char ucTokenIndex; - enum KeywordCode eKeyword; - - for(ucTokenIndex=0; ucTokenIndex < ucTokenNr; ucTokenIndex++){ - if((eStringToKeyword (asToken[ucTokenIndex].uValue.pcString, &eKeyword))== OK){ - asToken[ucTokenIndex].eType = KEYWORD; - asToken[ucTokenIndex].uValue.eKeyword=eKeyword; - }else if((eHexStringToUInt(asToken[ucTokenIndex].uValue.pcString,&uiNumber))== OK){ - asToken[ucTokenIndex].eType = NUMBER; - asToken[ucTokenIndex].uValue.uiNumber = uiNumber; - }else{ - asToken[ucTokenIndex].eType = STRING; - } - } -} - -void DecodeMsg(char *pcString){ - - ucTokenNr = ucFindTokensInString (pcString); - ReplaceCharactersInString(pcString,' ',NULL); - DecodeTokens(); -}
--- a/other/command_decoder.h Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ - -enum KeywordCode {CALLIB,GOTO,STEP,ID}; -enum TokenType { KEYWORD, NUMBER, STRING}; - -#define MAX_TOKEN_NR 2 -#define MAX_KEYWORD_STRING_LTH 15 -#define MAX_KEYWORD_NR 4 - -union TokenValue -{ - enum KeywordCode eKeyword; - unsigned int uiNumber; - char *pcString; -}; - -struct Token -{ - enum TokenType eType; - union TokenValue uValue; -}; - -struct Keyword -{ - enum KeywordCode eCode; - char cString[MAX_KEYWORD_STRING_LTH + 1]; -}; - - -extern struct Keyword asKeywordList[MAX_KEYWORD_NR]; -extern struct Token asToken[MAX_TOKEN_NR]; -extern unsigned char ucTokenNr; - -void DecodeMsg(char *pcString);
--- a/other/string.cpp Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -#include "string.h" - -void CopyString(char pcSource[], char pcDestination[]){ - - unsigned char ucCharacterCounter; - - for (ucCharacterCounter=0;(pcSource[ucCharacterCounter]!=NULL);ucCharacterCounter++){ - pcDestination[ucCharacterCounter]=pcSource[ucCharacterCounter]; - } - pcDestination[ucCharacterCounter]=pcSource[ucCharacterCounter]; -} - -enum CompResult eCompareString(char pcStr1[], char pcStr2[]){ - - unsigned char ucCharacterCounter; - - for(ucCharacterCounter=0;(pcStr1[ucCharacterCounter]!=NULL)&&(pcStr2[ucCharacterCounter]!=NULL);ucCharacterCounter++){ - if(pcStr1[ucCharacterCounter]!=pcStr2[ucCharacterCounter]){ - return NOTEQUAL; - } - } - if(pcStr1[ucCharacterCounter]!=pcStr2[ucCharacterCounter]){ - return NOTEQUAL; - } - return EQUAL; -} - -void AppendString (char pcSourceStr[], char pcDestinationStr[]){ - - unsigned char ucCharacterCounter; - - for (ucCharacterCounter=0;pcDestinationStr[ucCharacterCounter]!=NULL;ucCharacterCounter++){} - CopyString(pcSourceStr,pcDestinationStr+ucCharacterCounter); -} - -void ReplaceCharactersInString(char pcString[], char cOldChar,char cNewChar){ - - unsigned char ucCharacterCounter; - - for (ucCharacterCounter=0;pcString[ucCharacterCounter]!=NULL;ucCharacterCounter ++){ - if(pcString[ucCharacterCounter]==cOldChar){ - pcString[ucCharacterCounter]=cNewChar; - } - } -} - -void UIntToHexStr (unsigned int uiValue, char pcStr[]){ - - unsigned char ucBierzacaTetrada; - unsigned char ucLicznikTetrad; - - pcStr[0] = '0'; - pcStr[1] = 'x'; - pcStr[6] = NULL; - - for(ucLicznikTetrad = 0; ucLicznikTetrad < 4; ucLicznikTetrad++){ - ucBierzacaTetrada = (uiValue >> (ucLicznikTetrad*4)) & 0xF; - if(ucBierzacaTetrada < 10){ - pcStr[5 - ucLicznikTetrad] = '0' + ucBierzacaTetrada; - } - else{ - pcStr[5 - ucLicznikTetrad] = 'A' + ucBierzacaTetrada - 10; - } - } -} - -enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue){ - - char cZnakBiezacy; - unsigned char ucLicznikZnaku; - *puiValue=0; - - if((pcStr[0] != '0')||(pcStr[1] != 'x')||(pcStr[2] == NULL)){ - return (ERROR); - } - for(ucLicznikZnaku=2; pcStr[ucLicznikZnaku]!=NULL; ucLicznikZnaku++){ - cZnakBiezacy = pcStr[ucLicznikZnaku]; - if (ucLicznikZnaku>=6){ - return (ERROR); - } - *puiValue= *puiValue<<4; - if((cZnakBiezacy>='0')&&(cZnakBiezacy<='9')){ - *puiValue= *puiValue + (cZnakBiezacy-'0'); - }else if((cZnakBiezacy>='A')&&(cZnakBiezacy<='F')){ - *puiValue= *puiValue + (cZnakBiezacy-'A'+10); - }else{ - return (ERROR); - } - } - return (OK); -} - -void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]){ - - unsigned char ucLicznikZnakow; - - for (ucLicznikZnakow=0; pcDestinationStr[ucLicznikZnakow]!=NULL; ucLicznikZnakow++){} - UIntToHexStr(uiValue, pcDestinationStr+ucLicznikZnakow); -}
--- a/other/string.h Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#define NULL 0 -#define TERMINATOR '\r' -#define DELIMITER_CHAR 0x20 - -enum CompResult { NOTEQUAL, EQUAL }; -enum Result { OK, ERROR }; - -void CopyString(char pcSource[], char pcDestination[]); -void AppendString (char pcSourceStr[], char pcDestinationStr[]); -void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]); -void ReplaceCharactersInString(char pcString[], char cOldChar,char cNewChar); -void UIntToHexStr (unsigned int uiValue, char pcStr[]); -enum CompResult eCompareString(char pcStr1[], char pcStr2[]); -enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serwo.cpp Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,92 @@ +//#include <LPC21xx.H> +#include "serwo.h" +#include "led.h" +#include "mbed.h" +//#include "timer_interrupts.h" + +#define DETECTOR_bm (1<<10) +DigitalIn det(PA_0); +Ticker SerwoTim; + + + + +enum ServoState {CALLIB, IDDLE, IN_PROGRESS}; +struct Servo +{ +enum ServoState eState; +unsigned int uiCurrentPosition; +unsigned int uiDesiredPosition; +}; +struct Servo sServo; + +enum State {ACTIVE, INACTIVE}; +void DetectorInit(void){ + + //IO0DIR=IO0DIR&(~DETECTOR_bm); +} + +enum State eReadDetector (void){ + + //if((IO0PIN&DETECTOR_bm) == 0){ + if(det){ + return(INACTIVE); + }else{ + return(ACTIVE); + } +} + +void Servo_Callib(void){ + + sServo.eState = CALLIB; + while(eReadDetector()==INACTIVE); +} + +void Servo_GoTo(unsigned int uiPosition){ + + sServo.eState = IN_PROGRESS; + sServo.uiDesiredPosition = uiPosition; +} + +void SerwoAutomat(){ + + switch(sServo.eState){ + case IDDLE: + if(sServo.uiCurrentPosition != sServo.uiDesiredPosition){ + sServo.eState = IN_PROGRESS; + }else{ + sServo.eState = IDDLE; + } + break; + case IN_PROGRESS: + if(sServo.uiCurrentPosition > sServo.uiDesiredPosition){ + Led_StepLeft(); + sServo.eState = IN_PROGRESS; + sServo.uiCurrentPosition--; + }else if(sServo.uiCurrentPosition < sServo.uiDesiredPosition){ + sServo.eState = IN_PROGRESS; + Led_StepRight(); + sServo.uiCurrentPosition++; + }else{ + sServo.eState = IDDLE; + } + break; + case CALLIB: + if(eReadDetector()==INACTIVE){ + Led_StepRight(); + }else{ + sServo.eState = IDDLE; + sServo.uiCurrentPosition = 0; + sServo.uiDesiredPosition = 0; + } + break; + } +} +void Servo_Init(unsigned int uiServoFrequency){ + + //Timer0Interrupts_Init(1000000/uiServoFrequency, &SerwoAutomat); + SerwoTim.attach(&SerwoAutomat,float(1/float(uiServoFrequency))); + LedInt(); + DetectorInit(); + Servo_Callib(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serwo.h Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,5 @@ + + +void Servo_Init(unsigned int uiServoFrequency); +void Servo_Callib(void); +void Servo_GoTo(unsigned int uiPosition);
--- a/serwo.lib Fri May 05 19:54:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/Robsonik16/code/serwo/#a278d67d3ce8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/string.cpp Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,99 @@ +#include "string.h" + +void CopyString(char pcSource[], char pcDestination[]){ + + unsigned char ucCharacterCounter; + + for (ucCharacterCounter=0;(pcSource[ucCharacterCounter]!=NULL);ucCharacterCounter++){ + pcDestination[ucCharacterCounter]=pcSource[ucCharacterCounter]; + } + pcDestination[ucCharacterCounter]=pcSource[ucCharacterCounter]; +} + +enum CompResult eCompareString(char pcStr1[], char pcStr2[]){ + + unsigned char ucCharacterCounter; + + for(ucCharacterCounter=0;(pcStr1[ucCharacterCounter]!=NULL)&&(pcStr2[ucCharacterCounter]!=NULL);ucCharacterCounter++){ + if(pcStr1[ucCharacterCounter]!=pcStr2[ucCharacterCounter]){ + return NOTEQUAL; + } + } + if(pcStr1[ucCharacterCounter]!=pcStr2[ucCharacterCounter]){ + return NOTEQUAL; + } + return EQUAL; +} + +void AppendString (char pcSourceStr[], char pcDestinationStr[]){ + + unsigned char ucCharacterCounter; + + for (ucCharacterCounter=0;pcDestinationStr[ucCharacterCounter]!=NULL;ucCharacterCounter++){} + CopyString(pcSourceStr,pcDestinationStr+ucCharacterCounter); +} + +void ReplaceCharactersInString(char pcString[], char cOldChar,char cNewChar){ + + unsigned char ucCharacterCounter; + + for (ucCharacterCounter=0;pcString[ucCharacterCounter]!=NULL;ucCharacterCounter ++){ + if(pcString[ucCharacterCounter]==cOldChar){ + pcString[ucCharacterCounter]=cNewChar; + } + } +} + +void UIntToHexStr (unsigned int uiValue, char pcStr[]){ + + unsigned char ucBierzacaTetrada; + unsigned char ucLicznikTetrad; + + pcStr[0] = '0'; + pcStr[1] = 'x'; + pcStr[6] = NULL; + + for(ucLicznikTetrad = 0; ucLicznikTetrad < 4; ucLicznikTetrad++){ + ucBierzacaTetrada = (uiValue >> (ucLicznikTetrad*4)) & 0xF; + if(ucBierzacaTetrada < 10){ + pcStr[5 - ucLicznikTetrad] = '0' + ucBierzacaTetrada; + } + else{ + pcStr[5 - ucLicznikTetrad] = 'A' + ucBierzacaTetrada - 10; + } + } +} + +enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue){ + + char cZnakBiezacy; + unsigned char ucLicznikZnaku; + *puiValue=0; + + if((pcStr[0] != '0')||(pcStr[1] != 'x')||(pcStr[2] == NULL)){ + return (ERROR); + } + for(ucLicznikZnaku=2; pcStr[ucLicznikZnaku]!=NULL; ucLicznikZnaku++){ + cZnakBiezacy = pcStr[ucLicznikZnaku]; + if (ucLicznikZnaku>=6){ + return (ERROR); + } + *puiValue= *puiValue<<4; + if((cZnakBiezacy>='0')&&(cZnakBiezacy<='9')){ + *puiValue= *puiValue + (cZnakBiezacy-'0'); + }else if((cZnakBiezacy>='A')&&(cZnakBiezacy<='F')){ + *puiValue= *puiValue + (cZnakBiezacy-'A'+10); + }else{ + return (ERROR); + } + } + return (OK); +} + +void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]){ + + unsigned char ucLicznikZnakow; + + for (ucLicznikZnakow=0; pcDestinationStr[ucLicznikZnakow]!=NULL; ucLicznikZnakow++){} + UIntToHexStr(uiValue, pcDestinationStr+ucLicznikZnakow); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/string.h Fri May 05 20:01:41 2017 +0000 @@ -0,0 +1,14 @@ +#define NULL 0 +#define TERMINATOR '\r' +#define DELIMITER_CHAR 0x20 + +enum CompResult { NOTEQUAL, EQUAL }; +enum Result { OK, ERROR }; + +void CopyString(char pcSource[], char pcDestination[]); +void AppendString (char pcSourceStr[], char pcDestinationStr[]); +void AppendUIntToString (unsigned int uiValue, char pcDestinationStr[]); +void ReplaceCharactersInString(char pcString[], char cOldChar,char cNewChar); +void UIntToHexStr (unsigned int uiValue, char pcStr[]); +enum CompResult eCompareString(char pcStr1[], char pcStr2[]); +enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue);