Image rotation Sample. This program uses GraphicsFramework library and L3GD20 sensor.
Dependencies: GR-PEACH_video GraphicsFramework L3GD20 R_BSP mbed-rtos mbed
Fork of RGA_HelloWorld by
About L3GD20
L3GD20 is a 3-axis gyroscope sensor and can be controlled by using theI2C.
- Datasheet of L3GD20
http://www.st.com/content/ccc/resource/technical/document/datasheet/43/37/e3/06/b0/bf/48/bd/DM00036465.pdf/files/DM00036465.pdf/jcr:content/translations/en.DM00036465.pdf
About wiring
Sensor | GR-PEACH |
VDD | 3.3V |
SCL | D15 |
SDA | D14 |
SA0 | 3.3V |
CS | 3.3V |
GND | GND |
main.cpp
- Committer:
- 1050186
- Date:
- 2016-05-23
- Revision:
- 4:138b18a6ac7b
- Parent:
- 2:c7faef0ef374
File content as of revision 4:138b18a6ac7b:
#include "mbed.h" #include "math.h" #include "rga_func.h" #include "DisplayBace.h" #include "rtos.h" #include "L3GD20.h" /**** LCD Parameter **********/ #define LCD_DE_MODE (0) #define LCD_SYNC_MODE (1) #define LCD_DOT_CLOCK (13.40f) // 13.4MHz #define LCD_H_WIDTH (480u) #define LCD_H_BACK_PORCH (43u) #define LCD_H_FRONT_PORCH (52u) #define LCD_H_SYNC_WIDTH (41u) #define LCD_V_WIDTH (272u) #define LCD_V_BACK_PORCH (12u) #define LCD_V_FRONT_PORCH (2u) #define LCD_V_SYNC_WIDTH (10u) #define LCD_MODE (LCD_SYNC_MODE) /*****************************/ /* FRAME BUFFER Parameter */ #define FRAME_BUFFER_BYTE_PER_PIXEL (2) #define FRAME_BUFFER_STRIDE (((LCD_H_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u) DigitalOut lcd_pwon(P7_15); DigitalOut lcd_blon(P8_1); PwmOut lcd_cntrst(P8_15); DisplayBase Display; L3GD20 gyro(I2C_SDA, I2C_SCL); static uint8_t user_frame_buffer[FRAME_BUFFER_STRIDE * LCD_V_WIDTH]__attribute((aligned(32))); /* 32 bytes aligned */ static uint8_t user_frame_buffer2[FRAME_BUFFER_STRIDE * LCD_V_WIDTH]__attribute((aligned(32))); /* 32 bytes aligned */ static frame_buffer_t frame_buffer_info; static volatile int32_t vsync_count = 0; static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type) { /* Interrupt callback function for Vsync interruption */ if (vsync_count > 0) { vsync_count--; } } static void Wait_Vsync(const int32_t wait_count) { /* Wait for the specified number of times Vsync occurs */ vsync_count = wait_count; while (vsync_count > 0) { /* Do nothing */ } } static void Swap_FrameBuffer(frame_buffer_t * frmbuf_info) { if (frmbuf_info->draw_buffer_index == 1) { frmbuf_info->draw_buffer_index = 0; } else { frmbuf_info->draw_buffer_index = 1; } } static void Update_LCD_Display(frame_buffer_t * frmbuf_info) { Display.Graphics_Read_Change(DisplayBase::GRAPHICS_LAYER_0, (void *)frmbuf_info->buffer_address[frmbuf_info->draw_buffer_index]); Wait_Vsync(1); } int main(void) { /* Create DisplayBase object */ DisplayBase::graphics_error_t error; float gx; float gy; float gz; memset(user_frame_buffer, 0, sizeof(user_frame_buffer)); memset(user_frame_buffer2, 0, sizeof(user_frame_buffer2)); frame_buffer_info.buffer_address[0] = user_frame_buffer; frame_buffer_info.buffer_address[1] = user_frame_buffer2; frame_buffer_info.buffer_count = 2; frame_buffer_info.show_buffer_index = 0; frame_buffer_info.draw_buffer_index = 0; frame_buffer_info.width = LCD_H_WIDTH; frame_buffer_info.byte_per_pixel = FRAME_BUFFER_BYTE_PER_PIXEL; frame_buffer_info.stride = LCD_H_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL; frame_buffer_info.height = LCD_V_WIDTH; frame_buffer_info.pixel_format = PIXEL_FORMAT_RGB565; lcd_pwon = 0; lcd_blon = 0; Thread::wait(100); lcd_pwon = 1; lcd_blon = 1; Thread::wait(100); DisplayBase::lcd_config_t lcd_config; PinName lvds_pin[8] = { /* data pin */ P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0 }; DisplayBase::rect_t rect; lcd_config.lcd_type = DisplayBase::LCD_TYPE_LVDS; lcd_config.intputClock = 66.67f; lcd_config.outputClock = LCD_DOT_CLOCK; lcd_config.lcd_outformat = DisplayBase::LCD_OUTFORMAT_RGB888; lcd_config.lcd_edge = DisplayBase::EDGE_RISING; #if(LCD_MODE) //SYNC Mode lcd_config.h_toatal_period = (LCD_H_BACK_PORCH + LCD_H_WIDTH + LCD_H_FRONT_PORCH); lcd_config.v_toatal_period = (LCD_V_BACK_PORCH + LCD_V_WIDTH + LCD_V_FRONT_PORCH); lcd_config.h_disp_widht = (LCD_H_WIDTH); lcd_config.v_disp_widht = (LCD_V_WIDTH); lcd_config.h_back_porch = (LCD_H_BACK_PORCH); lcd_config.v_back_porch = (LCD_V_BACK_PORCH); lcd_config.h_sync_port = DisplayBase::LCD_TCON_PIN_2; lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_INVERTED; lcd_config.h_sync_width = LCD_H_SYNC_WIDTH; lcd_config.v_sync_port = DisplayBase::LCD_TCON_PIN_0; lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_INVERTED; lcd_config.v_sync_width = LCD_V_SYNC_WIDTH; lcd_config.de_port = DisplayBase::LCD_TCON_PIN_3; lcd_config.de_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED; #else //DE Mode lcd_config.h_toatal_period = (LCD_H_WIDTH + 80u); lcd_config.v_toatal_period = (LCD_V_WIDTH); lcd_config.h_disp_widht = (LCD_H_WIDTH); lcd_config.v_disp_widht = (LCD_V_WIDTH); lcd_config.h_back_porch = (68u); lcd_config.v_back_porch = (18u); lcd_config.h_sync_port = DisplayBase::LCD_TCON_PIN_NON; lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED; lcd_config.h_sync_width = 0; lcd_config.v_sync_port = DisplayBase::LCD_TCON_PIN_NON; lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED; lcd_config.v_sync_width = 0; lcd_config.de_port = DisplayBase::LCD_TCON_PIN_3; lcd_config.de_port_polarity = DisplayBase::SIG_POL_INVERTED; #endif /* Graphics initialization process */ error = Display.Graphics_init(&lcd_config); if (error != DisplayBase::GRAPHICS_OK) { printf("Line %d, error %d\n", __LINE__, error); while (1); } /* Interrupt callback function setting (Vsync signal output from scaler 0) */ error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_LO_VSYNC, 0, IntCallbackFunc_Vsync); if (error != DisplayBase::GRAPHICS_OK) { printf("Line %d, error %d\n", __LINE__, error); while (1); } Display.Graphics_Lvds_Port_Init(lvds_pin, 8); rect.vs = 0; rect.vw = LCD_V_WIDTH; rect.hs = 0; rect.hw = LCD_H_WIDTH; Display.Graphics_Read_Setting( DisplayBase::GRAPHICS_LAYER_0, (void *)frame_buffer_info.buffer_address[0], FRAME_BUFFER_STRIDE, DisplayBase::GRAPHICS_FORMAT_RGB565, DisplayBase::WR_RD_WRSWA_32_16BIT, &rect ); /* Display Top Screen */ Set_RGAObject(&frame_buffer_info); Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0); lcd_cntrst.write(1.0); graphics_matrix_float_t work_angle = 0; while (1) { gyro.read(&gx, &gy, &gz); gz = gz * 0.146; //gz = [dps]. One loop is about 146ms(process[10ms] + printf[36ms] + wait[100ms]) if ((gz >= 1) || (gz <= -1)) { /* When the sensor tilts to the right, the value becomes a negative, and the image is roteted to the right. */ /* When the sensor tilts to the left, the value becomes a active, and the image is roteted to the left. */ work_angle -= gz; work_angle = (graphics_matrix_float_t)fmod(work_angle, ROTATION_MAX_NUM); printf("acce : %f angle : %5.2f\n", gz, work_angle); } /* Draw screen */ Swap_FrameBuffer(&frame_buffer_info); RGA_Func_Rotation(&frame_buffer_info, work_angle); Update_LCD_Display(&frame_buffer_info); Thread::wait(100); } }