修改lvgl Linux demo使用tslib获取触摸坐标

  • Post author:
  • Post category:linux




1、为什么要修改/(ㄒoㄒ)/~~

交叉编译官方lvgl linux demo发现触摸坐标不准确。

官方demo获取坐标的做法是:

evdev_fd = open(EVDEV_NAME, O_RDWR | O_NOCTTY | O_NDELAY);
if(evdev_fd == -1) 
{
	perror("unable open evdev interface:");
	return;
}

fcntl(evdev_fd, F_SETFL, O_ASYNC | O_NONBLOCK);


while (read(evdev_fd, &in, sizeof(struct input_event)) > 0)
{
	....
}

如代码所示,做法是直接读取触摸的原始数据来作为lvgl的触摸数据,很显然这样不是很准确。



2、修改代码为tslib库获取触摸坐标


xxxx/lvgl/lv_port_linux_frame_buffer/lv_drivers/indev/evdev.c:

#if USE_TSLIB

struct tsdev *g_ts;

static struct ts_sample samp;

struct tsdev *init_ts(void)
{
	char *tsdevice = getenv("TSLIB_TSDEVICE");
	if (tsdevice == NULL)
	{
		tsdevice = "/dev/input/event2";
	}

	struct tsdev *ts = ts_open(tsdevice, 0);
	if (ts)
	{
		ts_config(ts);
	}

	return ts;
}

#endif


void evdev_init(void)
{
	// evdev_fd = open(EVDEV_NAME, O_RDWR | O_NOCTTY | O_NDELAY);
	// if(evdev_fd == -1) 
	// {
	// 	perror("unable open evdev interface:");
	// 	return;
	// }

	// fcntl(evdev_fd, F_SETFL, O_ASYNC | O_NONBLOCK);
#if USE_TSLIB
	g_ts = init_ts();
	if (g_ts == NULL)
	{
		printf("failed to open touchsreen\r\n");
		return;
	}
#endif

	evdev_root_x = 0;
	evdev_root_y = 0;
	evdev_key_val = 0;
	evdev_button = LV_INDEV_STATE_REL;
}

bool evdev_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
	// struct input_event in;
	if (ts_read(g_ts, &samp, 1) == 1) 
	{
		evdev_root_x = samp.x;
		evdev_root_y = samp.y;

		printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec, samp.x, samp.y, samp.pressure);
	}
	
	if (samp.pressure == 0)
	{
		evdev_button = LV_INDEV_STATE_REL;      //抬起
	}
	else
	{
		evdev_button = LV_INDEV_STATE_PR;      //按下	
	}
		
	data->point.x = evdev_root_x;
	data->point.y = evdev_root_y;  

	data->state = evdev_button;

	if (data->point.x < 0)
		data->point.x = 0;
	if (data->point.y < 0)
		data->point.y = 0;
	if (data->point.x >= lv_disp_get_hor_res(drv->disp))
		data->point.x = lv_disp_get_hor_res(drv->disp) - 1;
	if (data->point.y >= lv_disp_get_ver_res(drv->disp))
		data->point.y = lv_disp_get_ver_res(drv->disp) - 1;

	return false;
}



3、修改makfile

CC = arm-linux-gnueabihf-gcc
LVGL_DIR_NAME ?= lvgl
LVGL_DIR ?= ${shell pwd}
CFLAGS ?= -O3 -g0 -I$(LVGL_DIR)/ -Wall -Wshadow -Wundef -Wmaybe-uninitialized -Wmissing-prototypes -Wno-discarded-qualifiers -Wall -Wextra -Wno-unused-function -Wundef -Wno-error=strict-prototypes -Wpointer-arith -fno-strict-aliasing -Wno-error=cpp -Wuninitialized -Wmaybe-uninitialized -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code -Wno-switch-default -Wno-switch-enum -Wreturn-type -Wmultichar -Wformat-security -Wno-ignored-qualifiers -Wno-error=pedantic -Wno-sign-compare -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered -Wdeprecated -Wempty-body -Wtype-limits -Wshift-negative-value -Wstack-usage=1024 -Wno-unused-value -Wno-unused-parameter -Wno-missing-field-initializers -Wuninitialized -Wmaybe-uninitialized -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wpointer-arith -Wno-cast-qual -Wmissing-prototypes -Wunreachable-code -Wno-switch-default -Wswitch-enum -Wreturn-type -Wmultichar -Wno-discarded-qualifiers -Wformat-security -Wno-ignored-qualifiers -Wno-sign-compare -I /usr/local/tslib/include/
LDFLAGS ?= -lm -lts -L/usr/local/tslib/lib/
BIN = demo


CFLAGS

编译选项后加上 交叉编译好tslib库的头文件路径

-I /usr/local/tslib/include/



LDFLAGS

链接选项后加上交叉编译好tslib库的库路径

-lts -L/usr/local/tslib/lib/



4、存在的问题

做法上述修改后触摸坐标准确了,但存在反应有点慢的问题。。。待解决。



版权声明:本文为qq_36413982原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。