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
Diff: com/com.cpp
- Revision:
- 16:5f736b955d53
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com/com.cpp Thu Jun 06 00:18:29 2013 +0000 @@ -0,0 +1,106 @@ +/****************************** 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--; + } +} + + + + + + + + + + +