Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ESC SR04 TSI
com/com.cpp
- Committer:
- dereklmc
- Date:
- 2013-06-09
- Revision:
- 30:17297295ce0c
- Parent:
- 16:5f736b955d53
File content as of revision 30:17297295ce0c:
/****************************** 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--;
}
}
