Tedd OKANO / MARY_CAMERA
Revision:
28:b5d5a2f7f0d0
Parent:
27:f31bff7335ae
Child:
29:4432c5282c7b
--- a/MARY_CAMERA.cpp	Sat Mar 22 01:09:04 2014 +0000
+++ b/MARY_CAMERA.cpp	Sat Mar 22 01:26:45 2014 +0000
@@ -1,7 +1,7 @@
 #include    "mbed.h"
 #include    "MARY_CAMERA.h"
 
-#define PARAM_NUM       99
+
 #define CAM_I2C_ADDR    0x42
 
 #define     RESET_PULSE_WIDTH   100     //  mili-seconds
@@ -44,9 +44,12 @@
 #endif
 }
 
-int MARY_CAMERA::init( void )
+int MARY_CAMERA::init( CameraResolution res )
 {
-    const char camera_register_setting[ PARAM_NUM ][ 2 ] = {
+#define PARAM_NUM       99
+#define RES_CHANGE_PARAM_NUM     12
+
+    char camera_register_setting[ PARAM_NUM ][ 2 ] = {
         { 0x01, 0x40 }, { 0x02, 0x60 }, { 0x03, 0x02 }, { 0x0C, 0x0C },
         { 0x0E, 0x61 }, { 0x0F, 0x4B }, { 0x11, 0x81 }, { 0x12, 0x04 },
         { 0x15, 0x00 }, { 0x16, 0x02 }, { 0x17, 0x39 }, { 0x18, 0x03 },
@@ -73,6 +76,13 @@
         { 0xB0, 0x84 }, { 0xB1, 0x0C }, { 0xB2, 0x0E }, { 0xB3, 0x82 },
         { 0xB8, 0x0A }, { 0xC8, 0xF0 }, { 0xC9, 0x60 },
     };
+    const char  res_change_param[ 5 ][ RES_CHANGE_PARAM_NUM ] = {
+        { 0x17, 0x18, 0x32, 0x19, 0x1a, 0x03, 0x0c, 0x3e, 0x71, 0x72, 0x73, 0xa2 }, //  register addr
+        { 0x39, 0x03, 0x80, 0x03, 0x7b, 0x02, 0x0c, 0x11, 0x35, 0x11, 0xf1, 0x52 }, //  QSIF
+        { 0x13, 0x01, 0xb6, 0x02, 0x7a, 0x0a, 0x00, 0x00, 0x35, 0x11, 0xf0, 0x02 }, //  VGA
+        { 0x16, 0x04, 0x80, 0x02, 0x7a, 0x0a, 0x04, 0x19, 0x35, 0x11, 0xf1, 0x02 }, //  QVGA
+        { 0x16, 0x04, 0xa4, 0x02, 0x7a, 0x0a, 0x04, 0x1a, 0x35, 0x22, 0xf2, 0x02 }, //  QQVGA
+    };
     const char camera_reset_command[] = { 0x12, 0x80 };
 
     //  SPI settings
@@ -95,6 +105,16 @@
     _horizontal_size    = QCIF_PIXEL_PER_LINE;
     _vertical_size      = QCIF_LINE_PER_FRAME;
 
+    if ( QCIF != res ) {
+        for ( int i = 0; i < RES_CHANGE_PARAM_NUM; i++ ) {
+            for ( int j = 0; j < PARAM_NUM; j++ ) {
+                if ( camera_register_setting[ j ][ 0 ] == res_change_param[ 0 ][ i ] ) {
+                    camera_register_setting[ j ][ 1 ]   = res_change_param[ res ][ i ];
+                }
+            }
+        }
+    }
+
     for ( int i = 0; i < PARAM_NUM; i++ ) {
         if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting[ i ], 2 )) )
             break;
@@ -107,53 +127,7 @@
 
 void MARY_CAMERA::resolution( CameraResolution res )
 {
-#define     OVERWRITE_PARAM_NUM     12
-    const char  over_write_param[ 5 ][ OVERWRITE_PARAM_NUM ] = {
-        { 0x17, 0x18, 0x32, 0x19, 0x1a, 0x03, 0x0c, 0x3e, 0x71, 0x72, 0x73, 0xa2 }, //  register addr
-        { 0x39, 0x03, 0x80, 0x03, 0x7b, 0x02, 0x0c, 0x11, 0x35, 0x11, 0xf1, 0x52 }, //  QSIF
-        { 0x13, 0x01, 0xb6, 0x02, 0x7a, 0x0a, 0x00, 0x00, 0x35, 0x11, 0xf0, 0x02 }, //  VGA
-        { 0x16, 0x04, 0x80, 0x02, 0x7a, 0x0a, 0x04, 0x19, 0x35, 0x11, 0xf1, 0x02 }, //  QVGA
-        { 0x16, 0x04, 0xa4, 0x02, 0x7a, 0x0a, 0x04, 0x1a, 0x35, 0x22, 0xf2, 0x02 }, //  QQVGA
-    };
-    char    d[ 2 ];
-    
-    if ( init() )
-        return;
-
-    switch ( res ) {
-        case QCIF:
-            _horizontal_size    = QCIF_PIXEL_PER_LINE;
-            _vertical_size      = QCIF_LINE_PER_FRAME;
-            break;
-        case VGA:
-            _horizontal_size    = VGA_PIXEL_PER_LINE;
-            _vertical_size      = VGA_LINE_PER_FRAME;
-            break;
-        case QVGA:
-            _horizontal_size    = VGA_PIXEL_PER_LINE / 2;
-            _vertical_size      = VGA_LINE_PER_FRAME / 2;
-            break;
-        case QQVGA:
-            _horizontal_size    = VGA_PIXEL_PER_LINE / 4;
-            _vertical_size      = VGA_LINE_PER_FRAME / 4;
-            break;
-    }
-
-    //  set camera registers
-
-    for ( int i = 0; i < OVERWRITE_PARAM_NUM; i++ ) {
-        d[ 0 ]  = over_write_param[ 0 ][ i ];
-        d[ 1 ]  = over_write_param[ res ][ i ];
-
-        if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, d, 2 )) )
-            break;
-
-        wait_ms( 20 );  //  camera register writing requires this interval
-    }
-
-    //  wait 1 frame buffer update
-    open_transfer();
-    close_transfer();
+    init( res );
 }
 
 void MARY_CAMERA::colorbar( SwitchState sw )