mbed5b

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Robsonik16
Date:
Fri May 05 20:00:16 2017 +0000
Commit message:
a

Changed in this revision

command_decoder.cpp Show annotated file Show diff for this revision Revisions of this file
command_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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
serwo.cpp Show annotated file Show diff for this revision Revisions of this file
serwo.h Show annotated file Show diff for this revision Revisions of this file
string.cpp Show annotated file Show diff for this revision Revisions of this file
string.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command_decoder.cpp	Fri May 05 20:00:16 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:00:16 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:00:16 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:00:16 2017 +0000
@@ -0,0 +1,4 @@
+void LedInt(void);
+void Led_StepLeft(void);
+void Led_StepRight(void);
+void LedOn(unsigned char ucLedIndeks);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 05 20:00:16 2017 +0000
@@ -0,0 +1,244 @@
+#include "mbed.h"
+//#include "servo.h"
+//#include "command_decoder.h"
+#include "led.h"
+#include "serwo.h"
+#include "command_decoder.h"
+
+
+
+//#define DETECTOR_bm (PA4)
+
+
+//DigitalIn det(PA_5);
+//Ticker serwoTim;
+
+
+
+
+//************************************************************************/
+
+/******************************************************************/
+//------------------------------------
+// Hyperterminal configuration
+// 9600 bauds, 8-bit data, no parity
+//------------------------------------
+
+RawSerial pc(USBTX,USBRX,9600);
+
+DigitalOut led_green(LED1);
+DigitalOut led_red(LED2);
+
+
+
+Timeout LedTimeout;
+Ticker Sender;
+
+void LedOff()
+{
+    led_red=0;
+}
+
+int my_puts(char tab[],int size)
+{
+//retutn 0 if string is valid
+    bool StringIsValid=false;
+    for( int index=0; index<size; index++) {
+        if (tab[index]==NULL) {
+            StringIsValid = true;
+            break;
+        }
+    }
+    if(StringIsValid == false) {
+        LedTimeout.attach(&LedOff, 0.5);
+        led_red=1;
+        return(1);
+    }
+
+    for( int index=0; index<size; index++) {
+        if (tab[index]==NULL) {
+            pc.putc('\r');
+            while(!pc.writeable());
+            pc.putc('\n');
+            return(0);
+        }
+        while(!pc.writeable());
+        pc.putc(tab[index]);
+    }
+    return(0);
+}
+
+void hardbeatSend()
+{
+    static int count=0;
+    char tekst[25];
+    sprintf(tekst,"hardbeat number: %d \r\n", count );
+    //pc.printf("This program runs since %d seconds.\r\n", i);
+    my_puts(tekst,22);
+    count++;
+}
+
+char my_gets(char str[],int size)
+{
+    if (!pc.readable())return (NULL);
+    char c;
+    for(int index=0; index <size; index++) {
+        c=pc.getc();
+        pc.putc(c);
+        if ((c=='\r')||(c=='\n')) {
+            pc.putc('\r');
+            pc.putc('\n');
+            str[index]=NULL;
+            return (1);
+        }
+        str[index]=c;
+    }
+    LedTimeout.attach(&LedOff, 5);
+    led_red=1;
+    return (1);
+}
+
+
+
+
+
+
+int main (){
+
+    
+    unsigned int uiPreviusPos;
+    //ButtonInit(); 
+    //Servo_Init(500);
+    char cOdebranyString[20];
+    char init[] = "system start\r\n";
+    const char tekstSize = 20;
+    my_puts(init,tekstSize);
+    char tekst5[20];
+    
+    
+      int liczba=3;
+    char str[] = "ab234cid*(s349*(20kd"; 
+    char str2[]="-0123456789";
+   
+    
+
+  
+/*
+     sprintf(tekst5,"liczba: %d ", liczba );
+     my_puts(tekst5,10);
+     my_puts(str+liczba,20);
+     //long strtol( const char * start, char ** end, int base );
+         sprintf(tekst5,"caly string: %d ", atoi(str) );
+     my_puts(tekst5,20);
+     */
+     char * pZnak = strpbrk(str, str2);
+     my_puts(pZnak,20);
+     sprintf(tekst5,"string + li: %d ", atoi(pZnak) );
+     my_puts(tekst5,20);
+     
+     
+     
+     
+     Servo_Init(100);
+      //Servo_Callib();
+    
+    while(1){
+        
+
+         
+        if (my_gets(cOdebranyString,tekstSize)!=NULL) {
+            if(strstr( cOdebranyString, "callib" )){Servo_Callib();}
+            if(strstr( cOdebranyString, "id" )){my_puts("ID:0x1234",15);}
+            if(strstr( cOdebranyString, "goto" )){
+                    char * pZnak = strpbrk(cOdebranyString, str2);
+                    uiPreviusPos = atoi(pZnak) ;
+                    Servo_GoTo(uiPreviusPos);
+                    my_puts(pZnak,20);
+                }
+            if(strstr( cOdebranyString, "step" )){
+                    char * pZnak = strpbrk(cOdebranyString, str2);
+                    uiPreviusPos = uiPreviusPos+ atoi( pZnak );
+                    Servo_GoTo(uiPreviusPos);
+                    my_puts(pZnak,20);
+                }
+        }
+        
+
+        
+        /*
+        if (my_gets(cOdebranyString,tekstSize)!=NULL) {
+        //if(eReciever_GetStatus() == READY){
+            
+            //Reciever_GetStringCopy( cOdebranyString );
+            DecodeMsg( cOdebranyString );
+            if ((ucTokenNr > 0) && (asToken[0].eType == KEYWORD)){
+                switch (asToken[0].uValue.eKeyword){
+                    case CALLIB:
+                        Servo_Callib();
+                    break;
+                    case GOTO:
+                        if (asToken[1].eType == NUMBER){
+                            Servo_GoTo(asToken[1].uValue.uiNumber);
+                            uiPreviusPos = asToken[1].uValue.uiNumber;
+                        }
+                    break;
+                    case STEP:
+                        if (asToken[1].eType == NUMBER){
+                            Servo_GoTo(uiPreviusPos + asToken[1].uValue.uiNumber);
+                            uiPreviusPos = uiPreviusPos + asToken[1].uValue.uiNumber;
+                        }
+                    break;
+                    case ID:
+                        my_puts("ID:0x1234",15);
+                    break;
+                    default:
+                    break;
+                }
+            }
+        }
+        */
+    }
+}
+
+
+
+
+
+
+
+
+/*
+int main()
+{
+
+
+    bool flag=false;
+
+    const char tekstSize = 7;
+    char tekst[tekstSize];
+    //my_puts("[2J",5);//clear console
+    //pc.putc('[2J');
+    pc.putc(27);
+    char init[] = "system start\r\n";
+    my_puts(init,tekstSize);
+    Sender.attach(&hardbeatSend,1);
+
+
+
+    while(1) {
+
+
+
+        if (my_gets(tekst,tekstSize)!=NULL) {
+            
+            my_puts(tekst,tekstSize);
+            if(strcmp(tekst,"on"))flag = true;
+            if(strcmp(tekst,"off"))flag = false;
+            if(strcmp(tekst,"toggle"))flag = !flag;
+            led_green =flag;
+        }
+
+    }
+}
+*/
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 05 20:00:16 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serwo.cpp	Fri May 05 20:00:16 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:00:16 2017 +0000
@@ -0,0 +1,5 @@
+
+
+void Servo_Init(unsigned int uiServoFrequency);
+void Servo_Callib(void);
+void Servo_GoTo(unsigned int uiPosition);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/string.cpp	Fri May 05 20:00:16 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:00:16 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);