Sumio Morioka / Mbed 2 deprecated if201410_section4

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2014, Sumio Morioka
00003  * All rights reserved.
00004  *
00005  * This source code was originally written by Dr.Sumio Morioka for use in the Oct 2014 issue of 
00006  * "the Interface magazine", published by CQ publishing Co.Ltd in Japan (http://www.cqpub.co.jp).
00007  * The author has no responsibility on any results caused by using this code.
00008  *
00009  * - Distribution date of this code: Aug 26, 2014
00010  * - Author: Dr.Sumio Morioka (http://www002.upp.so-net.ne.jp/morioka)
00011  *
00012  *
00013  * IMPORTANT NOTICE:
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *   * Redistributions of source code must retain the above copyright
00017  *     notice, this list of conditions and the following disclaimer.
00018  *   * Redistributions in binary form must reproduce the above copyright
00019  *     notice, this list of conditions and the following disclaimer in the
00020  *     documentation and/or other materials provided with the distribution.
00021  *   * Neither the name of the copyright holder nor the
00022  *     names of its contributors may be used to endorse or promote products
00023  *     derived from this software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00030  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00032  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  */
00036 
00037 #include "mbed.h"
00038 #include "TextLCD.h"
00039 #include "LocalFileSystem.h"
00040 
00041 #include "ov7670.h"
00042 
00043 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00044 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00045 PwmOut sv0(p21), sv1(p22), sv2(p23);
00046 
00047 LocalFileSystem local("webfs");
00048 
00049 OV7670 camera(
00050     p9,p10,         // SDA,SCL(I2C / SCCB)
00051     p5,p6,p7,       // VSYNC,HREF,WEN(FIFO)
00052     p20,p19,p18,p17,p16,p15,p14,p13,    // D7-D0
00053     p8,p11,p12);    // RRST,OE,RCLK
00054 
00055 Timer tmr;
00056 
00057 //#define QQVGA
00058 #define QVGA
00059 //#define VGA34
00060 
00061 #ifdef QQVGA
00062 #define SIZEX 160
00063 #define SIZEY 120
00064 #endif
00065 
00066 #ifdef QVGA
00067 #define SIZEX 320
00068 #define SIZEY 240
00069 #endif
00070 
00071 #ifdef VGA34
00072 #define SIZEX 480
00073 #define SIZEY 360
00074 #endif
00075 
00076 
00077 void cam_cap(void);
00078 
00079 int main() 
00080 {
00081     led1 = 0;
00082     led2 = 0;
00083     led3 = 0;
00084     led4 = 0;
00085 
00086     sv0.period_us(20000);       // 20ms
00087     sv0.pulsewidth_us(5000);    // 5ms
00088 
00089     sv1.period_us(20000);       // 20ms
00090     sv1.pulsewidth_us(10000);   // 10ms
00091 
00092     sv2.period_us(20000);       // 20ms
00093     sv2.pulsewidth_us(15000);   // 15ms
00094     
00095     ////////////////////////////////////////////////////////////////////////////
00096     camera.WriteReg(0x12, 0x80);            // com7; reset
00097     wait_ms(200);
00098 
00099     camera.InitDefaultReg();
00100 
00101     // negate vsync
00102     camera.WriteReg(0x15, 0x02);            // com10; negative vsync
00103 
00104 #ifdef QQVGA
00105     camera.InitQQVGA();
00106 #endif
00107 #ifdef QVGA
00108     camera.InitQVGA();
00109 #endif
00110 #ifdef VGA34
00111     camera.InitVGA_3_4();
00112 #endif
00113 
00114     // data format
00115     camera.WriteReg(0x12, 0x04 + 0);    // com7 RGB (bit1...test pattern)
00116     camera.WriteReg(0x40, 0xD0);    // com15 RGB565
00117     camera.WriteReg(0x8c, 0x00);    // RGB444
00118 
00119     wait_ms(300);
00120 
00121     lcd.locate(0, 0);
00122     lcd.printf("Capture");
00123     tmr.start();    // timer
00124 
00125     cam_cap();
00126 
00127     tmr.stop();     // timer
00128     lcd.locate(0, 1);
00129     lcd.printf("%dms", tmr.read_ms());
00130 
00131     //////////////////////////////////////////////////
00132     while (1) {
00133         led1 = 1;
00134 //      wait_ms(100);
00135    }
00136 
00137 }
00138 
00139 
00140 //void cam_capture(char *input, char *output)       // compile error
00141 //void cam_cap(Arguments* input, Reply* output)
00142 void cam_cap(void)
00143 {
00144     FILE *fp_bmp;
00145     unsigned int d1, d2;
00146     unsigned char sort[3];
00147 
00148     led2 = 1;
00149 
00150     fp_bmp  = fopen("/webfs/cam.bmp", "wb");
00151 
00152     /////////////////////////
00153     // file header
00154     /////////////////////////
00155     fprintf(fp_bmp, "BM");
00156     int val = 14 + 40 + SIZEX * SIZEY * 3;   // file size
00157     fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000);
00158     fprintf(fp_bmp, "%c%c%c%c%c%c%c%c", 0, 0, 0, 0, 0x36, 0, 0, 0);
00159 
00160     /////////////////////////
00161     // information header
00162     /////////////////////////
00163     fprintf(fp_bmp, "%c%c%c%c", 0x28, 0, 0, 0);  // header size
00164     fprintf(fp_bmp, "%c%c%c%c", SIZEX % 0x100, SIZEX / 0x100, SIZEX / 0x10000, SIZEX / 0x1000000);
00165     fprintf(fp_bmp, "%c%c%c%c", SIZEY % 0x100, SIZEY / 0x100, SIZEY / 0x10000, SIZEY / 0x1000000);
00166     fprintf(fp_bmp, "%c%c", 1, 0);               // # of plane
00167     fprintf(fp_bmp, "%c%c", 24, 0);              // bit count
00168     fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);     // compression
00169     val = SIZEX * SIZEY * 3;         // data size
00170     fprintf(fp_bmp, "%c%c%c%c", val % 0x100, val / 0x100, val / 0x10000, val / 0x1000000);
00171     fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
00172     fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
00173     fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
00174     fprintf(fp_bmp, "%c%c%c%c", 0, 0, 0, 0);
00175 
00176     camera.CaptureNext();   // sample start!
00177 
00178     while(camera.CaptureDone() == false)
00179         ;
00180 
00181     camera.ReadStart();     // reset pointer
00182 
00183     led3 = 1;
00184 
00185     for (int y = 0;y < SIZEY;y++) {    
00186         for (int x = 0;x < SIZEX;x++) {
00187             d1 = camera.ReadOneByte() ; // upper nibble is XXX , lower nibble is B
00188             d2 = camera.ReadOneByte() ; // upper nibble is G   , lower nibble is R
00189 
00190             // RGB565
00191             sort[0] = ((d1 & 0xF8) >> 3) << 3;      // R
00192             sort[1] = ( ((d1 & 0x07) << 3) + ((d2 & 0xE0) >> 5) ) << 2;     // G
00193             sort[2] = (d2 & 0x1F) << 3;             // B
00194 
00195             fprintf(fp_bmp, "%c%c%c", sort[2], sort[1], sort[0]);       // B,G,R
00196         }
00197     }
00198 
00199     camera.ReadStop();
00200     fclose(fp_bmp);
00201 
00202     led2 = 0;
00203     led3 = 0;
00204 //  led4 = 0;
00205 }
00206 
00207 // end of file