04 触摸屏的使用

测试触摸屏功能

在Secure CRT上输入命令:cat /dev/input/event0
回车,然后点击屏幕,看看是否有反应

触摸屏原理

根据手指按下的位置,产生x、y坐标

触摸屏坐标获取、和其功能程序实现原理

  1. 如何产生x轴、y轴坐标值 在这里插入图片描述
  2. 如何判断手指左右滑动 在这里插入图片描述3. 如何判断手指放大或缩小 在这里插入图片描述
  3. 如何只获取一个点的坐标 在这里插入图片描述
  4. 页面跳转:在相同位置实现不同功能 在这里插入图片描述

输入子系统模型

头文件:#include <linux/input.h>

struct input_event
{
	struct timeval time; //输入事件的时间
	_u16 type;           //输入事件的类型
	_u16 code;           //输入事件的类型的编码
	_s32 value;          //输入事件的类型的编码的值
};

//输入事件的类型:
	EV_KEY //键盘
	EV_REL //鼠标
	EV_ABS //触摸屏
	...
//输入事件的类型的编码:
	ABS_X  //触摸屏的x轴的值
	ABS_Y  //触摸屏的y轴的值
	BTN_TOUCH  //触摸值,按下1,松开0
	...
//输入事件的类型的编码的值:你判断的是什么类型,什么编码,那么它的value值就是多少

示例代码

//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <linux/input.h>

int ts_fd=-1;

void init_ts(void);//打开触摸屏
void gets_pos(int *x,int *y); //获取触摸屏位置

int main(int argc, char const *argv[])
{
	init_ts();
	int x,y;
	while(1) //触摸屏
	{
		gets_pos(&x,&y);
		printf("(%d,%d)\n",x,y);
		if(x>0 && x<400 &&y>0 && y <480)
		{
			printf("左边\n");
		}
		else if(x>400 && x<800 && y>0 && y<480)
		{
			printf("右边\n");
		}
	}
	close(ts_fd);
	return 0;
}
void init_ts(void)
{
	//打开ts,路径:/dev/input/event0
	ts_fd = open("/dev/input/event0",O_RDWR);
	if (ts_fd < 0)
	{
		perror("open ts_fd failed");//打印错误信息
		return;
	}
}
//获取x,y坐标
void gets_pos(int *x,int *y)
{
	//读取触摸屏数据,获取坐标
	struct input_event buf;
	while(1)
	{
		read(ts_fd,&buf,sizeof(buf));
		if(buf.type == EV_ABS) //触摸屏事件
		{
			if(buf.code == ABS_X) //x轴
			{
				*x = buf.value*800/1024;
			}
			if(buf.code == ABS_Y) //Y轴
			{
				*y = buf.value*480/600;
			}
		}
		//判断手指松开
		if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0) break;
	}
}
  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值