Leo Merel
/
APP4
APP4
Fork of rtos_basic by
main.cpp
- Committer:
- leomerel
- Date:
- 2018-10-22
- Revision:
- 15:872151771dec
- Parent:
- 14:42a7d108b134
- Child:
- 16:a163a7c0315b
File content as of revision 15:872151771dec:
#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,16> 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}; //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(){ bool app=false; 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}; app=true; for(int i=0; i<sizeof(manchester3); i++){ dout = manchester3[i]; Thread::wait(periode); } } else if(!pin12 && app){ app=false; } } } //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; 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){ compteur=0; timer.start(); printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur); preambule[compteur] = f->front; etat = 1; } else if(etat==2){ if((t-prev_time)<=35 && (t-prev_time)>=25){ printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur); preambule[compteur] = f->front; } } } else if(f->front==1){ if(etat ==1){ if((t-prev_time)<=35 && (t-prev_time)>=25){ printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur); preambule[compteur] = f->front; etat = 2; } else{ etat=0; } } else if(etat ==2){ if((t-prev_time)<=35 && (t-prev_time)>=25){ printf("%d Time : %d , compteur: %d\r\n", f->front, t, compteur); preambule[compteur] = f->front; } } } prev_time = t; 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(" "); } printf("%d", preambule[i]); } printf("\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); } }