Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Revision:
14:6be57da62283
Parent:
13:a6d3cf2b018e
Child:
15:e3e03a9df89e
diff -r a6d3cf2b018e -r 6be57da62283 packet.h
--- a/packet.h	Mon Apr 07 01:30:04 2014 +0000
+++ b/packet.h	Thu Apr 10 02:19:07 2014 +0000
@@ -5,9 +5,10 @@
 // Packet 2. (SuperPackid=5,type=PT_DEFAULT,size=1024)
 // Packet 3. (SuperPackid=5,type=PT_DEFAULT,size=1000)
 // Packet 4. (SuperPackid=5,type=PT_END,size=0)
-#define XBEEON
+//#define XBEEON
 enum PACKET_TYPE{
-    PT_DEFAULT=0,
+    PT_EMPTY=0,
+    PT_DEFAULT,
     PT_END,
     PT_IMAGE,
     PT_SIZE
@@ -17,6 +18,7 @@
     unsigned int size;// Number of valid bits
     char data[PACKETSIZE];
     unsigned int superPackID;// 
+    char special[4];// Set to FF when
 }PacketStruct;
 
 class PacketSender{
@@ -30,13 +32,16 @@
         #else
         USB::getSerial()
         #endif
-    ){}
+    ),next(NULL){}
     Serial& outputDevice;
     void sendPacket(PacketStruct& output){
         for(int a=0;a<sizeof(PacketStruct);a++){
+            while(!outputDevice.writeable()){}
             outputDevice.putc(((char*)(&output))[a]);
+            //wait_ms(10);
             //USB::getSerial().putc(((char*)(&output))[a]);
         }
+        //wait_ms(100);
     }
     unsigned int min(unsigned int a,unsigned int b){
         return a<b ? a : b;
@@ -48,25 +53,70 @@
                 output.type=PT_DEFAULT;
                 output.superPackID=superPackID;
                 output.size=min(PACKETSIZE,size-i*PACKETSIZE);
-                for(int a=0;a<output.size;a++){
-                    //USB::getSerial().printf(">>%d/%d - %d\n",a,size,output.size);
-                    output.data[a]=data[a-i*PACKETSIZE];
-                }
+                for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA;
+                for(int a=0;a<output.size;a++){output.data[a]=data[a-i*PACKETSIZE];}
                 for(int a=output.size;a<PACKETSIZE;a++){output.data[a]='\0';}
-                //memcpy(output.data,&(data[i*PACKETSIZE]),min(PACKETSIZE,size-i*PACKETSIZE));
                 sendPacket(output);
             }
-            
         }else{
             PacketStruct output;
             output.type=type;
             output.size=0;
             output.superPackID=superPackID;
-            for(int a=0;a<PACKETSIZE;a++){output.data[a]=a;}
+            for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA;
+            // Check for empty packet
+            if(output.type==PT_EMPTY){output.type=0;output.size=0;output.superPackID=0;output.special[3]=0xFF;}
+            for(int a=0;a<PACKETSIZE;a++){output.data[a]='\0';}
             sendPacket(output);
         }
     }
+    
+    // Number of consecutive zeros
+    unsigned int numZeros;
+    // Return true if a resync command has been received
+    bool resetCheck(char input){
+        if(input=='\0'){
+            numZeros++;
+        }else if(numZeros==sizeof(PacketStruct)-1&&input==0xFF){
+            return true;
+        }else{
+            numZeros=0;
+        }
+        return false;
+    }
+    
+    // Temperary storage for next valid
+    PacketStruct* next;
+    // Number of valid bits in next packet
+    int nextValid;
+    /// \brief Grab the next packet
     PacketStruct* getNextPacket(){
+        // Check for null packet
+        if(next==NULL){next=new PacketStruct();nextValid=0;}
+        // Create reset packet which resets on re-sync command
+        bool resetPacket=false;
+
+        // While there is data to read
+        while(0<outputDevice.readable()){
+            // Check if a full packet has been received
+            if(nextValid==sizeof(PacketStruct))break;
+            // Read in next char
+            char input=outputDevice.getc();
+            //USB::getSerial().printf("Read ByteC %X %X\n",nextValid,input);
+            // Check for valid char
+            if(resetCheck(input)){resetPacket=true;break;}
+            // Set char
+            ((char*)next)[nextValid++] = input;
+        }
+        
+        if(nextValid==sizeof(PacketStruct)||resetPacket){
+            // Reset packet
+            PacketStruct* output=next;next=NULL;
+            // Return
+            return resetPacket?NULL:output;
+        }
+        return NULL;
+/*        
         int avail = outputDevice.readable();
         if(avail <= 0)return NULL;
         PacketStruct* output=new PacketStruct();
@@ -76,6 +126,7 @@
             ((char*)output)[i] = outputDevice.getc();
         }
         return output;
+*/
     }
     
 };