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.
Fork of com by
Revision 2:bd1621102cad, committed 2014-04-20
- Comitter:
- oprospero
- Date:
- Sun Apr 20 02:27:39 2014 +0000
- Parent:
- 1:4f53de75bc96
- Child:
- 3:42e37cdab039
- Child:
- 4:f8e953201251
- Commit message:
- clean ups
Changed in this revision
com.cpp | Show annotated file Show diff for this revision Revisions of this file |
com.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/com.cpp Sat Feb 15 19:54:45 2014 +0000 +++ b/com.cpp Sun Apr 20 02:27:39 2014 +0000 @@ -24,9 +24,10 @@ /* */ /*************************************************************************/ -com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, rx), xbeeRx( tx, rx), rssi(rssipin) +com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, NC), xbeeRx( NC, rx), rssi(rssipin) { bLength = 0; // How many bytes are in the buffer. + api_mode = false; // Xbee is in transparent mode. xbeeTx.baud(BAUDRATE); // Setup the serial baud rate. xbeeRx.baud(BAUDRATE); // Setup the serial baud rate. rxBuffer = new queue(); // Point to the rxQueue. @@ -43,6 +44,7 @@ bool com::isData() { + return !rxBuffer->isEmpty(); } @@ -60,13 +62,37 @@ void com::write( short command, short value ) { - short lvalue = (value % 128); - xbeeTx.putc( (char)command ); // Command - xbeeTx.putc( (char) value / 128 ); // First 8 bits in array. - xbeeTx.putc( (char) lvalue); // Second 8 bits in array. - xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. - xbeeTx.putc( (char)value ); // Sequence number. - xbeeTx.putc( 255 ); // End of message. + if (api_mode) + { + short hvalue = value / 128; + short lvalue = (value % 128); + short sum = (2*command + hvalue + 2*lvalue + value); + short check = sum % 256; + xbeeTx.putc( (char) 127); //Start Delimiter + + xbeeTx.putc( (char) 0); //Frame Data Length MSB + xbeeTx.putc( (char) 6); //Frame Data Length LSB + //Frame Data + xbeeTx.putc( (char) command); // Command + xbeeTx.putc( (char) hvalue); // First 8 bits in array. + xbeeTx.putc( (char) lvalue); // Second 8 bits in array. + xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. + xbeeTx.putc( (char) value); // Sequence number. + xbeeTx.putc( 255 ); // End of message. + + xbeeTx.putc( (char) 255 - check); //Checksum + + } + else + { + short lvalue = (value % 128); + xbeeTx.putc( (char)command ); // Command + xbeeTx.putc( (char) value / 128 ); // First 8 bits in array. + xbeeTx.putc( (char) lvalue); // Second 8 bits in array. + xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. + xbeeTx.putc( (char)value ); // Sequence number. + xbeeTx.putc( 255 ); // End of message. + } } /*************************** char read() ********************************/ @@ -91,6 +117,8 @@ { char data = xbeeRx.getc(); // xbeeTx.printf("data: %d\n\r", data); +// xbeeTx.printf("%d %d", data,(char) 255); +// xbeeTx.putc( data ); if( bLength++ < BUFFERSIZE ) buffer[bLength] = data; @@ -99,6 +127,15 @@ } } +//void com::api_callback() +//{ +// while( xbeeRx.readable() ) +// { +// buffer[bLength++] = xbeeRx.getc(); +// } +//} + + /********************** void packetBuilder() *****************************/ /* Creates a packet from the buffered data and places it in the rxBuffer */ /* queue to be read whenever convenient. Max value of +/- 8063. */ @@ -128,11 +165,18 @@ array[1] = value; // xbeeTx.printf("Cmd: %d,\tVal: %d\n\r,",array[0],array[1]); rxBuffer->add( array ); // Add to read buffer. +// xbeeTx.putc( 255 ); +// xbeeTx.putc( 255 ); write( (short)commandData[0], (short)commandData[4]); // Ack the packet with sequence nuber. } delete[] commandData; bLength = 0; // Reset the buffer length. } +// +//void com::api_packetBuilder() +//{ +// +//} /********************** bool isSignalGood() ******************************/ @@ -145,3 +189,8 @@ else return false; } +void com::setAPImode(bool mode) +{ + api_mode = mode; +} +
--- a/com.h Sat Feb 15 19:54:45 2014 +0000 +++ b/com.h Sun Apr 20 02:27:39 2014 +0000 @@ -20,7 +20,7 @@ #include "PwmIn.h" const int BAUDRATE = 38400; -const int BUFFERSIZE = 12; +const int BUFFERSIZE = 18; class com { @@ -30,6 +30,7 @@ bool isSignalGood(); void write( short, short ); // Write to the port. short * read(); // Read from the queue. + void setAPImode(bool mode); private: Serial xbeeTx; // tx - DIN, rx - DOUT @@ -37,6 +38,8 @@ queue *rxBuffer; // queue of commands ready to be read. PwmIn rssi; + bool api_mode; + void callback(); // Handle the interrupts. void packetBuilder(); // Called by callback to place commandes into the queue.