在上一篇linux按键中断处理中,我们采用按键中断处理获取按键,在read函数中阻塞读取,当按键发生时,read自动解除阻塞,实现应用层读取到相应的按键值。在上一节中如果没有按键到来,应用层会一直阻塞等待在read 函数中。本博客页尝试使用按键中断加poll和selcet机制的方式这个处理。实现在应用层,阻塞指定的时间,如果没有信息返回,就主动退出阻塞等待。
先贴一段代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
/* forthdrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;
struct pollfd fds[1];
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}
-----------------------------------我是漂亮的分割线-----------------------------------------------------------------------------------------
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)