CQ出版 Interface記事用サンプル。 トラ技カメラ+FIFOからデータを得て、 エリア判定後、デバイスに保存

Dependencies:   SDFileSystem mbed

main.cpp

Committer:
TETSUYA
Date:
2013-09-26
Revision:
0:1648bb4e70e5
Child:
1:c822ea77c7b2

File content as of revision 0:1648bb4e70e5:

/* 
Toragi-CCD-Cam OV7670andFIFO sample use driver

Sadaei Osakabe ( http://mbed.org/users/diasea/ )
  http://mbed.org/users/diasea/code/OV7670_with_AL422B_Color_Size_test/

*/
#include "mbed.h"
#include <algorithm>
#include "ov7670.h"

#define QQVGA
#ifdef QQVGA
# define SIZEX (160)
# define SIZEY (120)
#else
# define SIZEX (320)
# define SIZEY (240)
#endif

OV7670 camera(
    p28,p27,       // SDA,SCL(I2C / SCCB)
    p25,p26,p29,   // VSYNC,HREF,WEN(FIFO)
    p24,p23,p22,p21,p20,p19,p18,p17, // D7-D0
    p5,p6,p7) ; // RRST,OE,RCLK



LocalFileSystem local("local");

Serial pc(USBTX,USBRX) ;

int sizex = 0;
int sizey = 0;

int create_header(FILE *fp, int width, int height) ;

int main() {
    char color_format = 0;
    char c;
    int mx, pixel , i;
    int pixc;
    char packet[40] = { 0 };
    
    pc.baud(115200);
RESJMP:
    //pc.printf("Camera resetting..\r\n");
    camera.Reset();

    //pc.printf("Before Init...\r\n");
    //camera.PrintRegister();
    switch( color_format ){
    case '1':   
        camera.InitRGB444(); 
        break;
    case '2':   
        camera.InitRGB555(); 
        break;
    default:
        camera.InitRGB565(); 
        break;
    }
    
    //sizex = 160;
    //sizey = 120;
    camera.InitQQVGA();

    camera.InitForFIFOWriteReset();
    camera.InitDefaultReg(true, true);  // flipv/flipH

    //pc.printf("After Init...\r\n");
    //camera.PrintRegister();


    // CAPTURE and SEND LOOP
    while(1)
    {
        pc.printf("Hit Any Key to send RGBx160x120 Capture Data.\r\n") ;
        while(!pc.readable()) ;
        c = pc.getc() ;
        if (( c == '1' )|| ( c == '2' )||( c == '3' )){
            color_format = c;
            goto RESJMP;
        }
        camera.CaptureNext();   // sample start!
        while(camera.CaptureDone() == false);
        camera.ReadStart();
        pixc = 0;
        for (int y = 0;y < SIZEY;y++) {
            int r,g,b,d1,d2 ;
            mx = 0;
            for (int x = 0;x < SIZEX;x++) {
                d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B
                d2 = camera.ReadOneByte() ; // upper nibble is G   , lower nibble is R
                pixel = ((unsigned int)d2 << 8) + ((unsigned int)d1 & 0xff);

                // send <CRLF> and Count on each 16dot.
                if ( !(x%16) ){
                    pc.printf("\r\n%04x: ",pixc ) ;
                    pixc +=16;
                }
                pc.printf("%04x ",pixel) ;
            }
            
        }
        camera.ReadStop();
        //pc.printf("\r\n") ;
    }
}



/*

#if defined(BITMAPFILE) || defined(BAYERBITMAPFILE)
#define BITMAPFILE
#undef BAYERBITMAPFILE
#undef HEXFILE
#undef COLORBAR

#define FILEHEADERSIZE 14                   //ファイルヘッダのサイズ
#define INFOHEADERSIZE 40                   //情報ヘッダのサイズ
#define HEADERSIZE (FILEHEADERSIZE+INFOHEADERSIZE)

int create_header(FILE *fp, int width, int height) {
    int real_width;
    unsigned char header_buf[HEADERSIZE]; //ヘッダを格納する
    unsigned int file_size;
    unsigned int offset_to_data;
    unsigned long info_header_size;
    unsigned int planes;
    unsigned int color;
    unsigned long compress;
    unsigned long data_size;
    long xppm;
    long yppm;

    real_width = width*3 + width%4;

    //ここからヘッダ作成
    file_size = height * real_width + HEADERSIZE;
    offset_to_data = HEADERSIZE;
    info_header_size = INFOHEADERSIZE;
    planes = 1;
    color = 24;
    compress = 0;
    data_size = height * real_width;
    xppm = 1;
    yppm = 1;

    header_buf[0] = 'B';
    header_buf[1] = 'M';
    memcpy(header_buf + 2, &file_size, sizeof(file_size));
    header_buf[6] = 0;
    header_buf[7] = 0;
    header_buf[8] = 0;
    header_buf[9] = 0;
    memcpy(header_buf + 10, &offset_to_data, sizeof(offset_to_data));
    memcpy(header_buf + 14, &info_header_size, sizeof(info_header_size));
    memcpy(header_buf + 18, &width, sizeof(width));
    height = height * -1; // データ格納順が逆なので、高さをマイナスとしている
    memcpy(header_buf + 22, &height, sizeof(height));
    memcpy(header_buf + 26, &planes, sizeof(planes));
    memcpy(header_buf + 28, &color, sizeof(color));
    memcpy(header_buf + 30, &compress, sizeof(compress));
    memcpy(header_buf + 34, &data_size, sizeof(data_size));
    memcpy(header_buf + 38, &xppm, sizeof(xppm));
    memcpy(header_buf + 42, &yppm, sizeof(yppm));
    header_buf[46] = 0;
    header_buf[47] = 0;
    header_buf[48] = 0;
    header_buf[49] = 0;
    header_buf[50] = 0;
    header_buf[51] = 0;
    header_buf[52] = 0;
    header_buf[53] = 0;

    //ヘッダの書き込み
    fwrite(header_buf, sizeof(unsigned char), HEADERSIZE, fp);

    return 0;
}
#endif
*/