|
我测试2440板子的按键测试程序:
我发现一个很奇怪的现象,在红色的printf语句中,一定要打印含有\n的语句,如果没有,比如说printf(”aaa“);或者删除此打印语句。程序就无法正确显示开发板的按键信息(运行程序完全无法显示信息,按键也没作用)。不知道为什么?
还有一个问题,大家看printf这个语句写在这个位置,在每次for循环时都会打印\n,实际上运行时只有在按键时
才会打印一次,不知道为什么会有这种情况?
求各位大神赐教!!!
#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 i;
int buttons_fd;
char key_value[4];
char first = 1;
/*打开键盘设备文件*/
buttons_fd = open("/dev/IRQ-Test", 0);
if (buttons_fd < 0)
{
perror("open device buttons");
exit(1);
}
for (;;)
{
char key_value_temp[4];
int ret;
/*开始读取键盘驱动发出的数据,注意key_value和键盘驱动中定义为一致的类型*/
ret = read(buttons_fd, key_value_temp, sizeof(key_value_temp));
if (ret != sizeof(key_value_temp))
{
perror("read buttons:");
exit(1);
}
/*打印键值*/
for (i = 0; i < sizeof(key_value); i ++)
{
if(key_value != key_value_temp)
{
key_value = key_value_temp;
printf("K%d is %s%s", i+1, key_value == '0' ? "up" : "down", first ? ", " : "");
}
}
first = 0;
if(first == 0)
printf("\n");
}
/*关闭设备文件句柄*/
close(buttons_fd);
return 0;
}
|
|