Ryo Hagimoto / Mbed 2 deprecated GR-PEACH_Camera_in_barcode

Dependencies:   GR-PEACH_video mbed zbar_010

Fork of GR-PEACH_Camera_in by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DisplayBace.h"
00003 #if (1) // USB is not used
00004 #else
00005 #include "USBHostMSD.h"
00006 #endif
00007 #include "bitmap.h "
00008 #if defined(TARGET_RZ_A1H)
00009 #if (1) // USB is not used
00010 #else
00011 #include "usb_host_setting.h"
00012 #endif
00013 #else
00014 #define USB_HOST_CH     0
00015 #endif
00016 #if (1) // Add ZBar
00017 #include "zbar_lib.h"
00018 #endif
00019 
00020 #define VIDEO_CVBS             (0)                 /* Analog  Video Signal */
00021 #define VIDEO_CMOS_CAMERA      (1)                 /* Digital Video Signal */
00022 #define VIDEO_YCBCR422         (0)
00023 #define VIDEO_RGB888           (1)
00024 #define VIDEO_RGB565           (2)
00025 
00026 /**** User Selection *********/
00027 #define VIDEO_INPUT_METHOD     (VIDEO_CMOS_CAMERA) /* Select  VIDEO_CVBS or VIDEO_CMOS_CAMERA                       */
00028 #define VIDEO_INPUT_FORMAT     (VIDEO_YCBCR422)    /* Select  VIDEO_YCBCR422 or VIDEO_RGB888 or VIDEO_RGB565        */
00029 #define USE_VIDEO_CH           (0)                 /* Select  0 or 1            If selecting VIDEO_CMOS_CAMERA, should be 0.)               */
00030 #define VIDEO_PAL              (0)                 /* Select  0(NTSC) or 1(PAL) If selecting VIDEO_CVBS, this parameter is not referenced.) */
00031 /*****************************/
00032 
00033 #if USE_VIDEO_CH == (0)
00034 #define VIDEO_INPUT_CH         (DisplayBase::VIDEO_INPUT_CHANNEL_0)
00035 #define VIDEO_INT_TYPE         (DisplayBase::INT_TYPE_S0_VFIELD)
00036 #else
00037 #define VIDEO_INPUT_CH         (DisplayBase::VIDEO_INPUT_CHANNEL_1)
00038 #define VIDEO_INT_TYPE         (DisplayBase::INT_TYPE_S1_VFIELD)
00039 #endif
00040 
00041 #if ( VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 || VIDEO_INPUT_FORMAT == VIDEO_RGB565 )
00042 #define DATA_SIZE_PER_PIC      (2u)
00043 #else
00044 #define DATA_SIZE_PER_PIC      (4u)
00045 #endif
00046 
00047 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
00048     in accordance with the frame buffer burst transfer mode. */
00049 #define PIXEL_HW               (320u)  /* QVGA */
00050 #define PIXEL_VW               (240u)  /* QVGA */
00051 #define VIDEO_BUFFER_STRIDE    (((PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u)
00052 #define VIDEO_BUFFER_HEIGHT    (PIXEL_VW)
00053 
00054 #if (USB_HOST_CH == 1) //Audio Camera Shield USB1
00055 DigitalOut usb1en(P3_8);
00056 #endif
00057 DigitalOut led1(LED1);
00058 DigitalIn  button(USER_BUTTON0);
00059 
00060 #if defined(__ICCARM__)
00061 #pragma data_alignment=16
00062 static uint8_t FrameBuffer_Video_A[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]@ ".mirrorram";  //16 bytes aligned!;
00063 static uint8_t FrameBuffer_Video_B[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]@ ".mirrorram";  //16 bytes aligned!;
00064 #pragma data_alignment=4
00065 #else
00066 static uint8_t FrameBuffer_Video_A[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(16)));  //16 bytes aligned!;
00067 static uint8_t FrameBuffer_Video_B[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(16)));  //16 bytes aligned!;
00068 #endif
00069 static volatile int32_t vsync_count;
00070 static volatile int32_t vfield_count;
00071 
00072 #if (1) // Add image buffer */ 
00073 static unsigned char input_image_buff[320*240];
00074 #endif
00075 
00076 #if (1) // Add YCbCr422 to Grayscale converter */ 
00077 static void yuv2gray(void * dst_buff, void * src_buff, uint32_t stride, uint32_t height );
00078 #endif
00079 
00080 /**************************************************************************//**
00081  * @brief       Interrupt callback function
00082  * @param[in]   int_type    : VDC5 interrupt type
00083  * @retval      None
00084 ******************************************************************************/
00085 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type)
00086 {
00087     if (vfield_count > 0) {
00088         vfield_count--;
00089     }
00090 }
00091 
00092 /**************************************************************************//**
00093  * @brief       Wait for the specified number of times Vsync occurs
00094  * @param[in]   wait_count          : Wait count
00095  * @retval      None
00096 ******************************************************************************/
00097 static void WaitVfield(const int32_t wait_count)
00098 {
00099     vfield_count = wait_count;
00100     while (vfield_count > 0) {
00101         /* Do nothing */
00102     }
00103 }
00104 
00105 /**************************************************************************//**
00106  * @brief       Interrupt callback function for Vsync interruption
00107  * @param[in]   int_type    : VDC5 interrupt type
00108  * @retval      None
00109 ******************************************************************************/
00110 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type)
00111 {
00112     if (vsync_count > 0) {
00113         vsync_count--;
00114     }
00115 }
00116 
00117 /**************************************************************************//**
00118  * @brief       Wait for the specified number of times Vsync occurs
00119  * @param[in]   wait_count          : Wait count
00120  * @retval      None
00121 ******************************************************************************/
00122 static void WaitVsync(const int32_t wait_count)
00123 {
00124     vsync_count = wait_count;
00125     while (vsync_count > 0) {
00126         /* Do nothing */
00127     }
00128 }
00129 
00130 /**************************************************************************//**
00131  * @brief
00132  * @param[in]   void
00133  * @retval      None
00134 ******************************************************************************/
00135 int main(void)
00136 {
00137     DisplayBase::graphics_error_t error;
00138     uint8_t * write_buff_addr = FrameBuffer_Video_A;
00139     uint8_t * save_buff_addr  = FrameBuffer_Video_B;
00140 
00141 #if VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA
00142     DisplayBase::video_ext_in_config_t ext_in_config;
00143     PinName cmos_camera_pin[11] = {
00144         /* data pin */
00145         P2_7, P2_6, P2_5, P2_4, P2_3, P2_2, P2_1, P2_0,
00146         /* control pin */
00147         P10_0,      /* DV0_CLK   */
00148         P1_0,       /* DV0_Vsync */
00149         P1_1        /* DV0_Hsync */
00150     };
00151 #endif
00152 
00153     /* Create DisplayBase object */
00154     DisplayBase Display;
00155 
00156     /* Graphics initialization process */
00157     error = Display.Graphics_init(NULL);
00158     if (error != DisplayBase::GRAPHICS_OK) {
00159         printf("Line %d, error %d\n", __LINE__, error);
00160         while (1);
00161     }
00162 
00163 #if VIDEO_INPUT_METHOD == VIDEO_CVBS
00164     error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_VDEC, NULL);
00165     if( error != DisplayBase::GRAPHICS_OK ) {
00166         while(1);
00167     }
00168 
00169 #elif VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA
00170     /* MT9V111 camera input config */
00171     ext_in_config.inp_format     = DisplayBase::VIDEO_EXTIN_FORMAT_BT601; /* BT601 8bit YCbCr format */
00172     ext_in_config.inp_pxd_edge   = DisplayBase::EDGE_RISING;              /* Clock edge select for capturing data          */
00173     ext_in_config.inp_vs_edge    = DisplayBase::EDGE_RISING;              /* Clock edge select for capturing Vsync signals */
00174     ext_in_config.inp_hs_edge    = DisplayBase::EDGE_RISING;              /* Clock edge select for capturing Hsync signals */
00175     ext_in_config.inp_endian_on  = DisplayBase::OFF;                      /* External input bit endian change on/off       */
00176     ext_in_config.inp_swap_on    = DisplayBase::OFF;                      /* External input B/R signal swap on/off         */
00177     ext_in_config.inp_vs_inv     = DisplayBase::SIG_POL_NOT_INVERTED;     /* External input DV_VSYNC inversion control     */
00178     ext_in_config.inp_hs_inv     = DisplayBase::SIG_POL_INVERTED;         /* External input DV_HSYNC inversion control     */
00179     ext_in_config.inp_f525_625   = DisplayBase::EXTIN_LINE_525;           /* Number of lines for BT.656 external input */
00180     ext_in_config.inp_h_pos      = DisplayBase::EXTIN_H_POS_CRYCBY;       /* Y/Cb/Y/Cr data string start timing to Hsync reference */
00181     ext_in_config.cap_vs_pos     = 6;                                     /* Capture start position from Vsync */
00182     ext_in_config.cap_hs_pos     = 150;                                   /* Capture start position form Hsync */
00183     ext_in_config.cap_width      = 640;                                   /* Capture width  */
00184     ext_in_config.cap_height     = 468u;                                  /* Capture height Max 468[line]
00185                                                                              Due to CMOS(MT9V111) output signal timing and VDC5 specification */
00186     error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_EXT, &ext_in_config);
00187     if( error != DisplayBase::GRAPHICS_OK ) {
00188         printf("Line %d, error %d\n", __LINE__, error);
00189         while(1);
00190     }
00191 
00192     /* MT9V111 camera input port setting */
00193     error = Display.Graphics_Dvinput_Port_Init(cmos_camera_pin, 11);
00194     if( error != DisplayBase::GRAPHICS_OK ) {
00195         printf("Line %d, error %d\n", __LINE__, error);
00196         while (1);
00197     }
00198 #endif
00199 
00200     /* Interrupt callback function setting (Vsync signal input to scaler 0) */
00201     error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VI_VSYNC, 0, IntCallbackFunc_Vsync);
00202     if (error != DisplayBase::GRAPHICS_OK) {
00203         printf("Line %d, error %d\n", __LINE__, error);
00204         while (1);
00205     }
00206     /* Video capture setting (progressive form fixed) */
00207     error = Display.Video_Write_Setting(
00208                 VIDEO_INPUT_CH,
00209 #if VIDEO_PAL == 0
00210                 DisplayBase::COL_SYS_NTSC_358,
00211 #else
00212                 DisplayBase::COL_SYS_PAL_443,
00213 #endif
00214                 write_buff_addr,
00215                 VIDEO_BUFFER_STRIDE,
00216 #if VIDEO_INPUT_FORMAT == VIDEO_YCBCR422
00217                 DisplayBase::VIDEO_FORMAT_YCBCR422,
00218                 DisplayBase::WR_RD_WRSWA_NON,
00219 #elif VIDEO_INPUT_FORMAT == VIDEO_RGB565
00220                 DisplayBase::VIDEO_FORMAT_RGB565,
00221                 DisplayBase::WR_RD_WRSWA_32_16BIT,
00222 #else
00223                 DisplayBase::VIDEO_FORMAT_RGB888,
00224                 DisplayBase::WR_RD_WRSWA_32BIT,
00225 #endif
00226                 PIXEL_VW,
00227                 PIXEL_HW
00228             );
00229     if (error != DisplayBase::GRAPHICS_OK) {
00230         printf("Line %d, error %d\n", __LINE__, error);
00231         while (1);
00232     }
00233 
00234     /* Interrupt callback function setting (Field end signal for recording function in scaler 0) */
00235     error = Display.Graphics_Irq_Handler_Set(VIDEO_INT_TYPE, 0, IntCallbackFunc_Vfield);
00236     if (error != DisplayBase::GRAPHICS_OK) {
00237         printf("Line %d, error %d\n", __LINE__, error);
00238         while (1);
00239     }
00240 
00241     /* Video write process start */
00242     error = Display.Video_Start (VIDEO_INPUT_CH);
00243     if (error != DisplayBase::GRAPHICS_OK) {
00244         printf("Line %d, error %d\n", __LINE__, error);
00245         while (1);
00246     }
00247 
00248     /* Video write process stop */
00249     error = Display.Video_Stop (VIDEO_INPUT_CH);
00250     if (error != DisplayBase::GRAPHICS_OK) {
00251         printf("Line %d, error %d\n", __LINE__, error);
00252         while (1);
00253     }
00254 
00255     /* Video write process start */
00256     error = Display.Video_Start (VIDEO_INPUT_CH);
00257     if (error != DisplayBase::GRAPHICS_OK) {
00258         printf("Line %d, error %d\n", __LINE__, error);
00259         while (1);
00260     }
00261 
00262     /* Wait vsync to update resister */
00263     WaitVsync(1);
00264 
00265     /* Wait 2 Vfield(Top or bottom field) */
00266     WaitVfield(2);
00267 
00268 #if (1) // USB is not used
00269 #else
00270 #if (USB_HOST_CH == 1) //Audio Shield USB1
00271     //Audio Shield USB1 enable
00272     usb1en = 1;        //Outputs high level
00273     Thread::wait(5);
00274     usb1en = 0;        //Outputs low level
00275 #endif
00276     USBHostMSD msd("usb");
00277     char file_name[32];
00278     int file_name_index = 0;
00279     int save_file_size;
00280 #endif
00281 
00282     while (1) {
00283         /* button check */
00284         if (button == 0) {
00285             led1 = 1;
00286             if (write_buff_addr == FrameBuffer_Video_A) {
00287                 write_buff_addr = FrameBuffer_Video_B;
00288                 save_buff_addr  = FrameBuffer_Video_A;
00289             } else {
00290                 write_buff_addr = FrameBuffer_Video_A;
00291                 save_buff_addr  = FrameBuffer_Video_B;
00292             }
00293 
00294             /* Change write buffer */
00295             error = Display.Video_Write_Change(
00296                         VIDEO_INPUT_CH,
00297                         write_buff_addr,
00298                         VIDEO_BUFFER_STRIDE);
00299             if (error != DisplayBase::GRAPHICS_OK) {
00300                 printf("Line %d, error %d\n", __LINE__, error);
00301                 while (1);
00302             }
00303             /* Wait 2 Vfield(Top or bottom field) */
00304             WaitVfield(2);
00305 
00306 #if (1) // USB is not used
00307 #else
00308             /* FrameBuffer_Video_AorB capture completed */
00309             /* USB connect check */
00310             while (!msd.connected()) {
00311                 if (!msd.connect()) {
00312                     Thread::wait(500);
00313                 } else {
00314                     break;
00315                 }
00316             }
00317 #endif
00318 
00319             /* Data save */
00320 #if ( VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 || VIDEO_INPUT_FORMAT == VIDEO_RGB565 )
00321 #if (1)     /* converting YCbCr to Grayscale and calling zbar_main */
00322             yuv2gray(input_image_buff,save_buff_addr,VIDEO_BUFFER_STRIDE,VIDEO_BUFFER_HEIGHT);
00323             zbar_main(input_image_buff,PIXEL_HW,PIXEL_VW);
00324 #else
00325             /* Save ".bin" file */
00326             sprintf(file_name, "/usb/video_%d.bin", file_name_index++);
00327             FILE * fp = fopen(file_name, "w");
00328             save_file_size = fwrite(save_buff_addr, sizeof(char), (VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT), fp);
00329             fclose(fp);
00330 #endif
00331 #else
00332             /* Save ".bmp" file */
00333             sprintf(file_name, "/usb/video_%d.bmp", file_name_index++);
00334 
00335             bitmap bitmapfile;
00336             save_file_size = bitmapfile.Rgb888ToBmp(file_name, save_buff_addr, PIXEL_HW, PIXEL_VW);
00337 #endif
00338 #if (1) // USB is not used
00339 #else
00340             printf("file name %s, file size %d\n", file_name, save_file_size);
00341 #endif
00342             led1 = 0;
00343         }
00344     }
00345 }
00346 
00347 #if (1) // Add YCbCr422 to Grayscale converter */ 
00348 /**************************************************************************//**
00349  * @brief       Convert YCbCr422 to Grayscale
00350  * @param[in]   void * dst_buff
00351                 void * src_buff
00352                 uint32_t stride
00353                 uint32_t height
00354  * @retval      None
00355 ******************************************************************************/
00356 /* Convert YCbCr422 to Grayscale */
00357 static void yuv2gray(void * dst_buff, void * src_buff, uint32_t stride, uint32_t height )
00358 {
00359     uint32_t    count;
00360     uint32_t  * src;
00361     uint32_t  * dst;
00362     uint32_t    data1;
00363     uint32_t    data2;
00364 
00365     src = (uint32_t *)src_buff;
00366     dst = (uint32_t *)dst_buff;
00367 
00368     for( count = 0 ; count < stride * height -1 ; )
00369     {
00370         data1   = *src++;
00371         data2   = *src++;
00372 
00373         *dst++  = ( (data1 & 0x000000ff) << 24 )
00374                 + ( (data1 & 0x00ff0000) <<  0 )
00375                 + ( (data2 & 0x000000ff) <<  8 )
00376                 + ( (data2 & 0x00ff0000) >> 16 );
00377         count += 8;
00378     }
00379 }   /* End of function yuv2gray() */
00380 #endif