button按键测试

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

#define DEV_PATH0 "/dev/input/event0"   //button1 button2对应的是event0
#define DEV_PATH3 "/dev/input/event3"   //button3 button4对应的是event3


int main(int argc, char **argv)
{
    int i = 0;
    int but1 = 0;
    int but2 = 0;
    int but3 = 0;
    int but4 = 0;
    int times = 5; //缺省超时时间
    int ret = 0;
    char buffer[128] = {0};
    int keys_fd = 0;
    struct input_event t;

    if (argc == 2) {
        times = (int)strtoul(argv[1], NULL, 10);  //传入的超时时间
    }

    keys_fd=open(DEV_PATH0, O_RDONLY | O_NDELAY);   //非阻塞的方式打开
    if(keys_fd <= 0)
    {
        printf("open /dev/input/event0 device error!\n");
        return -1;
    }

    for(i=0;i<times*10;i++)
    {

        if ((but1 == 1) && (but2 == 1)) {
            break;
        }

        if(read(keys_fd, &t, sizeof(t)) == sizeof(t))
        {
            if(t.type==EV_KEY)
                if(t.value == 1)
                {
                    if (t.code == 466) {//button1对应的键值
                        but1 = 1;
                    } else if (t.code == 218) {//button2对应的键值
                        but2 = 1;
                    }
                }
        }
        usleep(100*1000);
    }
    close(keys_fd);

    keys_fd=open(DEV_PATH3, O_RDONLY | O_NDELAY);  //非阻塞方式打开
    if(keys_fd <= 0)
    {
        printf("open /dev/input/event3 device error!\n");
        return -1;
    }

    for(i=0;i<times*10;i++)
    {

        if ((but3 == 1) && (but4 == 1)) {
            break;
        }

        if(read(keys_fd, &t, sizeof(t)) == sizeof(t))
        {
            if(t.type==EV_KEY)
                if(t.value == 1)
                {
                    if (t.code == 114) {//button3对应的键值
                        but3 = 1;
                    } else if (t.code == 115) {//button4对应的键值
                        but4 = 1;
                    }
                }
        }
        usleep(100*1000);
    }
    close(keys_fd);

    ret = but4*8 + but3*4 + but2*2 + but1*1;
    if (ret == 15) {
        printf("test button success!\n");
    } else {
        printf("test button failed!\n");
    }

    return ret;
}
对应的硬件有四个按键需要测试,且按键12对应的是event0, 按键34对应的是event3.

猜你喜欢

转载自blog.csdn.net/csdn66_2016/article/details/79762378