Touch panel sample for GR-PEACH LCD Shield.

Dependencies:   GR-PEACH_video LCD_shield_config TouchKey_LCD_shield mbed-rtos mbed

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 "LCD_shield_config.h"
00005 #if (LCD_TYPE == 0)
00006   #include "TouchKey_4_3inch.h"
00007   #define TouckKey_LCD_shield               TouchKey_4_3inch
00008 #else
00009   #include "TouchKey_7_1inch.h"
00010   #define TouckKey_LCD_shield               TouchKey_7_1inch
00011 #endif
00012 
00013 /* FRAME BUFFER Parameter */
00014 #define FRAME_BUFFER_BYTE_PER_PIXEL         (2)
00015 #define FRAME_BUFFER_STRIDE                 (((LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u)
00016 
00017 #define TOUCH_NUM                           (2)
00018 #define DROW_POINT                          (3)
00019 
00020 DisplayBase Display;
00021 DigitalOut  lcd_pwon(P7_15);
00022 DigitalOut  lcd_blon(P8_1);
00023 PwmOut      lcd_cntrst(P8_15);
00024 Serial      pc(USBTX, USBRX);
00025 Semaphore   sem_touch_int(0);
00026 TouckKey_LCD_shield touch(P4_0, P2_13, I2C_SDA, I2C_SCL);
00027 
00028 static uint8_t user_frame_buffer[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));  /* 32 bytes aligned */
00029 static volatile int32_t vsync_count = 0;
00030 
00031 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type) {
00032     /* Interrupt callback function for Vsync interruption */
00033     if (vsync_count > 0) {
00034         vsync_count--;
00035     }
00036 }
00037 
00038 static void Wait_Vsync(const int32_t wait_count) {
00039     /* Wait for the specified number of times Vsync occurs */
00040     vsync_count = wait_count;
00041     while (vsync_count > 0) {
00042         /* Do nothing */
00043     }
00044 }
00045 
00046 static void Init_LCD_Display(void) {
00047     /* Create DisplayBase object */
00048     DisplayBase::graphics_error_t error;
00049     DisplayBase::lcd_config_t lcd_config;
00050     PinName lvds_pin[8] = {
00051         /* data pin */
00052         P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0
00053     };
00054 
00055     lcd_pwon = 0;
00056     lcd_blon = 0;
00057     Thread::wait(100);
00058     lcd_pwon = 1;
00059     lcd_blon = 1;
00060     Thread::wait(100);
00061     lcd_cntrst.write(1.0);
00062 
00063     /* Graphics initialization process */
00064     GetLcdConfig(&lcd_config);
00065     error = Display.Graphics_init(&lcd_config);
00066     if (error != DisplayBase::GRAPHICS_OK) {
00067         printf("Line %d, error %d\n", __LINE__, error);
00068         while (1);
00069     }
00070 
00071     /* Interrupt callback function setting (Vsync signal output from scaler 0) */
00072     error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_LO_VSYNC, 0, IntCallbackFunc_Vsync);
00073     if (error != DisplayBase::GRAPHICS_OK) {
00074         printf("Line %d, error %d\n", __LINE__, error);
00075         while (1);
00076     }
00077 
00078     Display.Graphics_Lvds_Port_Init(lvds_pin, 8);
00079 }
00080 
00081 static void drow_touch_pos(int id, int x, int y) {
00082     int idx_base;
00083     int wk_idx;
00084     int i;
00085     int j;
00086     uint8_t coller_pix[2];
00087 
00088     if ((x - (DROW_POINT / 2)) >= 0) {
00089         x -= (DROW_POINT / 2);
00090     }
00091     if (x > (LCD_PIXEL_WIDTH - DROW_POINT)) {
00092         x = (LCD_PIXEL_WIDTH - DROW_POINT);
00093     }
00094     if ((y - (DROW_POINT / 2)) >= 0) {
00095         y -= (DROW_POINT / 2);
00096     }
00097     if (y > (LCD_PIXEL_HEIGHT - DROW_POINT)) {
00098         y = (LCD_PIXEL_HEIGHT - DROW_POINT);
00099     }
00100     idx_base = (x + (LCD_PIXEL_WIDTH * y)) * FRAME_BUFFER_BYTE_PER_PIXEL;
00101 
00102     if (id == 0) {
00103         coller_pix[0] = 0xDD;
00104         coller_pix[1] = 0x9E;
00105     } else {
00106         coller_pix[0] = 0x34;
00107         coller_pix[1] = 0xDB;
00108     }
00109 
00110     for (i = 0; i < DROW_POINT; i++) {
00111         wk_idx = idx_base + (LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL * i);
00112         for (j = 0; j < DROW_POINT; j++) {
00113             user_frame_buffer[wk_idx++] = coller_pix[0];
00114             user_frame_buffer[wk_idx++] = coller_pix[1];
00115         }
00116     }
00117 }
00118 
00119 static void drow_touch_keyonoff(int id, bool onoff) {
00120     int idx_base;
00121     int wk_idx;
00122     int i;
00123     int j;
00124     uint8_t coller_pix[2];
00125 
00126     if (id == 0) {
00127         idx_base = 0;
00128     } else {
00129         idx_base = 5 * FRAME_BUFFER_BYTE_PER_PIXEL;
00130     }
00131 
00132     if (onoff == false) {
00133         coller_pix[0] = 0x00;
00134         coller_pix[1] = 0x00;
00135     } else {
00136         coller_pix[0] = 0xff;
00137         coller_pix[1] = 0xff;
00138     }
00139 
00140     for (i = 0; i < 5; i++) {
00141         wk_idx = idx_base + (LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL * i);
00142         for (j = 0; j < 5; j++) {
00143             user_frame_buffer[wk_idx++] = coller_pix[0];
00144             user_frame_buffer[wk_idx++] = coller_pix[1];
00145         }
00146     }
00147 }
00148 
00149 static void touch_int_callback(void) {
00150     sem_touch_int.release();
00151 }
00152 
00153 int main(void) {
00154     // Change the baud rate of the printf()
00155     pc.baud(921600);
00156 
00157     // Initialization of LCD
00158     DisplayBase::rect_t rect;
00159 
00160     Init_LCD_Display();
00161     memset(user_frame_buffer, 0, sizeof(user_frame_buffer));
00162     rect.vs = 0;
00163     rect.vw = LCD_PIXEL_HEIGHT;
00164     rect.hs = 0;
00165     rect.hw = LCD_PIXEL_WIDTH;
00166     Display.Graphics_Read_Setting(
00167         DisplayBase::GRAPHICS_LAYER_0,
00168         (void *)user_frame_buffer,
00169         FRAME_BUFFER_STRIDE,
00170         DisplayBase::GRAPHICS_FORMAT_RGB565,
00171         DisplayBase::WR_RD_WRSWA_32_16BIT,
00172         &rect
00173     );
00174     Wait_Vsync(2);
00175     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00176 
00177     // Touch panel processing
00178     TouchKey::touch_pos_t touch_pos[TOUCH_NUM];
00179     int touch_num = 0;
00180     int touch_num_last = 0;
00181     int i;
00182 
00183     touch.SetCallback(&touch_int_callback);
00184     touch.Reset();
00185     while (1) {
00186         sem_touch_int.wait();
00187         touch_num = touch.GetCoordinates(TOUCH_NUM, touch_pos);
00188         if ((touch_num != 0) && (touch_num_last == 0)) {
00189             memset(user_frame_buffer, 0, sizeof(user_frame_buffer));
00190         }
00191         for (i = 0; i < TOUCH_NUM; i ++) {
00192             printf("{valid=%d,x=%d,y=%d} ", touch_pos[i].valid, touch_pos[i].x, touch_pos[i].y);
00193             drow_touch_keyonoff(i, touch_pos[i].valid);
00194             if (touch_pos[i].valid) {
00195                 drow_touch_pos(i, touch_pos[i].x, touch_pos[i].y);
00196             }
00197         }
00198         printf("\n");
00199         touch_num_last = touch_num;
00200     }
00201 }