【正点原子RV1126 AI Linux开发板】 基于tslib库触摸屏测试
[复制链接]
本帖最后由 TL-LED 于 2024-2-18 08:11 编辑
根据开发教程来学习下开发板触摸屏基于tslib库的测试。
一、tslib简介
tslib 是专门为触摸屏设备所开发的 Linux 应用层函数库,并且是开源的, tslib 为触摸屏驱动和应用层之间的适配层, 它把应用程序中读取触摸屏 struct input_event 类型数据(这
是输入设备上报给应用层的原始数据)并进行解析的操作过程进行了封装,向使用者提供了封装好的 API 接口。 tslib 从触摸屏中获得原始的坐标数据, 并通过一系列的去噪、去抖、坐标变换等操作,来去除噪声并将原始的触摸屏坐标转换为相应的屏幕坐标。
二、测试代码及编译
开发板已经移植了tslib库,按照手册来测试下tslib应用。
2.1、ts_read.c
#include <stdio.h>
#include <stdlib.h>
#include <tslib.h> //包含tslib.h头文件
int main(int argc, char *argv[])
{
struct tsdev *ts = NULL;
struct ts_sample samp;
int pressure = 0;//用于保存上一次的按压力,初始为0,表示松开
/* 打开并配置触摸屏设备 */
ts = ts_setup(NULL, 0);
if (NULL == ts) {
fprintf(stderr, "ts_setup error");
exit(EXIT_FAILURE);
}
/* 读数据 */
for ( ; ; ) {
if (0 > ts_read(ts, &samp, 1)) {
fprintf(stderr, "ts_read error");
ts_close(ts);
exit(EXIT_FAILURE);
}
if (samp.pressure) {//按压力>0
if (pressure) //若上一次的按压力>0
printf("触摸坐标(%d, %d)\n", samp.x, samp.y);
else
printf("触摸按下(%d, %d)\n", samp.x, samp.y);
}
else
printf("触摸松开\n");//打印坐标
pressure = samp.pressure;
}
ts_close(ts);
exit(EXIT_SUCCESS);
}
2.2、tslib.h
#ifndef _TSLIB_H_
#define _TSLIB_H_
/*
* tslib/src/tslib.h
*
* Copyright (C) 2016 Martin Kepplinger <martink@posteo.de>
* Copyright (C) 2001 Russell King.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
* SPDX-License-Identifier: LGPL-2.1
*
*
* Touch screen library interface definitions.
*/
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdarg.h>
#include <sys/time.h>
#ifdef WIN32
#define TSIMPORT __declspec(dllimport)
#define TSEXPORT __declspec(dllexport)
#define TSLOCAL
#else
#define TSIMPORT
#ifdef GCC_HASCLASSVISIBILITY
#define TSEXPORT __attribute__ ((visibility("default")))
#define TSLOCAL __attribute__ ((visibility("hidden")))
#else
#define TSEXPORT
#define TSLOCAL
#endif
#endif
#ifdef TSLIB_INTERNAL
#define TSAPI TSEXPORT
#else
#define TSAPI TSIMPORT
#endif /* TSLIB_INTERNAL */
struct tsdev;
struct ts_sample {
int x;
int y;
unsigned int pressure;
struct timeval tv;
};
struct ts_sample_mt {
/* ABS_MT_* event codes. linux/include/uapi/linux/input-event-codes.h
* has the definitions.
*/
int x;
int y;
unsigned int pressure;
int slot;
int tracking_id;
int tool_type;
int tool_x;
int tool_y;
unsigned int touch_major;
unsigned int width_major;
unsigned int touch_minor;
unsigned int width_minor;
int orientation;
int distance;
int blob_id;
struct timeval tv;
/* BTN_TOUCH state */
short pen_down;
/* valid is set != 0 if this sample
* contains new data; see below for the
* bits that get set.
* valid is set to 0 otherwise
*/
short valid;
};
#define TSLIB_MT_VALID (1 << 0) /* any new data */
#define TSLIB_MT_VALID_TOOL (1 << 1) /* new tool_x or tool_y data */
struct ts_lib_version_data {
const char *package_version;
int version_num;
unsigned int features; /* bitmask, see below */
};
#define TSLIB_VERSION_MT (1 << 0) /* multitouch support */
#define TSLIB_VERSION_OPEN_RESTRICTED (1 << 1) /* ts_open_restricted() */
#define TSLIB_VERSION_EVENTPATH (1 << 2) /* ts_get_eventpath() */
enum ts_param {
TS_SCREEN_RES = 0, /* 2 integer args, x and y */
TS_SCREEN_ROT /* 1 integer arg, 1 = rotate */
};
/*
* Close the touchscreen device, free all resources.
*/
TSAPI int ts_close(struct tsdev *);
/*
* Reloads all modules - useful to reload calibration data.
*/
TSAPI int ts_reconfig(struct tsdev *);
/*
* Configure the touchscreen device.
*/
TSAPI int ts_config(struct tsdev *);
/*
* Changes a setting.
*/
TSAPI int ts_option(struct tsdev *, enum ts_param, ...);
/*
* Change this hook to point to your custom error handling function.
*/
extern TSAPI int (*ts_error_fn)(const char *fmt, va_list ap);
/*
* Implement this to override open() for the input device and return the fd.
*/
extern TSAPI int (*ts_open_restricted)(const char *path, int flags, void *user_data);
/*
* Implement this to override close().
*/
extern TSAPI void (*ts_close_restricted)(int fd, void *user_data);
/*
* Returns the file descriptor in use for the touchscreen device.
*/
TSAPI int ts_fd(struct tsdev *);
/*
* Load a filter/scaling module
*/
TSAPI int ts_load_module(struct tsdev *, const char *mod, const char *params);
/*
* Open the touchscreen device.
*/
TSAPI struct tsdev *ts_open(const char *dev_name, int nonblock);
/*
* Find, open and configure the touchscreen device.
*/
TSAPI struct tsdev *ts_setup(const char *dev_name, int nonblock);
/*
* Return a scaled touchscreen sample.
*/
TSAPI int ts_read(struct tsdev *, struct ts_sample *, int);
/*
* Returns a raw, unscaled sample from the touchscreen.
*/
TSAPI int ts_read_raw(struct tsdev *, struct ts_sample *, int);
/*
* Return a scaled touchscreen multitouch sample.
*/
TSAPI int ts_read_mt(struct tsdev *, struct ts_sample_mt **, int slots, int nr);
/*
* Return a raw, unscaled touchscreen multitouch sample.
*/
TSAPI int ts_read_raw_mt(struct tsdev *, struct ts_sample_mt **, int slots, int nr);
/*
* This function returns a pointer to a static copy of the version info struct.
*/
TSAPI struct ts_lib_version_data *ts_libversion(void);
/*
* This function returns the path to the opened touchscreen input device file.
*/
TSAPI char *ts_get_eventpath(struct tsdev *tsdev);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _TSLIB_H_ */
2.3、编译
root@ubuntu:/opt/atk-rv1126_app/tslib# arm-linux-gnueabihf-gcc -o ts_read ts_read.c -lts --sysroot=/opt/atk-dlrv1126-toolchain/arm-buildroot-linux-gnueabihf/sysroot编译完成后,生成的文件
三、运行
3.1、复制文件到开发板
将上步编译后生成的文件复制到开发板
使用adb工具复制
连接USB-OTG到Ubuntu系统,检测到的设备
复制文件
root@ubuntu:/opt/atk-rv1126_app/tslib# adb -s 20230331RV1126FD2G0026 push ts_read /opt/
3.2、开发板运行
执行./ts_read运行
|