Manchester decoder for VLC nog e ki geupload

Dependencies:   mbed

Fork of VLC_manchester_decoder by Jelle Raes

manchester.cpp

Committer:
JelleRaes
Date:
2018-05-15
Revision:
3:4fad63952e4c
Parent:
2:487a58ce256c

File content as of revision 3:4fad63952e4c:

#include "mbed.h"
#include "manchester.h"

DigitalIn signal(p7);
Serial pc(USBTX, USBRX);
Timer timer;
//Timeout timeout;
int datal[] = {1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0};
bool notTimedOut;

manchester::manchester()
{
    //pc.printf("object aag-nga-\r\n");
}

int manchester::decode()
{
    //pc.printf("bert zeg et\r\n");
    int previous,current,begin,end=0;
    int time=0;
    int i = 0;
    int datas[] = {1,1,1,1,0,0,0,0,0,0};
    notTimedOut = true;
    //pc.printf("start code\r\n");
    timer.start();
    while(i< DELAY) {
        previous = current;
        current = signal.read();
        if(current != previous) {
            i=0;
        }
        i++;
        wait_us(100);
    }
    i = 0;
    //timeout.attach(callback(this,&manchester::isTimedOut), 3);
    //pc.printf("start clockcalculation\r\n");
    while(i<4 ) {
        end = begin;
        previous = current;
        current = signal.read();
        if(current<previous) {
            begin = timer.read_us();
            if(end != 0) {
                time +=(begin - end);
            }
            i++;
        }
    }
    //pc.printf("loop is done\r\n");
    if(notTimedOut == true) {
        //pc.printf("not timed out\r\n");
        float atime=((float)time/3000000);
        i=4;
        wait(atime/2);
        //pc.printf("start data\r\n");
        while(i<10) {
            int x = signal.read();
            wait(atime/2);
            int y = signal.read();
            wait(atime/2);

            if(x<y) {
                datas[i]=0;
            } else if(x>y) {
                datas[i]=1;
            }
            i++;
        }
        wait(1);
        pc.printf("\n\raverage time is = %f s\n\r",atime);
        pc.printf("with frequency %f\n\r",1/atime);
        pc.printf("data is:");
        int direction = getDirection(datas);
        for(int j = 0; j<10; j++) {
            printf("%d",datas[j]);
        }
        printf("with direction:%d\r\n",direction);
        timer.stop();
        return direction;
    } else {
        printf("timed out\r\n");
        return 4;
    }
}

int manchester::getDirection(int* data)
{
    int direction;
    if(data[4]==0) {
        if(data[5]==0) {
            direction = 0;
        } else {
            direction = 1;
        }
    } else {
        if(data[5]==0) {
            direction = 2;
        } else {
            direction = 3;
        }
    }
    return direction;
}

bool manchester::vlcDetected()
{
    int i = 0;
    bool detected = false;
    while(i<DELAY) {
        if(signal.read() != 0) {
            detected = true;
        }
        wait_us(100);
    }
    return detected;
}

void manchester::isTimedOut()
{
    notTimedOut = false;
}

void manchester::printetwuk()
{
    pc.printf("test ng e ki\r\n");
}