• poll机制


    使用POLL机制代替linux输入子系统(input subsystem)之按键输入和LED控制中的异步通知,实现同样的效果。

    1.代码

    只简单修改input_subsys_test.c, input_subsys_drv.c不变

    input_subsys_test.c

     1 #include <sys/types.h>
     2 #include <sys/stat.h>
     3 #include <fcntl.h>
     4 #include <stdio.h>
     5 #include <poll.h>
     6 #include <signal.h>
     7 #include <sys/types.h>
     8 #include <unistd.h>
     9 #include <fcntl.h>
    10 
    11 #include <linux/input.h>
    12 
    13 
    14 
    15 int fd;
    16 
    17 void my_signal_fun(int signum)
    18 {
    19     struct input_event buttons_event, leds_event;
    20 
    21     /* [cgw]: 异步通知产生时返回的数据 */
    22     read(fd, &buttons_event, sizeof(struct input_event));
    23 
    24     /* [cgw]: 打印事件类型,事件码,事件值 */
    25     printf("type: 0x%x code: 0x%x value: 0x%x
    ", 
    26            buttons_event.type,
    27            buttons_event.code,  
    28            buttons_event.value);
    29 
    30     /* [cgw]: 返回的是KEY_L或KEY_S值 */
    31     if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {
    32         /* [cgw]: 按键弹起 */ 
    33         if (buttons_event.value == 0) {
    34 
    35             /* [cgw]: 构造一个EV_LED事件 */
    36             
    37             //leds_event.type = EV_SND;
    38             leds_event.type = EV_LED;
    39             //leds_event.code = SND_BELL;
    40             leds_event.code = LED_MUTE;
    41 
    42             /* [cgw]: KEY_L和KEY_S控制LED的亮灭 */
    43             if (buttons_event.code == KEY_L) {
    44                 leds_event.value = 0xAA;
    45             } else if (buttons_event.code == KEY_S) {
    46                 leds_event.value = 0xEE;    
    47             }
    48 
    49             /* [cgw]: 发送LED控制事件 */
    50             write(fd, &leds_event, sizeof(struct input_event));
    51             
    52             printf("led write!
    ");
    53         }
    54     }
    55 }
    56 
    57 int main(int argc, char **argv)
    58 {
    59     int ret, arg;
    60     struct pollfd fds[1];
    61     
    62     fd = open("/dev/event1", O_RDWR | O_NONBLOCK);
    63     
    64     //printf("fd = 0x%x
    ", fd);
    65     
    66     if (fd < 0)
    67     {
    68         printf("can't open!
    ");
    69     }
    70 
    71     /* [cgw]: 设置文件标识符 */
    72     fds[0].fd     = fd;
    73     /* [cgw]: 设置应用程序要响应的事件 */
    74     fds[0].events = POLLIN;
    75 
    76     while (1)
    77     {
    78         /* [cgw]: 休眠5S */
    79         ret = poll(fds, 1, 5000);
    80         
    81         /* [cgw]: 唤醒或超时 */
    82         printf("wake up!
    ");
    83         if (ret == 0)
    84         {
    85             printf("time out
    ");
    86         }
    87         else
    88         {
    89             my_signal_fun(arg);
    90         }
    91     }
    92     
    93     return 0;
    94 }


    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

     1 # ./input_subsys_test 
     2 wake up!
     3 type: 0x1 code: 0x26 value: 0x1
     4 wake up!
     5 type: 0x1 code: 0x26 value: 0x0
     6 led event!
     7 value: 0xaa
     8 led write!
     9 wake up!
    10 type: 0x11 code: 0x7 value: 0xaa
    11 wake up!
    12 type: 0x1 code: 0x1f value: 0x1
    13 wake up!
    14 type: 0x1 code: 0x1f value: 0x0
    15 led event!
    16 value: 0xee
    17 led write!
    18 wake up!
    19 type: 0x11 code: 0x7 value: 0xee
    20 wake up!
    21 type: 0x1 code: 0x1c value: 0x1
    22 wake up!
    23 type: 0x1 code: 0x1c value: 0x0
    24 wake up!
    25 time out
    26 wake up!
    27 time out


    3. 现象分析

    按一下按键KEY_L,终端输出:

    1 wake up!
    2 type: 0x1 code: 0x26 value: 0x1
    3 wake up!
    4 type: 0x1 code: 0x26 value: 0x0
    5 led event!
    6 value: 0xaa
    7 led write!
    8 wake up!
    9 type: 0x11 code: 0x7 value: 0xaa

  • 相关阅读:
    分享ASP.NET+jQuery MiniUI后台购物管理
    ASP.NET发送电子邮件
    LINQ学习(三):Where子句
    ASP.NET配置KindEditor文本编辑器
    一步步写自己SqlHelper类库(五):Command对象
    ASP.NET生成静态页面的简单实现
    兼职开发悟出的点点滴滴
    设计模式学习笔记
    if else替代者
    ASP.NET 一生命周期中的额事件
  • 原文地址:https://www.cnblogs.com/hackfun/p/5928989.html
Copyright © 2020-2023  润新知