Kart+xxxxx // Radar_RTOS

Dependencies:   Radar_RTOS mbed

Fork of Radar_RTOS by Contest IOT GSE5

Revision:
2:97cbf2a5ec78
Child:
3:422918db0559
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UART_Radar.cpp	Wed Dec 02 16:09:22 2015 +0000
@@ -0,0 +1,100 @@
+#include "UART_Radar.h"
+#include "mbed.h"
+#include <string.h>
+Serial carte(PA_9, PA_10); //emission --- tx, rx
+Serial pc(USBTX, USBRX); //reception --- tx, rx
+
+void USART1_puts(char *text){
+    while(*text != '\0'){
+        
+        if(carte.writeable()){ 
+            carte.putc(*text);
+            text++;
+        }
+        
+    }
+}
+        
+
+void format(char *msg, char *msg_formated){
+    int somme=5;
+    int i=0;
+    msg_formated[0]=2;
+    printf("\n\r");
+    while(msg[i]!=0){
+        somme= somme + msg[i];
+        msg_formated[i+1]=msg[i];
+        i++;
+    }
+    if(somme>255){
+            somme++;
+    }
+    somme=somme%256;
+    msg_formated[i+1]=~somme;
+    msg_formated[i+2]=3;
+    msg_formated[i+3]=0;
+
+   //A supprimer
+    i=0;
+    while(msg_formated[i]!=0){
+        printf("[%d] ", msg_formated[i]);
+        i++;
+    }
+}
+
+
+void send(char *data){
+        char trame[52]="";
+        format(data, trame);
+        USART1_puts(trame);
+}
+
+int receive(char *frame){
+    int I;
+    unsigned int sommeBuffer;
+    char r;
+    Timer timeout;
+    timeout.start();
+    while(1){
+        if(timeout.read_ms() > 100) return 1;
+        if(carte.readable()){
+            r = carte.getc();
+            
+            switch(r){
+                case 2: //Start
+                    I=0;
+                    sommeBuffer=2;
+                    break;
+                    
+                case 3: //Stop
+                    frame[I]=0;
+                    sommeBuffer+=3;
+                    
+                    if(sommeBuffer>255){
+                        sommeBuffer++;
+                    }
+                    sommeBuffer=~sommeBuffer;
+                    sommeBuffer=sommeBuffer%256;
+                
+                    if(sommeBuffer==0){ //Les sommes concordent, les données sont valides
+                        frame[I-1]=0;
+                        return 0;
+                    }else{ //Message invalidé par la somme
+                        frame[0]=0;
+                        return 1;
+                    }
+                    
+                default : 
+                    frame[I]=r;
+                    sommeBuffer+=r;        
+                    I++;
+                    break;
+                }
+            }
+        }
+}
+
+int controle(char *frame){
+    if (*frame!=6) return 0;
+    else return 1;  
+}
\ No newline at end of file