如何使用Linux获取触摸屏原始数据的坐标



我们有一个3米的微型触摸显示屏。它通过USB连接到我的Debian系统,并被识别为人机界面(hid)。我正在尝试访问和推送实时信息。。。如果它被触摸了,我想知道在哪里(x,y),并通过netcat将它传送到另一个主机。

不幸的是,我只能使用获取原始数据

cat /dev/input/event2 | hexdump

evtest

你得到的六进制代码似乎没有记录。。。

有人知道如何获得这些信息吗?必须有一种方法从六进制代码中提取它。不幸的是,我不知道如何解释六进制代码。我找不到任何有记录的来源。。。

内核有没有一种方法可以实时向我提供那些所需的信息?

作为一种变通方法,是否有X-Server可以告诉我的解决方案?触摸屏的行为就像X中的鼠标。实际上,我已经尝试过通过xlib获取鼠标的X,y位置。但它太慢了,不会告诉我是否有人在触摸。。。

evtest样本输出:

Event: time 1425319271.595631, type 3 (EV_ABS), code 57 (ABS_MT_TRACKING_ID), value 51
Event: time 1425319271.595631, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 10304
Event: time 1425319271.595631, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 30629
Event: time 1425319271.595631, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 893
Event: time 1425319271.595631, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 414
Event: time 1425319271.595631, type 1 (EV_KEY), code 330 (BTN_TOUCH), value 1
Event: time 1425319271.595631, type 3 (EV_ABS), code 0 (ABS_X), value 10304
Event: time 1425319271.595631, type 3 (EV_ABS), code 1 (ABS_Y), value 30629
Event: time 1425319271.595631, -------------- SYN_REPORT ------------
Event: time 1425319271.601632, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 10306
Event: time 1425319271.601632, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 30625
Event: time 1425319271.601632, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 962
Event: time 1425319271.601632, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 421
Event: time 1425319271.601632, type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 1
Event: time 1425319271.601632, type 3 (EV_ABS), code 57 (ABS_MT_TRACKING_ID), value 52
Event: time 1425319271.601632, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 15416
Event: time 1425319271.601632, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 24159
Event: time 1425319271.601632, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 649
Event: time 1425319271.601632, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 354
Event: time 1425319271.601632, type 3 (EV_ABS), code 0 (ABS_X), value 10306
Event: time 1425319271.601632, type 3 (EV_ABS), code 1 (ABS_Y), value 30625
Event: time 1425319271.601632, -------------- SYN_REPORT ------------
Event: time 1425319271.606626, type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 0
Event: time 1425319271.606626, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 10318
Event: time 1425319271.606626, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 30609
Event: time 1425319271.606626, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 1014
Event: time 1425319271.606626, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 426
Event: time 1425319271.606626, type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 1
Event: time 1425319271.606626, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 24161
Event: time 1425319271.606626, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 681
Event: time 1425319271.606626, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 376
Event: time 1425319271.606626, type 3 (EV_ABS), code 0 (ABS_X), value 10318
Event: time 1425319271.606626, type 3 (EV_ABS), code 1 (ABS_Y), value 30609
Event: time 1425319271.606626, -------------- SYN_REPORT ------------
Event: time 1425319271.611629, type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 0
Event: time 1425319271.611629, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 10320
Event: time 1425319271.611629, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 30605
Event: time 1425319271.611629, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 1053
Event: time 1425319271.611629, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 430
Event: time 1425319271.611629, type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 1
Event: time 1425319271.611629, type 3 (EV_ABS), code 48 (ABS_MT_TOUCH_MAJOR), value 705
Event: time 1425319271.611629, type 3 (EV_ABS), code 49 (ABS_MT_TOUCH_MINOR), value 392
Event: time 1425319271.611629, type 3 (EV_ABS), code 0 (ABS_X), value 10320
Event: time 1425319271.611629, type 3 (EV_ABS), code 1 (ABS_Y), value 30605

基于控制台的解决方案

您可以使用evtest工具获取解析后的坐标。

  1. 如果您只需要单点触摸坐标:查找ABS_XABS_Y字段:

     type 3 (EV_ABS), code 0 (ABS_X), value 10306
     type 3 (EV_ABS), code 1 (ABS_Y), value 30625
    
  2. 如果您需要多点触摸坐标:

    • ABS_MT_SLOT表示手指数
    • ABS_MT_POSITION_XABS_MT_POSITION_Y——坐标

    手指#0:

     type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 0
     type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 10318
     type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 30609
    

    手指#1:

     type 3 (EV_ABS), code 47 (ABS_MT_SLOT), value 1
     type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 20301
     type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 24161
    

例如,如果您需要通过网络发送单点触摸坐标,您可以使用以下脚本:

#!/bin/sh
# ---- Global variables ----
input=/dev/input/event0
code_prefix="ABS"
code="${code_prefix}_[XY]"
val_regex=".*(${code_prefix}_(.)), value ([-]?[0-9]+)"
val_subst="1=2"
# ---- Functions ----
send_axis() {
    # 1. Convert axis value ($1) from device specific units
    # 2. Send this axis value via UDP packet
    echo $1
}
process_line() {  
    while read line; do
        axis=$(echo $line | grep "^Event:" | grep $code | 
               sed "s/$val_regex/$val_subst/")
        if [ -n "$axis" ]; then
            send_axis $axis
        fi
    done
}
# ---- Entry point ----
if [ $(id -u) -ne 0 ]; then
    echo "This script must be run from root" >&2
    exit 1
fi
evtest $input | process_line

基于程序的解决方案

您可以编写C应用程序来读取您的事件文件。获得的二进制数据可以很容易地解释,请参阅内核文档中的第5节。您可以使用select()系统调用等待下一个数据部分。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
/* Change it to your dev file for the touch screen */
#define EVENT_DEVICE    "/dev/input/event2"
#define EVENT_TYPE      EV_ABS
#define EVENT_CODE_X    ABS_X
#define EVENT_CODE_Y    ABS_Y
/* TODO: Close fd on SIGINT (Ctrl-C), if it's open */
static int fd = -1;
int main(int argc, char *argv[])
{
    struct input_event ev;
    char name[256] = "Unknown";
    fd_set readfds;
    /* /dev/input/event* files are only readable by root:input */
    if ((getuid()) != 0) {
        fprintf(stderr, "You are not root! This may not work...n");
        return EXIT_SUCCESS;
    }
    /* Open device for non-blocking read */
    fd = open(EVENT_DEVICE, O_RDONLY | O_NONBLOCK);
    if (fd == -1) {
        fprintf(stderr, "%s is not a vaild devicen", EVENT_DEVICE);
        return EXIT_FAILURE;
    }
    /* Print device name */
    ioctl(fd, EVIOCGNAME(sizeof(name)), name);
    printf("Reading from:n");
    printf("  - device file: %sn", EVENT_DEVICE);
    printf("  - device name: %sn", name);
    /* Prepare for select(): zero and set the fd into fd_set */
    FD_ZERO(&readfds);
    FD_SET(fd, &readfds);
    /* Press Ctrl-C to stop the program */
    for (;;) {
        const size_t ev_size = sizeof(struct input_event);
        ssize_t size;
        int ret;
        /* struct timeval timeout = { 30, 0 }; */
        /*
         * select(): no-timeout version, just stop on errors or
         * interrupts.
         */
        ret = select(fd + 1, &readfds, NULL, NULL, NULL);
        /*
         * select(): timeout-enabled version, stop if no event has
         * occurred until timeout; might be useful e.g. for background
         * tasks.
         */
        /* ret = select(fd + 1, &readfds, NULL, NULL, &timeout); */
        if (ret == -1) {
            perror("Error: select() failed");
            goto err;
        } else if (ret == 0) {
            fprintf(stderr, "Error: select() timeoutn");
            continue;
        }
        size = read(fd, &ev, ev_size);
        if (size < ev_size) {
            fprintf(stderr, "Error: Wrong size when readingn");
            goto err;
        }
        if (ev.type == EVENT_TYPE && (ev.code == EVENT_CODE_X
                                      || ev.code == EVENT_CODE_Y)) {
            /* TODO: convert value to pixels */
            printf("%s = %dn", ev.code == EVENT_CODE_X ? "X" : "Y",
                   ev.value);
        }
    }
    return EXIT_SUCCESS;
err:
    close(fd);
    return EXIT_FAILURE;
}

坐标单位

首先,你需要知道接下来的事情:

  • 其中为坐标原点(即[x=0;y=0]
  • 设备用于表示坐标的单位

这些信息通常可以在设备的驱动程序代码中找到。

这是您设备的驱动程序。

因此,您似乎需要将evtest的轴值除以65535,然后乘以设备的宽度或高度(以像素为单位)。例如,若你们的X=30000,并且你们的LCD面板的宽度是1080像素,你们需要做:

X = round((30000 / 65535) * 1080) = 494 pixels

你得到的六进制代码似乎没有记录。。。

这是由设备本身记录的,hid-multitouch驱动程序就是这样解释它接收的USB数据的。

您可以使用以下命令查找信息:

$ lsusb              # determine the bus & device numbers
...
Bus 001 Device 067: ID 1aad:000f KeeTouch
...
$ sudo usbhid-dump -a 1:67 -e d
001:067:002:DESCRIPTOR         1615651625.434241
 05 0D 09 04 A1 01 85 03 09 22 09 00 15 00 26 FF
 00 75 08 95 09 81 02 A1 00 05 0D 09 51 15 00 26
 FF 00 75 08 95 01 81 02 05 0D 09 42 15 00 25 01
 75 01 95 01 81 02 09 32 81 02 09 47 81 02 95 05
 81 03 05 01 09 30 26 FF 7F 55 00 65 00 35 00 46
 00 00 75 10 95 01 81 02 09 31 35 00 46 00 00 81
 02 05 0D 09 48 15 00 26 FF 7F 75 10 95 01 81 02
 09 49 15 00 26 FF 7F 75 10 95 01 81 02 C0 A1 00
 05 0D 09 51 15 00 26 FF 00 75 08 95 01 81 02 05
 0D 09 42 15 00 25 01 75 01 95 01 81 02 09 32 81
 02 09 47 81 02 95 05 81 03 05 01 09 30 26 FF 7F
 55 00 65 00 35 00 46 00 00 75 10 95 01 81 02 09
 31 35 00 46 00 00 81 02 05 0D 09 48 15 00 26 FF
 7F 75 10 95 01 81 02 09 49 15 00 26 FF 7F 75 10
 95 01 81 02 C0 A1 00 05 0D 09 51 15 00 26 FF 00
 75 08 95 01 81 02 05 0D 09 42 15 00 25 01 75 01
 95 01 81 02 09 32 81 02 09 47 81 02 95 05 81 03
 05 01 09 30 26 FF 7F 55 00 65 00 35 00 46 00 00
 75 10 95 01 81 02 09 31 35 00 46 00 00 81 02 05
 0D 09 48 15 00 26 FF 7F 75 10 95 01 81 02 09 49
 15 00 26 FF 7F 75 10 95 01 81 02 C0 A1 00 05 0D
 09 51 15 00 26 FF 00 75 08 95 01 81 02 05 0D 09
 42 15 00 25 01 75 01 95 01 81 02 09 32 81 02 09
 47 81 02 95 05 81 03 05 01 09 30 26 FF 7F 55 00
 65 00 35 00 46 00 00 75 10 95 01 81 02 09 31 35
 00 46 00 00 81 02 05 0D 09 48 15 00 26 FF 7F 75
 10 95 01 81 02 09 49 15 00 26 FF 7F 75 10 95 01
 81 02 C0 A1 00 05 0D 09 51 15 00 26 FF 00 75 08
 95 01 81 02 05 0D 09 42 15 00 25 01 75 01 95 01
 81 02 09 32 81 02 09 47 81 02 95 05 81 03 05 01
 09 30 26 FF 7F 55 00 65 00 35 00 46 00 00 75 10
 95 01 81 02 09 31 35 00 46 00 00 81 02 05 0D 09
 48 15 00 26 FF 7F 75 10 95 01 81 02 09 49 15 00
 26 FF 7F 75 10 95 01 81 02 C0 A1 00 05 0D 09 54
 15 00 25 1F 75 08 95 01 81 02 C0 09 55 85 FD 15
 00 26 FF 00 75 08 95 01 B1 02 C0 05 0D 09 0E A1
 01 85 FC 09 52 09 53 15 00 26 FF 00 75 08 95 02
 B1 02 C0
001:067:001:DESCRIPTOR         1615651625.436655
 05 01 09 02 A1 01 85 02 09 01 A1 00 06 00 FF 09
 00 15 00 26 FF 00 75 08 95 09 81 02 05 01 09 30
 09 31 15 00 26 FF 7F 75 10 95 02 81 02 05 09 19
 01 29 08 15 00 25 01 95 08 75 01 81 02 C0 C0
001:067:000:DESCRIPTOR         1615651625.437001
 06 00 FF 09 00 A1 01 09 00 85 01 A1 00 09 00 15
 00 26 FF 00 35 00 46 FF 00 75 08 95 3F 81 02 C0
 09 02 85 FE A1 00 09 06 15 00 26 FF 00 35 00 46
 FF 00 75 08 95 3F B1 02 C0 C0

我使用lsusb命令来确定总线和设备编号,并将其传递给usbhid-dump命令。你可以在没有这些数字的情况下使用dump命令,但你会得到所有设备的列表。在大多数情况下可能不是你想要的。

其中的代码由内核中的hid_parser()命令进行解释。只有某些设备可能无法正确识别,此时您需要这些信息。

USB联盟定义了如何解释这些数据。目前,它是1.11版本,可以在这个页面上找到隐藏类型的设备。

这主要包括键盘、鼠标、触摸屏、触摸板、操纵杆,尽管其他一些类型的设备也支持这一点(电源按钮、显示器上的各种按钮等)

最新更新