#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <stdlib.h>
#include <linux/input.h>
#include <math.h>
// struct input_event {
// struct timeval time;
// __u16 type;
// __u16 code;
// __s32 value;
// };
int ts_fd;
int get_xy(int *x, int *y)
{
struct input_event ie;
int start_x=-1, start_y=-1, end_x=-1, end_y=-1;
while(1)
{
read(ts_fd, &ie, sizeof(struct input_event));
if (ie.type == EV_ABS)
{
if (ABS_X == ie.code)
{
*x = ie.value;//蓝色
// *x = ie.value*800/1024;//黑色
}
if (ABS_Y == ie.code)
{
*y = ie.value;//蓝色
// *y = ie.value*480/600;//黑色
}
}
if (ie.type == EV_KEY && ie.code == BTN_TOUCH)
{
if (ie.value == 0)
{
// printf("松开\n");
end_x = *x;
end_y = *y;
break;
}
else
{
// printf("按下\n");
if (start_x<0 || start_y<0)
{
start_x = *x;
start_y = *y;
}
}
}
}
int tmp_x = start_x - end_x;
int tmp_y = start_y - end_y;
if (10*10 > abs(tmp_x)*abs(tmp_y))
{
// printf("点击\n");
return 0;
}
else if ( (tmp_y>0) && (tmp_y>abs(tmp_x)) ) //上滑
{
// printf("上滑\n");
return 1;
}
else if ( (tmp_y<0) && (abs(tmp_y)>abs(tmp_x)) ) //下滑
{
// printf("下滑\n");
return 2;
}
else if ( (tmp_x>0) && (abs(tmp_y)<tmp_x) ) //左滑
{
// printf("左滑\n");
return 3;
}
else if ( (tmp_x<0) && (abs(tmp_y)<abs(tmp_x)) ) //右滑
{
// printf("右滑\n");
return 4;
}
return -1;
}
int main(int argc, char const *argv[])
{
// 1、打开液晶屏,做内存映射
ts_fd = open("/dev/input/event0", O_RDWR);
if (ts_fd == -1)
{
perror("open ts");
return -1;
}
int x, y;
int ret;
while(1)
{
ret = get_xy(&x, &y);
printf("%d %d\n", x, y);
if (ret==0)
{
printf("点击\n");
}
else if (ret==1)
{
printf("上滑\n");
}
else if (ret==2)
{
printf("下滑\n");
}
else if (ret==3)
{
printf("左滑\n");
}
else if (ret==4)
{
printf("右滑\n");
}
}
return 0;
}
案例五-6818开发板(点击、上滑、下滑、左滑、右滑)操作
最新推荐文章于 2024-09-21 09:22:27 发布