tkt

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003  
00004 InterruptIn button(p5);
00005 DigitalOut led1(LED1);
00006 DigitalOut led3(LED3);
00007 DigitalOut flash(LED4);
00008 
00009 Thread recevoir;
00010 
00011 //Timer fref;
00012 Timer tintervale;
00013 
00014 int table_recu[100]={};
00015 int table_octet[8]={};
00016 int table_test[32]={};
00017 
00018 int table_message[584]={};
00019 int table_CRC16_recu[16]={};
00020 int CRC16[] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00021 
00022 int i,j,go=0;
00023 float tempsbits=0.01;;
00024 
00025  int prevtest = 0;
00026 int compteur_front=0;
00027 
00028 int flag =0;
00029    int test=3;
00030 
00031 float fref=0.01;
00032  
00033 int table_longueur[8]={};
00034 
00035 int longeurMessage=0;
00036 
00037 Thread ledclignote;
00038 
00039 void front_montant() {
00040     if(flag==0&&tintervale.read()>fref*0.8){
00041         table_test[j]=0;
00042         j++;
00043         tintervale.reset();
00044 
00045     if(j==32){flag=1;j=0;}  
00046     }
00047     
00048     if(flag==2&&tintervale.read()>fref*0.8){
00049         table_message[j]=0;
00050         j++;
00051         tintervale.reset();
00052         if(j==(longeurMessage-7)*8){flag=3;j=0;}  
00053     }
00054     
00055     if(flag==4&&tintervale.read()>fref*0.8){
00056         table_CRC16_recu[j]=0;
00057         j++;
00058         tintervale.reset();
00059         if(j==16){flag=5;j=0;}  
00060     }
00061     
00062    
00063     
00064     
00065 }
00066 
00067 
00068 
00069 void front_d() {
00070     if(flag==0&&tintervale.read()>fref*0.8){
00071         table_test[j]=1;
00072         j++;
00073         tintervale.reset();
00074         
00075     if(j==32){flag=1;j=0;}
00076     }
00077     
00078         if(flag==2&&tintervale.read()>fref*0.8){
00079         table_message[j]=1;
00080         j++;
00081         tintervale.reset();
00082         if(j==(longeurMessage-7)*8){flag=3;j=0;}  
00083     }
00084     
00085     if(flag==4&&tintervale.read()>fref*0.8){
00086         table_CRC16_recu[j]=1;
00087         j++;
00088         tintervale.reset();
00089         if(j==16){flag=5;j=0;}  
00090     
00091    
00092 }
00093 }
00094 
00095 
00096 void recevoir_message(){
00097 
00098  while(1){
00099         tintervale.start();
00100         
00101         
00102         if(flag==0){
00103             Thread::wait(1);
00104         }
00105         
00106         if(flag==1){
00107             // on verifie qu'on a bien l'octets de start 
00108             int octetsStart = 0;
00109             longeurMessage=0;
00110             if(table_test[8]==0){octetsStart++;}
00111             for(int k=9;k<15;k++){
00112                     if(table_test[k]==1){octetsStart++;}
00113                 }
00114             if(table_test[8]==0){octetsStart++;}
00115             if(octetsStart==8){
00116                //ici le Start est valide 
00117                // on récupère la longeur du paquet 
00118                for(int i =24;i<32;i++){
00119                  int a=1;
00120                 // printf("%d",table_test[i]);
00121                 //met a la puissance
00122                  for(int j=0;j<31-i;j++){a = a*2;}
00123                 longeurMessage+=table_test[i]*a;
00124                }
00125                //printf("  %d\n\r",longeurMessage);
00126                flag =2;
00127               }
00128             if(octetsStart!=8){ flag=0;} // start non valide on recommence du début 
00129             }
00130             
00131             
00132         if(flag==3){
00133           /*   printf("message recu : ");
00134              for(int i=0;i<(longeurMessage-7)*8;i++){
00135              printf("%d ",table_message[i]);
00136              }
00137              printf("\n\r");
00138              flag = 0;
00139              }    */
00140              
00141         // on calcul le crc
00142             int longueurData = (longeurMessage-7)*8;
00143             int *data_p = table_message;
00144           //  int CRC16[] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00145     
00146             unsigned short crc = 0xFFFF;
00147             unsigned char x;
00148 
00149             while (longueurData--){
00150                 x = crc >> 8 ^ *data_p++;
00151                 x ^= x>>4;
00152                 crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
00153              }
00154         
00155             // convertit le crc en binaire 
00156             go =15;
00157                 while(go>0){
00158             CRC16[go]=crc%2;
00159             crc/=2;
00160             go=go-1;
00161          }
00162 
00163             CRC16[go]=crc%2;
00164             flag = 4;
00165         }
00166             
00167         // on compare les crc
00168         int crc_valid=0;
00169         if(flag==5){
00170                 for(int i =0;i<16;i++){
00171                     if(CRC16[i]==table_CRC16_recu[i]){crc_valid++;}
00172                 }
00173                 if(crc_valid==16){flag=6;}
00174                 if(crc_valid!=16){printf(" error CRC \n\r");flag=0;}
00175              }
00176              
00177              
00178         if(flag==6){
00179              printf("\n\r");
00180              printf("message recu : ");
00181              for(int i=0;i<(longeurMessage-7)*8;i++){
00182              printf("%d",table_message[i]);
00183              }
00184              printf(" \n\r");
00185              flag = 0;
00186              }    
00187              
00188              
00189             
00190             
00191         Thread::wait(0.1);
00192           
00193             
00194             
00195         
00196             
00197             
00198         
00199     
00200     }
00201     }
00202     
00203     
00204 int main() {
00205     button.fall(&front_d);
00206     button.rise(&front_montant);  // attach the address of the flip function to the rising edge
00207     recevoir.start(recevoir_message);
00208     while(1) {           // wait around, interrupts will interrupt this!
00209    // printf("   state : %d   ",flag);
00210   // Thread::wait(10);
00211 
00212    printf(" ");
00213    //printf(" ");
00214       //           printf("temps bits : %f",tempsbits);
00215     }
00216 }
00217