Daiki Kato / Mbed OS GR-Boads_Camera_DisplayApp

Dependencies:   DisplayApp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EasyAttach_CameraAndLCD.h"
00003 #include "JPEG_Converter.h"
00004 #include "dcache-control.h"
00005 #include "DisplayApp.h"
00006 
00007 /**** User Selection *********/
00008 /** JPEG out setting **/
00009 #define JPEG_ENCODE_QUALITY    (75)    /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */
00010 #define VFIELD_INT_SKIP_CNT    (0)     /* A guide for GR-LYCHEE.  0:60fps, 1:30fps, 2:20fps, 3:15fps, 4:12fps, 5:10fps */
00011 /*****************************/
00012 
00013 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
00014     in accordance with the frame buffer burst transfer mode. */
00015 #define VIDEO_PIXEL_HW         (640u)  /* VGA */
00016 #define VIDEO_PIXEL_VW         (480u)  /* VGA */
00017 
00018 #define FRAME_BUFFER_STRIDE    (((VIDEO_PIXEL_HW * 2) + 31u) & ~31u)
00019 #define FRAME_BUFFER_HEIGHT    (VIDEO_PIXEL_VW)
00020 
00021 DisplayBase Display;
00022 
00023 #if defined(__ICCARM__)
00024 #pragma data_alignment=32
00025 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]@ ".mirrorram";
00026 #pragma data_alignment=32
00027 static uint8_t JpegBuffer[2][1024 * 64];
00028 #else
00029 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));
00030 static uint8_t JpegBuffer[2][1024 * 64]__attribute((aligned(32)));
00031 #endif
00032 static size_t jcu_encode_size[2];
00033 static JPEG_Converter Jcu;
00034 static int jcu_buf_index_write = 0;
00035 static int jcu_buf_index_write_done = 0;
00036 static int jcu_buf_index_read = 0;
00037 static volatile int jcu_encoding = 0;
00038 static volatile int image_change = 0;
00039 static DisplayApp  display_app;
00040 static int Vfield_Int_Cnt = 0;
00041 
00042 static void JcuEncodeCallBackFunc(JPEG_Converter::jpeg_conv_error_t err_code) {
00043     if (err_code == JPEG_Converter::JPEG_CONV_OK) {
00044         jcu_buf_index_write_done = jcu_buf_index_write;
00045         image_change = 1;
00046     }
00047     jcu_encoding = 0;
00048 }
00049 
00050 static void snapshot(void) {
00051     while ((jcu_encoding == 1) || (image_change == 0)) {
00052         Thread::wait(1);
00053     }
00054     jcu_buf_index_read = jcu_buf_index_write_done;
00055     image_change = 0;
00056     display_app.SendJpeg(JpegBuffer[jcu_buf_index_read], (int)jcu_encode_size[jcu_buf_index_read]);
00057 }
00058 
00059 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) {
00060     if (Vfield_Int_Cnt < VFIELD_INT_SKIP_CNT) {
00061         Vfield_Int_Cnt++;
00062         return;
00063     }
00064     Vfield_Int_Cnt = 0;
00065 
00066     //Interrupt callback function
00067     if (jcu_encoding == 0) {
00068         JPEG_Converter::bitmap_buff_info_t bitmap_buff_info;
00069         JPEG_Converter::encode_options_t   encode_options;
00070 
00071         bitmap_buff_info.width              = VIDEO_PIXEL_HW;
00072         bitmap_buff_info.height             = VIDEO_PIXEL_VW;
00073         bitmap_buff_info.format             = JPEG_Converter::WR_RD_YCbCr422;
00074         bitmap_buff_info.buffer_address     = (void *)user_frame_buffer0;
00075 
00076         encode_options.encode_buff_size     = sizeof(JpegBuffer[0]);
00077         encode_options.p_EncodeCallBackFunc = &JcuEncodeCallBackFunc;
00078         encode_options.input_swapsetting    = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT;
00079 
00080         jcu_encoding = 1;
00081         if (jcu_buf_index_read == jcu_buf_index_write) {
00082             jcu_buf_index_write ^= 1;  // toggle
00083         }
00084         jcu_encode_size[jcu_buf_index_write] = 0;
00085         dcache_invalid(JpegBuffer[jcu_buf_index_write], sizeof(JpegBuffer[0]));
00086         if (Jcu.encode(&bitmap_buff_info, JpegBuffer[jcu_buf_index_write],
00087             &jcu_encode_size[jcu_buf_index_write], &encode_options) != JPEG_Converter::JPEG_CONV_OK) {
00088             jcu_encode_size[jcu_buf_index_write] = 0;
00089             jcu_encoding = 0;
00090         }
00091     }
00092 }
00093 
00094 static void Start_Video_Camera(void) {
00095     // Initialize the background to black
00096     for (uint32_t i = 0; i < sizeof(user_frame_buffer0); i += 2) {
00097         user_frame_buffer0[i + 0] = 0x10;
00098         user_frame_buffer0[i + 1] = 0x80;
00099     }
00100 
00101     // Interrupt callback function setting (Field end signal for recording function in scaler 0)
00102     Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VFIELD, 0, IntCallbackFunc_Vfield);
00103 
00104     // Video capture setting (progressive form fixed)
00105     Display.Video_Write_Setting(
00106         DisplayBase::VIDEO_INPUT_CHANNEL_0,
00107         DisplayBase::COL_SYS_NTSC_358,
00108         (void *)user_frame_buffer0,
00109         FRAME_BUFFER_STRIDE,
00110         DisplayBase::VIDEO_FORMAT_YCBCR422,
00111         DisplayBase::WR_RD_WRSWA_32_16BIT,
00112         VIDEO_PIXEL_VW,
00113         VIDEO_PIXEL_HW
00114     );
00115     EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0);
00116 }
00117 
00118 int main(void) {
00119     Jcu.SetQuality(JPEG_ENCODE_QUALITY);
00120 
00121     EasyAttach_Init(Display);
00122     Start_Video_Camera();
00123 
00124     while (1) {
00125         snapshot();
00126     }
00127 }