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 Prosper Van

Revision:
12:bf4642578682
Parent:
11:d1f488302d06
Child:
13:2fb7b19dcd70
--- a/com.cpp	Fri Apr 25 06:21:19 2014 +0000
+++ b/com.cpp	Sat Apr 26 02:38:08 2014 +0000
@@ -26,11 +26,12 @@
 
 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.
+    index1 = 0;                            // How many bytes are in the buffer.
+    index2 = 0;                            // How many bytes are in the buffer.
+    pindex = 0;                            // How many bytes are in the buffer.
+    rdy2build = 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.
     txBuffer = new queue();
     signalStrength = 0;
     xbeeRx.attach( this, &com::callback );    // Set callback as the interrupt handler. 
@@ -46,7 +47,7 @@
 bool com::isData()
 {
     
-    return !rxBuffer->isEmpty();
+    return rdy2build;
 }
 
 /************************ void write( char ) *****************************/
@@ -62,38 +63,14 @@
 /*************************************************************************/
 
 void com::write( short command, short value  )
-{
-    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.
-    }
+{  
+    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 ackCheck()  ********************************/
@@ -117,17 +94,7 @@
     return !txBuffer->isEmpty();
 }
 
-/*************************** char read()  ********************************/
-/*                                                                       */
-/*************************************************************************/
 
-short * com::read()
-{
-    if( !rxBuffer->isEmpty())
-        return rxBuffer->pop();
-        
-    return NULL;
-}
 
 /************************ void callback() ********************************/
 /*                                                                       */
@@ -139,78 +106,95 @@
     while( xbeeRx.readable() )
     {
         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;      
-     //
-//        xbeeTx.putc( bLength );
-//        xbeeTx.putc( bLength );
-//        xbeeTx.putc( 255 );
-        //2nd condition ensure that we have a full size packet before building
-        if( data == 255 && bLength > 5) 
-            packetBuilder();
+        
+        if (isA1)
+        {
+            if ( data == 255 && index1 > 4 )
+            {
+                rdy2build = true;
+                pindex = index1;
+                index1 = 0;
+                isA1 = false;
+            }
+            else if ( index1 < BUFFERSIZE )
+            {
+                buffer1[index1++] = data;
+            }
+        }
+        else
+        {
+            if ( data == 255 && index2 > 4 )
+            {
+                rdy2build = true;
+                pindex = index2;
+                index2 = 0;
+                isA1 = true;
+            }
+            else if ( index2 < BUFFERSIZE )
+            {
+                buffer2[index2++] = data;
+            }
+        }
     }
     __enable_irq();
 }
 
-//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.          */
 /*************************************************************************/
 
-void com::packetBuilder()
+short * com::read()
 {
-    char * commandData = new char[bLength];
-    commandData[4] = buffer[--bLength];     // Sequence Number.
-    commandData[3] = buffer[--bLength];     // CheckSum value.
-    commandData[2] = buffer[--bLength];     // Second 7 bits.
-    commandData[1] = buffer[--bLength];     // Fisrt 7 bits.
-    commandData[0] = buffer[--bLength];     // Command.
-    
-      
-    if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
+    if (rdy2build)
     {
-        short * array = new short[2];
-        
-        array[0] = (short)commandData[0];
-        
-        short value = (short)(commandData[1] * 128 + commandData[2]);
-        
-        if( value > 8062 )
-            value = (short)value + 57344;
+        char * commandData = new char[pindex];
+        if (!isA1)
+        {
+            commandData[4] = buffer1[--pindex];     // Sequence Number.
+            commandData[3] = buffer1[--pindex];     // CheckSum value.
+            commandData[2] = buffer1[--pindex];     // Second 7 bits.
+            commandData[1] = buffer1[--pindex];     // Fisrt 7 bits.
+            commandData[0] = buffer1[--pindex];     // Command.
+        }
+        else 
+        {
+            commandData[4] = buffer2[--pindex];     // Sequence Number.
+            commandData[3] = buffer2[--pindex];     // CheckSum value.
+            commandData[2] = buffer2[--pindex];     // Second 7 bits.
+            commandData[1] = buffer2[--pindex];     // Fisrt 7 bits.
+            commandData[0] = buffer2[--pindex];     // Command.
+        }
         
-        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 );
-        if ( commandData[0] != 0)
+          
+        if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
         {
-            short * ackPacket = new short[2];
-            ackPacket[0] = commandData[0];
-            ackPacket[1] = commandData[4];
-            txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
-        }
-    } 
-    delete[] commandData;
-    bLength = 0;    // Reset the buffer length.                                           
+            short * array = new short[2];
+            array[0] = (short)commandData[0];
+            
+            short value = (short)(commandData[1] * 128 + commandData[2]);
+            
+            if( value > 8062 )
+                value = (short)value + 57344;
+            
+            array[1] = value;
+            if ( commandData[0] != 0)
+            {
+                short * ackPacket = new short[2];
+                ackPacket[0] = commandData[0];
+                ackPacket[1] = commandData[4];
+                txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
+            }
+            
+            delete[] commandData;
+            return array;
+        } 
+        delete[] commandData; 
+    }        
+    return NULL;                                 
 }
-//
-//void com::api_packetBuilder()
-//{
-//    
-//}
+
 
 
 /********************** bool isSignalGood() ******************************/
@@ -222,9 +206,3 @@
     if (signalStrength > RSSI_THRES) return true;
     else return false;
 }
-
-void com::setAPImode(bool mode)
-{
-    api_mode = mode;
-}
-