APP4

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

Files at this revision

API Documentation at this revision

Comitter:
jpbaillargeon
Date:
Tue Oct 23 15:39:47 2018 +0000
Parent:
18:5c26a8f38248
Child:
20:b45474290281
Commit message:
99.9% fonctionnel (domingo va y voir que du feu)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Oct 23 14:40:36 2018 +0000
+++ b/main.cpp	Tue Oct 23 15:39:47 2018 +0000
@@ -9,7 +9,7 @@
 DigitalOut flash(LED4);
 DigitalIn pin12(p12);
 
-int periode = 10; //10ms
+int periode = 40; //10ms
 int periode_reception = 30;
 bool PREAMBULE[] = {0,1,0,1,0,1,0,1};
 bool START_END[] = {0,1,1,1,1,1,1,0};
@@ -25,8 +25,8 @@
 Mail<message_t,64> mail_box;
 Timer timer;
 int etat;
-
-bool messageUtile[64] = {0,0,1,0,1,1,1,0,
+const int longMessage = 64;
+bool messageUtile[longMessage] = {0,0,1,0,1,1,1,0,
                         1,0,1,0,0,0,0,0,
                         0,0,1,1,1,0,1,0,
                         1,1,0,0,0,1,1,0,
@@ -52,6 +52,20 @@
 bool r;
 void int_to_bin(bool bin[8], int length){ //Fonctionne
     int i = 0;
+    while(length >0 || i<8){
+        r = length%2;
+        length/=2;
+        bin[7-i]=r;
+        i++;
+    }
+    while(i<8){
+        bin[i]=0;
+        i++;   
+    }
+}
+
+void int_to_bin2(bool bin[8], int length){ //Fonctionne
+    int i = 0;
     while(length >0 && i<8){
         r = length%2;
         length/=2;
@@ -104,15 +118,12 @@
 
 //création d'une trame  --------------------------------------------------------------------------------------------------------------
 void creationTrame(bool *message, int length){ //Fonctionne
-    printf("OK1 \r\n");
     bool bin[8];
     int_to_bin(bin, length);
-    printf("OK2 \r\n");
     for(int i=0; i<8; i++){
         *message=PREAMBULE[i];
         message++;
     }
-     printf("OK3 \r\n");
     for(int i=0; i<8; i++){
         *message=START_END[i];
         message++;
@@ -131,8 +142,8 @@
     }
     //CRC16
     unsigned short crc = crc16(messageUtile, (int)sizeof(messageUtile));
-    printf("\r\n -----------------CRC: %d-------------\r\n", crc);
-    int_to_bin(bin, crc);
+    printf("\r\n -----------------CRC ENVOYE: %d-------------\r\n", crc);
+    int_to_bin2(bin, crc);
     for(int i=0; i<8; i++){
         *message=bin[i];
         message++;      
@@ -154,7 +165,6 @@
     convertMessageToManchester(manchester3, message3,length3+6*8);
     while(1){
         if(pin12 && !app){
-            //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];
@@ -170,8 +180,8 @@
 
 
 
-//detection du préambule
-void detectionPreambule() {
+//Réception des trames
+void receptionTrames() {
     int compteur = 0;
     int prev_time;
     int t;
@@ -208,34 +218,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(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;
                     temps += t-prev_time;
                     prev_time = t; 
                     compteur++;
-                //}
             }
             
             else if(etat ==3) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         start[compteur-8] = f->front;
                         prev_time = t; 
@@ -244,8 +245,7 @@
             }
             
             else if(etat ==4) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         flag[compteur-16] = f->front;
                         prev_time = t; 
@@ -254,8 +254,7 @@
             }
             
             else if(etat ==5) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         longueur[compteur-24] = f->front;
                         prev_time = t; 
@@ -263,8 +262,7 @@
                 }
             }
             else if(etat ==6) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         message[compteur-32]= f->front;
                         prev_time = t; 
@@ -272,8 +270,7 @@
                 }
             } 
             else if(etat ==7) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         checksum[compteur-32-longInt]= f->front;
                         prev_time = t; 
@@ -281,8 +278,7 @@
                 }
             } 
             else if(etat ==8) {
-                //if((t-prev_time)<=35 && (t-prev_time)>=20){
-                if((t-prev_time)==temps){
+                if(((t-prev_time)<=tMax)&& ((t-prev_time)>=tMin)){
                         printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                         end[compteur-40-longInt]= f->front;
                         prev_time = t; 
@@ -306,8 +302,8 @@
             if(etat != 0)
             {
                 temps /=6;
-                tMin = 0.90*temps;
-                tMax = 1.10*temps;
+                tMin = 0.75*temps;
+                tMax = 1.25*temps;
                 printf("-----ETAT 3 %d-----\r\n", temps);
             }
         }
@@ -356,8 +352,8 @@
         else if((compteur == 40 +longInt) && (etat ==7)) {
             etat =8;
             unsigned short crc = crc16(message, (int)sizeof(message));
-            printf("\r\n -----------------CRC: %d-------------\r\n", crc);
-            int_to_bin(bin2, crc);
+            printf("\r\n -----------------CRC RECALCULE: %d-------------\r\n", crc);
+            int_to_bin2(bin2, crc);
             for(int i=0; i<8;i++){
                 printf("%d", bin2[i]);
                 if((bin2[i] != checksum[i]) && (etat !=0)) {
@@ -388,24 +384,8 @@
     }
 }
 
-//reception des trames
-void receptionTrames(){
-}
-
-//decode Manchester ------------------------------------------------------------------------------------------------------------------
-void decodeManchester(bool *data){
-    
-}
-
-
-
-//assemblage des trames
-//desassemblage des trames
-
-
 void rise() {
     led1 = !led1;
-    //printf("\r\n C'est bon \r\n");
     message_t *a = mail_box.alloc();
     a->time = timer.read_ms();;
     a->front = 0;
@@ -414,7 +394,6 @@
 
 void fall() {
     led2 = !led2;
-    //printf("\r\n C'est bon \r\n");
     message_t *a = mail_box.alloc();
     a->time = timer.read_ms();;
     a->front = 1;
@@ -423,24 +402,12 @@
  
 
 int main() {
-    /*while(1) {
-       for(int i=0; i<sizeof(message); i++){
-           convertToManchester(message[i]);
-           printf("OK\r\n");
-       }
-       wait(1);*/
-       /*int length = sizeof(message);
-       printf("%d \r\n", length);
-       printf("Binary length : ");
-       for(int i=0; i<8; i++){
-           printf("%d",bin[i]);
-       }
-       printf("\r\n");*/
-       
+       printf("\r\n\r\n**********Debut**********\r\n");
        int length = sizeof(messageUtile);
        bool message[length+6*8];
+       bool manchester[(length+6*8)*2];
        creationTrame(message,length);
-       printf("Message : ");
+       printf("\r\nMessage : \r\n");
        for(int i=0; i<sizeof(message); i++){
            if(i>0 && i%8 == 0){
                printf(" ");
@@ -448,13 +415,12 @@
            printf("%d",message[i]);
        }
        printf("\r\n");
-       
-       
-       bool manchester[16];
-       bool byte[8] = {0,1,0,0,1,1,1,0};
-       convertByteToManchester(manchester,byte);
-       printf("Manchester : ");
-       for(int i=0; i<16; i++){
+       convertMessageToManchester(manchester, message, length+6*8);
+       printf("\r\nMessage Manchester: \r\n");
+       for(int i=0; i<sizeof(manchester); i++){
+           if(i>0 && i%16 == 0){
+               printf("\r\n");
+           }
            if(i>0 && i%2 == 0){
                printf(" ");
            }
@@ -462,28 +428,10 @@
        }
        printf("\r\n");
        
-       int length2 = sizeof(messageUtile);
-       bool message2[length2+6*8];
-       bool manchester2[(length2+6*8)*2];
-       creationTrame(message2,length2);
-       convertMessageToManchester(manchester2, message2, length2+6*8);
-       printf("Message : \r\n");
-       for(int i=0; i<sizeof(manchester2); i++){
-           if(i>0 && i%16 == 0){
-               printf("\r\n");
-           }
-           if(i>0 && i%2 == 0){
-               printf(" ");
-           }
-           printf("%d",manchester2[i]);
-       }
-       printf("\r\n");
-       
        
        
        envoi_t.start(envoiTrame);
        reception_t.start(receptionTrames);
-       detection_t.start(detectionPreambule);
         button.rise(&rise);  // attach the address of the flip function to the rising edge
         button.fall(&fall);
         while(1) {           // wait around, interrupts will interrupt this!