<arm开发版>arm开发板触摸屏坐标的获取

1. 头文件

/* head file */
#ifndef __TS_XY__
#define __TS_XY__

struct ts_sample 
{
	int		x;
	int		y;
};

void ts_read(char *device, struct ts_sample *ts_sp);

#endif

2. 获取坐标功能函数

#include <stdio.h>
#include <sys/types.h>//open
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>//close/read
#include <linux/input.h>//struct input_event//EV_ABS...
#include <strings.h>//bzero
#include "ts_xy.h"

void ts_read(char *device, struct ts_sample *ts_sp)
{
	//①open:打开设备文件:/dev/input/event0
	int ts = open(device, O_RDONLY);
	if (ts == -1)
	{
		printf("Open %s error!\n", device);
		return;
	}

	//②read:读取数据
	struct input_event buf;
	bzero(&buf, sizeof(buf));

	int count = 0;

	while(1)
	{
		read(ts, &buf, sizeof(buf));
		if(buf.type == EV_ABS)
		{
			if (buf.code == ABS_X)
			{
				ts_sp->x = buf.value;
				count++;
				// printf("X:%u\t", buf.value);
			}
			if (buf.code == ABS_Y)
			{
				ts_sp->y = buf.value;
				count++;
				// printf("Y:%u\t", buf.value);
			}
			if (count == 2)
			{
				break;
			}
		}
	}

	//③close:关闭设备文件
	close(ts);
}

3. main函数测试

#include <stdio.h>
#include <stdlib.h>//malloc
#include "ts_xy.h"

#define DEVICE "/dev/input/event0"

int main(int argc, char const *argv[])
{
	struct ts_sample *ts_s = malloc(sizeof(struct ts_sample));

	while(1)
	{
		ts_read(DEVICE, ts_s);

		printf("坐标:%d, %d\n", ts_s->x, ts_s->y);

		//function?
		if (ts_s->x < 400)
		{
			printf("按到了左边!\n");
		}
		else if (ts_s->x > 400)
		{
			printf("按到了右边!\n");
		}
		if (ts_s->y < 240)
		{
			printf("按到了上边!\n");
		}
		else if (ts_s->y > 240)
		{
			printf("按到了下边!\n");
		}
	}

	return 0;
}



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页