Daiki Kato / GR-Boads_Camera_sample

Dependencies:   mbed-os-lychee

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 "SdUsbConnect.h"
00004 #include "JPEG_Converter.h"
00005 #include "dcache-control.h"
00006 
00007 #define MOUNT_NAME             "storage"
00008 
00009 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
00010     in accordance with the frame buffer burst transfer mode. */
00011 #define VIDEO_PIXEL_HW         (640u)  /* VGA */
00012 #define VIDEO_PIXEL_VW         (480u)  /* VGA */
00013 
00014 #define FRAME_BUFFER_STRIDE    (((VIDEO_PIXEL_HW * 2) + 31u) & ~31u)
00015 #define FRAME_BUFFER_HEIGHT    (VIDEO_PIXEL_VW)
00016 
00017 #if defined(__ICCARM__)
00018 #pragma data_alignment=32
00019 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]@ ".mirrorram";
00020 #else
00021 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));
00022 #endif
00023 static int file_name_index = 1;
00024 static volatile int Vfield_Int_Cnt = 0;
00025 /* jpeg convert */
00026 static JPEG_Converter Jcu;
00027 #if defined(__ICCARM__)
00028 #pragma data_alignment=32
00029 static uint8_t JpegBuffer[1024 * 63];
00030 #else
00031 static uint8_t JpegBuffer[1024 * 63]__attribute((aligned(32)));
00032 #endif
00033 
00034 DisplayBase Display;
00035 DigitalIn   button0(USER_BUTTON0);
00036 DigitalOut  led1(LED1);
00037 
00038 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) {
00039     if (Vfield_Int_Cnt > 0) {
00040         Vfield_Int_Cnt--;
00041     }
00042 }
00043 
00044 static void wait_new_image(void) {
00045     Vfield_Int_Cnt = 1;
00046     while (Vfield_Int_Cnt > 0) {
00047         Thread::wait(1);
00048     }
00049 }
00050 
00051 static void Start_Video_Camera(void) {
00052     // Initialize the background to black
00053     for (uint32_t i = 0; i < sizeof(user_frame_buffer0); i += 2) {
00054         user_frame_buffer0[i + 0] = 0x10;
00055         user_frame_buffer0[i + 1] = 0x80;
00056     }
00057 
00058     // Field end signal for recording function in scaler 0
00059     Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VFIELD, 0, IntCallbackFunc_Vfield);
00060 
00061     // Video capture setting (progressive form fixed)
00062     Display.Video_Write_Setting(
00063         DisplayBase::VIDEO_INPUT_CHANNEL_0,
00064         DisplayBase::COL_SYS_NTSC_358,
00065         (void *)user_frame_buffer0,
00066         FRAME_BUFFER_STRIDE,
00067         DisplayBase::VIDEO_FORMAT_YCBCR422,
00068         DisplayBase::WR_RD_WRSWA_32_16BIT,
00069         VIDEO_PIXEL_VW,
00070         VIDEO_PIXEL_HW
00071     );
00072     EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0);
00073 }
00074 
00075 #if MBED_CONF_APP_LCD
00076 static void Start_LCD_Display(void) {
00077     DisplayBase::rect_t rect;
00078 
00079     rect.vs = 0;
00080     rect.vw = VIDEO_PIXEL_VW;
00081     rect.hs = 0;
00082     rect.hw = VIDEO_PIXEL_HW;
00083     Display.Graphics_Read_Setting(
00084         DisplayBase::GRAPHICS_LAYER_0,
00085         (void *)user_frame_buffer0,
00086         FRAME_BUFFER_STRIDE,
00087         DisplayBase::GRAPHICS_FORMAT_YCBCR422,
00088         DisplayBase::WR_RD_WRSWA_32_16BIT,
00089         &rect
00090     );
00091     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00092 
00093     Thread::wait(50);
00094     EasyAttach_LcdBacklight(true);
00095 }
00096 #endif
00097 
00098 static void save_image_jpg(void) {
00099     size_t jcu_encode_size = 0;
00100     JPEG_Converter::bitmap_buff_info_t bitmap_buff_info;
00101     JPEG_Converter::encode_options_t   encode_options;
00102 
00103     bitmap_buff_info.width              = VIDEO_PIXEL_HW;
00104     bitmap_buff_info.height             = VIDEO_PIXEL_VW;
00105     bitmap_buff_info.format             = JPEG_Converter::WR_RD_YCbCr422;
00106     bitmap_buff_info.buffer_address     = (void *)user_frame_buffer0;
00107 
00108     encode_options.encode_buff_size     = sizeof(JpegBuffer);
00109     encode_options.p_EncodeCallBackFunc = NULL;
00110     encode_options.input_swapsetting    = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT;
00111 
00112     dcache_invalid(JpegBuffer, sizeof(JpegBuffer));
00113     if (Jcu.encode(&bitmap_buff_info, JpegBuffer, &jcu_encode_size, &encode_options) == JPEG_Converter::JPEG_CONV_OK) {
00114         char file_name[32];
00115         sprintf(file_name, "/"MOUNT_NAME"/img_%d.jpg", file_name_index++);
00116         FILE * fp = fopen(file_name, "w");
00117         fwrite(JpegBuffer, sizeof(char), (int)jcu_encode_size, fp);
00118         fclose(fp);
00119         printf("Saved file %s\r\n", file_name);
00120     }
00121 }
00122 
00123 int main() {
00124     EasyAttach_Init(Display);
00125     Start_Video_Camera();
00126 #if MBED_CONF_APP_LCD
00127     Start_LCD_Display();
00128 #endif
00129     SdUsbConnect storage(MOUNT_NAME);
00130 
00131     while (1) {
00132         storage.wait_connect();
00133         if (button0 == 0) {
00134             wait_new_image(); // wait for image input
00135             led1 = 1;
00136             save_image_jpg(); // save as jpeg
00137             led1 = 0;
00138         }
00139     }
00140 }