Touch Screen Driver

This section describes how to use the Touch framework of RT-Thread to connect the touch screen driver to the LVGL input device framework.

At present, RT-Thread's Touch framework and touch driver software package are already very complete, covering a variety of commonly used touch chips:

Here I take stm32f469-st-disco BSP as an example. This development board has two touch chips in different batches, namely FT6336 and FT6206. Here we take FT6336 as an example. First we should check the chip manual:

Why do we ask you to read the data sheet first? Obviously, the software package does not support the FT6336 touch chip. When we look up the data sheet, we know that they belong to the FT6x36 series of touch chips. Then we can know that the software package contains the FT6236 touch driver. Therefore, we can use this touch driver for porting.

For details, refer to: Software Package Introduction

config BSP_USING_TOUCH_FT6X36
    bool "FT6x36"
    select BSP_USING_I2C1
    select PKG_USING_TOUCH_DRIVERS
    select PKG_USING_FT6236copymistakeCopy Success
  • Select the specific I2C bus according to the schematic diagram (according to the communication method of your own BSP)

  • Enable the touch driver framework of the software package

  • Select a specific touch driver software package

config BSP_USING_LVGL
    bool "Enable LVGL for LCD"
    select BSP_USING_LCD_OTM8009A
    select PKG_USING_LVGL
    select BSP_USING_TOUCH_FT6X36
    default ncopymistakeCopy Success
  • Add in LVGL configuration itemselect BSP_USING_TOUCH_FT6X36

3.2.1 Touch Hardware Initialization

static int lv_hw_touch_init(void)
{
    struct rt_touch_config cfg;

    cfg.dev_name = BSP_TOUCH_I2C_BUS_NAME;/* 使用的I2C设备名 */
#ifdef BSP_USING_TOUCH_FT6X36
    rt_hw_ft6236_init(TOUCH_DEV_NAME, &cfg, BSP_TOUCH_I2C_RESET_PIN); /* 软件包提供的初始化函数 */
#endif /* BSP_USING_TOUCH_FT6X36 */

    touch_dev = rt_device_find(TOUCH_DEV_NAME);
    if (rt_device_open(touch_dev, RT_DEVICE_FLAG_RDONLY) != RT_EOK)
    {
        LOG_E("Can't open touch device:%s", TOUCH_DEV_NAME);
        return -RT_ERROR;
    }

    return RT_EOK;
}

INIT_COMPONENT_EXPORT(lv_hw_touch_init);copymistakeCopy Success

Here is how to use the software package:

3.2.2 LVGL output device initialization

void lv_port_indev_init(void)
{
    static lv_indev_drv_t indev_drv;

    lv_indev_drv_init(&indev_drv); /*Basic initialization*/
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = input_read;

    /*Register the driver in LVGL and save the created input device object*/
    touch_indev = lv_indev_drv_register(&indev_drv);
}copymistakeCopy Success

3.2.3 Complete LVGL's read callback function input_read

static void input_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data)
{
    struct rt_touch_data *read_data;
    /* 可以将内存分配这个步骤改为全局变量,以提高读取效率 */
    read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));

    rt_device_read(touch_dev, 0, read_data, 1);

    /* 如果没有触摸事件,直接返回 */
    if (read_data->event == RT_TOUCH_EVENT_NONE)
        return;

    /* 这里需要注意的是:触摸驱动的原点可能和LCD的原点不一致,所以需要我们进行一些处理 */
#ifdef BSP_USING_TOUCH_FT6X36
    data->point.x = read_data->y_coordinate;
    data->point.y = LCD_HEIGHT - read_data->x_coordinate;
#endif /* BSP_USING_TOUCH_FT6X36 */

    if (read_data->event == RT_TOUCH_EVENT_DOWN)
        data->state = LV_INDEV_STATE_PR;
    if (read_data->event == RT_TOUCH_EVENT_MOVE)
        data->state = LV_INDEV_STATE_PR;
    if (read_data->event == RT_TOUCH_EVENT_UP)
        data->state = LV_INDEV_STATE_REL;
}copymistakeCopy Success

For this, please refer to the usage of Touch framework in RT-Thread Document Center :

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University