APP4

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

main.cpp

Committer:
jpbaillargeon
Date:
2018-10-23
Revision:
18:5c26a8f38248
Parent:
17:53d4674cb37f
Child:
19:ae8e7c9292d6

File content as of revision 18:5c26a8f38248:

#include "mbed.h"
#include "rtos.h"

DigitalOut dout(p18);
InterruptIn button(p5);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut flash(LED4);
DigitalIn pin12(p12);

int periode = 10; //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};
Thread envoi_t;
Thread reception_t;
Thread detection_t;

typedef struct {
    int time;
    bool front;
} message_t;

Mail<message_t,64> mail_box;
Timer timer;
int etat;

bool messageUtile[64] = {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,
                        0,1,0,1,1,0,1,1,
                        1,0,1,0,1,1,1,0,
                        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
    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++;   
    }
}

//convert to Manchester ------------------------------------------------------------------------------------------------
void convertBitToManchester(bool *manchester, bool bit){ //Fonctionne
    if(bit == 0){
        manchester[0]=0;
        manchester[1]=1;
    } 
    else if(bit == 1){
        manchester[0]=1;
        manchester[1]=0; 
    }
}

void convertByteToManchester(bool *manchester, bool byte[8]){ //Fonctionne
    for(int i=0; i<8; i++){
        if(byte[i] == 0){
            manchester[i*2]=0;
            manchester[i*2+1]=1;
        } 
        else if(byte[i] == 1){
            manchester[i*2]=1;
            manchester[i*2+1]=0; 
        }     
    }     
}

void convertMessageToManchester(bool *manchester, bool *message, int length){
        for(int i=0; i<length; i++){
            if(message[i] == 0){
                manchester[i*2]=0;
                manchester[i*2+1]=1;
            } 
            else if(message[i] == 1){
                manchester[i*2]=1;
                manchester[i*2+1]=0; 
            }   
        }     
}

//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++;
    }
    for(int i=0; i<8; i++){
        *message=0;
        message++;
    }
    for(int i=0; i<8; i++){
        *message=bin[i];
        message++;
    }
    for(int i=0; i<sizeof(messageUtile); i++){
        *message=messageUtile[i];
        message++;        
    }
    //CRC16
    unsigned short crc = crc16(messageUtile, (int)sizeof(messageUtile));
    printf("\r\n -----------------CRC: %d-------------\r\n", crc);
    int_to_bin(bin, crc);
    for(int i=0; i<8; i++){
        *message=bin[i];
        message++;      
    }
    for(int i=0; i<8; i++){
        *message=START_END[i];
        message++;
    }            
}

//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){
        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];
                Thread::wait(periode);   
            }
        }
        else if(!pin12 && app){
            app=false;
        }    
    }
}




//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];
    int temps =0;
    int tMax;
    int tMin;
    while(1){
        osEvent evt = mail_box.get();
        if (evt.status == osEventMail) {
            message_t *f = (message_t*)evt.value.p;
            led3 = f->front;
            t = f->time;
            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==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){
                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                        start[compteur-8] = f->front;
                        prev_time = t; 
                        compteur++;
                }
            }
            
            else if(etat ==4) {
                //if((t-prev_time)<=35 && (t-prev_time)>=20){
                if((t-prev_time)==temps){
                        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){
                if((t-prev_time)==temps){
                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                        longueur[compteur-24] = f->front;
                        prev_time = t; 
                        compteur++;
                }
            }
            else if(etat ==6) {
                //if((t-prev_time)<=35 && (t-prev_time)>=20){
                if((t-prev_time)==temps){
                        printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur);
                        message[compteur-32]= f->front;
                        prev_time = t; 
                        compteur++;
                }
            } 
            else if(etat ==7) {
                //if((t-prev_time)<=35 && (t-prev_time)>=20){
                if((t-prev_time)==temps){
                        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){
                if((t-prev_time)==temps){
                        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);
        }
        
        if((compteur ==8) && (etat ==2)){
            etat = 3;
            for(int i=0; i<8;i++){
                if((preambule[i]!= PREAMBULE[i]) && (etat !=0 ))
                {
                    etat =0;
                    printf("\r\n------Pas le bon PREAMBULE------\r\n");
                }
                printf("%d", preambule[i]);    
            }
            printf("\r\n");
            if(etat != 0)
            {
                temps /=6;
                tMin = 0.90*temps;
                tMax = 1.10*temps;
                printf("-----ETAT 3 %d-----\r\n", temps);
            }
        }
        else if((compteur ==16) && (etat == 3)) {
            etat =4;
            for(int i=0; i<8;i++){
                if((start[i]!= START_END[i]) && (etat != 0))
                {
                    etat =0;
                    printf("\r\n------Pas le bon START------\r\n");
                }
                printf("%d", start[i]);    
            }
            printf("\r\n");
             if(etat != 0)
            {
                printf("-----ETAT 4-----\r\n");
            }
        }
        else if((compteur==24) && (etat ==4)) {
            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) && (etat ==5)) {
            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) && (etat ==6)) {
            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) && (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);
            for(int i=0; i<8;i++){
                printf("%d", bin2[i]);
                if((bin2[i] != checksum[i]) && (etat !=0)) {
                    etat =0;
                    printf("\r\n------Pas le bon CRC------\r\n");
                }
            }
            
            printf("\r\n");
            if(etat != 0)
            {
                printf("-----ETAT 8-----\r\n");
            }
        }
        else if((compteur == 48 +longInt) && (etat ==8)) {
            for(int i=0; i<8;i++){
                if(end[i]!= START_END[i])
                {
                    etat =0;
                    printf("\r\n------Pas le bon END------\r\n");
                }
                printf("%d", end[i]);    
            }
            printf("\r\n");
            etat =0;
            printf("-----ETAT 0-----\r\n");
        }        
    }
}

//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;
    mail_box.put(a);
}

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;
    mail_box.put(a);
}
 

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");*/
       
       int length = sizeof(messageUtile);
       bool message[length+6*8];
       creationTrame(message,length);
       printf("Message : ");
       for(int i=0; i<sizeof(message); i++){
           if(i>0 && i%8 == 0){
               printf(" ");
           }
           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++){
           if(i>0 && i%2 == 0){
               printf(" ");
           }
           printf("%d",manchester[i]);
       }
       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!
            flash = !flash;
            wait(0.25);
        }
}