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

Dependencies:   SDFileSystem mbed

Committer:
TETSUYA
Date:
Thu Sep 26 06:59:31 2013 +0000
Revision:
0:1648bb4e70e5
Child:
1:c822ea77c7b2
V0.00 FirstVersions;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TETSUYA 0:1648bb4e70e5 1 /*
TETSUYA 0:1648bb4e70e5 2 Toragi-CCD-Cam OV7670andFIFO sample use driver
TETSUYA 0:1648bb4e70e5 3
TETSUYA 0:1648bb4e70e5 4 Sadaei Osakabe ( http://mbed.org/users/diasea/ )
TETSUYA 0:1648bb4e70e5 5 http://mbed.org/users/diasea/code/OV7670_with_AL422B_Color_Size_test/
TETSUYA 0:1648bb4e70e5 6
TETSUYA 0:1648bb4e70e5 7 */
TETSUYA 0:1648bb4e70e5 8 #include "mbed.h"
TETSUYA 0:1648bb4e70e5 9 #include <algorithm>
TETSUYA 0:1648bb4e70e5 10 #include "ov7670.h"
TETSUYA 0:1648bb4e70e5 11
TETSUYA 0:1648bb4e70e5 12 #define QQVGA
TETSUYA 0:1648bb4e70e5 13 #ifdef QQVGA
TETSUYA 0:1648bb4e70e5 14 # define SIZEX (160)
TETSUYA 0:1648bb4e70e5 15 # define SIZEY (120)
TETSUYA 0:1648bb4e70e5 16 #else
TETSUYA 0:1648bb4e70e5 17 # define SIZEX (320)
TETSUYA 0:1648bb4e70e5 18 # define SIZEY (240)
TETSUYA 0:1648bb4e70e5 19 #endif
TETSUYA 0:1648bb4e70e5 20
TETSUYA 0:1648bb4e70e5 21 OV7670 camera(
TETSUYA 0:1648bb4e70e5 22 p28,p27, // SDA,SCL(I2C / SCCB)
TETSUYA 0:1648bb4e70e5 23 p25,p26,p29, // VSYNC,HREF,WEN(FIFO)
TETSUYA 0:1648bb4e70e5 24 p24,p23,p22,p21,p20,p19,p18,p17, // D7-D0
TETSUYA 0:1648bb4e70e5 25 p5,p6,p7) ; // RRST,OE,RCLK
TETSUYA 0:1648bb4e70e5 26
TETSUYA 0:1648bb4e70e5 27
TETSUYA 0:1648bb4e70e5 28
TETSUYA 0:1648bb4e70e5 29 LocalFileSystem local("local");
TETSUYA 0:1648bb4e70e5 30
TETSUYA 0:1648bb4e70e5 31 Serial pc(USBTX,USBRX) ;
TETSUYA 0:1648bb4e70e5 32
TETSUYA 0:1648bb4e70e5 33 int sizex = 0;
TETSUYA 0:1648bb4e70e5 34 int sizey = 0;
TETSUYA 0:1648bb4e70e5 35
TETSUYA 0:1648bb4e70e5 36 int create_header(FILE *fp, int width, int height) ;
TETSUYA 0:1648bb4e70e5 37
TETSUYA 0:1648bb4e70e5 38 int main() {
TETSUYA 0:1648bb4e70e5 39 char color_format = 0;
TETSUYA 0:1648bb4e70e5 40 char c;
TETSUYA 0:1648bb4e70e5 41 int mx, pixel , i;
TETSUYA 0:1648bb4e70e5 42 int pixc;
TETSUYA 0:1648bb4e70e5 43 char packet[40] = { 0 };
TETSUYA 0:1648bb4e70e5 44
TETSUYA 0:1648bb4e70e5 45 pc.baud(115200);
TETSUYA 0:1648bb4e70e5 46 RESJMP:
TETSUYA 0:1648bb4e70e5 47 //pc.printf("Camera resetting..\r\n");
TETSUYA 0:1648bb4e70e5 48 camera.Reset();
TETSUYA 0:1648bb4e70e5 49
TETSUYA 0:1648bb4e70e5 50 //pc.printf("Before Init...\r\n");
TETSUYA 0:1648bb4e70e5 51 //camera.PrintRegister();
TETSUYA 0:1648bb4e70e5 52 switch( color_format ){
TETSUYA 0:1648bb4e70e5 53 case '1':
TETSUYA 0:1648bb4e70e5 54 camera.InitRGB444();
TETSUYA 0:1648bb4e70e5 55 break;
TETSUYA 0:1648bb4e70e5 56 case '2':
TETSUYA 0:1648bb4e70e5 57 camera.InitRGB555();
TETSUYA 0:1648bb4e70e5 58 break;
TETSUYA 0:1648bb4e70e5 59 default:
TETSUYA 0:1648bb4e70e5 60 camera.InitRGB565();
TETSUYA 0:1648bb4e70e5 61 break;
TETSUYA 0:1648bb4e70e5 62 }
TETSUYA 0:1648bb4e70e5 63
TETSUYA 0:1648bb4e70e5 64 //sizex = 160;
TETSUYA 0:1648bb4e70e5 65 //sizey = 120;
TETSUYA 0:1648bb4e70e5 66 camera.InitQQVGA();
TETSUYA 0:1648bb4e70e5 67
TETSUYA 0:1648bb4e70e5 68 camera.InitForFIFOWriteReset();
TETSUYA 0:1648bb4e70e5 69 camera.InitDefaultReg(true, true); // flipv/flipH
TETSUYA 0:1648bb4e70e5 70
TETSUYA 0:1648bb4e70e5 71 //pc.printf("After Init...\r\n");
TETSUYA 0:1648bb4e70e5 72 //camera.PrintRegister();
TETSUYA 0:1648bb4e70e5 73
TETSUYA 0:1648bb4e70e5 74
TETSUYA 0:1648bb4e70e5 75 // CAPTURE and SEND LOOP
TETSUYA 0:1648bb4e70e5 76 while(1)
TETSUYA 0:1648bb4e70e5 77 {
TETSUYA 0:1648bb4e70e5 78 pc.printf("Hit Any Key to send RGBx160x120 Capture Data.\r\n") ;
TETSUYA 0:1648bb4e70e5 79 while(!pc.readable()) ;
TETSUYA 0:1648bb4e70e5 80 c = pc.getc() ;
TETSUYA 0:1648bb4e70e5 81 if (( c == '1' )|| ( c == '2' )||( c == '3' )){
TETSUYA 0:1648bb4e70e5 82 color_format = c;
TETSUYA 0:1648bb4e70e5 83 goto RESJMP;
TETSUYA 0:1648bb4e70e5 84 }
TETSUYA 0:1648bb4e70e5 85 camera.CaptureNext(); // sample start!
TETSUYA 0:1648bb4e70e5 86 while(camera.CaptureDone() == false);
TETSUYA 0:1648bb4e70e5 87 camera.ReadStart();
TETSUYA 0:1648bb4e70e5 88 pixc = 0;
TETSUYA 0:1648bb4e70e5 89 for (int y = 0;y < SIZEY;y++) {
TETSUYA 0:1648bb4e70e5 90 int r,g,b,d1,d2 ;
TETSUYA 0:1648bb4e70e5 91 mx = 0;
TETSUYA 0:1648bb4e70e5 92 for (int x = 0;x < SIZEX;x++) {
TETSUYA 0:1648bb4e70e5 93 d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B
TETSUYA 0:1648bb4e70e5 94 d2 = camera.ReadOneByte() ; // upper nibble is G , lower nibble is R
TETSUYA 0:1648bb4e70e5 95 pixel = ((unsigned int)d2 << 8) + ((unsigned int)d1 & 0xff);
TETSUYA 0:1648bb4e70e5 96
TETSUYA 0:1648bb4e70e5 97 // send <CRLF> and Count on each 16dot.
TETSUYA 0:1648bb4e70e5 98 if ( !(x%16) ){
TETSUYA 0:1648bb4e70e5 99 pc.printf("\r\n%04x: ",pixc ) ;
TETSUYA 0:1648bb4e70e5 100 pixc +=16;
TETSUYA 0:1648bb4e70e5 101 }
TETSUYA 0:1648bb4e70e5 102 pc.printf("%04x ",pixel) ;
TETSUYA 0:1648bb4e70e5 103 }
TETSUYA 0:1648bb4e70e5 104
TETSUYA 0:1648bb4e70e5 105 }
TETSUYA 0:1648bb4e70e5 106 camera.ReadStop();
TETSUYA 0:1648bb4e70e5 107 //pc.printf("\r\n") ;
TETSUYA 0:1648bb4e70e5 108 }
TETSUYA 0:1648bb4e70e5 109 }
TETSUYA 0:1648bb4e70e5 110
TETSUYA 0:1648bb4e70e5 111
TETSUYA 0:1648bb4e70e5 112
TETSUYA 0:1648bb4e70e5 113 /*
TETSUYA 0:1648bb4e70e5 114
TETSUYA 0:1648bb4e70e5 115 #if defined(BITMAPFILE) || defined(BAYERBITMAPFILE)
TETSUYA 0:1648bb4e70e5 116 #define BITMAPFILE
TETSUYA 0:1648bb4e70e5 117 #undef BAYERBITMAPFILE
TETSUYA 0:1648bb4e70e5 118 #undef HEXFILE
TETSUYA 0:1648bb4e70e5 119 #undef COLORBAR
TETSUYA 0:1648bb4e70e5 120
TETSUYA 0:1648bb4e70e5 121 #define FILEHEADERSIZE 14 //ファイルヘッダのサイズ
TETSUYA 0:1648bb4e70e5 122 #define INFOHEADERSIZE 40 //情報ヘッダのサイズ
TETSUYA 0:1648bb4e70e5 123 #define HEADERSIZE (FILEHEADERSIZE+INFOHEADERSIZE)
TETSUYA 0:1648bb4e70e5 124
TETSUYA 0:1648bb4e70e5 125 int create_header(FILE *fp, int width, int height) {
TETSUYA 0:1648bb4e70e5 126 int real_width;
TETSUYA 0:1648bb4e70e5 127 unsigned char header_buf[HEADERSIZE]; //ヘッダを格納する
TETSUYA 0:1648bb4e70e5 128 unsigned int file_size;
TETSUYA 0:1648bb4e70e5 129 unsigned int offset_to_data;
TETSUYA 0:1648bb4e70e5 130 unsigned long info_header_size;
TETSUYA 0:1648bb4e70e5 131 unsigned int planes;
TETSUYA 0:1648bb4e70e5 132 unsigned int color;
TETSUYA 0:1648bb4e70e5 133 unsigned long compress;
TETSUYA 0:1648bb4e70e5 134 unsigned long data_size;
TETSUYA 0:1648bb4e70e5 135 long xppm;
TETSUYA 0:1648bb4e70e5 136 long yppm;
TETSUYA 0:1648bb4e70e5 137
TETSUYA 0:1648bb4e70e5 138 real_width = width*3 + width%4;
TETSUYA 0:1648bb4e70e5 139
TETSUYA 0:1648bb4e70e5 140 //ここからヘッダ作成
TETSUYA 0:1648bb4e70e5 141 file_size = height * real_width + HEADERSIZE;
TETSUYA 0:1648bb4e70e5 142 offset_to_data = HEADERSIZE;
TETSUYA 0:1648bb4e70e5 143 info_header_size = INFOHEADERSIZE;
TETSUYA 0:1648bb4e70e5 144 planes = 1;
TETSUYA 0:1648bb4e70e5 145 color = 24;
TETSUYA 0:1648bb4e70e5 146 compress = 0;
TETSUYA 0:1648bb4e70e5 147 data_size = height * real_width;
TETSUYA 0:1648bb4e70e5 148 xppm = 1;
TETSUYA 0:1648bb4e70e5 149 yppm = 1;
TETSUYA 0:1648bb4e70e5 150
TETSUYA 0:1648bb4e70e5 151 header_buf[0] = 'B';
TETSUYA 0:1648bb4e70e5 152 header_buf[1] = 'M';
TETSUYA 0:1648bb4e70e5 153 memcpy(header_buf + 2, &file_size, sizeof(file_size));
TETSUYA 0:1648bb4e70e5 154 header_buf[6] = 0;
TETSUYA 0:1648bb4e70e5 155 header_buf[7] = 0;
TETSUYA 0:1648bb4e70e5 156 header_buf[8] = 0;
TETSUYA 0:1648bb4e70e5 157 header_buf[9] = 0;
TETSUYA 0:1648bb4e70e5 158 memcpy(header_buf + 10, &offset_to_data, sizeof(offset_to_data));
TETSUYA 0:1648bb4e70e5 159 memcpy(header_buf + 14, &info_header_size, sizeof(info_header_size));
TETSUYA 0:1648bb4e70e5 160 memcpy(header_buf + 18, &width, sizeof(width));
TETSUYA 0:1648bb4e70e5 161 height = height * -1; // データ格納順が逆なので、高さをマイナスとしている
TETSUYA 0:1648bb4e70e5 162 memcpy(header_buf + 22, &height, sizeof(height));
TETSUYA 0:1648bb4e70e5 163 memcpy(header_buf + 26, &planes, sizeof(planes));
TETSUYA 0:1648bb4e70e5 164 memcpy(header_buf + 28, &color, sizeof(color));
TETSUYA 0:1648bb4e70e5 165 memcpy(header_buf + 30, &compress, sizeof(compress));
TETSUYA 0:1648bb4e70e5 166 memcpy(header_buf + 34, &data_size, sizeof(data_size));
TETSUYA 0:1648bb4e70e5 167 memcpy(header_buf + 38, &xppm, sizeof(xppm));
TETSUYA 0:1648bb4e70e5 168 memcpy(header_buf + 42, &yppm, sizeof(yppm));
TETSUYA 0:1648bb4e70e5 169 header_buf[46] = 0;
TETSUYA 0:1648bb4e70e5 170 header_buf[47] = 0;
TETSUYA 0:1648bb4e70e5 171 header_buf[48] = 0;
TETSUYA 0:1648bb4e70e5 172 header_buf[49] = 0;
TETSUYA 0:1648bb4e70e5 173 header_buf[50] = 0;
TETSUYA 0:1648bb4e70e5 174 header_buf[51] = 0;
TETSUYA 0:1648bb4e70e5 175 header_buf[52] = 0;
TETSUYA 0:1648bb4e70e5 176 header_buf[53] = 0;
TETSUYA 0:1648bb4e70e5 177
TETSUYA 0:1648bb4e70e5 178 //ヘッダの書き込み
TETSUYA 0:1648bb4e70e5 179 fwrite(header_buf, sizeof(unsigned char), HEADERSIZE, fp);
TETSUYA 0:1648bb4e70e5 180
TETSUYA 0:1648bb4e70e5 181 return 0;
TETSUYA 0:1648bb4e70e5 182 }
TETSUYA 0:1648bb4e70e5 183 #endif
TETSUYA 0:1648bb4e70e5 184 */