button_test.c

来自「我自己编的基于arm9的矩阵键盘驱动」· C语言 代码 · 共 55 行

C
55
字号
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/ioctl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <sys/select.h> 
#include <sys/time.h> 
#include <errno.h> 

int main(void) 
{ 
	int buttons_fd,i; 
	int key_value; 
	buttons_fd = open("/dev/matrix_button", 0); 
	if (buttons_fd < 0) { 
	perror("open device buttons"); 
	exit(1); 
	} 
	for (;;) 
	{ 
		fd_set rds; 
		int ret; 
		FD_ZERO(&rds); 
		FD_SET(buttons_fd, &rds); 
		ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL); 
		if (ret < 0) 
		{ 
			perror("select"); 
			exit(1); 
		} 
		if (ret == 0) 
		{ 
			printf("Timeout.\n"); 
		} 
		else if (FD_ISSET(buttons_fd, &rds)) 
		{ 
			int ret = read(buttons_fd, &key_value, sizeof key_value); 
			if (ret != sizeof key_value) 
			{ 
				if (errno != EAGAIN) 
					perror("read buttons\n"); 
				continue; 
			} 
			else 
			{ 
				printf("buttons_value: %d\n", key_value); 
			} 
		} 
	} 
	close(buttons_fd); 
	return 0; 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?