简介:
我们在上一篇适配了LVGL(8.3.10)显示的功能,还没有对接输入响应,板子上已经集成了一块GT911主控的触摸屏,我们在之前的基础上继续添加触摸屏的输入响应支持。对于触摸的驱动程序我们就不重复造轮子了,直接使用STM32Cube_FW_H7RS_V1.1.0\Drivers\BSP\STM32H7S78-DK 软件包里的驱动即可,把对应的驱动文件加入工程编译。
将上述驱动文件加入编译成功后对应的驱动程序已经ready,我么只要对接到 LVGL touch 设备依赖的驱动函数即可完成对应的适配,主要依赖我们porting的接口如下。
touchpad_init 接口初始化touch对应代码如下:
TS_Init_t hTS; TS_State_t TS_State; /*Initialize your touchpad*/ static void touchpad_init(void) { uint32_t ts_status = BSP_ERROR_NONE; /*Your code comes here*/ hTS.Width = 800; hTS.Height = 480; hTS.Orientation = TS_SWAP_NONE; hTS.Accuracy = 5; /* Touchscreen initialization */ ts_status = BSP_TS_Init(0, &hTS); if(ts_status != BSP_ERROR_NONE) { printf("touch init failed %d\r\n",ts_status); } else { printf("touch init ok\r\n"); } }
touch 按键状态检测接口touchpad_is_pressed
/*Return true is the touchpad is pressed*/ static bool touchpad_is_pressed(void) { uint32_t ts_status = BSP_ERROR_NONE; /*Your code comes here*/ ts_status = BSP_TS_GetState(0, &TS_State); return TS_State.TouchDetected ? true : false; }
获取touch 位置坐标接口 touchpad_is_pressed
/*Get the x and y coordinates if the touchpad is pressed*/ static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y) { /*Your code comes here*/ (*x) = TS_State.TouchX; (*y) = TS_State.TouchY; printf("x %d y %d \r\n",TS_State.TouchX,TS_State.TouchY); }
touch 初始化接口,向lvgl 注册输入设备
void lv_port_indev_init(void) { /** * Here you will find example implementation of input devices supported by LittelvGL: * - Touchpad * - Mouse (with cursor support) * - Keypad (supports GUI usage only with key) * - Encoder (supports GUI usage only with: left, right, push) * - Button (external buttons to press points on the screen) * * The `..._read()` function are only examples. * You should shape them according to your hardware */ static lv_indev_drv_t indev_drv; /*------------------ * Touchpad * -----------------*/ /*Initialize your touchpad if you have*/ touchpad_init(); /*Register a touchpad input device*/ lv_indev_drv_init(&indev_drv); indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = touchpad_read; indev_touchpad = lv_indev_drv_register(&indev_drv); }
在lvgl task 内添加lv_port_indev_init 调用完成touch 输入设备的注册。
板卡验证,LVGL 已经按照预期的可以响应触摸事件了。