案例五-6818开发板(点击、上滑、下滑、左滑、右滑)操作

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值