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/

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Mon Jul 15 20:51:31 2013 +0000
Child:
1:0e4d23df43ea
Commit message:
First Rev

Changed in this revision

EthernetInterface.lib 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
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
ov7670.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/EthernetInterface/#40640efbfcae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,231 @@
+/*
+ * Author: Edoardo De Marchi
+ * Date: 15/07/13
+ * Notes: OV7670 camera with Ethernet Connection
+*/
+
+#include "main.h"
+
+#define VGA     307200         //640*480
+#define QVGA    76800          //320*240
+#define QQVGA   19200          //160*120
+
+static char format = ' ';
+static int resolution = 0;
+
+
+int Init()
+{
+    led1 = 0;               
+    led2 = 0;        // mbed status       
+    led3 = 0;        // Ethernet link enable/disable      
+    led4 = 0;               
+    
+    
+    
+    //ETHERNET
+    eth.init(ip, mask, gateway);
+    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); 
+       
+    return 0;
+}
+
+
+
+int main (void) 
+{
+    Init();
+
+    t.start();
+    
+    while (true)
+    {
+        //mbed is alive?
+        led2 = !led2;
+        osDelay(500);
+    }     
+}
+
+
+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;
+            int n = client.receive(bufferRX, sizeof(bufferRX));  
+            if (n <= 0) break;
+            
+            bufferRX[n]=0; // make terminater
+            parse_cmd();
+        }
+        client.close();
+        //printf("Connection close.\r\n");
+    } 
+} 
+
+
+void Send()
+{
+        client.send_all(bufferTX, sizeof(bufferTX));
+        memset(bufferTX, 0, sizeof(bufferTX));
+}
+
+
+void parse_cmd(){
+            new_send = false;
+            
+            if(strcmp("snap", bufferRX) == 0)              
+            {
+                    CameraSnap();
+                    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   
+            {
+                    format = 'y';
+                    resolution = QVGA;
+                    if(camera.Init('b', QVGA) != 1)
+                    {
+                      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   
+            {
+                    format = 'r';
+                    resolution = QVGA;
+                    if(camera.Init('r', QVGA) != 1)
+                    {
+                      printf("Init Fail\r\n");
+                    }
+                    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)
+                    {
+                      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");
+                    }
+                    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)
+                    {
+                      printf("Init Fail\r\n");
+                    }
+                    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));
+            }else
+            if(strcmp("reset", bufferRX) == 0)              
+            {
+                    mbed_reset();        
+            }else
+            if(strcmp("time", bufferRX) == 0)              
+            {
+                    sprintf(bufferTX, "Tot time acq + send (mbed): %dms\r\n", t2-t1);
+                    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/9600; x++)
+            {
+                for(int q = 0; q<9600; 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/9600)*2; x++)
+            {
+                for(int q = 0; q<9600/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);
+        
+        //pc.printf("Snap_done\r\n");   
+        led4 = 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,53 @@
+#pragma once 
+#include "mbed.h"
+#include "ov7670.h"
+#include "EthernetInterface.h"
+
+
+#define ECHO_SERVER_PORT   2000
+
+
+Timer t;
+bool new_send = false;
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+//Camera
+OV7670 camera
+(
+    p28,p27,            // SDA,SCL(I2C / SCCB)
+    p23,p24,p25,        // VSYNC,HREF,WEN(FIFO)  
+    Port0,0x07878000,   // PortIn data        p18(P0.26),p17(P0.25),p16(P0.24),p15(P0.23),p11(P0.18),p12(P0.17),p14(P0.16),p13(P0.15)
+    p26,p29,p30         // RRST,OE,RCLK
+); 
+
+//ETHERNET
+char* ip = "192.168.153.153";             // ip
+char* mask = "255.255.255.0";           // mask
+char* gateway = "192.168.153.5";          // gateway
+EthernetInterface eth;
+TCPSocketConnection client;
+TCPSocketServer server; 
+
+//ETHERNET BUFFER
+char bufferRX[20];
+char bufferTX[9600];
+
+//RESET
+extern "C" void mbed_reset();
+
+//TIMER
+int t1 = 0; 
+int t2 = 0;
+
+//THREAD
+void net_thread(void const *argument);
+osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE); 
+
+//FUNCTION
+void Send();
+void parse_cmd();
+void CameraSnap();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#58b30ac3f00e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ov7670.lib	Mon Jul 15 20:51:31 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/edodm85/code/ov7670/#354a00023f79