Tomo Yamanaka / Mbed 2 deprecated ImageRotaion_Sample

Dependencies:   GR-PEACH_video GraphicsFramework L3GD20 R_BSP mbed-rtos mbed

Fork of RGA_HelloWorld 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 "math.h"
00003 #include "rga_func.h"
00004 #include "DisplayBace.h"
00005 #include "rtos.h"
00006 #include "L3GD20.h"
00007 
00008 /**** LCD Parameter **********/
00009 #define LCD_DE_MODE            (0)
00010 #define LCD_SYNC_MODE          (1)
00011 
00012 #define LCD_DOT_CLOCK          (13.40f)     // 13.4MHz
00013 
00014 #define LCD_H_WIDTH            (480u)
00015 #define LCD_H_BACK_PORCH       (43u)
00016 #define LCD_H_FRONT_PORCH      (52u)
00017 #define LCD_H_SYNC_WIDTH       (41u)
00018 
00019 #define LCD_V_WIDTH            (272u)
00020 #define LCD_V_BACK_PORCH       (12u)
00021 #define LCD_V_FRONT_PORCH      (2u)
00022 #define LCD_V_SYNC_WIDTH       (10u)
00023 
00024 #define LCD_MODE               (LCD_SYNC_MODE)
00025 
00026 /*****************************/
00027 
00028 /* FRAME BUFFER Parameter */
00029 #define FRAME_BUFFER_BYTE_PER_PIXEL         (2)
00030 #define FRAME_BUFFER_STRIDE                 (((LCD_H_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u)
00031 
00032 DigitalOut  lcd_pwon(P7_15);
00033 DigitalOut  lcd_blon(P8_1);
00034 PwmOut      lcd_cntrst(P8_15);
00035 DisplayBase Display;
00036 L3GD20      gyro(I2C_SDA, I2C_SCL);
00037 
00038 static uint8_t user_frame_buffer[FRAME_BUFFER_STRIDE * LCD_V_WIDTH]__attribute((aligned(32)));  /* 32 bytes aligned */
00039 static uint8_t user_frame_buffer2[FRAME_BUFFER_STRIDE * LCD_V_WIDTH]__attribute((aligned(32))); /* 32 bytes aligned */
00040 static frame_buffer_t frame_buffer_info;
00041 static volatile int32_t vsync_count = 0;
00042 
00043 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type) {
00044     /* Interrupt callback function for Vsync interruption */
00045     if (vsync_count > 0) {
00046         vsync_count--;
00047     }
00048 }
00049 
00050 static void Wait_Vsync(const int32_t wait_count) {
00051     /* Wait for the specified number of times Vsync occurs */
00052     vsync_count = wait_count;
00053     while (vsync_count > 0) {
00054         /* Do nothing */
00055     }
00056 }
00057 
00058 static void Swap_FrameBuffer(frame_buffer_t * frmbuf_info) {
00059     if (frmbuf_info->draw_buffer_index == 1) {
00060         frmbuf_info->draw_buffer_index = 0;
00061     } else {
00062         frmbuf_info->draw_buffer_index = 1;
00063     }
00064 }
00065 
00066 static void Update_LCD_Display(frame_buffer_t * frmbuf_info) {
00067     Display.Graphics_Read_Change(DisplayBase::GRAPHICS_LAYER_0,
00068      (void *)frmbuf_info->buffer_address[frmbuf_info->draw_buffer_index]);
00069     Wait_Vsync(1);
00070 }
00071 
00072 int main(void) {
00073     /* Create DisplayBase object */
00074     DisplayBase::graphics_error_t error;
00075     float gx;
00076     float gy;
00077     float gz;
00078 
00079     memset(user_frame_buffer, 0, sizeof(user_frame_buffer));
00080     memset(user_frame_buffer2, 0, sizeof(user_frame_buffer2));
00081     frame_buffer_info.buffer_address[0] = user_frame_buffer;
00082     frame_buffer_info.buffer_address[1] = user_frame_buffer2;
00083     frame_buffer_info.buffer_count      = 2;
00084     frame_buffer_info.show_buffer_index = 0;
00085     frame_buffer_info.draw_buffer_index = 0;
00086     frame_buffer_info.width             = LCD_H_WIDTH;
00087     frame_buffer_info.byte_per_pixel    = FRAME_BUFFER_BYTE_PER_PIXEL;
00088     frame_buffer_info.stride            = LCD_H_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL;
00089     frame_buffer_info.height            = LCD_V_WIDTH;
00090     frame_buffer_info.pixel_format      = PIXEL_FORMAT_RGB565;
00091 
00092     lcd_pwon = 0;
00093     lcd_blon = 0;
00094     Thread::wait(100);
00095  
00096     lcd_pwon = 1;
00097     lcd_blon = 1;
00098     Thread::wait(100);
00099 
00100     DisplayBase::lcd_config_t lcd_config;
00101     PinName lvds_pin[8] = {
00102         /* data pin */
00103         P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0
00104     };
00105     DisplayBase::rect_t rect;
00106 
00107     lcd_config.lcd_type             = DisplayBase::LCD_TYPE_LVDS;
00108     lcd_config.intputClock          = 66.67f;
00109     lcd_config.outputClock          = LCD_DOT_CLOCK;
00110     lcd_config.lcd_outformat        = DisplayBase::LCD_OUTFORMAT_RGB888;
00111     lcd_config.lcd_edge             = DisplayBase::EDGE_RISING;
00112 #if(LCD_MODE) //SYNC Mode
00113     lcd_config.h_toatal_period      = (LCD_H_BACK_PORCH + LCD_H_WIDTH + LCD_H_FRONT_PORCH);
00114     lcd_config.v_toatal_period      = (LCD_V_BACK_PORCH + LCD_V_WIDTH + LCD_V_FRONT_PORCH);
00115  
00116     lcd_config.h_disp_widht         = (LCD_H_WIDTH);
00117     lcd_config.v_disp_widht         = (LCD_V_WIDTH);
00118     lcd_config.h_back_porch         = (LCD_H_BACK_PORCH);
00119     lcd_config.v_back_porch         = (LCD_V_BACK_PORCH);
00120  
00121     lcd_config.h_sync_port          = DisplayBase::LCD_TCON_PIN_2;
00122     lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_INVERTED;
00123     lcd_config.h_sync_width         = LCD_H_SYNC_WIDTH;
00124  
00125     lcd_config.v_sync_port          = DisplayBase::LCD_TCON_PIN_0;
00126     lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_INVERTED;
00127     lcd_config.v_sync_width         = LCD_V_SYNC_WIDTH;
00128  
00129     lcd_config.de_port              = DisplayBase::LCD_TCON_PIN_3;
00130     lcd_config.de_port_polarity     = DisplayBase::SIG_POL_NOT_INVERTED;
00131 #else  //DE Mode
00132     lcd_config.h_toatal_period      = (LCD_H_WIDTH + 80u);
00133     lcd_config.v_toatal_period      = (LCD_V_WIDTH);
00134 
00135     lcd_config.h_disp_widht         = (LCD_H_WIDTH);
00136     lcd_config.v_disp_widht         = (LCD_V_WIDTH);
00137     lcd_config.h_back_porch         = (68u);
00138     lcd_config.v_back_porch         = (18u);
00139 
00140     lcd_config.h_sync_port          = DisplayBase::LCD_TCON_PIN_NON;
00141     lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED;
00142     lcd_config.h_sync_width         = 0;
00143 
00144     lcd_config.v_sync_port          = DisplayBase::LCD_TCON_PIN_NON;
00145     lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED;
00146     lcd_config.v_sync_width         = 0;
00147 
00148     lcd_config.de_port              = DisplayBase::LCD_TCON_PIN_3;
00149     lcd_config.de_port_polarity     = DisplayBase::SIG_POL_INVERTED;
00150 #endif
00151 
00152     /* Graphics initialization process */
00153     error = Display.Graphics_init(&lcd_config);
00154     if (error != DisplayBase::GRAPHICS_OK) {
00155         printf("Line %d, error %d\n", __LINE__, error);
00156         while (1);
00157     }
00158 
00159     /* Interrupt callback function setting (Vsync signal output from scaler 0) */
00160     error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_LO_VSYNC, 0, IntCallbackFunc_Vsync);
00161     if (error != DisplayBase::GRAPHICS_OK) {
00162         printf("Line %d, error %d\n", __LINE__, error);
00163         while (1);
00164     }
00165 
00166     Display.Graphics_Lvds_Port_Init(lvds_pin, 8);
00167     rect.vs = 0;
00168     rect.vw = LCD_V_WIDTH;
00169     rect.hs = 0;
00170     rect.hw = LCD_H_WIDTH;
00171 
00172     Display.Graphics_Read_Setting(
00173         DisplayBase::GRAPHICS_LAYER_0,
00174         (void *)frame_buffer_info.buffer_address[0],
00175         FRAME_BUFFER_STRIDE,
00176         DisplayBase::GRAPHICS_FORMAT_RGB565,
00177         DisplayBase::WR_RD_WRSWA_32_16BIT,
00178         &rect
00179     );
00180 
00181     /* Display Top Screen */
00182     Set_RGAObject(&frame_buffer_info);
00183     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00184     lcd_cntrst.write(1.0);
00185 
00186     graphics_matrix_float_t work_angle = 0;
00187     while (1) {
00188         gyro.read(&gx, &gy, &gz);
00189         gz = gz * 0.146;             //gz = [dps]. One loop is about 146ms(process[10ms] + printf[36ms] + wait[100ms])
00190         if ((gz >= 1) || (gz <= -1)) {
00191             /* When the sensor tilts to the right, the value becomes a negative, and the image is roteted to the right. */
00192             /* When the sensor tilts to the left, the value becomes a active, and the image is roteted to the left. */
00193             work_angle -= gz;
00194             work_angle = (graphics_matrix_float_t)fmod(work_angle, ROTATION_MAX_NUM);
00195             printf("acce : %f  angle : %5.2f\n", gz, work_angle);
00196         }
00197         /* Draw screen */
00198         Swap_FrameBuffer(&frame_buffer_info);
00199         RGA_Func_Rotation(&frame_buffer_info, work_angle);
00200         Update_LCD_Display(&frame_buffer_info);
00201         Thread::wait(100);
00202     }
00203 }