Test Code for OV7670 Camera module. The images are sent over ethernet link.

Dependencies:   EthernetInterface mbed-rtos mbed ov7670

You can find more information in this page: https://mbed.org/users/edodm85/notebook/ov7670-camera-and-ethernet-stream/

Revision:
3:df8de18d7fa9
Parent:
2:d6b38a4512eb
--- a/main.cpp	Sat Feb 15 15:17:08 2014 +0000
+++ b/main.cpp	Sat Apr 05 14:02:07 2014 +0000
@@ -1,7 +1,7 @@
 /*
  * Author: Edoardo De Marchi
- * Date: 20/07/13
- * Notes: OV7670 camera with Ethernet Connection
+ * Date: 05/04/14
+ * Notes: This firmware required "OV7670 Grabber v2.0"
 */
 
 #include "main.h"
@@ -10,9 +10,6 @@
 #define QVGA    76800          //320*240
 #define QQVGA   19200          //160*120
 
-static char format = ' ';
-static int resolution = 0;
-
 
 int Init()
 {
@@ -26,41 +23,29 @@
     eth.init(ip, mask, gateway);
     //DHCP
     //eth.init();
+    
     eth.connect();
     server.bind(ECHO_SERVER_PORT);
     server.listen(1);
+    
     printf("IP Address is %s\r\n", eth.getIPAddress());
     
     //THREAD 
-    osThreadCreate(osThread(net_thread), NULL); 
-       
+    osThreadCreate(osThread(Net_Thread), NULL);
+    tencid = osThreadCreate(osThread(Grab_Thread), NULL);  
+    
     return 0;
 }
 
 
-
-int main (void) 
-{
-    Init();
-    t.start();
-    
-    while (true)
-    {
-        //mbed is alive?
-        led2 = !led2;
-        osDelay(500);
-    }     
-}
-
-
-void net_thread(void const *argument)
+void Net_Thread(void const *argument)
 {
     while (true) 
     {
         led3 = 1;
-        //printf("\r\nWait for new connection...\r\n");
         server.accept(client);
         printf("Connection from: %s\r\n", client.get_address());
+        
         while (true) 
         {
             led3 = 0;
@@ -68,10 +53,9 @@
             if (n <= 0) break;
             
             bufferRX[n]=0; // make terminater
-            parse_cmd();
+            parse_cmd(n);
         }
         client.close();
-        //printf("Connection close.\r\n");
     } 
 } 
 
@@ -83,83 +67,204 @@
 }
 
 
-void parse_cmd(){
+void Grab_Thread(void const *argument)
+{
+    while(true)
+    {    
+        osSignalWait(0x1, osWaitForever);
+                    
+        while(bSnap_on || bGrab_on)
+        {
+            CameraSnap();
+            bSnap_on = false;
+        }
+    }
+}
+
+
+void CameraSnap()
+{
+        led4 = 1;
+        int p = 0;  
+
+        camera.CaptureNext();
+        while(camera.CaptureDone() == false);      
+                                 
+        camera.ReadStart();                             // Start reading in the image data from the camera hardware buffer  
+        
+        t1 = t.read_ms(); 
+                  
+        client.send_all(StartCondition, sizeof(StartCondition));
+                    
+        if(strcmp("BAW", CMDCamera.format) == 0 || strcmp("RAW", CMDCamera.format) == 0)                   // B&W  
+        {  
+            for(int x = 0; x<CMDCamera.resolution/sizeof(bufferTX); x++)
+            {
+                for(int q = 0; q<sizeof(bufferTX); q++)
+                {               
+                   if(CMDCamera.resolution != VGA)
+                        camera.ReadOnebyte();                   // Read in the first half of the image
+                   
+                   bufferTX[q] = camera.ReadOnebyte();          // Y only      // Read in the Second half of the image                                  
+                }  
+                Send();       
+            } 
+        }     
+        
+        if(strcmp("YUV", CMDCamera.format) == 0 || strcmp("RGB", CMDCamera.format) == 0)                    // Color
+        {
+            for(int x = 0; x<(CMDCamera.resolution/sizeof(bufferTX))*2; x++)
+            {
+                for(int q = 0; q<sizeof(bufferTX)/2; q++)
+                {
+                       p = q*2;                               
+                       bufferTX[p] = camera.ReadOnebyte();          // Read in the first half of the image                       
+                       bufferTX[p+1] = camera.ReadOnebyte();        // Read in the Second half of the image                      
+                }  
+                Send();       
+            }         
+        }       
+           
+        camera.ReadStop();               
+        t2 = t.read_ms(); 
+                                      
+        led4 = 0;
+}
+
+
+int main (void) 
+{
+    led3 = 0;
+    int iRet=Init();
+
+    if (iRet<0)
+    {
+        printf("Init Error!");
+        return iRet;
+    }
+    t.start();
+    
+    while (true)
+    {
+        //mbed is alive?
+        led2 = !led2;
+        osDelay(500);
+    }     
+}
+
+
+void parse_cmd(int sizeCMD)
+{
             new_send = false;
+            //printf("bufferRX: %s\r\n", bufferRX);
             
             if(strcmp("snap", bufferRX) == 0)              
             {
-                    CameraSnap();
+                    bSnap_on = true;
+                    osSignalSet(tencid, 0x1);
                     memset(bufferRX, 0, sizeof(bufferRX));      
             }else
-            if(strcmp("init_bw_VGA", bufferRX) == 0)                  // Set up for 640*480 pixels RAW     
-            {
-                    format = 'b';
-                    resolution = VGA;
-                    if(camera.Init('b', VGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_yuv_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels YUV422   
+            if(strncmp("grab", bufferRX, 4) == 0)               // grab start       grab stop  
             {
-                    format = 'y';
-                    resolution = QVGA;
-                    if(camera.Init('b', QVGA) != 1)
+                    if(strncmp("start", &bufferRX[5], 5) == 0)
+                    {
+                        bGrab_on = true;
+                        osSignalSet(tencid, 0x1);
+                        memset(bufferRX, 0, sizeof(bufferRX));
+                    }else
                     {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_rgb_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels RGB565   
+                        bGrab_on = false;
+                        memset(bufferRX, 0, sizeof(bufferRX));
+                    }      
+            }else
+            if(strncmp("init", bufferRX, 4) == 0)               // init RGB 19200
             {
-                    format = 'r';
-                    resolution = QVGA;
-                    if(camera.Init('r', QVGA) != 1)
+                    char word[10];
+                    strncpy(CMDCamera.format, &bufferRX[5], 3);
+                    strncpy(word, &bufferRX[9], sizeCMD-9);                  
+                    CMDCamera.resolution = atoi(word);
+                    
+                    printf("CMDCamera.format: %s\r\n", CMDCamera.format);
+                    printf("CMDCamera.resolution: %d\r\n", CMDCamera.resolution); 
+
+                    if(camera.Init(CMDCamera.format, CMDCamera.resolution) != 1)
                     {
                       printf("Init Fail\r\n");
                     }
+                    
+                    sprintf(bufferTX, "OV Init OK\r\n");
+                    client.send_all(bufferTX, sizeof(bufferTX));
                     memset(bufferRX, 0, sizeof(bufferRX));
             }else 
-            if(strcmp("init_bw_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels YUV (Only Y)         
-            {
-                    format = 'b';
-                    resolution = QVGA;
-                    if(camera.Init('b', QVGA) != 1)
+            if(strncmp("reg", bufferRX, 3) == 0)                // reg r 018       reg w 018 004        reg 
+            {        
+                    if(!bGrab_on)
                     {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_yuv_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels YUV422
-            {                            
-                    format = 'y';
-                    resolution = QQVGA;
-                    if(camera.Init('b', QQVGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
+                        strncpy(&Reg.type, &bufferRX[4], 1);
+                        char word[3];                    
+                    
+        
+                        if(Reg.type == 'r')
+                        {
+                            strncpy(word, &bufferRX[6], 3);
+                            Reg.addr = atoi(word);
+                            memset(word, 0, sizeof(word));
+                            sprintf(bufferTX, "reg: %x", camera.ReadReg(Reg.addr));  
+                        }else
+                        {
+                            strncpy(word, &bufferRX[6], 3);
+                            Reg.addr = atoi(word);
+                            strncpy(word, &bufferRX[10], 3);
+                            Reg.value = atoi(word);
+                            memset(word, 0, sizeof(word));
+                            camera.WriteReg(Reg.addr, Reg.value);
+                            sprintf(bufferTX, "%x", camera.ReadReg(Reg.addr));
+                        }
+                        if(sizeCMD == 3)
+                        {
+                            int n = 0;
+                            int g = 0;
+                            int c = 650;
+                            sprintf(bufferTX, "reg_tab: ");
+                            for (int i=0;i<201;i++) 
+                            {
+                                n = snprintf(9+bufferTX+g, c, "%02X", camera.ReadReg(i));
+                                g = g + n; 
+                                c = c - n - 1;                                          
+                            }                  
+                            client.send_all(bufferTX, sizeof(bufferTX));                      
+                        }
+                  
+                        client.send_all(bufferTX, sizeof(bufferTX));
                     }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else   
-            if(strcmp("init_rgb_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels RGB565
-            {                            
-                    format = 'r';
-                    resolution = QQVGA;
-                    if(camera.Init('r', QQVGA) != 1)
+            }else
+            if(strncmp("w_bit", bufferRX, 5) == 0)                  //  w_bit0 018 001         w_bit1 018 001 
+            {        
+                    int type;
+                    int reg_addr = 0;                
+                    int reg_value = 0;
+                    char word[3];
+                    strncpy(word, &bufferRX[5], 1); 
+                    type = atoi(word);
+                    strncpy(word, &bufferRX[7], 3);  
+                    reg_addr = atoi(word);
+                    memset(word, 0, sizeof(word));
+                    strncpy(word, &bufferRX[11], 3); 
+                    reg_value = atoi(word); 
+                    //printf("reg read write: addr: %x - value %x\r\n", reg_addr, reg_value);
+                    int reg_value_now = camera.ReadReg(reg_addr);
+                    
+                    if(type == 0)
                     {
-                      printf("Init Fail\r\n");
+                        reg_value = reg_value_now ^ reg_value; 
+                    }else
+                    {
+                        reg_value = reg_value_now | reg_value;
                     }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else
-            if(strcmp("init_bw_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels YUV (Only Y)
-            {                        
-                    format = 'b';
-                    resolution = QQVGA;
-                    if(camera.Init('b', QQVGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
+                    //printf("reg value new: reg_value_now: %x - reg_value %x\r\n", reg_value_now, reg_value);                   
+                    camera.WriteReg(reg_addr, reg_value);
+                    memset(word, 0, sizeof(word));
+                    memset(bufferTX, 0, sizeof(bufferRX));
             }else
             if(strcmp("reset", bufferRX) == 0)              
             {
@@ -167,79 +272,15 @@
             }else
             if(strcmp("time", bufferRX) == 0)              
             {
-                    sprintf(bufferTX, "Tot time acq + send (mbed): %dms\r\n", t2-t1);
+                    sprintf(bufferTX, "OV Tot time acq + send (mbed): %dms\r\n", t2-t1);                  
                     client.send_all(bufferTX, sizeof(bufferTX));
                     memset(bufferTX, 0, sizeof(bufferTX));
-            }else
-            if(strcmp("reg", bufferRX) == 0)              
+            }else   
+            if(strcmp("ciao", bufferRX) == 0)              
             {
-                    int n = 0;
-                    int g = 0;
-                    int c = 650;
-                    for (int i=0;i<201;i++) 
-                    {
-                        n = snprintf(bufferTX+g, c, "%02X", camera.ReadReg(i));
-                        g = g + n; 
-                        c = c - n - 1;                                          
-                    }                    
-                    client.send_all(bufferTX, sizeof(bufferTX));                       
-                    memset(bufferTX, 0, sizeof(bufferTX));     
-            }    
+                    sprintf(bufferTX, "OV Test OK\r\n");                   
+                    client.send_all(bufferTX, sizeof(bufferTX));
+                    memset(bufferTX, 0, sizeof(bufferTX));                         
+            }            
             memset(bufferRX, 0, sizeof(bufferRX));           
 }
-
-
-void CameraSnap(){
-        led4 = 1;
-        t1 = t.read_ms();
-        
-                // Kick things off by capturing an image
-        camera.CaptureNext();
-        while(camera.CaptureDone() == false);      
-                // Start reading in the image data from the camera hardware buffer                   
-        camera.ReadStart();  
-
-     
-        int p = 0;         
-        if(format == 'b')                       // B&W  
-        {  
-            for(int x = 0; x<resolution/sizeof(bufferTX); x++)
-            {
-                for(int q = 0; q<sizeof(bufferTX); q++)
-                { 
-                       // Read in the first half of the image
-                       if(resolution != VGA)
-                            camera.ReadOnebyte();  
-                       // Read in the Second half of the image
-                       bufferTX[q] = camera.ReadOnebyte();      // Y only                                        
-                }  
-                Send();       
-            } 
-        }     
-        
-        if(format == 'y' || format == 'r')          // Color
-        {
-            for(int x = 0; x<(resolution/sizeof(bufferTX))*2; x++)
-            {
-                for(int q = 0; q<sizeof(bufferTX)/2; q++)
-                {
-                       p = q*2;         
-                       // Read in the first half of the image
-                       bufferTX[p] = camera.ReadOnebyte();   
-                       // Read in the Second half of the image
-                       bufferTX[p+1] = camera.ReadOnebyte();                           
-                }  
-                Send();       
-            }         
-        }   
-        camera.ReadStop();               
-        t2 = t.read_ms(); 
-                        
-                 // Immediately request the next image to be captured (takes around 45ms)
-        camera.CaptureNext();                             
-                // Now wait for the image to finish being captured
-        while (camera.CaptureDone() == false);
-        
-        //printf("Snap_done\r\n");   
-        led4 = 0;
-}
\ No newline at end of file