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

Dependencies:   SDFileSystem mbed

Revision:
2:3aed552f3385
Parent:
1:c822ea77c7b2
Child:
3:8d5594ce5b00
--- a/main.cpp	Fri Sep 27 03:19:01 2013 +0000
+++ b/main.cpp	Fri Sep 27 09:21:46 2013 +0000
@@ -11,6 +11,7 @@
 #include <algorithm>
 #include "ov7670.h"
 
+
 #define QQVGA
 #ifdef QQVGA
 # define SIZEX (160)
@@ -29,7 +30,8 @@
 
 
 LocalFileSystem local("local");
-
+BusOut myleds(LED1, LED2, LED3, LED4);
+ 
 Serial pc(USBTX,USBRX) ;
 
 int sizex = 0;
@@ -37,11 +39,16 @@
 
 #define AREA_X 32
 #define AREA_Y 24
+#define AREA_DIV_X 5
+#define AREA_DIV_Y 5
+//#define AREA_THRESH  12000
+#define AREA_THRESH  8000
+
 
 char canvas[160][120] ; //18.5KB
-signed int  AreaDiff[2][5][5];   // 5x5 Area = 32x24dot Each Block
+signed int  AreaDiff[2][AREA_DIV_X][AREA_DIV_Y];   // 5x5 Area = 32x24dot Each Block
 int face_bank = 0;
-
+int  Dbmode = 0;    
 
 
 int main() {
@@ -84,18 +91,30 @@
     //pc.printf("After Init...\r\n");
     //camera.PrintRegister();
 
-
     // CAPTURE and SEND LOOP
     while(1)
     {
         memset( &AreaDiff[face_bank], '\0', sizeof(AreaDiff[face_bank]));
-        
-        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;
+        if ( Dbmode ){
+            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;
+            }else  if (( c == 'd' )|| ( c == 'D' )){
+                    Dbmode = 0;     // GO FreeRun
+                    continue;
+            }
+        }else{
+            if ( pc.readable()) {
+                c = pc.getc() ;
+                if (( c == 'd' )|| ( c == 'D' )){
+                    Dbmode = 1;     // GO DEBUGmode
+                    continue;
+                }
+            }
+            pc.printf(" \r\n") ;
         }
         camera.CaptureNext();   // sample start!
         while(camera.CaptureDone() == false);
@@ -124,18 +143,20 @@
                 
                 pixelg = ((gray>>1)<<11) + (gray<<5) + (gray>>1);
                 pixel = pixelg;
-                canvas[x][y] = (char)( pixelg ) & 0x3f;    // 6bit gray scale
+                canvas[x][y] = (char)( gray ) & 0x3f;    // 6bit gray scale
                 
                 // Make Difference AreaData
-                AreaDiff[face_bank][x/AREA_X][ay] += ( gray << 2 );    // 8bit scaler! 
+                //AreaDiff[face_bank][x/AREA_X][ay] += ( gray << 2 );    // 8bit scaler! 
+                AreaDiff[face_bank][x/AREA_X][ay] += ( gray  );    // 6bit scaler! 
                 
-                // send <CRLF> and Count on each 16dot.
-                if ( !(x%16) ){
-                    pc.printf("\r\n%04x: ",pixc ) ;
-                    pixc +=16;
+                if ( Dbmode ){
+                    // send <CRLF> and Count on each 16dot.
+                    if ( !(x%16) ){
+                        pc.printf("\r\n%04x: ",pixc ) ;
+                        pixc +=16;
+                    }
+                    pc.printf("%04x ",pixel) ;
                 }
-                pc.printf("%04x ",pixel) ;
-                
             }
             
         }
@@ -143,23 +164,77 @@
         
         // Difference Check!!
         pc.printf("\r\n" ) ;
+        
+        int err_c=0;
         int backArea = ( face_bank + 1 )&0x01;
-        for (int y = 0; y < 5; y++) {
-            pc.printf("Diff: " ) ;
-            for (int x = 0; x < 5; x++) {
+        for (int y = 0; y < AREA_DIV_Y; y++) {
+            if ( Dbmode ) pc.printf("Diff: " ) ;
+
+            for (int x = 0; x < AREA_DIV_X; x++) {
                 int dif = abs( AreaDiff[backArea][x][y] - AreaDiff[face_bank][x][y] );
-                if (dif >= 20000) {
-                    pc.printf("X%5d " , dif) ;
+                if (dif >= AREA_THRESH) {
+                    err_c++;        // Error Count
+
+                    if ( Dbmode ) pc.printf("X%5d " , dif) ;
                 }else{
-                    pc.printf("O%5d " , dif) ;
+                    if ( Dbmode ) pc.printf("O%5d " , dif) ;
                 }
             }
-            pc.printf("                                             " ) ;
-            pc.printf("                                             \r\n" ) ;
+            if ( Dbmode ) {
+                pc.printf("                                             " ) ;
+                pc.printf("                                             \r\n" ) ;
+            }
         }
-        
-        
-                
+        int led_s =0;
+        switch ( err_c ){
+        case 1:
+            led_s =1;
+            break;
+        case 2:
+            led_s =3;
+            break;
+        case 3:
+            led_s =7;
+            break;
+        default:
+            if ( err_c >= 3) {
+                led_s =7;
+            }else{
+                led_s =0;
+            }
+            break;
+        } 
+
+        led_s |=  ( face_bank )? 0x08 : 0x00 ;
+        myleds = led_s ;  
+        if ( err_c >= 3 ){
+            //pc.printf("\r\n") ;
+            pixc = 0;
+            for (int y = 0;y < SIZEY;y++) {
+                for (int x = 0;x < SIZEX;x++) {
+                    char gray = 0;
+                    gray = canvas[x][y];
+                    pixel = ((gray>>1)<<11) + (gray<<5) + (gray>>1);
+
+                    // send <CRLF> and Count on each 16dot.
+                    if ( !(x%16) ){
+                        pc.printf("\r\n%04x: ",pixc ) ;
+                        pixc +=16;
+                    }
+                    pc.printf("%04x ",pixel) ;
+                }
+            }
+        }        
+        face_bank++;
+        face_bank&=0x01;
+
+
+        //pc.printf("\r\n") ;
+    }
+}
+
+
+    
 /*
         // Median Filter
         for (int y = 1;y < SIZEY-1;y++) {
@@ -200,17 +275,8 @@
                 pc.printf("%04x ",canvasM[x][y]) ;
             }
         }
-        */
-        face_bank++;
-        face_bank&=0x01;
 
-
-        //pc.printf("\r\n") ;
-    }
-}
-
-
-
+*/
 /*
 int create_header(FILE *fp, int width, int height) ;