first

Dependencies:   mbed mbed-rtos

main.cpp

Committer:
ThierryLeonard
Date:
2017-10-25
Revision:
8:7c56fb1ed8c0
Parent:
7:332766fb3114
Child:
9:8f479f7c1b54

File content as of revision 8:7c56fb1ed8c0:

#include "mbed.h"
#include "rtos.h"
#include <vector>
#include "sender.h"

#include "Receiver.h"
#include "defines.h"

#include "CRC.h"

DigitalOut myled(LED1);


DigitalOut MO(p22);
Serial pc(USBTX, USBRX);

#ifndef TESTPERIOD
const int us_period = 20;
const int us_timeout = us_period * 5/4
const int us_prepare = us_period *3/4
#else
int us_period = 100;
#endif


const unsigned char preamble_byte = 85;

const unsigned char STARTBYTE = 126;
const unsigned char ENDBYTE = 126;

Sender manchesterSender;

const int testByte = 106;
void sendMessage()
{
    unsigned char c = 0;
    vector<unsigned char> message;
    while ( c != '\n')
    {
        while(!pc.readable())
        {
           Thread::wait(1);
        }
        c = pc.getc();
        pc.printf("%c",c);
        message.push_back(c);
    }
    vector<unsigned char> trame = createFrame(message);
    
    manchesterSender.sendMessage(trame);
    
}


#ifdef TESTPERIOD
const int testCount = 500;
void sendTestData()
{
    vector<unsigned char> trame;
    
    for(int i =0; i < testCount;i++)
    {
        trame.push_back(106);   
    }

    while(true)
    {    
        manchesterSender.sendMessage(trame);
    }
    
}

int testMain()
{
    ManchesterReceiver r;
    
    for (int period = 100; period > 10; period -= 5)
    {
        us_period = period;
        sendTestData();
        unsigned char val;
        bool fail = false;
        while(!r.getByte(val))
        {    }
        if(val!=106)
        {
            fail = true;
            pc.printf("Transmit error");
        }
        for(int i = 1; i <testCount ; i++)
        {
            if(!r.getByte(val)){
                pc.printf("transmitError");
                fail = true;
            }
            if(val!=106){
                pc.printf("WrongData");
                fail = true;
            }
        }
        pc.printf("Period = %i\n",period);
    }
    
}
#endif

int consoleCommMain() {    
    ManchesterReceiver r;
    
    
    
    Callback<void()> cb(&r, &ManchesterReceiver::getMessages);
    Thread receiving(cb,osPriorityHigh);
    
    pc.printf("\n******************\nStarting to listen\n");
    
    while(1)
    {
        sendMessage();
        Thread::wait(1000);
    }
}


int main()
{
    return consoleCommMain();
    //return testMain();
}