cat /proc/bus/input/devices
PROP:device properties and quirks(设备属性)
EV:types of events supported by the device(设备支持的事件类型)
KEY:keys/buttons this device has(此设备具有的键/按钮)
MSC:miscellaneous events supported by the device(设备支持的其他事件)
LED:leds present on the device(设备上的指示灯)
hexdump /dev/input/event0
struct pollfd fds[1];
int timeout_ms = 5000;
int ret;
fds[0].fd = fd;
fds[0].events = POLLIN;
ret = poll(fds, 1, timeout_ms);
if ((ret == 1) && (fds[0].revents & POLLIN))
{
read(fd, &val, 4);
printf("get button : 0x%x\n", val);
}
核心源码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* ./01_get_input_info /dev/input/event0 */
int main(int argc, char **argv)
{
int fd;
int err;
int len;
int ret;
int i;
unsigned char byte;
int bit;
struct input_id id;
unsigned int evbit[2];
struct input_event event;
struct pollfd fds[1];
nfds_t nfds = 1;
char *ev_names[] = {
"EV_SYN ",
"EV_KEY ",
"EV_REL ",
"EV_ABS ",
"EV_MSC ",
"EV_SW ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"EV_LED ",
"EV_SND ",
"NULL ",
"EV_REP ",
"EV_FF ",
"EV_PWR ",
};
if (argc != 2)
{
printf("Usage: %s \n", argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR | O_NONBLOCK);//打开设备文件。
if (fd < 0)
{
printf("open %s err\n", argv[1]);
return -1;
}
err = ioctl(fd, EVIOCGID, &id);
if (err == 0)
{
printf("bustype = 0x%x\n", id.bustype );
printf("vendor = 0x%x\n", id.vendor );
printf("product = 0x%x\n", id.product );
printf("version = 0x%x\n", id.version );
}
len = ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
if (len > 0 && len <= sizeof(evbit))
{
printf("support ev type: ");
for (i = 0; i < len; i++)
{
byte = ((unsigned char *)evbit)[i];
for (bit = 0; bit < 8; bit++)
{
if (byte & (1< 0)
{
if (fds[0].revents == POLLIN)
{
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
}
else if (ret == 0)//判断返回值:大于 0 表示期待的事件发生了,等于 0 表示超时。
{
printf("time out\n");
}
else
{
printf("poll err\n");
}
}
return 0;
}
static void sig_func(int sig)
{
int val;
read(fd, &val, 4);
printf("get button : 0x%x\n", val);
}
2.注册信号处理函数:
signal(SIGIO, sig_func);
3.打开驱动:
fd = open(argv[1], O_RDWR);
4.把进程 ID 告诉驱动:
fcntl(fd, F_SETOWN, getpid());
5.使能驱动的 FASYNC 功能:
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | FASYNC);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fd;
void my_sig_handler(int sig)
{
struct input_event event;
while (read(fd, &event, sizeof(event)) == sizeof(event))
{
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
}
}
/* ./05_input_read_fasync /dev/input/event0 */
int main(int argc, char **argv)
{
int err;
int len;
int ret;
int i;
unsigned char byte;
int bit;
struct input_id id;
unsigned int evbit[2];
unsigned int flags;
int count = 0;
char *ev_names[] = {
"EV_SYN ",
"EV_KEY ",
"EV_REL ",
"EV_ABS ",
"EV_MSC ",
"EV_SW ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"NULL ",
"EV_LED ",
"EV_SND ",
"NULL ",
"EV_REP ",
"EV_FF ",
"EV_PWR ",
};
if (argc != 2)
{
printf("Usage: %s \n", argv[0]);
return -1;
}
/* 注册信号处理函数 */
signal(SIGIO, my_sig_handler);
/* 打开驱动程序 */
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd < 0)
{
printf("open %s err\n", argv[1]);
return -1;
}
err = ioctl(fd, EVIOCGID, &id);
if (err == 0)
{
printf("bustype = 0x%x\n", id.bustype );
printf("vendor = 0x%x\n", id.vendor );
printf("product = 0x%x\n", id.product );
printf("version = 0x%x\n", id.version );
}
len = ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
if (len > 0 && len <= sizeof(evbit))
{
printf("support ev type: ");
for (i = 0; i < len; i++)
{
byte = ((unsigned char *)evbit)[i];
for (bit = 0; bit < 8; bit++)
{
if (byte & (1<
4 电阻屏和电容屏
触摸屏分为电阻屏、电容屏。电阻屏结构简单,在以前很流行;电容屏支持多点触摸,现在的手机基本都是使用电容屏。
注意 : LCD 、触摸屏不是一回事, LCD 是输出设备,触摸屏是输入设备。制作触摸屏时特意把它的尺寸做得跟 LCD 一模一样,并且把触摸屏覆盖在 LCD 上。
4.1 电阻屏
4.1.1复习一下欧姆定律
图中的电阻假设是均匀的,就是长度和阻值成正比关系。电阻长度为 L,阻值为 R ,在两端施加 3.3V 电压。在某点测得电阻为 V ,求上图中长度 X 。
根据欧姆定律:3.3/R = V/Rx ,
因为长度和阻值成正比关系,上述公式转换为: 3.3/L = V/X ,所以 X=LV/3.3 。
4.1.2电阻屏原理
电阻屏就是基于欧姆定律制作的,它有上下两层薄膜,这两层薄膜就是两个电阻,如下图所示:
平时上下两层薄膜无触触,当点击触摸屏时,上下两层薄膜接触:这时就可以测量触点电压。过程如下:
1. 测量 X 坐标:
在 xp 、 xm 两端施加 3.3V 电压, yp 和 ym 不施加电压 (yp 就相当于探针 ) ,测量 yp 电压值。该电压值就跟 X 坐标成正比关系,假设:
X = 3.3*Vyp/Xmax
2. 测量 Y 坐标:
在 yp 、 ym 两端施加 3.3V 电压, xp 和 xm 不施加电压 (xp 就相当于探针 ) ,测量 xp 电压值。该电压值就跟 Y 坐标成正比关系,假设:
Y = 3.3*Vxp/Ymax
在实际使用时,电阻屏的 Xmax 、 Ymax 无从得知,所以使用之前要先较准:依次点击触摸屏的四个角和中心点,推算出 X 、 Y 坐标的公式:
X = func(Vyp)
Y = func(Vxp)
3. 电阻屏数据
Linux 驱动程序中,会上报触点的 X 、 Y 数据,注意:这不是 LCD 的坐标值,需要 APP 再次处理才能转换为 LCD 坐标值。
对应的 input_event 结构体中,“ type 、 code 、 value ”如下:
按下时:
EV_KEY BTN_TOUCH 1 /* 按下 */
EV_ABS ABS_PRESSURE 1 /* 压力值,可以上报,也可以不报,可以是其他压力值 */
EV_ABS ABS_X x_value /* X 坐标 */
EV_ABS ABS_Y y_value /* Y 坐标 */
EV_SYNC 0 0 /* 同步事件 */
松开时:
EV_KEY BTN_TOUCH 0 /* 松开 */
EV_ABS ABS_PRESSURE 0 /* 压力值,可以上报,也可以不报 */
EV_SYNC 0 0 /* 同步事件 */
4.2 电容屏
4.2.1 原理
电容屏中有一个控制芯片,它会周期性产生驱动信号,接收电极接收到信号,并可测量电荷大小。当电容屏被按下时,相当于引入了新的电容,从而影响了接收电极接收到的电荷大小。主控芯片根据电荷大小即可计算出触点位置。
怎么通过电荷计算出触点位置?这由控制芯片实现,这类芯片一般是 I2C 接口。
我们只需要编写程序,通过 I2C 读取芯片寄存器即可得到这些数据。
4.2.2 电容屏数据
电容屏可以支持多点触摸(Multi touch) ,驱动程序上报的数据中怎么分辨触点?
这有两种方法: Type A 、 Type B ,这也对应两种类型的触摸屏:
1. Type A
该类型的触摸屏不能分辨是哪一个触点,它只是把所有触点的坐标一股脑地上报,由软件来分辨这些数据分别属于哪一个触点。
Type A 已经过时, Linux 内核中都没有 Type A 的源码了。
2. Type B
该类型的触摸屏能分辨是哪一个触点,上报数据时会先上报触点 ID ,再上报它的数据。
具体例子如下,这是最简单的示例,使用场景分析来看看它上报的数据。
当有 2 个触点时 (type, code, value) :
EV_ABS ABS_MT_SLOT 0 // 这表示“我要上报一个触点信息了”,用来分隔触点信息
EV_ABS ABS_MT_TRACKING_ID 45 // 这个触点的 ID 是 45
EV_ABS ABS_MT_POSITION_X x[0] // 触点 X 坐标
EV_ABS ABS_MT_POSITION_Y y[0] // 触点 Y 坐标
EV_ABS ABS_MT_SLOT 1 // 这表示“我要上报一个触点信息了”,用来分隔触点信息
EV_ABS ABS_MT_TRACKING_ID 46 // 这个触点的 ID 是 46
EV_ABS ABS_MT_POSITION_X x[1] // 触点 X 坐标
EV_ABS ABS_MT_POSITION_Y y[1] // 触点 Y 坐标
EV_SYNC SYN_REPORT 0 // 全部数据上报完毕
当 ID 为 45 的触点正在移动时:
EV_ABS ABS_MT_SLOT 0 // 这表示“我要上报一个触点信息了”,之前上报过 ID,就不用再上报 ID了
EV_ABS ABS_MT_POSITION_X x[0] // 触点 X 坐标
EV_SYNC SYN_REPORT 0 // 全部数据上报完毕
松开 ID 为 45 的触点时 ( 在前面 slot 已经被设置为 0 ,这里这需要再重新设置 slot, slot 就像一个全局变量一样:如果它没变化的话,就无需再次设置 ) :
// 刚刚设置了 ABS_MT_SLOT 为 0,它对应 ID 为 45,这里设置 ID 为-1 就表示 ID 为 45 的触点被松开了
EV_ABS ABS_MT_TRACKING_ID -1
EV_SYNC SYN_REPORT 0 // 全部数据上报完毕
最后,松开 ID 为 46 的触点:
EV_ABS ABS_MT_SLOT 1 // 这表示“我要上报一个触点信息了”,在前面设置过 slot 1 的 ID为 46
EV_ABS ABS_MT_TRACKING_ID -1 // ID 为-1,表示 slot 1 被松开,即 ID 为 46 的触点被松开
EV_SYNC SYN_REPORT // 全部数据上报完毕
4.3 电容屏的实验数据
假设你的开发板上电容屏对应的设备节点是/dev/input/event0 ,执行以下命令:
在上面的数据中,为了兼容老程序,它也上报了 ABS_X 、 ABS_Y 数据,电阻触摸屏就是使用这类型的数据。所以基于电阻屏的程序,也可以用在电容屏上。使用两个手指点击触摸屏时,得到类似如下的数据:
为了兼容老程序,它也上报了 ABS_X 、 ABS_Y 数据,但是只上报第 1 个触点的数据。
5 tslib
核心在于“plugins ”目录里的“插件”,或称为“ module ”。这个目录下的每个文件都是一个 module ,每个 module 都提供 2 个函数: read 、 read_mt ,前者用于读取单点触摸屏的数据,后者用于读取多点触摸屏的数据。
要分析 tslib 的框架,先看看示例程序怎么使用,我们参考 ts_test.c 和ts_test_mt.c,前者用于一般触摸屏 ( 比如电阻屏、单点电容屏 ) ,后者用于多点触摸屏。
调用 ts_open 后,可以打开某个设备节点,构造出一个 tsdev 结构体。然后调用 ts_config 读取配置文件的处理,假设 /etc/ts.conf 内容如下:
module_raw input
module pthres pmin=1
module dejitter delta=100
module linear
每行表示一个“module”或“moduel_raw”。
对于所有的“module”,都会插入 tsdev.list 链表头,也就是 tsdev.list 执行配置文件中最后一个“module”,配置文件中第一个“module”位于链表的 尾部。
对于所有的“module_raw ”,都会插入 tsdev.list_raw 链表头,一般只有一个“module_raw ”。
注意 : tsdev.list 中最后一个“ module ”会指向 ts_dev.list_raw 的头部。
无论是调用 ts_read 还是 ts_read_mt ,都是通过 tsdev.list 中的模块来处理数据的。这写模块是递归调用的,比如linear 模块的 read 函数如图
因为是递归调用,所有最先使用 input 模块读取设备节点得到原始数据,再依次经过 pthres 模块、 dejitter 模块、 linear 模块处理后,才返回最终数据。
5.2 交叉编译、测试 tslib
5.2.1 交叉编译 tslib
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin
交叉编译 tslib :
./configure --host=arm-linux-gnueabihf --prefix=/
make
make install DESTDIR=$PWD/tmp
确定工具链中头文件、库文件目录:
echo 'main(){}'| arm-linux-gnueabihf-gcc -E -v -
把头文件、库文件放到工具链目录下:
cd tslib-1.21/tmp/
cp include/* /home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/include
cp -d lib/*so* /home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/lib/
5.2.2 测试 tslib
把库文件放到单板上:运行程序要用。先在开发板上使用 NFS 挂载 Ubuntu 的目录,再把前面编译出来的 tslib-1.21/tmp/ 部分文件复制到板子上,示例命令如下:
cp /mnt/tslib-1.21/tmp/lib/*so* -d /lib
cp /mnt/tslib-1.21/tmp/bin/* /bin
cp /mnt/tslib-1.21/tmp/etc/ts.conf -d /etc
对于 IMX6ULL ,首先需要关闭默认的 qt gui 程序,才可以执行 ts_test_mt 测试命令,关闭 qt 命令如下所示:
mv /etc/init.d/S07hmi /root
reboot
在单板上执行测试程序:
ts_test_mt
5.3 自己写一个测试程序
5.3.1 接口函数深入分析
驱动程序使用 slot 、 tracking_id 来标识一个触点;当 tracking_id 等于 -1 时,标识这个触点被松开了。
触摸屏可能支持多个触点,比如 5 个: tslib 为了简化处理,即使只有 2 个触点,ts_read_mt 函数也会返回 5 个触点数据,可以根据标志位判断数据是否
有效。
ts_read_mt 函数原型如图 :
假设nr 设置为 1 , max_slots 设置为 5 ,那么读到的数据保存在: samp[0][0] 、 samp[0][1]、 samp[0][2] 、 samp[0][3] 、 samp[0][4] 中。
假设nr 设置为 2 , max_slots 设置为 5 ,那么读到的数据保存在:samp[0][0]、 samp[0][1] 、 samp[0][2] 、 samp[0][3] 、 samp[0][4] 和 samp[1][0] 、 samp[1][1]、 samp[1][2] 、 samp[1][3] 、 samp[1][4] 中。
ts_sample_mt 结构体如图
你可能感兴趣的:(Linux系统应用,linux)