linux应用编程--触摸屏多点触摸


 

平台:tiny4412SDK 1161B + HD700

kernel:linux 3.5

touch control:ft5406

  学习HD700的过程中,显示图片后,需要对该屏幕的触摸控制进行了解。阅读HD700原理图可知该款LCD使用的是ft5406型号的控制器。在学习linux驱动过程中,知道linux3.5的内核具有一个ft5X0X的驱动,我们是可以使用的;该驱动是多点触控驱动(5个点)。屏幕的驱动是通过input子系统的框架编程,跟系统当中的键盘、鼠标一样,是通过该子系统的事件进行上报的。所以编程过程中需要先了解input子系统。

准备工作:

  确保自己的内核包含该驱动,并编译进内核;该驱动内核菜单选项路径如下图所示。

终端使用设备:

1)、查找我们的触摸屏设备,在input子系统中,通过配置文件进行查找。在开发板终端中输入cat /proc/bus/input/devices,结果如下:

 2)、从上一步我们能够获知到ft5x0x的驱动设备名是event0(不同的环境可能是其他),在/dev下能够找到该文件。通过cat /dev/event0读取该设备数据,执行完该命令后,我们触及LCD屏幕,能够发现终端有乱码输出,说明该驱动是能够正常工作的。

3)、使用另外的命令hexdump  /dev/event0,能获得input子系统hex格式的数据输出。其中每一行为一条上报信息。

 

 通过对上报信息的分析,发现触击屏幕会上报事件类型为3的绝对事件和为0的同步事件,同步事件只为同步事件使用不需理会。

在绝对事件中,又有码值为0x35、0x36、0x30、0x39四条信息,对应的分别是ABS_MT_POSITION_X(触点x坐标)、ABS_MT_POSITION_Y(触点y坐标)、ABS_MT_TOUCH_MAJOR(触点轴直径,一直为默认为200,不改变)、ABS_MT_TRACKING_ID(多点触控中第几个点,0~4);在编程过程中只要获取对应码值的具体数值就能知道触击的具体坐标以及是第几个点啦。

4)、上报的数据格式:

struct input_event {
    struct timeval time;  //时间戳
    __u16 type;       //事件类型
    __u16 code;       //码值
    __s32 value;      //数值
};

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

#if 0
/*
 * Event types
 */
#define EV_SYN            0x00
#define EV_KEY            0x01
#define EV_REL            0x02
#define EV_ABS            0x03
#define EV_MSC            0x04
#define EV_SW             0x05
#define EV_LED            0x11
#define EV_SND            0x12
#define EV_REP            0x14
#define EV_FF             0x15
#define EV_PWR            0x16
#define EV_FF_STATUS      0x17
#define EV_MAX            0x1f
#define EV_CNT            (EV_MAX+1)

#define ABS_MT_SLOT           0x2f    /* MT slot being modified */
#define ABS_MT_TOUCH_MAJOR    0x30    /* Major axis of touching ellipse */
#define ABS_MT_TOUCH_MINOR    0x31    /* Minor axis (omit if circular) */
#define ABS_MT_WIDTH_MAJOR    0x32    /* Major axis of approaching ellipse */
#define ABS_MT_WIDTH_MINOR    0x33    /* Minor axis (omit if circular) */
#define ABS_MT_ORIENTATION    0x34    /* Ellipse orientation */
#define ABS_MT_POSITION_X     0x35    /* Center X ellipse position */
#define ABS_MT_POSITION_Y     0x36    /* Center Y ellipse position */
#define ABS_MT_TOOL_TYPE      0x37    /* Type of touching device */
#define ABS_MT_BLOB_ID        0x38    /* Group a set of packets as a blob */
#define ABS_MT_TRACKING_ID    0x39    /* Unique ID of initiated contact */
#define ABS_MT_PRESSURE       0x3a    /* Pressure on contact area */
#define ABS_MT_DISTANCE       0x3b    /* Contact hover distance */

struct input_event {
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};
#endif


int main(void)
{    
    struct input_event touchData;
    
    int fd = open("/dev/event0", O_RDWR);
    if (fd < 3) {
        printf("Not device /dev/event0!\n");
        exit(-1);
    }
    while(1) {
        read(fd, &touchData, sizeof(struct input_event));
    /*    printf("------------------------------\n");
        printf("touchData.type = %d\n", touchData.type);
        printf("touchData.code = %d\n", touchData.code);
        printf("touchData.value = %d\n", touchData.value);*/
        if (touchData.type == EV_ABS) {
            switch(touchData.code) {
                case ABS_MT_POSITION_X:
                    printf("x = %d\n", touchData.value);
                break;
                case ABS_MT_POSITION_Y:
                    printf("y = %d\n", touchData.value);
                break;
                case ABS_MT_TOUCH_MAJOR:
                    printf("major = %d\n", touchData.value);
                break;
                case ABS_MT_TRACKING_ID:
                    printf("id = %d\n", touchData.value);
                break;
                default:
                    printf("other input value!\n");
                    break;
            }        
            
        }    
    }
    close(fd);
    return 0;
}                                                                                                                                           

结果:

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM