/* * ===================================================================================== * * Filename: mybuttons.c * * Description: Tiny6440 Buttons Poll Test Program * * Version: 1.0 * Created: 05/09/2011 02:39:08 PM * Revision: none * Compiler: gcc * * Author: Bertrand Huang (cn), weigan.huang@gmail.com * Company: SZU TI DSPs Lab * * ===================================================================================== */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/poll.h> #define DEV_NAME "/dev/buttons" #define BUTTON_NUM 8 int main(int argc, char **argv) { int fd; struct pollfd event; char pre_value[BUTTON_NUM] = {'0','0','0','0','0','0','0','0'}; char cur_value[BUTTON_NUM]; int status; int i; fd = open(DEV_NAME, O_RDWR | O_NONBLOCK); if(fd < 0) { perror("open device buttons"); exit(1); } while(1) { memset(&event, 0, sizeof(event)); event.fd = fd; event.events = POLLIN ;//| POLLRDNORM; status = poll(&event, 1, 5000); if(status < 0) { perror("poll error!/n"); exit(1); } if(status == 0) { perror("Time out!/n"); continue; } if(event.revents & status) { if(read(fd, cur_value, BUTTON_NUM) != BUTTON_NUM) { perror("read error!/n"); exit(1); } for(i = 0; i < BUTTON_NUM; i++) { if(pre_value[i] != cur_value[i]) { pre_value[i] = cur_value[i]; printf("KEY %d==>%s/n",i+1, pre_value[i]=='0'?"UP":"DOWN"); } } } } return 0; }