加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Linux poll机制介绍

发布时间:2021-11-18 14:46:57 所属栏目:教程 来源:互联网
导读:1.代码 只简单修改input_subsys_test.c, input_subsys_drv.c不变 input_subsys_test.c #include sys/types.h #include sys/stat.h #include fcntl.h #include stdio.h #include poll.h #include signal.h #include sys/types.h #include unistd.h #include

1.代码
 
只简单修改input_subsys_test.c, input_subsys_drv.c不变
 
input_subsys_test.c
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
 
#include <linux/input.h>
 
 
 
int fd;
 
void my_signal_fun(int signum)
{
    struct input_event buttons_event, leds_event;
 
    /* [cgw]: 异步通知产生时返回的数据 */
    read(fd, &buttons_event, sizeof(struct input_event));
 
    /* [cgw]: 打印事件类型,事件码,事件值 */
    printf("type: 0x%x code: 0x%x value: 0x%xn",
          buttons_event.type,
          buttons_event.code,
          buttons_event.value);
 
    /* [cgw]: 返回的是KEY_L或KEY_S值 */
    if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {
        /* [cgw]: 按键弹起 */
        if (buttons_event.value == 0) {
 
            /* [cgw]: 构造一个EV_LED事件 */
           
            //leds_event.type = EV_SND;
            leds_event.type = EV_LED;
            //leds_event.code = SND_BELL;
            leds_event.code = LED_MUTE;
 
            /* [cgw]: KEY_L和KEY_S控制LED的亮灭 */
            if (buttons_event.code == KEY_L) {
                leds_event.value = 0xAA;
            } else if (buttons_event.code == KEY_S) {
                leds_event.value = 0xEE;   
            }
 
            /* [cgw]: 发送LED控制事件 */
            write(fd, &leds_event, sizeof(struct input_event));
           
            printf("led write!n");
        }
    }
}
 
int main(int argc, char **argv)
{
    int ret, arg;
    struct pollfd fds[1];
   
    fd = open("/dev/event1", O_RDWR | O_NONBLOCK);
   
    //printf("fd = 0x%xn", fd);
   
    if (fd < 0)
    {
        printf("can't open!n");
    }
 
    /* [cgw]: 设置文件标识符 */
    fds[0].fd    = fd;
    /* [cgw]: 设置应用程序要响应的事件 */
    fds[0].events = POLLIN;
 
    while (1)
    {
        /* [cgw]: 休眠5S */
        ret = poll(fds, 1, 5000);
       
        /* [cgw]: 唤醒或超时 */
        printf("wake up!n");
        if (ret == 0)
        {
            printf("time outn");
        }
        else
        {
            my_signal_fun(arg);
        }
    }
   
    return 0;
}
 
2. 实验
 
2.1
 
安装驱动程序:
 
insmod input_subsys_drv.ko
 
1 # insmod input_subsys_drv.ko
2 input: input_subsys_dev as /class/input/input1
3 input subsys open!
4 input subsys init!
运行应用程序
 
./input_subsys_test
 
# ./input_subsys_test
wake up!
type: 0x1 code: 0x26 value: 0x1
wake up!
type: 0x1 code: 0x26 value: 0x0
led event!
value: 0xaa
led write!
wake up!
type: 0x11 code: 0x7 value: 0xaa
wake up!
type: 0x1 code: 0x1f value: 0x1
wake up!
type: 0x1 code: 0x1f value: 0x0
led event!
value: 0xee
led write!
wake up!
type: 0x11 code: 0x7 value: 0xee
wake up!
type: 0x1 code: 0x1c value: 0x1
wake up!
type: 0x1 code: 0x1c value: 0x0
wake up!
time out
wake up!
time out
 
3. 现象分析
 
按一下按键KEY_L,终端输出:
 
wake up!
type: 0x1 code: 0x26 value: 0x1
wake up!
type: 0x1 code: 0x26 value: 0x0
led event!
value: 0xaa
led write!
wake up!
type: 0x11 code: 0x7 value: 0xaa
 
 

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读