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:
16:89695823d407
Parent:
15:3f742edaa359
Child:
17:acef0fb07510
--- a/com.cpp	Wed Aug 27 06:20:23 2014 +0000
+++ b/com.cpp	Thu Sep 25 02:30:25 2014 +0000
@@ -20,15 +20,14 @@
 #ifdef DEBUG_COM
     Timer timerCom;
     #define NL "\n\r"
-    #define PRINT(x) xbeeTx.printf(x)   //Serial.print(x)
-    #define PRINTF(x, y) xbeeTx.printf( (x) ,(y) )   //Serial.print(x, y)
-    #define PRINTLN(x) PRINT(x);PRINT(NL)
-    #define PRINTLNF(x, y) PRINTF((x) ,(y));PRINT(NL)
-    #define START timerCom.start(); 
-    #define STOP timerCom.stop()
-    #define RESET timerCom.reset()
-    #define READ timerCom.read_us()
-    #define GET(x) x = READ
+    #define PRINT(x)        xbeeTx.printf(x)   //Serial.print(x)
+    #define PRINTF          xbeeTx.printf   //Serial.print(x, y)
+    #define PRINTLN(x)      PRINT(x);PRINT(NL)
+    #define START           timerCom.start(); 
+    #define STOP            timerCom.stop()
+    #define RESET           timerCom.reset()
+    #define READ            timerCom.read_us()
+    #define GET(x)          x = READ
 #else
     #define PRINT(x)
     #define PRINTF(x, y)
@@ -63,90 +62,6 @@
     START;
 }
 
-/************************* bool isData()  ********************************/
-/*                                                                       */
-/*************************************************************************/
-
-//bool com::isData()
-//{
-//    return rdy2build;
-//}
-
-bool com::isData()
-{
-    static short packetIndex = 0;
-    static short pack_cmd = 0;
-    static short pack_value[2] = {0,0};
-    static short pack_seq = 0;
-    static short pack_checksum = 0;
-    while ( !rxBuffer->isEmpty() )
-    {
-        short * data = rxBuffer->pop();
-        switch (packetIndex)
-        {
-            case 0:
-            {
-                if ( *data == 255 )
-                packetIndex++;
-                break;
-            }
-            case 1:
-            {
-                if ( *data < 13 )
-                {
-                    pack_cmd = *data;
-                    packetIndex++;
-                }
-                else
-                    packetIndex = 0;
-                break;
-            }
-            case 2:
-            {
-                pack_value[1] = *data;
-                packetIndex++;
-                break;
-            }
-            case 3:
-            {
-                pack_value[0] = *data;
-                packetIndex++;
-                break;
-            }
-            case 4:
-            {
-                pack_seq = *data;
-                packetIndex++;
-                break;   
-            }
-            case 5:
-            {
-                short temp = pack_value[0] + pack_cmd;
-                pack_checksum = *data;
-                if ( temp == pack_checksum )
-                {
-                    short * ackPacket = new short[2];
-                    ackPacket[0] = pack_cmd;
-                    ackPacket[1] = pack_seq;
-                    txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
-                    
-                    short * array = new short[2];
-                    array[0] = pack_cmd;
-                    array[1] = pack_value[1] * 128 + pack_value[0];
-                    cmdBuffer->add( array );
-                }
-                
-                packetIndex = 0;
-                break;   
-            }
-            default:
-            packetIndex = 0;
-            break;
-        }
-        
-    }
-    return !cmdBuffer->isEmpty();
-}
 
 /************************ void write( char ) *****************************/
 /* Write a packet out the xbee com port.                                 */
@@ -178,13 +93,12 @@
     {
     __disable_irq();
         short * pkt = txBuffer->pop();
+        write(pkt[0],pkt[1]); //may need to disable interrupt
     __enable_irq();
-        write(pkt[0],pkt[1]); //may need to disable interrupt
 //        #ifdef DEBUG_COM
         if(pkt[1] % 5 == 0) 
         {
-            PRINTLNF("len: %d",txBuffer->queueLength());
-            xbeeTx.printf("len: %d\n\r",txBuffer->queueLength());
+            PRINTF("len: %d\n\r",txBuffer->queueLength());
         }
 //        #endif
         delete[] pkt;
@@ -198,7 +112,6 @@
 }
 
 
-
 /************************ void callback() ********************************/
 /*                                                                       */
 /*************************************************************************/
@@ -246,11 +159,12 @@
 void com::callback()
 {   
     __disable_irq();
+    PRINTF("Reading\n\r");
     while( xbeeRx.readable() )
     {
         short *data = new short;
-        *data = xbeeRx.getc(); 
-        
+        data[0] = xbeeRx.getc(); 
+        PRINTF("d: %d\n\r", data[0]);
         rxBuffer->add( data );
     }
     __enable_irq();
@@ -322,6 +236,105 @@
 }
 
 
+/************************* bool isData()  ********************************/
+/*                                                                       */
+/*************************************************************************/
+
+//bool com::isData()
+//{
+//    return rdy2build;
+//}
+
+bool com::isData()
+{
+    static short packetIndex = 0;
+    static short pack_cmd = 0;
+    static short pack_value[2] = {0,0};
+    static short pack_seq = 0;
+    static short pack_checksum = 0;
+    
+    __disable_irq();
+    while ( !rxBuffer->isEmpty() )
+    {
+        short * data = rxBuffer->pop();
+        __enable_irq();
+        PRINTF("d: %d \n\r", *data);
+        switch (packetIndex)
+        {
+            case 0:
+            {
+                if ( *data == 255 )
+                packetIndex++;
+                break;
+            }
+            case 1:
+            {
+                if ( *data < 13 )
+                {
+                    pack_cmd = *data;
+                    packetIndex++;
+                }
+                else
+                    packetIndex = 0;
+                break;
+            }
+            case 2:
+            {
+                pack_value[1] = *data;
+                packetIndex++;
+                break;
+            }
+            case 3:
+            {
+                if ( *data < 128 )
+                {
+                    pack_value[0] = *data;
+                    packetIndex++;
+                }
+                else
+                    packetIndex = 0;
+                break;
+            }
+            case 4:
+            {
+                pack_seq = *data;
+                packetIndex++;
+                break;   
+            }
+            case 5:
+            {
+                short temp = pack_value[0] + pack_cmd;
+                pack_checksum = *data;
+                if ( temp == pack_checksum )
+                {
+                    short * ackPacket = new short[2];
+                    ackPacket[0] = pack_cmd;
+                    ackPacket[1] = pack_seq;
+                    txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
+                    
+                    short * array = new short[2];
+                    array[0] = pack_cmd;
+                    array[1] = pack_value[1] * 128 + pack_value[0];
+                    cmdBuffer->add( array );
+                }
+                
+                packetIndex = 0;
+                break;   
+            }
+            default:
+            packetIndex = 0;
+            break;
+        }
+        
+        __disable_irq();
+        delete[] data;
+        
+    }
+    __enable_irq();
+    return !cmdBuffer->isEmpty();
+}
+
+
 /********************** bool isSignalGood() ******************************/
 /* For future use   */
 /*************************************************************************/