Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Revision:
20:81d5655fecc2
Parent:
19:8c1f2a2204fb
Child:
32:9cb7bc3fc9e0
--- a/packet.h	Sun Apr 20 22:01:05 2014 +0000
+++ b/packet.h	Tue Apr 22 04:26:31 2014 +0000
@@ -1,4 +1,10 @@
+#ifndef _PACKET_H_
+#define _PACKET_H_
+
 #include <InterruptIn.h>
+#include "adapt/xbee.h"
+#include "adapt/usb.h"
+#include "adapt/Timeout.h"
 
 #define PACKETSIZE 256
 
@@ -18,6 +24,8 @@
     PT_SENDLOC,
     PT_WAY,
     PT_GETCOMMANDS,
+    PT_STARTCOMMANDS,
+    PT_ENDCOMMANDS,
     PT_SIZE
 };
 typedef struct PacketStruct{
@@ -60,24 +68,7 @@
         //wait_ms(100);
     }
     
-    void wait_for_egg(){
-        char response[40];
-        while(1){
-            while(!outputDevice.readable()){}
-            outputDevice.scanf("%s",response);
-            if(strcmp(response,"DHCP:") == 0){
-                USB::getSerial().printf("Egg Connected!\r\n");
-                for(int a=0;a<100000000;a++);
-                while(outputDevice.readable()){
-                    outputDevice.getc();
-                }
-                return;
-            }
-        }
-    }
-    
     void openConnection(char close_conn = 0){
-        __disable_irq();
         do{
             USB::getSerial().printf("trying to connect...\r\n");
             if(getTCPConStatus){
@@ -88,87 +79,9 @@
             setTCPConStatus = 1;
             while(setTCPConStatus==1 && !getTCPConStatus){}          
         }while(!getTCPConStatus);
-
-        __enable_irq();
-        //for(int a=0;a<10000000;a++);
-//        char connected = 0;
-//        char in_cmd_mode = 0;
-//        char response[80];
-//        int charcount;
-//        
-//        while(in_cmd_mode == 0){
-//            //get into command mode
-//            while(!outputDevice.writeable()){}
-//            outputDevice.putc('$');
-//            while(!outputDevice.writeable()){}
-//            outputDevice.putc('$');
-//            while(!outputDevice.writeable()){}
-//            outputDevice.putc('$');
-//            while(!outputDevice.writeable()){}
-//            while(!outputDevice.readable()){}
-//            //outputDevice.scanf("%s",response);
-//            charcount = 0;
-//            while(outputDevice.readable()){
-//                response[charcount] = outputDevice.getc();
-//                charcount++;
-//                for(int a=0;a<10000;a++);
-//            }
-//            response[charcount] = 0;
-//            USB::getSerial().printf("%s",response);
-//            if(strcmp(response+strlen(response)-5,"CMD\r\n") == 0){
-//                in_cmd_mode = 1;
-//            }else{
-//                while(1);
-//            }
-//            USB::getSerial().printf("in cmd mode: %d\r\n",in_cmd_mode);
-//        }
-//        //open the connection!
-//        while(connected == 0){
-//            for(int a=0;a<10000000;a++);
-//            while(!outputDevice.writeable()){}
-//            if(close_conn){
-//                outputDevice.printf("close");
-//                while(!outputDevice.writeable()){}
-//                outputDevice.putc(0);
-//                while(!outputDevice.writeable()){}
-//                outputDevice.printf("exit");
-//            }else{
-//                outputDevice.printf("open 1.2.3.14 7000");
-//            }
-//            while(!outputDevice.writeable()){}
-//            outputDevice.putc(0);
-//            while(!outputDevice.writeable()){}
-//            if(0 && !close_conn){
-//                for(int a=0;a<5;a++){
-//                    while(!outputDevice.readable()){}
-//                    outputDevice.scanf("%s",response);
-//                    USB::getSerial().printf("%s ",response);
-//                }
-//            }
-//            while(!outputDevice.readable()){}
-//            for(int a=0;a<10000000;a++);
-//            charcount = 0;
-//            while(outputDevice.readable()){
-//                response[charcount] = outputDevice.getc();
-//                USB::getSerial().putc(response[charcount]);
-//                charcount++;
-//                for(int a=0;a<10000;a++);
-//            }
-//            response[charcount] = 0;
-//            USB::getSerial().printf("RESPONSE: %s\r\n",response);
-//            if(close_conn){
-//                connected = 1;
-//            }else{
-//                if(strcmp(response+strlen(response)-6,"*OPEN*") == 0){
-//                    connected = 1;
-//                }
-//            }
-//            USB::getSerial().printf("connected: %d\r\n",connected);
-//        }
-//        for(int a=0;a<1000000;a++);
     }
+    
     void closeConnection(){
-        __disable_irq();
         wait_us(50000);
         do{
             USB::getSerial().printf("disconnecting...\r\n");
@@ -176,9 +89,77 @@
             wait_us(50000);
         }while(getTCPConStatus);
     }
+    
+    void clearRXBuffer(){
+        while(outputDevice.readable()){
+            outputDevice.getc();
+        }
+    }
+    
+    char rx_ready_with_timeout(){
+        if(outputDevice.readable()){
+            return 1;
+        }else{
+            EvTimer t;
+            t.set_s_period(3.0);
+            t.start_timer();
+            while(t.get_num_trips() == 0){
+                if(outputDevice.readable()){
+                    return 1;
+                }
+            }
+            t.stop_timer();
+        }
+        return 0;
+    }
+    
+    char getSynced(){
+        int zero_count = 0; //count of the number of consecutive 0's
+        char sel_ret;
+        unsigned char temp;
+        while(1){
+            if((sel_ret = rx_ready_with_timeout()) != 1){
+                return sel_ret;
+            }
+            temp = outputDevice.getc();
+            //USB::getSerial().printf("%x ",temp);
+            if(temp==0){
+                zero_count++;
+            }else if(temp == 0xFF){
+                printf("TESTING SYNC COMPLETE: %d. expecting %d\n",zero_count,sizeof(PacketStruct)-sizeof(char));
+                if(zero_count == sizeof(PacketStruct)-sizeof(char)){
+                    USB::getSerial().printf("!!!!DONE SYNCING!!!!\r\n");
+                    return 1;
+                }
+            }else{
+                zero_count = 0;
+            }
+            if(zero_count > sizeof(PacketStruct)-sizeof(char)){
+                zero_count = 0;
+            }
+        }
+    }
+    
+    char receivePacket(PacketStruct* pack){
+        char rx_ret;
+        char temp;
+        int total_rec_bytes = 0;//total bytes received for this packet.
+        while(total_rec_bytes < sizeof(PacketStruct)){
+            if((rx_ret = rx_ready_with_timeout()) != 1){
+                USB::getSerial().printf("timed out waiting for packet. Received %d/%d bytes\r\n",total_rec_bytes,sizeof(PacketStruct));
+                return rx_ret;
+            }
+            temp = outputDevice.getc();
+            *(((char*)pack) + total_rec_bytes) = temp;
+            total_rec_bytes++;
+        }
+        return 1;
+    }
+    
     unsigned int min(unsigned int a,unsigned int b){
         return a<b ? a : b;
     }
+    
     void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){
         if(data!=NULL && size>0){
             for(int i=0;i<=(size-1)/PACKETSIZE;i++){
@@ -192,7 +173,6 @@
                 sendPacket(output);
             }
         }else{
-            //openConnection();
             PacketStruct output;
             output.type=type;
             output.size=0;
@@ -203,10 +183,8 @@
             for(int a=0;a<PACKETSIZE;a++){output.data[a]='\0';}
             sendPacket(output);
         }
-        if(type == PT_END){
-            //closeConnection();
-        }
     }
+    
     PacketStruct* lastValid;
     bool found;
     void handleUpdate(){
@@ -279,3 +257,5 @@
     if(ps==NULL)ps=new PacketSender();
     return *ps;
 }
+
+#endif
\ No newline at end of file