APP4

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

Revision:
16:a163a7c0315b
Parent:
15:872151771dec
Child:
17:53d4674cb37f
--- a/main.cpp	Mon Oct 22 21:16:22 2018 +0000
+++ b/main.cpp	Tue Oct 23 00:56:17 2018 +0000
@@ -22,7 +22,7 @@
     bool front;
 } message_t;
 
-Mail<message_t,16> mail_box;
+Mail<message_t,64> mail_box;
 Timer timer;
 int etat;
 
@@ -35,6 +35,19 @@
                         0,0,1,0,1,0,0,0,
                         1,0,1,0,1,1,0,0};
 
+//CRC16 (détection des erreurs) ----------------------------------------------------------------------------------------------------
+unsigned short crc16(bool* data_p, int length){
+    unsigned char x;
+    unsigned short crc = 0xFFFF;
+
+    while (length--){
+        x = crc >> 8 ^ *data_p++;
+        x ^= x>>4;
+        crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
+    }
+    return crc;
+}
+
 //convert length from int to binary -----------------------------------------------------------------------------------------------------
 bool r;
 void int_to_bin(bool bin[8], int length){ //Fonctionne
@@ -117,9 +130,12 @@
         message++;        
     }
     //CRC16
+    unsigned short crc = crc16(messageUtile, (int)sizeof(messageUtile));
+    printf("/r/n -----------------%d-------------/r/n", crc);
+    int_to_bin(bin, crc);
     for(int i=0; i<8; i++){
-        *message=0;
-        message++;        
+        *message=bin[i];
+        message++;      
     }
     for(int i=0; i<8; i++){
         *message=START_END[i];
@@ -130,16 +146,15 @@
 //Envoi d'une trame ----------------------------------------------------------------------------------------------------------------
 void envoiTrame(){
     bool app=false;
+    int length3 = sizeof(messageUtile);
+    bool message3[length3+6*8];
+    bool manchester3[(length3+6*8)*2];
+    
+    creationTrame(message3,length3);
+    convertMessageToManchester(manchester3, message3,length3+6*8);
     while(1){
-        /*int length3 = sizeof(messageUtile);
-        bool message3[length3+6*8];
-        bool manchester3[(length3+6*8)*2];
-        
-        creationTrame(message3,length3);
-        convertMessageToManchester(manchester3, message3,length3+6*8);*/
-        
         if(pin12 && !app){
-            bool manchester3[16] = {0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0};
+            //bool manchester3[32] = {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0};
             app=true;
             for(int i=0; i<sizeof(manchester3); i++){
                 dout = manchester3[i];
@@ -152,81 +167,202 @@
     }
 }
 
-//CRC16 (détection des erreurs) ----------------------------------------------------------------------------------------------------
-unsigned short crc16(const unsigned char* data_p, unsigned char length){
-    unsigned char x;
-    unsigned short crc = 0xFFFF;
+
 
-    while (length--){
-        x = crc >> 8 ^ *data_p++;
-        x ^= x>>4;
-        crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
-    }
-    return crc;
-}
 
-bool preambule[8];
 //detection du préambule
 void detectionPreambule() {
     int compteur = 0;
     int prev_time;
     int t;
+    bool preambule[8];
+    bool start[8];
+    bool flag[8];
+    bool longueur[8];
+    bool message[64];
+    bool checksum[8];
+    bool end[8];
+    int longInt;
+    bool bin2[8];
     while(1){
         osEvent evt = mail_box.get();
         if (evt.status == osEventMail) {
             message_t *f = (message_t*)evt.value.p;
-            //printf("%d",*f);
             led3 = f->front;
             t = f->time;
-            if(f->front == 0){
-                if(etat==0){
+            if(etat==0) {
+                if(f->front == 0){
                     compteur=0;
                     timer.start();
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         preambule[compteur] = f->front;
+                        prev_time = t; 
+                        compteur++;
                         etat = 1;
+                        printf("-----ETAT 1-----\r\n");
                 }
-                else if(etat==2){
-                    if((t-prev_time)<=35 && (t-prev_time)>=25){
+            }
+            
+            else if(etat==1) {
+                if(f->front==1){
+                    if((t-prev_time)<=35 && (t-prev_time)>=20){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         preambule[compteur] = f->front;
+                        prev_time = t; 
+                        compteur++;
+                        etat = 2;
+                        printf("-----ETAT 2-----\r\n");
+                    }
+                    else{
+                        etat=0;
+                        printf("------FAIL------\r\n");    
                     }
                 }
             }
-            else if(f->front==1){
-                if(etat ==1){
-                    if((t-prev_time)<=35 && (t-prev_time)>=25){
+            
+            else if(etat==2) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
+                    printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
+                    preambule[compteur] = f->front;
+                    prev_time = t; 
+                    compteur++;
+                }
+            }
+            
+            else if(etat ==3) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
-                        preambule[compteur] = f->front;
-                        etat = 2;
-                    }
-                    else{
-                        etat=0;    
-                    }
+                        start[compteur-8] = f->front;
+                        prev_time = t; 
+                        compteur++;
                 }
-                else if(etat ==2){
-                    if((t-prev_time)<=35 && (t-prev_time)>=25){
+            }
+            
+            else if(etat ==4) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
+                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
+                        flag[compteur-16] = f->front;
+                        prev_time = t; 
+                        compteur++;
+                }
+            }
+            
+            else if(etat ==5) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
-                        preambule[compteur] = f->front;
-                    }
+                        longueur[compteur-24] = f->front;
+                        prev_time = t; 
+                        compteur++;
+                }
+            }
+            else if(etat ==6) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
+                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
+                        message[compteur-32]= f->front;
+                        prev_time = t; 
+                        compteur++;
                 }
-                
-            }
-            prev_time = t; 
+            } 
+            else if(etat ==7) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
+                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
+                        checksum[compteur-32-longInt]= f->front;
+                        prev_time = t; 
+                        compteur++;
+                }
+            } 
+            else if(etat ==8) {
+                if((t-prev_time)<=35 && (t-prev_time)>=20){
+                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
+                        end[compteur-40-longInt]= f->front;
+                        prev_time = t; 
+                        compteur++;
+                }
+            } 
             mail_box.free(f);
-            compteur++;
         }
-        printf("----------------- compteur: %d, temps: %d\r\n", compteur, t);
+        
         if(compteur ==8){
-            etat = 0;
-            for(int i=0; i<sizeof(preambule);i++){
-                if(i>0 && i%2 == 0){
-                   printf(" ");
+            for(int i=0; i<8;i++){
+                if(preambule[i]!= PREAMBULE[i])
+                {
+                    etat =0;
+                    printf("------FAIL------\r\n");
                 }
                 printf("%d", preambule[i]);    
             }
             printf("\r\n");
+            etat = 3;
+            printf("-----ETAT 3-----\r\n");
         }
+        else if(compteur ==16) {
+            for(int i=0; i<8;i++){
+                if(start[i]!= START_END[i])
+                {
+                    etat =0;
+                    printf("------FAIL------\r\n");
+                }
+                printf("%d", start[i]);    
+            }
+            printf("\r\n");
+            etat =4;
+            printf("-----ETAT 4-----\r\n");
+        }
+        else if(compteur==24) {
+            for(int i=0; i<8;i++){
+                printf("%d", flag[i]);    
+            }
+            printf("\r\n");
+            etat =5;
+            printf("-----ETAT 5 -----\r\n");
+        }
+        else if(compteur==32) {
+            longInt=0;
+            for(int i = 0; i<8; i++) {
+                longInt = longInt*2+longueur[i];    
+            }
+            printf("%d\r\n", longInt);
+            etat =6;
+            printf("-----ETAT 6 -----\r\n");
+        }
+        else if(compteur==32+longInt) {
+            for(int i=0; i<longInt;i++){
+                printf("%d", message[i]);    
+            }
+            printf("\r\n");
+            etat =7;
+            printf("-----ETAT 7-----\r\n");
+        }
+        
+        else if(compteur == 40 +longInt) {
+            unsigned short crc = crc16(message, (int)sizeof(message));
+            printf("/r/n -----------------%d-------------/r/n", crc);
+            int_to_bin(checksum, crc);
+            for(int i=0; i<8;i++){
+                printf("%d", checksum[i]);
+                /*if(bin[i] != checksum[i]) {
+                    etat =0;
+                    printf("------FAIL------\r\n");
+                }    */
+            }
+            
+            printf("\r\n");
+            etat =8;
+            printf("-----ETAT 8-----\r\n");
+        }
+        else if(compteur == 48 +longInt) {
+            for(int i=0; i<8;i++){
+                if(end[i]!= START_END[i])
+                {
+                    etat =0;
+                    printf("------FAIL------\r\n");
+                }
+                printf("%d", end[i]);    
+            }
+            printf("\r\n");
+            etat =0;
+            printf("-----ETAT 0-----\r\n");
+        }        
     }
 }
 
@@ -333,5 +469,4 @@
             wait(0.25);
         }
 }
-
-
+ 
\ No newline at end of file