xbee communication for UWB quadcopter project Originally by Greg Abdo Forking to reduce impact of interrupt by moving packetbuilder out of the interrupt and letting be handled in the main loop
Fork of com by
Diff: com.cpp
- Revision:
- 19:dc9d18037565
- Parent:
- 18:19bcb2dbf3c8
- Child:
- 20:95244d4f47f6
--- a/com.cpp Wed Oct 15 04:57:15 2014 +0000 +++ b/com.cpp Tue Oct 21 21:22:21 2014 +0000 @@ -24,7 +24,7 @@ #define GET(x) #endif -com::com(PinName tx, PinName rx): xbee(tx,rx) +com::com(PinName tx, PinName rx): xbee(NC,rx), xbtx(tx,NC) { xbee.baud(BAUDRATE); xbee.attach(this,&com::callback); @@ -36,12 +36,14 @@ void com::callback() { + __disable_irq(); while(xbee.readable()) { char data = xbee.getc(); // xbee.putc(data); rxBuffer.add(data); } + __enable_irq(); } bool com::isData() @@ -106,6 +108,7 @@ pack_checksum = data; if ( temp == pack_checksum ) { + __disable_irq(); short * ackPacket = new short[2]; ackPacket[0] = pack_cmd; ackPacket[1] = pack_seq; @@ -115,6 +118,7 @@ array[0] = pack_cmd; array[1] = pack_value[1] * 128 + pack_value[0]; cmdBuffer.add( array ); + __enable_irq(); } packetIndex = 0; @@ -148,22 +152,28 @@ void com::sendACK() { + __disable_irq(); if( !txBuffer.isEmpty()) { short * pkt = txBuffer.pop(); - __disable_irq(); write(pkt[0],pkt[1]); //may need to disable interrupt - __enable_irq(); delete[] pkt; } + __enable_irq(); } void com::write( short command, short seq ) { - xbee.putc( 255 ); // End of message. - xbee.putc( (char)command ); // Command - xbee.putc( (char) seq); // Second 8 bits in array. - xbee.putc( (char) command + seq ); // Checksum array[0] + array[1]. + xbtx.putc( 255 ); // End of message. + xbtx.putc( (char)command ); // Command + xbtx.putc( (char) seq); // Second 8 bits in array. + xbtx.putc( (char) command + seq ); // Checksum array[0] + array[1]. } +void com::print(char* str) +{ + __disable_irq(); + xbee.printf(str); + __enable_irq(); +}