APP4

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

main.cpp

Committer:
leomerel
Date:
2018-10-21
Revision:
14:42a7d108b134
Parent:
13:3c020f9bfdc7
Child:
15:872151771dec

File content as of revision 14:42a7d108b134:

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

DigitalOut dout(p18);
InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);

int periode = 10; //10ms
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;
Mutex mutex;
Mail<bool,16> mailbox;

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};

//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
    for(int i=0; i<8; i++){
        *message=0;
        message++;        
    }
    for(int i=0; i<8; i++){
        *message=START_END[i];
        message++;
    }            
}

//Envoi d'une trame ----------------------------------------------------------------------------------------------------------------
void envoiTrame(){
    int length3 = sizeof(messageUtile);
    bool message3[length3+6*8];
    bool manchester3[(length3+6*8)*2];
    
    creationTrame(message3,length3);
    convertMessageToManchester(manchester3, message3,length3+6*8);
    
    for(int i=0; i<sizeof(manchester3); i++){
        dout = manchester3[i];
        Thread::wait(periode);   
    }
}

//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;
}

//detection du préambule
void detectionPreambule() {
    while(1){
        printf("\r\n C'est bon \r\n");
        mutex.lock();
    }
}

//reception des trames
void receptionTrames(){
     printf("\r\n C'est bon 2 \r\n");
      
}

//decode Manchester ------------------------------------------------------------------------------------------------------------------
void decodeManchester(bool *data){
    
}



//assemblage des trames
//desassemblage des trames


void flip() {
    led = !led;
    //printf("\r\n C'est bon \r\n");
}
 

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);
        button.rise(&flip);  // attach the address of the flip function to the rising edge
        while(1) {           // wait around, interrupts will interrupt this!
            flash = !flash;
            wait(0.25);
        }
}