GEC6818开发板——触摸屏

本文介绍了Linux下如何读取触摸屏设备文件,解析`input_event`结构体,获取触摸屏的x轴和y轴坐标,并讨论了触摸屏分辨率与LCD分辨率的转换。同时讲解了事件类型`EV_ABS`、编码`ABS_X`和`ABS_Y`的含义,以及如何处理按键按下的事件。最后给出了触摸屏滑动原理和应用实例。

一、触摸屏(touch screen =TS)
1.分类
电阻触摸屏:压力感应 x+ x- y+ y-
电容触摸屏:电压感应 vcc gnd int(中断) rst(复位) scl(i2c的时钟) sda(i2c的数据)

2.触摸屏的文件路径名
/dev/input/event0

 

3.触摸屏有哪些信息
1)需要读取触摸屏的设备文件信息
2)设备文件信息包含3个信息 type\code\value

4.设备文件信息
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/05/code/zuoye$ vi /usr/include/linux/input.h 
 23 struct input_event {
 24     struct timeval time;
 25     __u16 type;
 26     __u16 code;
 27     __s32 value;
 28 };

说明(头文件的使用方法):
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/06/code$ ls /usr/include/stdio.h
/usr/include/stdio.h    ----->#include <stdio.h>
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/06/code$ ls /usr/include/linux/input.h 
/usr/include/linux/input.h   ----->#include <linux/input.h>

5.怎样读取触摸屏的设备文件
struct input_event ts;
read(fd,&ts,sizeof(struct input_event));
练习1:
通过指针传参来取值---指针的高级用法1
int get_value(int *x,int *y)
{
    *x = 100;
    *y = 250;    
    return 0;//不用通过返回值就可以从子函数中取值
}
int main()
{
    int num1=0,num2=0; //注意这里不要定义成int *num1与int *num2
    get_value(&num1,&num2);
    printf("num1=%d num2=%d\n",num1,num2);
    return 0;
}

6.分析触摸屏读取的type\code\value的值
练习2:(代码要在开发板上面执行)
1)读取一遍 type\code\value的值是什么?

	//打开触摸屏
	int fd;
	fd = open("/dev/input/event0",O_RDWR);//要用系统IO open函数打开硬件设备文件
	if(fd < 0)
	{
		perror("open ts fail");
		return 0;
	}
	
	//定义一个存储触摸屏信息的结构体,将读取的设备文件信息存储在ts中
	struct input_event ts;
	
	while(1)
	{	
		//读触摸屏信息--阻塞函数(点击触摸屏之后才会往下执行)
		read(fd,&ts,sizeof(struct input_event));
		if(ts.type == EV_ABS &&ts.code == ABS_X)
		{
			//printf("x=%d ",(int)(ts.value*0.78));  //黑色板x轴的坐标值
			printf("x=%d ",ts.value*800/1024);  //黑色板x轴的坐标值
			//printf("x=%d ",ts.value);  //蓝色板x轴的坐标值
		}
		if(ts.type == EV_ABS &&ts.code == ABS_Y)
		{
			//printf("y=%d\n",(int)(ts.value*0.8)); //黑色板y轴的坐标值
			printf("y=%d\n",ts.value*480/600); //黑色板y轴的坐标值
			//printf("y=%d\n",ts.value); //蓝色板y轴的坐标值
		}
	}	

	//关闭触摸屏
	close(fd);


2)死循环读取type\code\value的值是什么,观察规律?
ts.type=3
ts.code=0
ts.value=493  //触摸屏x轴的坐标
ts.type=3
ts.code=1
ts.value=286 //触摸屏y轴的坐标
ts.type=1
ts.code=330
ts.value=1    //按键按下去
ts.type=1
ts.code=330
ts.value=0     //按键松手后

说明:
黑色的板触摸屏分辨率:1024*460;如果是这种,它的分辨率和LCD不一致,需要软件转换
蓝色的板触摸屏分辨率:800*480;如果是这种,它的分辨率和LCD一致,不需要软件转换

7.如何获取触摸屏里面的x轴和y轴的值
//读触摸屏信息--阻塞函数(点击触摸屏之后才会往下执行)
read(fd,&ts,sizeof(struct input_event));
if(ts.type == 3 &&ts.code == 0)
    printf("x=%d ",ts.value);  //x轴的坐标值
if(ts.type == 3 &&ts.code == 1)
    printf("y=%d\n",ts.value); //y轴的坐标值
练习3:用代码分别获取触摸屏5个点的坐标(4个边角点和1个中心点)

8.type、code、value的含义
type:输入事件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值