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/

main.cpp

Committer:
edodm85
Date:
2014-02-15
Revision:
2:d6b38a4512eb
Parent:
1:0e4d23df43ea
Child:
3:df8de18d7fa9

File content as of revision 2:d6b38a4512eb:

/*
 * Author: Edoardo De Marchi
 * Date: 20/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;        // Snap status       
      
    //ETHERNET
    //Static IP
    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); 
       
    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));
            }else
            if(strcmp("reg", 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));     
            }    
            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;
}