Adrian Nowak
/
__M2__5b____copy
ll
Revision 0:a8e3f4b25a52, committed 2017-05-09
- Comitter:
- adrianow795
- Date:
- Tue May 09 11:59:23 2017 +0000
- Commit message:
- siema;
Changed in this revision
diff -r 000000000000 -r a8e3f4b25a52 command_decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.cpp Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,111 @@ +#include "command_decoder.h" +#include "string.h" + +#define NULL 0 + +struct Token asToken[MAX_TOKEN_NR]; + +struct Keyword asKeywordList[MAX_KEYWORD_NR]={ +{ID, "id"}, +{CALLIB, "callib"}, +{GOTO, "goto"}, +{STEP, "step"} +}; + +unsigned char ucTokenNr; + +enum Stan{TOKEN, DELIMITER}; + +unsigned char ucFindTokensInString(char *pcString){ + + unsigned char ucCharacterCounter; + unsigned char ucTokenNr=0; + unsigned char ucCurrentCharacter; + enum Stan eStan=DELIMITER; + + + for(ucCharacterCounter=0;;ucCharacterCounter++){ + ucCurrentCharacter=pcString[ucCharacterCounter]; + + switch(eStan){ + case TOKEN: + { + if(ucTokenNr==MAX_TOKEN_NR){ + return ucTokenNr; + } + else if(ucCurrentCharacter==NULL){ + return ucTokenNr; + } + else if(ucCurrentCharacter!=' '){ + eStan=TOKEN; + } + else{ + eStan=DELIMITER; + } + break; + } + case DELIMITER: + { + if(ucCurrentCharacter==NULL){ + return ucTokenNr; + } + else if(ucCurrentCharacter==' '){ + eStan=DELIMITER; + } + else{ + eStan=TOKEN; + asToken[ucTokenNr].uValue.pcString=pcString+ucCharacterCounter; + ucTokenNr++; + } + break; + } + } + } +} + +enum Result {OK, ERROR}; + +enum Result eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){ + + unsigned char ucKeywordCounter; + + for(ucKeywordCounter=0; ucKeywordCounter< MAX_KEYWORD_NR; ucKeywordCounter++){ + if(EQUAL == eCompareString(asKeywordList[ucKeywordCounter].cString, pcStr)){ + *peKeywordCode= asKeywordList[ucKeywordCounter].eCode; + return OK; + } + } + return ERROR; +} + +void DecodeTokens(void){ + + unsigned char ucTokenCounter; + struct Token *psCurrentToken; + unsigned int uiTokenValue; + enum KeywordCode eTokenCode; + + for(ucTokenCounter= 0; ucTokenCounter< ucTokenNr; ucTokenCounter++){ + psCurrentToken= &asToken[ucTokenCounter]; + + if(OK== eHexStringToUInt(psCurrentToken->uValue.pcString, &uiTokenValue)){ + 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 DecodeMsg(char *pcString){ + + ucTokenNr= ucFindTokensInString(pcString); + ReplaceCharactersInString(pcString, ' ', NULL); + DecodeTokens(); +} +
diff -r 000000000000 -r a8e3f4b25a52 command_decoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.h Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,32 @@ +#ifndef COMMAND_DECODER_H +#define COMMAND_DECODER_H + +#define MAX_TOKEN_NR 3 +#define MAX_KEYWORD_STRING_LTH 10 +#define MAX_KEYWORD_NR 4 + +enum KeywordCode {ID, CALLIB, GOTO, STEP}; + +union TokenValue{ +enum KeywordCode eKeyword; +unsigned int uiNumber; +char* pcString; +}; + +enum TokenType {KEYWORD, NUMBER, STRING}; + +struct Token{ +enum TokenType eType; +union TokenValue uValue; +}; + + +struct Keyword{ +enum KeywordCode eCode; +char cString[MAX_KEYWORD_STRING_LTH + 1]; +}; + + +void DecodeMsg(char *pcString); + +#endif \ No newline at end of file
diff -r 000000000000 -r a8e3f4b25a52 led.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.cpp Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,64 @@ +#include "mbed.h" +#include "led.h" + +DigitalOut pin_0(PG_2); +DigitalOut pin_1(PG_3); +DigitalOut pin_2(PG_4); +DigitalOut pin_3(PG_5); + +enum StepDirection {Left, Right}; + +void Led_Init(){ + pin_0=1; + pin_1=0; + pin_2=0; + pin_3=0; + +} + +void Led_On(unsigned char ucLedIndeks){ + pin_0=0; + pin_1=0; + pin_2=0; + pin_3=0; + switch(ucLedIndeks){ + case 0: + pin_0=1; + break; + case 1: + pin_1=1; + break; + case 2: + pin_2=1; + break; + case 3: + pin_3=1; + break; + default: + break; + } +} + + +void Led_Step(enum StepDirection Direction){ + static unsigned int uiPozycjaPunktu; + switch(Direction){ + case Left: + uiPozycjaPunktu = (++uiPozycjaPunktu) % 4; + break; + case Right: + uiPozycjaPunktu = (--uiPozycjaPunktu) % 4; + break; + default: + break; + } + Led_On(uiPozycjaPunktu); +} + +void Led_StepLeft(){ + Led_Step(Left); +} + +void Led_StepRight(){ + Led_Step(Right); +}
diff -r 000000000000 -r a8e3f4b25a52 led.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.h Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,8 @@ +#ifndef LED_H +#define LED_H + +void Led_Init(void); +void Led_StepLeft(void); +void Led_StepRight(void); + +#endif \ No newline at end of file
diff -r 000000000000 -r a8e3f4b25a52 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,212 @@ +#include "mbed.h" +#include "string.h" +#include "command_decoder.h" +#include "servo.h" + +DigitalOut led(LED1); +DigitalOut led2(LED2); + +Serial pc(USBTX, USBRX); + +//================================================ +// PUT_STRING +//================================================ + +int puts( char * tab , int size ) +{ + int ret_val; + for ( int i = 0; i < size; i++) + { + if(tab[i] == NULL) + { + ret_val = 0; + tab[i] = '\r'; + break; + } + else + { + ret_val = 1; + } + } + + if(ret_val == 0) + { + while(*tab != '\r') + { + if(pc.writeable()) + { + pc.putc((int)(*tab)); + tab++; + } + } + pc.putc((int)(*tab)); + } + else + { + //empty else + } + + + return ret_val; +} + +//================================================ +// GET_STRING +//================================================ + +int gets( char * tab , int size ) +{ + int Counter = 0, ret_val = 0; + while((*tab = pc.getc()) != '\r') + { + if(Counter < size) + { + + } + else + { + ret_val = 1; + } + Counter++; + tab++; + } + + *tab = NULL; + + if(ret_val) + while(pc.readable()) + { + pc.getc(); + } + + return ret_val; +} + +//================================================ +// INT_TO_CHAR +//================================================ + +void IntToChar ( int number, char * wynik) +{ + // zakres do tysiecy + int temp = 0; + + for (int i = 3 ; i >= 0; i--) + { + temp = number % 10; + wynik [i] = 48 + temp; + number = number - temp; + number = number / 10; + } +} + +//================================================ +// COMPARE_STRINGS +//================================================ + +int compstr( char * a, char * b) +{ + + while(*b != NULL ) + if(*a == *b) + { + a++; + b++; + } + else + return 0; + return 1; +} + +//================================================ +// ToLower +//================================================ + +void ToLower(char * chain, int size) +{ + for(int i = 0; i < size; i++) + { + if(chain[i] >= 65 && chain[i] <= 90) + { + chain[i] = chain[i] + 32; + } + else + { + //empty else + } + } +} + +//================================================ +// ClearTab +//================================================ + +void ClearTab(char * tab, int size) +{ + for(int i = 0; i < size; i++) + tab[i] = NULL; +} + +void FillTab(char * tab, const char * data, int size) +{ + ClearTab(tab, size); + while(*data != NULL) + { + *tab = *data; + data++; + tab++; + } + *tab = *data; + +} + +extern unsigned char ucTokenNr; +extern struct Token asToken[MAX_TOKEN_NR]; + + +int main() +{ + + char Receive [24]; + Servo_Init(10); + while(1) { + + if( gets(Receive,24) == 0 ) + { + DecodeMsg(Receive); + + { + switch(asToken[0].uValue.eKeyword){ + case ID: + FillTab(Receive, "id 3234", 24); + puts(Receive,24); + break; + case CALLIB: + Servo_Callib(); + FillTab(Receive, "callib_ok", 24); + puts(Receive,24); + led = !led; + break; + case GOTO: + Servo_GoTo(asToken[1].uValue.uiNumber); + FillTab(Receive, "goto_ok", 24); + puts(Receive,24); + led = !led; + break; + case STEP: + Servo_Step(asToken[1].uValue.uiNumber); + FillTab(Receive, "step_ok", 24); + puts(Receive,24); + led = !led; + break; + default: + FillTab(Receive, "unknowncommend", 24); + puts(Receive,24); + break; + } + } + } + } +} + +
diff -r 000000000000 -r a8e3f4b25a52 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file
diff -r 000000000000 -r a8e3f4b25a52 servo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/servo.cpp Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,88 @@ +#include "servo.h" +#include "led.h" +#include "mbed.h" + +Ticker timer; +DigitalIn detector(PG_6); + +enum eStan {ACTIVE, INACTIVE}; + +enum eStan eReadDetector(){ + if(1==detector){ + return INACTIVE; + } + else{ + return ACTIVE; + } +} + +enum ServoState {CALLIB, IDDLE, IN_PROGRESS}; + +struct Servo +{ + enum ServoState eState; + unsigned int uiCurrentPosition; + unsigned int uiDesiredPosition; +}; + +struct Servo sServo; + +void Automat(){ + switch(sServo.eState){ + case CALLIB: + if(eReadDetector()==INACTIVE){ + Led_StepLeft(); + sServo.eState=CALLIB; + } + else{ + sServo.uiCurrentPosition=0; + sServo.uiDesiredPosition=0; + sServo.eState=IDDLE; + } + break; + case IDDLE: + if(sServo.uiCurrentPosition != sServo.uiDesiredPosition){ + sServo.eState=IN_PROGRESS; + } + else{ + sServo.eState=IDDLE; + } + break; + case IN_PROGRESS: + if(sServo.uiCurrentPosition == sServo.uiDesiredPosition){ + sServo.eState=IDDLE; + } + else if(sServo.uiCurrentPosition < sServo.uiDesiredPosition){ + Led_StepRight(); + sServo.uiCurrentPosition++; + sServo.eState=IN_PROGRESS; + } + else { + Led_StepLeft(); + sServo.uiCurrentPosition--; + sServo.eState=IN_PROGRESS; + } + break; + } +} + +void Servo_Init(unsigned int uiServoFrequency){ + float fServoFrequency; + const float ucConstOne=1; + fServoFrequency= ucConstOne / uiServoFrequency; + Led_Init(); + sServo.eState=CALLIB; + timer.attach(&Automat,fServoFrequency); +} + +void Servo_Callib(void){ + sServo.eState=CALLIB; +} + +void Servo_GoTo(unsigned int uiPosition){ + sServo.uiDesiredPosition=uiPosition; +} + +void Servo_Step(unsigned int uiStep){ + sServo.uiDesiredPosition=sServo.uiCurrentPosition+uiStep; +}
diff -r 000000000000 -r a8e3f4b25a52 servo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/servo.h Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,10 @@ +#ifndef SERVO_H +#define SERVO_H + +void Servo_Init(unsigned int uiServoFrequency); +void Servo_Callib(void); +void Servo_GoTo(unsigned int uiPosition); +void Servo_Step(unsigned int uiStep); + + +#endif \ No newline at end of file
diff -r 000000000000 -r a8e3f4b25a52 string.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/string.cpp Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,103 @@ +#include "string.h" +#include <stdio.h> +#include <string.h> + +#define NULL 0 + +void CopyString(char pcSource[],char pcDestination[]) { + + strcpy (pcDestination, pcSource); +} + +enum CompResult eCompareString(char pcStr1[],char pcStr2[]) { + + if(!strcmp(pcStr1, pcStr2)) + return EQUAL; + else + return DIFFERENT; +} + +void AppendString(char pcSourceStr[],char pcDestinationStr[]) { + + unsigned char ucLicznikZnaku; + + for(ucLicznikZnaku=0;pcDestinationStr[ucLicznikZnaku]!=NULL;ucLicznikZnaku++) {} + CopyString(pcSourceStr,pcDestinationStr+ucLicznikZnaku); +} + +void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){ + + unsigned char ucLicznikZnaku; + + for(ucLicznikZnaku=0;pcString[ucLicznikZnaku]!=NULL;ucLicznikZnaku++) { + if(cOldChar==pcString[ucLicznikZnaku]){ + pcString[ucLicznikZnaku]=cNewChar; + } + } +} + +///////////// + +void UIntToHexStr(unsigned int uiValue, char pcStr[]){ + + unsigned char ucNibbleCounter; + unsigned char ucCurrentNibble; + + pcStr[0]='0'; + pcStr[1]='x'; + pcStr[6]=NULL; + + for(ucNibbleCounter=0; ucNibbleCounter<4; ucNibbleCounter++){ + ucCurrentNibble=(uiValue >> (ucNibbleCounter*4))&0x0F; + if(ucCurrentNibble<10){ + pcStr[5-ucNibbleCounter] = '0'+ucCurrentNibble; + } + else{ + pcStr[5-ucNibbleCounter] = 'A'+(ucCurrentNibble-10); + } + } +} + + +enum Result {OK, ERROR}; + +enum Result eHexStringToUInt(char pcStr[], unsigned int *puiValue){ + + unsigned char ucCharacterCounter; + unsigned char ucCurrentCharacter; + + *puiValue=0; + + if((pcStr[0]!='0') || (pcStr[1]!='x') || (pcStr[2]==NULL)){ + return ERROR; + } + for(ucCharacterCounter=2; ucCharacterCounter<7; ucCharacterCounter++){ + ucCurrentCharacter = pcStr[ucCharacterCounter]; + if(ucCurrentCharacter==NULL){ + return OK; + } + else if(ucCharacterCounter==6){ + return ERROR; + } + *puiValue = *puiValue << 4; + if((ucCurrentCharacter <= '9') && (ucCurrentCharacter >= '0')){ + ucCurrentCharacter = ucCurrentCharacter-'0'; + } + else if((ucCurrentCharacter <= 'F') && (ucCurrentCharacter >= 'A')){ + ucCurrentCharacter = ucCurrentCharacter-'A'+10; + } + else{ + return ERROR; + } + *puiValue = (*puiValue) | ucCurrentCharacter; + } + return ERROR; +} + +void AppendUIntToString(unsigned int uiValue, char pcDestinationStr[]){ + + unsigned char ucIndeksKonca; + + for(ucIndeksKonca=0; pcDestinationStr[ucIndeksKonca]!=NULL; ucIndeksKonca++){} + UIntToHexStr(uiValue, &pcDestinationStr[ucIndeksKonca]); +}
diff -r 000000000000 -r a8e3f4b25a52 string.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/string.h Tue May 09 11:59:23 2017 +0000 @@ -0,0 +1,11 @@ +#ifndef STRING_H +#define STRING_H + +enum CompResult {DIFFERENT,EQUAL}; + +enum CompResult eCompareString(char pcStr1[],char pcStr2[]); +void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar); +void UIntToHexStr(unsigned int uiValue, char pcStr[]); +enum Result eHexStringToUInt(char pcStr[], unsigned int *puiValue); + +#endif \ No newline at end of file