QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Files at this revision

API Documentation at this revision

Comitter:
dylanembed123
Date:
Sat Apr 05 22:27:18 2014 +0000
Parent:
11:97625c27ab90
Child:
13:a6d3cf2b018e
Commit message:
Update packet.h (interface to other c++ code).

Changed in this revision

adapt/camera.h Show annotated file Show diff for this revision Revisions of this file
adapt/gps.cpp Show annotated file Show diff for this revision Revisions of this file
adapt/gps.h Show annotated file Show diff for this revision Revisions of this file
adapt/usb.cpp Show annotated file Show diff for this revision Revisions of this file
adapt/usb.h Show annotated file Show diff for this revision Revisions of this file
adapt/xbee.cpp Show annotated file Show diff for this revision Revisions of this file
adapt/xbee.h Show annotated file Show diff for this revision Revisions of this file
handle/handleCamera.cpp Show annotated file Show diff for this revision Revisions of this file
handle/handleCamera.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
packet.h Show annotated file Show diff for this revision Revisions of this file
--- a/adapt/camera.h	Sat Apr 05 17:49:57 2014 +0000
+++ b/adapt/camera.h	Sat Apr 05 22:27:18 2014 +0000
@@ -6,8 +6,8 @@
 #include "mbed.h"
 
 /// Define Pinout
-#define CAMERAPINTX p9
-#define CAMERAPINRX p10
+#define CAMERAPINTX p9 //p13
+#define CAMERAPINRX p10 //p14
 
 /// Define Baud
 #define CAMERABAUD 38400
--- a/adapt/gps.cpp	Sat Apr 05 17:49:57 2014 +0000
+++ b/adapt/gps.cpp	Sat Apr 05 22:27:18 2014 +0000
@@ -6,7 +6,7 @@
     if(gps==NULL){
         // Init Serial USB
         gps=new Serial(GPSPINTX,GPSPINRX);
-        gps->baud(GPSBAUD);//gps->baud(115200);//57600);
+        gps->baud(GPSBAUD);
     }
-//    return *pc;
+    return *gps;
 }
\ No newline at end of file
--- a/adapt/gps.h	Sat Apr 05 17:49:57 2014 +0000
+++ b/adapt/gps.h	Sat Apr 05 22:27:18 2014 +0000
@@ -4,8 +4,8 @@
 
 
 /// Define Pinout
-#define GPSPINTX p9
-#define GPSPINRX p10
+#define GPSPINTX p28
+#define GPSPINRX p27
 
 /// Define Baud
 #define GPSBAUD 57600
--- a/adapt/usb.cpp	Sat Apr 05 17:49:57 2014 +0000
+++ b/adapt/usb.cpp	Sat Apr 05 22:27:18 2014 +0000
@@ -7,7 +7,7 @@
     if(pc==NULL){
         // Init Serial USB
         pc=new Serial(USBTX,USBRX);
-        pc->baud(115200);//57600);
+        pc->baud(USBBAUD);
     }
     return *pc;
 }
\ No newline at end of file
--- a/adapt/usb.h	Sat Apr 05 17:49:57 2014 +0000
+++ b/adapt/usb.h	Sat Apr 05 22:27:18 2014 +0000
@@ -1,6 +1,10 @@
 #ifndef _USB_H_
 #define _USB_H_
 #include "mbed.h"
+
+/// Define Baud
+#define USBBAUD 115200
+
 class USB{
 private:
     static Serial* pc;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adapt/xbee.cpp	Sat Apr 05 22:27:18 2014 +0000
@@ -0,0 +1,12 @@
+#include "xbee.h"
+
+Serial* XBEE::xbee=NULL;
+
+Serial& XBEE::getSerial(){
+    if(xbee==NULL){
+        // Init Serial USB
+        xbee=new Serial(XBEEPINTX,XBEEPINRX);
+        xbee->baud(XBEEBAUD);
+    }
+    return *xbee;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adapt/xbee.h	Sat Apr 05 22:27:18 2014 +0000
@@ -0,0 +1,20 @@
+#ifndef _XBEE_H_
+#define _XBEE_H_
+#include "mbed.h"
+
+
+/// Define Pinout
+#define XBEEPINTX p9
+#define XBEEPINRX p10
+
+/// Define Baud
+#define XBEEBAUD 57600
+
+class XBEE{
+private:
+    static Serial* xbee;
+public:
+    /// \brief Call this function to get a hold of the serial for USB.
+    static Serial& getSerial();
+};
+#endif
\ No newline at end of file
--- a/handle/handleCamera.cpp	Sat Apr 05 17:49:57 2014 +0000
+++ b/handle/handleCamera.cpp	Sat Apr 05 22:27:18 2014 +0000
@@ -1,7 +1,8 @@
 #include "handleCamera.h"
 
 void ImageHandle::setup(){
-    //char* version = cam.getVersion();
+    char* version = cam.getVersion();
+    outputDevice.printf("Version %s\n",version);
     uint8_t targetSize=VC0706_160x120;//VC0706_640x480;//VC0706_160x120;
     cam.setImageSize(targetSize);
     uint8_t realSize=cam.getImageSize();
@@ -9,21 +10,25 @@
 
 void ImageHandle::take(){
     if (! cam.takePicture()) {
-        USB::getSerial().printf("Failed to snap!\n");
+        outputDevice.printf("Failed to snap!\n");
         while(1){}
     }
+    unsigned int sID=getPS().getSuperID();
+    getPS().sendPacket(sID,NULL,0,PT_IMAGE);
     int size=cam.frameLength();
-    USB::getSerial().printf("Image Start\n",size);
+    outputDevice.printf("Image Start\n",size);
     int i;
     for(i=0;i<size;){
         // read 32 bytes at a time;
         uint8_t bytesToRead = std::min(64, size-i); // change 32 to 64 for a speedup but may not work with all setups!
         uint8_t bytesRead=0;
-        uint8_t* buffer = cam.readPicture(bytesToRead,&bytesRead);
-        for(int a=0;a<bytesRead;a++){USB::getSerial().putc(buffer[a]);}
+        char* buffer = (char*)cam.readPicture(bytesToRead,&bytesRead);
+        getPS().sendPacket(sID,buffer,bytesRead);
+        //for(int a=0;a<bytesRead;a++){outputDevice.putc(buffer[a]);}
         i+=bytesRead;
     }
-    USB::getSerial().printf("Image End\n",size);
+    outputDevice.printf("Image End\n",size);
+    getPS().sendPacket(sID,NULL,0,PT_END);
 }
 
 bool ImageHandle::check(){
@@ -33,9 +38,11 @@
 void ImageHandle::run(){
     if(!initialized){
         initialized=true;
+        outputDevice.printf("Setup cam\n");
         setup();
     }
     if(check()){
         take();
+        while(true){}
     }
 }
\ No newline at end of file
--- a/handle/handleCamera.h	Sat Apr 05 17:49:57 2014 +0000
+++ b/handle/handleCamera.h	Sat Apr 05 22:27:18 2014 +0000
@@ -3,11 +3,14 @@
 
 #include "adapt/usb.h"
 #include "adapt/camera.h"
+#include "packet.h"
 #include <algorithm>
+
 class ImageHandle{
 private:
     Camera cam;
     bool initialized;
+    Serial& outputDevice;
     
     /// \brief Setup the camera.
     void setup();
@@ -15,9 +18,11 @@
     void take();
     /// \brief Check if an image must be taken
     bool check();
+
 public:
+
     /// \brief Constructor
-    ImageHandle():initialized(false){}
+    ImageHandle():initialized(false),outputDevice(USB::getSerial()){}
     /// \brief Run an instance of this
     void run();
 };
--- a/main.cpp	Sat Apr 05 17:49:57 2014 +0000
+++ b/main.cpp	Sat Apr 05 22:27:18 2014 +0000
@@ -106,11 +106,12 @@
     ImageHandle imageHand;
     GPSHandle gpsHand;
     /// Main Loop
+    USB::getSerial().printf("Starting\n");
     while(1){
         // Run image handler
         imageHand.run();
         // Run GPS handler
-        gpsHand.run();
+        //gpsHand.run();
     }
     /// Main Loop
     while(true) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/packet.h	Sat Apr 05 22:27:18 2014 +0000
@@ -0,0 +1,77 @@
+#define PACKETSIZE 256
+
+// Example
+// Packet 1. (SuperPackid=5,type=PT_IMAGE,size=0)
+// 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)
+
+enum PACKET_TYPE{
+    PT_DEFAULT=0,
+    PT_END,
+    PT_IMAGE,
+    PT_SIZE
+};
+typedef struct PacketStruct{
+    PACKET_TYPE type;
+    unsigned int size;// Number of valid bits
+    char data[PACKETSIZE];
+    unsigned int superPackID;// 
+}PacketStruct;
+
+class PacketSender{
+    private:
+    unsigned int superID;
+    public:
+    unsigned int getSuperID(){return superID++;}
+    PacketSender():superID(0),outputDevice(USB::getSerial()){}
+    Serial& outputDevice;
+    void sendPacket(PacketStruct& output){
+        for(int a=0;a<sizeof(PacketStruct);a++){
+            outputDevice.putc(((char*)(&output))[a]);
+        }
+    }
+    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/PACKETSIZE;i++){
+                PacketStruct output;
+                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];
+                }
+                
+                //memcpy(output.data,&(data[i*PACKETSIZE]),min(PACKETSIZE,size-i*PACKETSIZE));
+                sendPacket(output);
+            }
+            
+        }else{
+            PacketStruct output;
+            output.type=type;
+            output.superPackID=superPackID;
+            sendPacket(output);
+        }
+    }
+    PacketStruct* getNextPacket(){
+        int avail = outputDevice.readable();
+        if(avail <= 0)return NULL;
+        PacketStruct* output=new PacketStruct();
+        for(int i=0;i<sizeof(PacketStruct);i++){
+            // Wait for byte
+            while(outputDevice.readable()<=0){}
+            ((char*)output)[i] = outputDevice.getc();
+        }
+        return output;
+    }
+    
+};
+static PacketSender* ps=NULL;
+static PacketSender& getPS(){
+    if(ps==NULL)ps=new PacketSender();
+    return *ps;
+}