/********************************************************************************* * Copyright: (C) 2024 LingYun IoT System Studio * All rights reserved. * * Filename: key.c * Description: This file is linux GPIO button driver test code. * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if 0 /* Just for comment here, Reference to linux-3.3/include/linux/input.h */ struct input_event { struct timeval time; __u16 type; /* 0x00:EV_SYN 0x01:EV_KEY 0x04:EV_MSC 0x11:EV_LED*/ __u16 code; /* key value, which key */ __s32 value; /* 1: Pressed 0:Not pressed 2:Always Pressed */ }; #endif #define EV_RELEASED 0 #define EV_PRESSED 1 #define EV_REPEAT 2 #define BUTTON_CNT 1 #define MODE_POLL 0x01 #define MODE_NORMAL 0x02 void usage(char *name); void display_button_event(struct input_event *ev, int cnt); static void program_usage(const char *progname) { printf("Usage: %s [OPTION]...\n", progname); printf(" This is linux GPIO button driver test program.\n"); printf(" -d[device ] Specify input device number, such as 0\n"); printf(" -h[help ] Display this help information\n"); printf("\n"); printf("%s program Version v1.0.0\n", progname); printf("Copyright (C) 2023 LingYun IoT System Studio.\n"); return; } int main(int argc, char **argv) { char *progname=NULL; char kbd_dev[128]; char kbd_name[128] = "Unknown"; int kbd_fd = -1; struct input_event ev[BUTTON_CNT]; int size = sizeof (struct input_event); int which = -1; int rv, opt; fd_set rds; struct option long_options[] = { {"device", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; progname = basename(argv[0]); /* Parser the command line parameters */ while ((rv = getopt_long(argc, argv, "d:h", long_options, NULL)) != -1) { switch (rv) { case 'd': /* Set infrared number, such as 0...max */ which = atoi(optarg); break; case 'h': /* Get help information */ program_usage(progname); return 0; default: break; } } if( !which ) { program_usage(progname); return 1; } if ((getuid ()) != 0) printf ("You are not root! This may not work...\n"); snprintf(kbd_dev, sizeof(kbd_dev), "/dev/input/event%d", which); if ((kbd_fd = open(kbd_dev, O_RDONLY)) < 0) { printf("Open %s failure: %s", kbd_dev, strerror(errno)); return -1; } ioctl (kbd_fd, EVIOCGNAME (sizeof (kbd_name)), kbd_name); printf ("Monitor input device %s (%s) event on poll mode:\n", kbd_dev, kbd_name); while (1) { FD_ZERO(&rds); FD_SET(kbd_fd, &rds); rv = select(kbd_fd + 1, &rds, NULL, NULL, NULL); if (rv < 0) { printf("select() system call failure: %s\n", strerror(errno)); goto cleanup; } else if (FD_ISSET(kbd_fd, &rds)) { if ((rv=read(kbd_fd, ev, size*BUTTON_CNT )) < size) { printf("Reading data from kbd_fd failure: %s\n", strerror(errno)); break; } else { display_button_event(ev, rv/size); } } } cleanup: close(kbd_fd); return 0; } void display_button_event(struct input_event *ev, int cnt) { int i; struct timeval pressed_time, duration_time; for(i=0; i