歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> poll機制,linuxpoll機制

poll機制,linuxpoll機制

日期:2017/3/3 18:10:51   编辑:學習Linux

poll機制,linuxpoll機制

poll機制,linuxpoll機制


使用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\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

http://xxxxxx/Linuxjc/1162666.html TechArticle

Copyright © Linux教程網 All Rights Reserved