poll机制

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

1.代码

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

input_subsys_test.c

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include 
 8 #include 
 9 #include 
10 
11 #include 
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\n", 
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!\n");
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\n", fd);
65     
66     if (fd < 0)
67     {
68         printf("can't open!\n");
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!\n");
83         if (ret == 0)
84         {
85             printf("time out\n");
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

 

poll机制_第1张图片

 

你可能感兴趣的:(poll机制)