Daiki Kato / Mbed OS GR-PEACH_Digital_Signage Featured

Dependencies:   Display_shield_config GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP SDBlockDevice_GR_PEACH USBHost_custom scan_folder

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 #include "rtos.h"
00004 #include "RGA.h"
00005 #include "BinaryImage_RZ_A1H.h"
00006 #include "FATFileSystem.h"
00007 #include "SDBlockDevice_GR_PEACH.h"
00008 #include "USBHostMSD.h"
00009 #include "USBHostMouse.h"
00010 #include "scan_folder.h"
00011 
00012 /**** User Selection *********/
00013 #define WAIT_TIME                           (10000) /* wait time (ms) 0:infinite */
00014 #define DISSOLVE_STEP_NUM                   (16)    /* minimum 1 */
00015 #define SCROLL_STEP_NUM                     (8)     /* minimum 1 */
00016 #define SCROLL_DIRECTION                    (-1)    /* Select 1(left to right) or -1(right to left) */
00017 /** LCD setting **/
00018 #define LCD_TYPE                            (0)     /* Select 0(4.3inch) , 1(7.1inch), 2(Display shield) or 3(RSK TFT) */
00019 /*****************************/
00020 
00021 #define GRAPHICS_FORMAT                     (DisplayBase::GRAPHICS_FORMAT_RGB565)
00022 #define WR_RD_WRSWA                         (DisplayBase::WR_RD_WRSWA_32_16BIT)
00023 
00024 /** LCD shield config **/
00025 #if (LCD_TYPE == 0)
00026   #include "LCD_shield_config_4_3inch.h"
00027 #elif (LCD_TYPE == 1)
00028   #include "LCD_shield_config_7_1inch.h"
00029 #elif (LCD_TYPE == 2)
00030   #include "Display_shield_config.h"
00031 #else
00032   #include "LCD_shield_config_RSK_TFT.h"
00033 #endif
00034 
00035 #if (SCROLL_DIRECTION == -1)
00036 #define SCROLL_DIRECTION_NEXT               (-1)
00037 #define SCROLL_DIRECTION_PREV               (1)
00038 #else
00039 #define SCROLL_DIRECTION_NEXT               (1)
00040 #define SCROLL_DIRECTION_PREV               (-1)
00041 #endif
00042 
00043 /* FRAME BUFFER Parameter */
00044 #define FRAME_BUFFER_BYTE_PER_PIXEL         (2)
00045 #define FRAME_BUFFER_STRIDE                 (((LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u)
00046 
00047 #define EVENT_NONE                          (0)
00048 #define EVENT_DISP                          (1)
00049 #define EVENT_NEXT                          (2)
00050 #define EVENT_PREV                          (3)
00051 #define EVENT_MOVE                          (4)
00052 #define EVENT_MOVE_END                      (5)
00053 
00054 #define MAX_JPEG_SIZE                       (1024 * 450)
00055 #define MOUNT_NAME                          "storage"
00056 
00057 #define TOUCH_NUM                           (2u)
00058 
00059 typedef struct {
00060     int  pos_x;
00061     int  pos_y;
00062     bool disp_pos;
00063     bool disp_time;
00064     bool change;
00065 } mouse_info_t;
00066 
00067 typedef struct {
00068     int       event_req;
00069     uint8_t * p_pic_next;
00070     uint8_t * p_pic_now;
00071     float32_t magnification;
00072     int       x_move;
00073     int       swipe_end_move;
00074     int       drow_pos_x;
00075     int       drow_pos_y;
00076     int       scale_end_move_x;
00077     int       scale_end_move_y;
00078     int       min_x;
00079     int       min_y;
00080     bool      scroll;
00081 } efect_info_t;
00082 
00083 static const char_t * extension_tbl[] = {
00084     ".jpg",
00085     NULL
00086 };
00087 static InterruptIn button(USER_BUTTON0);
00088 static DisplayBase Display;
00089 static Canvas2D_ContextClass canvas2d;
00090 #if (LCD_TYPE != 2)
00091 static DigitalOut  lcd_pwon(P7_15);
00092 static DigitalOut  lcd_blon(P8_1);
00093 static PwmOut      lcd_cntrst(P8_15);
00094 #endif
00095 static Serial pc(USBTX, USBRX);
00096 static Timer system_timer;
00097 static Semaphore   sem_touch_int(0);
00098 static Thread mouseTask(osPriorityNormal, 1024);
00099 #ifdef TouckKey_LCD_shield
00100 static Thread touchTask(osPriorityNormal, 1024 * 8);
00101 #endif
00102 
00103 #if defined(__ICCARM__)
00104 #pragma data_alignment=32
00105 static uint8_t user_frame_buffer1[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT];
00106 #pragma data_alignment=32
00107 static uint8_t user_frame_buffer2[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT];
00108 #pragma data_alignment=8
00109 static uint8_t JpegBuffer[MAX_JPEG_SIZE]@ ".mirrorram";  //8 bytes aligned!;
00110 #else
00111 static uint8_t user_frame_buffer1[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT]__attribute((aligned(32)));  /* 32 bytes aligned */
00112 static uint8_t user_frame_buffer2[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT]__attribute((aligned(32))); /* 32 bytes aligned */
00113 static uint8_t JpegBuffer[MAX_JPEG_SIZE]__attribute((section("NC_BSS"),aligned(8)));  //8 bytes aligned!;
00114 #endif
00115 static frame_buffer_t frame_buffer_info;
00116 static volatile int32_t vsync_count = 0;
00117 static ScanFolder scan_foler;
00118 
00119 static mouse_info_t mouse_info = {0, 0, false, false, false};
00120 
00121 static int disp_wait_time = WAIT_TIME;
00122 static uint32_t file_id_now = 0xffffffff;
00123 static uint32_t file_id_next = 0xffffffff;
00124 static int dissolve_seq = 0;
00125 static efect_info_t efect_info;
00126 
00127 #define ROM_PIC_NUM             (4)
00128 static bool storage_mode = false;
00129 static uint32_t total_file_num = ROM_PIC_NUM;
00130 
00131 static const graphics_image_t* number_tbl[10] = {
00132     number0_Img,
00133     number1_Img,
00134     number2_Img,
00135     number3_Img,
00136     number4_Img,
00137     number5_Img,
00138     number6_Img,
00139     number7_Img,
00140     number8_Img,
00141     number9_Img
00142 };
00143 
00144 /****** LCD ******/
00145 static void IntCallbackFunc_LoVsync(DisplayBase::int_type_t int_type) {
00146     /* Interrupt callback function for Vsync interruption */
00147     if (vsync_count > 0) {
00148         vsync_count--;
00149     }
00150 }
00151 
00152 static void Wait_Vsync(const int32_t wait_count) {
00153     /* Wait for the specified number of times Vsync occurs */
00154     vsync_count = wait_count;
00155     while (vsync_count > 0) {
00156         /* Do nothing */
00157     }
00158 }
00159 
00160 static void Init_LCD_Display(void) {
00161     DisplayBase::graphics_error_t error;
00162     DisplayBase::lcd_config_t lcd_config;
00163 #if (LCD_TYPE == 2)
00164     PinName lcd_pin[28] = {
00165         /* data pin */
00166         P11_15, P11_14, P11_13, P11_12, P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0,
00167         P4_7, P4_6, P4_5, P4_4, P10_12, P10_13, P10_14, P10_15, P3_15, P3_14, P3_13,
00168         P3_12, P3_11, P3_10, P3_9, P3_8
00169     };
00170     Display.Graphics_Lcd_Port_Init(lcd_pin, 28);
00171 #else
00172     PinName lvds_pin[8] = {
00173         /* data pin */
00174         P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0
00175     };
00176 
00177     lcd_pwon = 0;
00178     lcd_blon = 0;
00179     Thread::wait(100);
00180     lcd_pwon = 1;
00181     lcd_blon = 1;
00182 
00183     Display.Graphics_Lvds_Port_Init(lvds_pin, 8);
00184 #endif
00185 
00186     /* Graphics initialization process */
00187     lcd_config = LcdCfgTbl_LCD_shield;
00188     error = Display.Graphics_init(&lcd_config);
00189     if (error != DisplayBase::GRAPHICS_OK) {
00190         printf("Line %d, error %d\n", __LINE__, error);
00191         mbed_die();
00192     }
00193 
00194     /* Interrupt callback function setting (Vsync signal output from scaler 0) */
00195     error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_LO_VSYNC, 0, IntCallbackFunc_LoVsync);
00196     if (error != DisplayBase::GRAPHICS_OK) {
00197         printf("Line %d, error %d\n", __LINE__, error);
00198         mbed_die();
00199     }
00200 }
00201 
00202 static void Start_LCD_Display(uint8_t * p_buf) {
00203     DisplayBase::rect_t rect;
00204 
00205     rect.vs = 0;
00206     rect.vw = LCD_PIXEL_HEIGHT;
00207     rect.hs = 0;
00208     rect.hw = LCD_PIXEL_WIDTH;
00209     Display.Graphics_Read_Setting(
00210         DisplayBase::GRAPHICS_LAYER_0,
00211         (void *)p_buf,
00212         FRAME_BUFFER_STRIDE,
00213         GRAPHICS_FORMAT,
00214         WR_RD_WRSWA,
00215         &rect
00216     );
00217     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00218 }
00219 
00220 static void Update_LCD_Display(frame_buffer_t * frmbuf_info) {
00221     Display.Graphics_Read_Change(DisplayBase::GRAPHICS_LAYER_0,
00222      (void *)frmbuf_info->buffer_address[frmbuf_info->draw_buffer_index]);
00223     Wait_Vsync(1);
00224 }
00225 
00226 static void Swap_FrameBuffer(frame_buffer_t * frmbuf_info) {
00227     if (frmbuf_info->draw_buffer_index == 1) {
00228         frmbuf_info->draw_buffer_index = 0;
00229     } else {
00230         frmbuf_info->draw_buffer_index = 1;
00231     }
00232 }
00233 
00234 
00235 /****** Draw Image ******/
00236 static void draw_mouse_pos(void) {
00237     if (mouse_info.disp_pos != false) {
00238         /* Draw a image */
00239         canvas2d.drawImage(mousu_pos_Img, mouse_info.pos_x, mouse_info.pos_y);
00240         R_OSPL_CLEAR_ERROR();
00241     }
00242 }
00243 
00244 static void draw_disp_time(void) {
00245     if (mouse_info.disp_time != false) {
00246         int wk_ofs = (LCD_PIXEL_WIDTH - 215) / 2 + 103;
00247         int wk_sec;
00248 
00249         /* Draw a image */
00250         canvas2d.drawImage(disp_xsec_Img, (LCD_PIXEL_WIDTH - 215) / 2, LCD_PIXEL_HEIGHT / 2);
00251         R_OSPL_CLEAR_ERROR();
00252 
00253         if (disp_wait_time == 0) {
00254             /* Draw a image */
00255             canvas2d.drawImage(number_inf_Img, wk_ofs, LCD_PIXEL_HEIGHT / 2);
00256             R_OSPL_CLEAR_ERROR();
00257         } else {
00258             wk_sec = (disp_wait_time / 10000) % 10;
00259             if (wk_sec != 0) {
00260                 /* Draw a image */
00261                 canvas2d.drawImage(number_tbl[wk_sec], wk_ofs, LCD_PIXEL_HEIGHT / 2);
00262                 R_OSPL_CLEAR_ERROR();
00263                 wk_ofs += 12;
00264             } else {
00265                 wk_ofs += 6;
00266             }
00267             wk_sec = (disp_wait_time / 1000) % 10;
00268             /* Draw a image */
00269             canvas2d.drawImage(number_tbl[wk_sec], wk_ofs, LCD_PIXEL_HEIGHT / 2);
00270             R_OSPL_CLEAR_ERROR();
00271         }
00272     }
00273 }
00274 
00275 static void draw_image_scroll(frame_buffer_t* frmbuf_info, const graphics_image_t* image_last,
00276                               const graphics_image_t* image_new, float32_t scroll, int ditection) {
00277     Swap_FrameBuffer(frmbuf_info);
00278     /* Clear */
00279     canvas2d.clearRect(0, 0, frmbuf_info->width, frmbuf_info->height);
00280     /* Draw a image */
00281     canvas2d.globalAlpha = 1.0f;
00282     canvas2d.drawImage((const graphics_image_t*)image_last,
00283                        (int_t)(frmbuf_info->width * scroll) * ditection, 0,
00284                        frmbuf_info->width, frmbuf_info->height);
00285     R_OSPL_CLEAR_ERROR();
00286     canvas2d.globalAlpha = 1.0f;
00287     canvas2d.drawImage((const graphics_image_t*)image_new,
00288                        ((int_t)(frmbuf_info->width * scroll) - frmbuf_info->width) * ditection, 0,
00289                        frmbuf_info->width, frmbuf_info->height);
00290     R_OSPL_CLEAR_ERROR();
00291     /* mouse pos */
00292     draw_mouse_pos();
00293     draw_disp_time();
00294     /* Complete drawing */
00295     R_GRAPHICS_Finish(canvas2d.c_LanguageContext);
00296     Update_LCD_Display(frmbuf_info);
00297 }
00298 
00299 static void draw_image_dissolve(frame_buffer_t* frmbuf_info, const graphics_image_t* image_last,
00300                                 const graphics_image_t* image_new, float32_t alpha) {
00301     Swap_FrameBuffer(frmbuf_info);
00302     /* Clear */
00303     canvas2d.clearRect(0, 0, frmbuf_info->width, frmbuf_info->height);
00304     /* Draw a image */
00305     canvas2d.globalAlpha = 1.0f - alpha;
00306     canvas2d.drawImage((const graphics_image_t*)image_last, 0, 0, frmbuf_info->width, frmbuf_info->height);
00307     R_OSPL_CLEAR_ERROR();
00308     canvas2d.globalAlpha = alpha;
00309     canvas2d.drawImage((const graphics_image_t*)image_new, 0, 0, frmbuf_info->width, frmbuf_info->height);
00310     R_OSPL_CLEAR_ERROR();
00311     /* mouse pos */
00312     draw_mouse_pos();
00313     draw_disp_time();
00314     /* Complete drawing */
00315     R_GRAPHICS_Finish(canvas2d.c_LanguageContext);
00316     Update_LCD_Display(frmbuf_info);
00317 }
00318 
00319 static void draw_image(frame_buffer_t* frmbuf_info, const graphics_image_t* image_new, 
00320  uint32_t pos_x, uint32_t pos_y, graphics_matrix_float_t zoom) {
00321     int_t dest_width;
00322     int_t dest_height;
00323 
00324     Swap_FrameBuffer(frmbuf_info);
00325     /* Clear */
00326     canvas2d.clearRect(0, 0, frmbuf_info->width, frmbuf_info->height);
00327     /* Draw a image */
00328     canvas2d.globalAlpha = 1.0f;
00329     dest_width  = frmbuf_info->width * zoom;
00330     dest_height = frmbuf_info->height * zoom;
00331     canvas2d.drawImage((const graphics_image_t*)image_new,
00332                         pos_x, pos_y, dest_width, dest_height);
00333     R_OSPL_CLEAR_ERROR();
00334     /* mouse pos */
00335     draw_mouse_pos();
00336     draw_disp_time();
00337     /* Complete drawing */
00338     R_GRAPHICS_Finish(canvas2d.c_LanguageContext);
00339     Update_LCD_Display(frmbuf_info);
00340 }
00341 
00342 
00343 /****** File Access ******/
00344 static bool read_new_file(uint8_t ** pp_buf, uint32_t file_id) {
00345     bool ret = false;
00346 
00347     if (storage_mode == false) {
00348         ret = true;
00349         switch (file_id) {
00350             case 0:     *pp_buf = (uint8_t *)img01_File;    break;
00351             case 1:     *pp_buf = (uint8_t *)img02_File;    break;
00352             case 2:     *pp_buf = (uint8_t *)img03_File;    break;
00353             case 3:     *pp_buf = (uint8_t *)img04_File;    break;
00354             default:    ret = false;                        break;
00355         }
00356     } else {
00357         FILE * fp = scan_foler.open(file_id);
00358         size_t read_size;
00359 
00360         read_size = fread(*pp_buf, sizeof(char), MAX_JPEG_SIZE, fp);
00361         scan_foler.close(fp);
00362         if (read_size < MAX_JPEG_SIZE) {
00363             ret = true;
00364         }
00365     }
00366 
00367     return ret;
00368 }
00369 
00370 static void swap_file_buff(void) {
00371     uint8_t * p_wk_pic = efect_info.p_pic_now;
00372     uint32_t wk_file_id = file_id_now;
00373 
00374     efect_info.p_pic_now = efect_info.p_pic_next;
00375     efect_info.p_pic_next = p_wk_pic;
00376     file_id_now = file_id_next;
00377     file_id_next = wk_file_id;
00378 }
00379 
00380 static bool read_next_file(void) {
00381     uint32_t wk_file_id;
00382     bool ret = false;
00383 
00384     if (file_id_now < (total_file_num - 1)) {
00385         wk_file_id = file_id_now + 1;
00386     } else {
00387         wk_file_id = 0;
00388     }
00389     if (wk_file_id != file_id_next) {
00390         ret = read_new_file(&efect_info.p_pic_next, wk_file_id);
00391         if (ret != false) {
00392             file_id_next = wk_file_id;
00393         }
00394     } else {
00395         ret = true;
00396     }
00397 
00398     return ret;
00399 }
00400 
00401 static bool read_prev_file(void) {
00402     uint32_t wk_file_id;
00403     bool ret = false;
00404 
00405     if (file_id_now >= total_file_num) {
00406         wk_file_id = 0;
00407     } else if (file_id_now != 0) {
00408         wk_file_id = file_id_now - 1;
00409     } else {
00410         wk_file_id = total_file_num - 1;
00411     }
00412     if (wk_file_id != file_id_next) {
00413         ret = read_new_file(&efect_info.p_pic_next, wk_file_id);
00414         if (ret != false) {
00415             file_id_next = wk_file_id;
00416         }
00417     } else {
00418         ret = true;
00419     }
00420 
00421     return ret;
00422 }
00423 
00424 
00425 /****** Efect setting ******/
00426 static void zoom_scroll(int x, int y) {
00427     efect_info.drow_pos_x += x;
00428     if (efect_info.drow_pos_x < efect_info.min_x) {
00429         efect_info.drow_pos_x = efect_info.min_x;
00430     }
00431     if (efect_info.drow_pos_x > 0) {
00432         efect_info.drow_pos_x = 0;
00433     }
00434     efect_info.drow_pos_y += y;
00435     if (efect_info.drow_pos_y < efect_info.min_y) {
00436         efect_info.drow_pos_y = efect_info.min_y;
00437     }
00438     if (efect_info.drow_pos_y > 0) {
00439         efect_info.drow_pos_y = 0;
00440     }
00441 }
00442 
00443 static void move_func(int x, int y) {
00444     if (efect_info.magnification != 1.0f) {
00445         zoom_scroll(x, y);
00446         efect_info.event_req = EVENT_DISP;
00447     } else {
00448         efect_info.scroll = true;
00449         efect_info.x_move += x;
00450         efect_info.event_req = EVENT_MOVE;
00451     }
00452 }
00453 
00454 static void move_end_func(int x, int y) {
00455     if (efect_info.magnification != 1.0f) {
00456         efect_info.scale_end_move_x = x;
00457         efect_info.scale_end_move_y = y;
00458     } else {
00459         if (efect_info.scroll != false) {
00460             efect_info.scroll = false;
00461             if (efect_info.x_move != 0) {
00462                 efect_info.swipe_end_move = x;
00463                 efect_info.event_req = EVENT_MOVE_END;
00464             }
00465         }
00466     }
00467 }
00468 
00469 static void zoom_func(uint32_t center_x_last, uint32_t center_y_last, uint32_t center_x, uint32_t center_y, float32_t new_magnification) {
00470     if (new_magnification != efect_info.magnification) {
00471         if (new_magnification < 1.0f) {
00472             new_magnification = 1.0f;
00473         }
00474         if (new_magnification > 10.0f) {
00475             new_magnification = 10.0f;
00476         }
00477         efect_info.min_x =  0 - (frame_buffer_info.width * (new_magnification - 1.0f));
00478         efect_info.min_y =  0 - (frame_buffer_info.height * (new_magnification - 1.0f));
00479 
00480         efect_info.drow_pos_x = -((center_x_last - efect_info.drow_pos_x) / efect_info.magnification * new_magnification - center_x);
00481         if (efect_info.drow_pos_x < efect_info.min_x) {
00482             efect_info.drow_pos_x = efect_info.min_x;
00483         }
00484         if (efect_info.drow_pos_x > 0) {
00485             efect_info.drow_pos_x = 0;
00486         }
00487 
00488         efect_info.drow_pos_y = -((center_y_last - efect_info.drow_pos_y) / efect_info.magnification * new_magnification - center_y);
00489         if (efect_info.drow_pos_y < efect_info.min_y) {
00490             efect_info.drow_pos_y = efect_info.min_y;
00491         }
00492         if (efect_info.drow_pos_y > 0) {
00493             efect_info.drow_pos_y = 0;
00494         }
00495         if (new_magnification == 1.0f) {
00496             efect_info.drow_pos_x = 0;
00497             efect_info.drow_pos_y = 0;
00498         }
00499         efect_info.x_move = 0;
00500         efect_info.magnification = new_magnification;
00501         if (efect_info.event_req == EVENT_NONE) {
00502             efect_info.event_req = EVENT_DISP;
00503         }
00504     }
00505 }
00506 
00507 /****** Mouse ******/
00508 void onMouseEvent(uint8_t buttons, int8_t x, int8_t y, int8_t z) {
00509     static uint8_t last_buttons = 0;
00510     int wk_pos;
00511     int wk_disp_time;
00512     int mouse_move_x;
00513     int mouse_move_y;
00514 
00515 //    printf("buttons: %d, x: %d, y: %d, z: %d\r\n", buttons, x, y, z);
00516 
00517     wk_pos = mouse_info.pos_x;
00518     wk_pos += x;
00519     mouse_move_x = x;
00520     if (wk_pos < 0) {
00521         wk_pos = 0;
00522         mouse_move_x = 0;
00523     }
00524     if (wk_pos > (LCD_PIXEL_WIDTH - 10)) {
00525         wk_pos = LCD_PIXEL_WIDTH - 10;
00526         mouse_move_x = 0;
00527     }
00528     mouse_info.pos_x = wk_pos;
00529 
00530     wk_pos = mouse_info.pos_y;
00531     wk_pos += y;
00532     mouse_move_y = y;
00533     if (wk_pos < 0) {
00534         wk_pos = 0;
00535         mouse_move_y = 0;
00536     }
00537     if (wk_pos > (LCD_PIXEL_HEIGHT - 10)) {
00538         wk_pos = (LCD_PIXEL_HEIGHT - 10);
00539         mouse_move_y = 0;
00540     }
00541     mouse_info.pos_y = wk_pos;
00542 
00543     if (mouse_info.disp_pos != false) {
00544         if ((buttons & 0x01) != 0) {
00545             move_func(mouse_move_x, mouse_move_y);
00546         } else if (((buttons & 0x01) == 0) && ((last_buttons & 0x01) != 0)) {
00547             move_end_func(mouse_move_x, mouse_move_y);
00548         } else if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
00549             efect_info.event_req = EVENT_DISP;
00550         } else {
00551             /* do nothing */
00552         }
00553     } else {
00554         // left
00555         if (((buttons & 0x01) == 0) && ((last_buttons & 0x01) != 0)) {
00556             efect_info.event_req = EVENT_NEXT;
00557         }
00558 
00559         // rigth
00560         if (((buttons & 0x02) == 0) && ((last_buttons & 0x02) != 0)) {
00561             efect_info.event_req = EVENT_PREV;
00562         }
00563     }
00564 
00565     if (((buttons & 0x04) != 0) && ((last_buttons & 0x04) == 0)) {
00566         mouse_info.disp_pos = !mouse_info.disp_pos;
00567         if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
00568             efect_info.event_req = EVENT_DISP;
00569         }
00570     }
00571 
00572     if (z != 0) {
00573         if (mouse_info.disp_pos == false) {
00574             wk_disp_time = disp_wait_time;
00575             if (z > 0) {
00576                 wk_disp_time += 1000;
00577                 if (wk_disp_time > 15000) {
00578                     wk_disp_time = 15000;
00579                 }
00580             } else {
00581                 wk_disp_time -= 1000;
00582                 if (wk_disp_time < 1000) {
00583                     wk_disp_time = 0;
00584                 }
00585             }
00586             disp_wait_time = wk_disp_time;
00587             system_timer.reset();
00588             system_timer.start();
00589             mouse_info.disp_time = true;
00590             if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
00591                 efect_info.event_req = EVENT_DISP;
00592             }
00593         } else {
00594             zoom_func(mouse_info.pos_x + 3, mouse_info.pos_y + 3,
00595                       mouse_info.pos_x + 3, mouse_info.pos_y + 3,
00596                       efect_info.magnification * (1.0f + (float_t)z / 10.0f));
00597         }
00598     }
00599 
00600     last_buttons = buttons;
00601 }
00602 
00603 void mouse_task(void) {
00604     USBHostMouse mouse;
00605 
00606     while (1) {
00607         /* try to connect a USB mouse */
00608         while (!mouse.connect()) {
00609             Thread::wait(500);
00610         }
00611 
00612         /* when connected, attach handler called on mouse event */
00613         mouse.attachEvent(onMouseEvent);
00614 
00615         /* wait until the mouse is disconnected */
00616         while (mouse.connected()) {
00617             Thread::wait(500);
00618         }
00619         mouse_info.disp_pos = false;
00620         mouse_info.disp_time = false;
00621         if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
00622             efect_info.event_req = EVENT_DISP;
00623         }
00624     }
00625 }
00626 
00627 
00628 /****** Touch ******/
00629 #ifdef TouckKey_LCD_shield
00630 static float32_t get_distance(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1) {
00631     float32_t distance_x;
00632     float32_t distance_y;
00633 
00634     if (x0 > x1) {
00635         distance_x = x0 - x1;
00636     } else {
00637         distance_x = x1 - x0;
00638     }
00639     if (y0 > y1) {
00640         distance_y = y0 - y1;
00641     } else {
00642         distance_y = y1 - y0;
00643     }
00644     return hypotf(distance_x, distance_y);
00645 }
00646 
00647 static uint32_t get_center(uint32_t xy0, uint32_t xy1) {
00648     uint32_t center_pos;
00649 
00650     if (xy0 > xy1) {
00651         center_pos = (xy0 - xy1) / 2 + xy1;
00652     } else {
00653         center_pos = (xy1 - xy0) / 2 + xy0;
00654     }
00655 
00656     return center_pos;
00657 }
00658 
00659 static void touch_int_callback(void) {
00660     sem_touch_int.release();
00661 }
00662 
00663 static void touch_task(void) {
00664     bool skip = false;
00665     bool zoom_on = false;
00666     uint32_t center_x;
00667     uint32_t center_y;
00668     uint32_t center_x_last = 0;
00669     uint32_t center_y_last = 0;
00670     int click_cnt = 0;
00671     int move_x = 0;
00672     int move_y = 0;
00673     float32_t distance;
00674     float32_t distance_last;
00675     int down_time = 0;;
00676     int last_down_time = 0;
00677     int event_time = 0;;
00678     int event_time_last = 0;
00679     TouchKey::touch_pos_t touch_pos[TOUCH_NUM];
00680     TouchKey::touch_pos_t touch_pos_last[TOUCH_NUM];
00681     int touch_num = 0;
00682     int touch_num_last = 0;
00683     Timer touch_timer;
00684     TouckKey_LCD_shield touch(P4_0, P2_13, I2C_SDA, I2C_SCL);
00685 
00686     /* Callback setting */
00687     touch.SetCallback(&touch_int_callback);
00688 
00689     /* Reset touch IC */
00690     touch.Reset();
00691 
00692     touch_timer.reset();
00693     touch_timer.start();
00694 
00695     while (1) {
00696         /* Wait touch event */
00697         sem_touch_int.wait();
00698 
00699         /* Get touch coordinates */
00700         touch_num = touch.GetCoordinates(TOUCH_NUM, touch_pos);
00701         event_time = touch_timer.read_ms();
00702         if (touch_num > touch_num_last) {
00703             if (touch_num_last == 0) {
00704                 down_time = event_time;
00705             }
00706             if (touch_num == 2) {
00707                 zoom_on = true;
00708             }
00709             move_x = 0;
00710             move_y = 0;
00711             distance_last = 0;
00712             if ((down_time - last_down_time) > 500) {
00713                 click_cnt = 0;
00714             }
00715             last_down_time = down_time;
00716         } else if ((touch_num == 0) && (touch_num_last != 0)) {
00717             if (((event_time - down_time) < 200)
00718              && (abs(move_x) < 10) && (abs(move_y) < 10)) {
00719                 click_cnt++;
00720                 if (click_cnt == 2) {
00721                     zoom_func(0, 0, 0, 0, 1.0f);
00722                 }
00723                 move_x = 0;
00724                 move_y = 0;
00725             } else {
00726                 click_cnt = 0;
00727             }
00728             if (zoom_on == false) {
00729                 move_end_func(move_x, move_y);
00730             }
00731             zoom_on = false;
00732             distance_last = 0;
00733         } else if ((touch_num == 1) && (touch_num_last == 2)) {
00734             distance_last = 0;
00735         } else if ((touch_num != 0) && ((event_time - event_time_last) >= 50)) {
00736             event_time_last = event_time;
00737             if (touch_num == 1) {
00738                 if (zoom_on == false) {
00739                     move_x = (touch_pos[0].x - touch_pos_last[0].x);
00740                     move_y = (touch_pos[0].y - touch_pos_last[0].y);
00741                     move_func(move_x, move_y);
00742                 }
00743             } else {
00744                 center_x = get_center(touch_pos[0].x, touch_pos[1].x);
00745                 center_y = get_center(touch_pos[0].y, touch_pos[1].y);
00746                 distance = get_distance(touch_pos[0].x, touch_pos[0].y, touch_pos[1].x, touch_pos[1].y);
00747                 if (distance < 1) {
00748                     distance = 1;
00749                 }
00750                 if (distance_last != 0) {
00751                     zoom_func(center_x_last, center_y_last, center_x, center_y, efect_info.magnification * (distance / distance_last));
00752                 }
00753                 center_x_last = center_x;
00754                 center_y_last = center_y;
00755                 distance_last = distance;
00756             }
00757         } else {
00758             skip = true;
00759         }
00760         if (skip == false) {
00761             touch_pos_last[0] = touch_pos[0];
00762             touch_pos_last[1] = touch_pos[1];
00763             touch_num_last = touch_num;
00764         }
00765         skip = false;
00766     }
00767 }
00768 #endif
00769 
00770 static void button_rise(void) {
00771     int wk_disp_time;
00772 
00773     wk_disp_time = disp_wait_time;
00774     wk_disp_time += 1000;
00775     if (wk_disp_time > 15000) {
00776         wk_disp_time = 0;
00777     }
00778     disp_wait_time = wk_disp_time;
00779     system_timer.reset();
00780     system_timer.start();
00781     mouse_info.disp_time = true;
00782     if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
00783         efect_info.event_req = EVENT_DISP;
00784     }
00785 }
00786 
00787 int main(void) {
00788     errnum_t err;
00789     Canvas2D_ContextConfigClass config;
00790 
00791     /* Change the baud rate of the printf() */
00792     pc.baud(921600);
00793 
00794     /* Initialization of LCD */
00795     Init_LCD_Display();    /* When using LCD, please call before than Init_Video(). */
00796 
00797     memset(user_frame_buffer1, 0, sizeof(user_frame_buffer1));
00798     memset(user_frame_buffer2, 0, sizeof(user_frame_buffer2));
00799     frame_buffer_info.buffer_address[0] = user_frame_buffer1;
00800     frame_buffer_info.buffer_address[1] = user_frame_buffer2;
00801     frame_buffer_info.buffer_count      = 2;
00802     frame_buffer_info.show_buffer_index = 0;
00803     frame_buffer_info.draw_buffer_index = 0;
00804     frame_buffer_info.width             = LCD_PIXEL_WIDTH;
00805     frame_buffer_info.byte_per_pixel    = FRAME_BUFFER_BYTE_PER_PIXEL;
00806     frame_buffer_info.stride            = LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL;
00807     frame_buffer_info.height            = LCD_PIXEL_HEIGHT;
00808     frame_buffer_info.pixel_format      = PIXEL_FORMAT_RGB565;
00809 
00810     config.frame_buffer = &frame_buffer_info;
00811     canvas2d = R_RGA_New_Canvas2D_ContextClass(config);
00812     err = R_OSPL_GetErrNum();
00813     if (err != 0) {
00814         printf("Line %d, error %d\n", __LINE__, err);
00815         mbed_die();
00816     }
00817 
00818     /* Start of LCD */
00819     Start_LCD_Display(frame_buffer_info.buffer_address[0]);
00820 
00821 #if (LCD_TYPE != 2)
00822     /* Backlight on */
00823     Thread::wait(200);
00824     lcd_cntrst.write(1.0);
00825 #endif
00826 
00827     mouseTask.start(callback(mouse_task));
00828 #ifdef TouckKey_LCD_shield
00829     touchTask.start(callback(touch_task));
00830 #endif
00831     button.rise(&button_rise);
00832 
00833     FATFileSystem fs(MOUNT_NAME);
00834     SDBlockDevice_GR_PEACH sd;
00835     USBHostMSD usb;
00836     int storage_type = 0;
00837     int next_storage_type = 0;
00838     int wait_time = 0;
00839     uint8_t * temp_buf[2];
00840     bool touch_key_in = false;
00841     int direction;
00842     int i;
00843     int wk_event_req;
00844     int type;
00845     int wk_abs_x_pos;
00846 
00847     temp_buf[0] = JpegBuffer;
00848     temp_buf[1] = new uint8_t[MAX_JPEG_SIZE];
00849     efect_info.event_req = EVENT_DISP;
00850     efect_info.p_pic_now = (uint8_t *)Renesas_logo_File;
00851     efect_info.p_pic_next = NULL;
00852     efect_info.magnification = 1.0f;
00853     efect_info.swipe_end_move = 0;
00854     efect_info.drow_pos_x = 0;
00855     efect_info.drow_pos_y = 0;
00856     efect_info.scale_end_move_x = 0;
00857     efect_info.scale_end_move_y = 0;
00858     efect_info.min_x = 0;
00859     efect_info.min_y = 0;
00860     efect_info.scroll = false;
00861     zoom_func(0, 0, 0, 0, 1.0f);
00862 
00863     while (1) {
00864         if (storage_type == 1) {
00865             if (!sd.connected()) {
00866                 fs.unmount();
00867                 next_storage_type = 0;
00868             }
00869         }
00870         if (storage_type == 2) {
00871             if (!usb.connected()) {
00872                 fs.unmount();
00873                 next_storage_type = 0;
00874             }
00875         }
00876         if (storage_type == 0) {
00877             if (sd.connect()) {
00878                 next_storage_type = 1; // SD
00879                 fs.mount(&sd);
00880             } else if (usb.connect()) {
00881                 next_storage_type = 2; // USB
00882                 fs.mount(&usb);
00883             } else {
00884                 // do nothing
00885             }
00886         }
00887         if (next_storage_type != storage_type) {
00888             if (next_storage_type == 0) {
00889                 storage_mode = false;
00890                 total_file_num = ROM_PIC_NUM;
00891                 file_id_now  = 0xffffffff;
00892                 file_id_next = 0xffffffff;
00893                 efect_info.p_pic_now    = (uint8_t *)Renesas_logo_File;
00894                 efect_info.p_pic_next   = NULL;
00895                 zoom_func(0, 0, 0, 0, 1.0f);
00896                 efect_info.event_req    = EVENT_DISP;
00897             } else {
00898                 storage_mode = true;
00899                 scan_foler.scan("/"MOUNT_NAME, extension_tbl);
00900                 total_file_num = scan_foler.getTotalFile();
00901                 file_id_now  = 0;
00902                 file_id_next = 0xffffffff;
00903                 efect_info.p_pic_now    = temp_buf[0];
00904                 efect_info.p_pic_next   = temp_buf[1];
00905                 read_new_file(&efect_info.p_pic_now, file_id_now);
00906                 zoom_func(0, 0, 0, 0, 1.0f);
00907                 efect_info.event_req    = EVENT_DISP;
00908             }
00909             storage_type = next_storage_type;
00910         }
00911 
00912         wk_event_req = efect_info.event_req;
00913         efect_info.event_req = EVENT_NONE;
00914 
00915         if (wk_event_req != EVENT_NONE) {
00916             dissolve_seq = 0;
00917         }
00918 
00919         switch (wk_event_req) {
00920             case EVENT_NEXT:
00921             case EVENT_PREV:
00922                 zoom_func(0, 0, 0, 0, 1.0f);
00923                 if (wk_event_req == EVENT_NEXT) {
00924                     direction = SCROLL_DIRECTION_NEXT;
00925                     read_next_file();
00926                 } else {
00927                     direction = SCROLL_DIRECTION_PREV;
00928                     read_prev_file();
00929                 }
00930                 for (i = 1; i <= SCROLL_STEP_NUM; i++) {
00931                     draw_image_scroll(&frame_buffer_info,
00932                                       (const graphics_image_t*)efect_info.p_pic_now,
00933                                       (const graphics_image_t*)efect_info.p_pic_next,
00934                                       (float32_t)i / (float32_t)SCROLL_STEP_NUM, direction);
00935                 }
00936                 swap_file_buff();
00937                 break;
00938             case EVENT_MOVE:
00939                 if ((efect_info.x_move * SCROLL_DIRECTION) >= 0) {
00940                     direction = SCROLL_DIRECTION_NEXT;
00941                     read_next_file();
00942                 } else {
00943                     direction = SCROLL_DIRECTION_PREV;
00944                     read_prev_file();
00945                 }
00946                 draw_image_scroll(&frame_buffer_info,
00947                                   (const graphics_image_t*)efect_info.p_pic_now,
00948                                   (const graphics_image_t*)efect_info.p_pic_next,
00949                                   (float32_t)abs(efect_info.x_move) / (float32_t)LCD_PIXEL_WIDTH, direction);
00950                 break;
00951             case EVENT_MOVE_END:
00952                 type = 0;
00953                 if ((efect_info.x_move * SCROLL_DIRECTION) >= 0) {
00954                     direction = SCROLL_DIRECTION_NEXT;
00955                     read_next_file();
00956                 } else {
00957                     direction = SCROLL_DIRECTION_PREV;
00958                     read_prev_file();
00959                 }
00960 
00961                 wk_abs_x_pos = abs(efect_info.x_move);
00962                 if (abs(efect_info.swipe_end_move) > 10) {
00963                     if ((efect_info.swipe_end_move * SCROLL_DIRECTION) >= 0) {
00964                         if (direction != SCROLL_DIRECTION_NEXT) {
00965                             type = 1;
00966                         }
00967                     } else {
00968                         if (direction != SCROLL_DIRECTION_PREV) {
00969                             type = 1;
00970                         }
00971                     }
00972                 } else {
00973                     if (wk_abs_x_pos < (LCD_PIXEL_WIDTH / 2)) {
00974                         type = 1;
00975                     }
00976                 }
00977 
00978                 if (type == 0) {
00979                     while (wk_abs_x_pos < LCD_PIXEL_WIDTH) {
00980                         wk_abs_x_pos += (LCD_PIXEL_WIDTH / SCROLL_STEP_NUM);
00981                         if (wk_abs_x_pos > LCD_PIXEL_WIDTH) {
00982                             wk_abs_x_pos = LCD_PIXEL_WIDTH;
00983                         }
00984                         draw_image_scroll(&frame_buffer_info,
00985                                           (const graphics_image_t*)efect_info.p_pic_now,
00986                                           (const graphics_image_t*)efect_info.p_pic_next,
00987                                           (float32_t)wk_abs_x_pos / (float32_t)LCD_PIXEL_WIDTH, direction);
00988                     }
00989                     swap_file_buff();
00990                 } else {
00991                     while (wk_abs_x_pos > 0) {
00992                         wk_abs_x_pos -= (LCD_PIXEL_WIDTH / SCROLL_STEP_NUM);
00993                         if (wk_abs_x_pos < 0) {
00994                             wk_abs_x_pos = 0;
00995                         }
00996                         draw_image_scroll(&frame_buffer_info,
00997                                           (const graphics_image_t*)efect_info.p_pic_now,
00998                                           (const graphics_image_t*)efect_info.p_pic_next,
00999                                           (float32_t)wk_abs_x_pos / (float32_t)LCD_PIXEL_WIDTH, direction);
01000                     }
01001                 }
01002                 efect_info.x_move = 0;
01003                 break;
01004             case EVENT_NONE:
01005             case EVENT_DISP:
01006             default:
01007                 if ((efect_info.scale_end_move_x != 0) || (efect_info.scale_end_move_y != 0) || (wk_event_req == EVENT_DISP)) {
01008                     if ((efect_info.scale_end_move_x != 0) || (efect_info.scale_end_move_y != 0)) {
01009                         efect_info.scale_end_move_x *= 0.8f;
01010                         efect_info.scale_end_move_y *= 0.8f;
01011                         zoom_scroll(efect_info.scale_end_move_x, efect_info.scale_end_move_y);
01012                         if (abs(efect_info.scale_end_move_x) < 5) {
01013                             efect_info.scale_end_move_x = 0;
01014                         }
01015                         if (abs(efect_info.scale_end_move_y) < 5) {
01016                             efect_info.scale_end_move_y = 0;
01017                         }
01018                     }
01019                     draw_image(&frame_buffer_info,
01020                                (const graphics_image_t*)efect_info.p_pic_now,
01021                                efect_info.drow_pos_x,
01022                                efect_info.drow_pos_y,
01023                                efect_info.magnification);
01024                     wait_time = 0;
01025                 } else if (((file_id_now == 0xffffffff) && (wait_time >= 3000))
01026                         || ((disp_wait_time != 0) && (wait_time >= disp_wait_time) && (mouse_info.disp_pos == false))) {
01027                     efect_info.magnification = 1.0f;
01028                     read_next_file();
01029                     dissolve_seq = 1;
01030                 } else {
01031                     /* do nothing */
01032                 }
01033                 if (dissolve_seq != 0) {
01034                     draw_image_dissolve(&frame_buffer_info,
01035                                         (const graphics_image_t*)efect_info.p_pic_now,
01036                                         (const graphics_image_t*)efect_info.p_pic_next,
01037                                         (float32_t)dissolve_seq / (float32_t)DISSOLVE_STEP_NUM);
01038                     if (dissolve_seq >= DISSOLVE_STEP_NUM) {
01039                         dissolve_seq = 0;
01040                         swap_file_buff();
01041                     } else {
01042                         dissolve_seq++;
01043                     }
01044                     wait_time = 0;
01045                 }
01046                 break;
01047         }
01048         if ((mouse_info.disp_time != false) && (system_timer.read_ms() > 1500)) {
01049             mouse_info.disp_time = false;
01050             if ((efect_info.event_req == EVENT_NONE) && (dissolve_seq == 0)) {
01051                 draw_image(&frame_buffer_info,
01052                            (const graphics_image_t*)efect_info.p_pic_now,
01053                            efect_info.drow_pos_x,
01054                            efect_info.drow_pos_y,
01055                            efect_info.magnification);
01056             }
01057         }
01058         if ((wk_event_req != EVENT_NONE) || (touch_key_in != false) || (efect_info.magnification != 1.0f) || (dissolve_seq != 0)
01059          || (mouse_info.disp_pos != false) || (mouse_info.disp_time != false) || (efect_info.scroll != false)) {
01060             wait_time = 0;
01061         } else {
01062             Thread::wait(50);
01063             if (wait_time < 100000) {
01064                 wait_time += 50;
01065             }
01066         }
01067     }
01068 }