嵌入式linux应用层读取触摸屏坐标简单示例

嵌入式linux应用层读取触摸屏简单示例

linux下面触摸屏读取方法就是读取/dev/input/event*事件

1、检查event编号

hexdump /dev/input/event1

按触摸屏会输出信息的就是触摸屏对应文件
000b9b0 0003 0032 0017 0000 1b59 0000 0000 0000
000b9c0 09f2 000e 0000 0000 0003 0039 0000 0000
000b9d0 1b59 0000 0000 0000 09f2 000e 0000 0000
000b9e0 0000 0002 0000 0000 1b59 0000 0000 0000
2、打开文件

fd = open("/dev/input/event1",O_RDWR);

3、读取文件解析

read(ts_fd,&ts,sizeof(ts));
if(ts.type == EV_ABS && ts.code == ABS_X)
{
    
    
	x = ts.value;
	cnt++;
}
if(ts.type == EV_ABS && ts.code == ABS_Y)
{
    
    
	y = ts.value;
	cnt++;
}

解析说明

在Linux内核的include\linux\input.h定义如下

struct timeval {
__kernel_time_t tv_sec; /* seconds,32bit /
__kernel_suseconds_t tv_usec; /
microseconds,32bit */
};

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

#define EV_SYN 0x00 //同步事件,用于分隔事件
#define EV_KEY 0x01 //按键事件,例如按键、鼠标按键、触摸屏按键
#define EV_REL 0x02 //相对位置事件,常见于鼠标坐标
#define EV_ABS 0x03 //绝对位置事件,常见于触摸屏坐标

#define KEY_1 2
#define KEY_2 3
#define KEY_3 4

#define KEY_A 30
#define KEY_S 31
#define KEY_D 32

当type == EV_KEY,
code表示是哪一个按键发生的事件,
value:0表示按键松开,1表示按键按下

当type == EV_ABS
code表示绝对坐标轴向,当code ==ABS_X时
value表示x坐标轴的值
code表示绝对坐标轴向,当code ==ABS_Y时
value表示Y坐标轴的值

c编程示例
touch.c

#include <stdio.h>
#include <errno.h>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


static int fd = 0;
static struct input_event ts;

int touch_init()
{
    
    
	fd = open("/dev/input/event1", O_RDONLY);
	if(fd == -1)
	{
    
    
		printf("[%s]:[%d] open event file error\r\n", __FUNCTION__, __LINE__);
	}
}

int get_xy(int *x,int *y)
{
    
    
	read(fd,&ts,sizeof(ts));
	if(ts.type == EV_ABS && ts.code == ABS_X)
	{
    
    
		*x = ts.value;
		return 0;
	}
	if(ts.type == EV_ABS && ts.code == ABS_Y)
	{
    
    
		*y = ts.value;
		return 0;
	}
	
	return -1;
}

main.c

#include <stdio.h>

int main()
{
    
    
	int x,y,tmp;
	touch_init();
	while(1)
	{
    
    
		tmp = get_xy(&x, &y);
		if(tmp == 0)
		{
    
    
			printf("get position x:%d,y:%d\r\n",x,y);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/u010835747/article/details/108638660