Derek McLean / Mbed 2 deprecated uwb-quadcopter

Dependencies:   mbed ESC SR04 TSI

com/com.cpp

Committer:
gabdo
Date:
2013-06-06
Revision:
16:5f736b955d53

File content as of revision 16:5f736b955d53:

/****************************** com.cpp **********************************/
/* Version: 1.0                                                          */
/* Last Updated: June 1, 2013                                            */
/*************************************************************************/

#include "mbed.h"
#include "com.h"

/*********************** com( PinName, PinName ) *************************/
/*                                                                       */
/*************************************************************************/

com::com( PinName tx, PinName rx ) : xbee( tx, rx)
{
    bLength = 0;
    xbee.attach( this, &com::callback );
    readBuffer = new queue();
}

/************************* bool isData()  ********************************/
/*                                                                       */
/*************************************************************************/

bool com::isData()
{
    if( readBuffer->isEmpty() )
        return false;
        
    return true;
}

/************************ void write( char ) *****************************/
/*                                                                       */
/*************************************************************************/

void com::write( char value )
{

}

/*************************** char read()  ********************************/
/*                                                                       */
/*************************************************************************/

char * com::read()
{
    // Are there commands in the readBuffer queue?    
    if( !readBuffer->isEmpty())
        return readBuffer->pop();
        
    return NULL;
}

/********************** void eventHandler() ******************************/
/*                                                                       */
/*************************************************************************/

void com::callback()
{   
    while( xbee.readable() )
    {
        char data = xbee.getc();    
        
        if( bLength++ < 15 )
            buffer[bLength] = data;      
     
        if( data == 255 )
            packetBuilder();
    }
}

/********************** void eventHandler() ******************************/
/*                                                                       */
/*************************************************************************/

void com::packetBuilder()
{
    // As long as there is data in the buffer, we need to read it.
    while( bLength > 0 )
    {
        // If the buffer has at least 3 chars and the 3rd one is 2555
        //then we have a complete string, we must read it.
        if( bLength > 2 && buffer[bLength] == 255 )
        {
            char * commandData = new char[2];
            commandData[1] = buffer[--bLength];
            commandData[0] = buffer[--bLength];
            readBuffer->add( commandData );
            --bLength;
        }
        // There must have been a read error, just flush the buffer.
        else
            bLength--;
    }
}