Paul Sainrat / Mbed 2 deprecated App4_Projet

Dependencies:   mbed-rtos mbed

DO/DO.cpp

Committer:
Sainratp
Date:
2017-10-23
Revision:
1:5c0ca9bdf810
Parent:
0:703249a56b9d
Child:
4:e0ca69606fdf

File content as of revision 1:5c0ca9bdf810:

#include "rtos.h"
#include "mbed.h"
#include "DO.h"
#include "config.h"

Queue <void,16> queueBitReception;
Queue <char,16> queueCharReception;
extern Serial pc;


void receiveChar()
{
    char res;
    while(1) {
        res=0x00;
        for(int i =0; i<8; i++) {
            osEvent evt = queueBitReception.get();
            if(evt.status == osEventMessage) {
                res = res | (((char)evt.value.p)<<i);
            }
        }
        queueCharReception.put((char*)res);
    }

}

void receiveBit()
{
    Timer timer;
    int oldTime;
    bool bitType=false;
    Thread::signal_wait(0x02);
    oldTime = timer.read_ms();
    //TODO SYNCHRONISATION
    while(1) {
        Thread::signal_wait(0x01);
        if(timer.read_ms()-oldTime<T) {
            Thread::signal_wait(0x01);
        } else {
            bitType=!bitType;
        }

        timer.reset();
        oldTime=timer.read_ms();


        if(bitType) {
            queueBitReception.put((void*)0x01);
        } else {
            queueBitReception.put((void*)0x00);
        }
    }
}


char calcCRC16(char* trame)
{
    char length = trame[2];
    int CRC16 = 0xFFFF;
    for(char i =0x00; i<length; i++) {
        CRC16 = CRC16 ^ trame[3+i];
        for(int j = 0 ; j<8 ; j++) {
            if((CRC16 & 0x1)==0x01) {
                CRC16 = CRC16/2 ^ 0xA001;
            } else {
                CRC16 = CRC16/2;
            }
        }
    }
    return CRC16 & 0x00FF ;
}


void recieveData()
{
    osEvent evt;
    char reception;
    while(1) {
        evt = queueCharReception.get();
        if(evt.status == osEventMessage) {
            reception = (char)evt.value.p;
            if(reception == START) {
                do {
                    evt = queueCharReception.get();
                    if(evt.status == osEventMessage) {
                        reception = (char)evt.value.p;
                        pc.putc(reception);
                        //TODO remplacer le putc par la fabrication d'un trame propre + calcul du crc16 avec cette derniere
                    }
                }while(reception!=STOP);
            }
        }
    }
}