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:
15:3f742edaa359
Parent:
14:d2acb373d81c
Child:
16:89695823d407
--- a/com.cpp	Sun May 25 22:54:45 2014 +0000
+++ b/com.cpp	Wed Aug 27 06:20:23 2014 +0000
@@ -67,9 +67,85 @@
 /*                                                                       */
 /*************************************************************************/
 
+//bool com::isData()
+//{
+//    return rdy2build;
+//}
+
 bool com::isData()
 {
-    return rdy2build;
+    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 ) *****************************/
@@ -86,13 +162,10 @@
 
 void com::write( short command, short value  )
 {  
-    short lvalue = (value % 128);
+    xbeeTx.putc( 255 );                     // End of message.
     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.
+    xbeeTx.putc( (char) value);            // Second 8 bits in array.
+    xbeeTx.putc( command + value );        // Checksum array[0] + array[1].
 }
 
 /*************************** char ackCheck()  ********************************/
@@ -130,106 +203,125 @@
 /*                                                                       */
 /*************************************************************************/
 
+//void com::callback()
+//{   
+//    __disable_irq();
+//    while( xbeeRx.readable() )
+//    {
+//        char data = xbeeRx.getc(); 
+////        xbeeRx.putc(data);
+//        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::callback()
 {   
     __disable_irq();
     while( xbeeRx.readable() )
     {
-        char data = xbeeRx.getc(); 
-//        xbeeRx.putc(data);
-        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;
-            }
-        }
+        short *data = new short;
+        *data = xbeeRx.getc(); 
+        
+        rxBuffer->add( data );
     }
     __enable_irq();
 }
 
-
 /********************** 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.          */
 /*************************************************************************/
 
+//short * com::read()
+//{
+//    if (rdy2build)
+//    {
+//        rdy2build = false;
+//        char * commandData = new char[5];
+//        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.
+//        }
+//        
+////        xbeeTx.printf("Copied: %d %d %d %d %d\n\r",commandData[0],commandData[1],commandData[2],commandData[3],commandData[4]);
+//          
+//        if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
+//        {
+//            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;                                 
+//}
+
 short * com::read()
 {
-    if (rdy2build)
-    {
-        rdy2build = false;
-        char * commandData = new char[5];
-        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.
-        }
-        
-//        xbeeTx.printf("Copied: %d %d %d %d %d\n\r",commandData[0],commandData[1],commandData[2],commandData[3],commandData[4]);
-          
-        if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
-        {
-            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;                                 
+    if ( !cmdBuffer->isEmpty() )
+        return cmdBuffer->pop();
+    else
+        return NULL;    
 }
 
 
-
 /********************** bool isSignalGood() ******************************/
 /* For future use   */
 /*************************************************************************/