Full publicity _input

Quanzhi_input


Platform: Quanzhi A40I< /p>

Source code: Android 7.1 Linux 3.10

input_drv.c

 1 #include 

2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include string.h>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #include
25 #include
26 #include
27 #include
28
29 #define IR_GPIO GPIOH(10) //External interrupt pin
30
31 static struct input_dev *btn_input;
32 static int irqno;
33
34 irqreturn_t button_interrupt(int irq, void * dev_id)
35 {
36 int value;
37 printk("--------------^_^ %s--------------------- ",__FUNCTION__);
38
39 //Get key data
40 value = gpio_get_value(IR_GPIO);
41 if(value){
42 //Release
43 //Report data to users
44 //Parameter 1-Which device reports the data
45 //Parameter 2-Which type of data to report
46 //Parameter 3-Key value reported
47 //Parameter 4-State of the button
48 //input_report_key(btn_input, KEY_DOWN, 0);
49 input_event(btn_input, EV_KEY, KEY_DOWN, 0);
50 input_sync(btn_input); //Every time data is reported, synchronization/end must be called
51 }else{
52 //Press
53 input_report_key(btn_input, KEY_DOWN, 1);
54 input_sync(btn_input);
55 }
56 return IRQ_HANDLED;
57 }
58
59
60 static int __init simple_input_btn_init(void)
61 {
62 int ret;
63 printk("--------------^_^ %s--------------------- ",__FUNCTION__);
64
65 // 1. Allocate space for an input device object
66 btn_input = input_allocate_device();
67 if (btn_input == NULL) {
68 printk(KERN_ERR "input_allocate_device error ");
69 return -ENOMEM;
70 }
71
72 // 2. Initialize the input device object
73 //Which type of data the current device can generate------EV_KEY means to generate key data
74 btn_input->evbit[0] = BIT_MASK(EV_KEY);
75
76 //Which key can be generated-----For example: Can generate the next key: KEYD_DOWN,KEY_ESC
77 btn_input->keybit[BIT_WORD(KEY_DOWN)] = BIT_MASK(KEY_DOWN);
78
79 //3, register the input device object
80 ret = input_register_device(btn_input);
81 if (ret != 0) {
82 printk(KERN_ERR "input_register_device error! ");
83 goto err_free_input;
84 }
85
86 //4, the initialization of the hardware
87 irqno = gpio_to_irq(IR_GPIO);
88 ret = request_irq(irqno, button_interrupt, IRQF_TRIGGER_FALLING| IRQF_TRIGGER_RISING," key_input_eint1" , NULL);
89 if(ret != 0){
90 printk(KERN_ERR "request_irq error! ");
91 goto err_unregistger;
92 }
93 return 0;
94
95 err_unregistger:
96 input_unregister_device(btn_input);
97
98 err_free_input:
99 input_free_device(btn_input);
100 return ret;
101 }
102
103
104 static void __exit simple_input_btn_exit(void)
105 {
106 free_irq(irqno, NULL);
107 input_unregister_device(btn_input);
108 input_free_device(btn_input);
109 }
110
111
112 module_init(simple_input_btn_init);
113 module_exit(simple_input_btn_exit);
114 MODULE_LICENSE("GPL");

text. cpp

 1 #include 

2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8
9 int main(void)
10 {
11 int ret;
12 int fd;
13 struct input_event event;
14
15 fd = open("/dev/input/event4",O_RDWR);
16 if(fd <0){
17 perror("open");
18 exit(1);
19 }
20 printf("open success ! ");
21
22 while(1){
23 ret = read(fd,&event, sizeof(event));
24 if(ret <0){
25 perror("read");
26 exit(1);
27 }
28 if(event.type == EV_KEY){
29 if(event.code == KEY_DOWN){
30 if(event.value){
31 //Press
32 printf("app---->KEY_DOWN pressed! ");
33 }else{
34 //Release
35 printf("app---->KEY_DOWN up! ");
36 }
37 }
38 }
39 }
40 return 0;
41 }

Makefile:

Share a picture

Test:

Share pictures

share picture

success!

 1 #include 

2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include string.h>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #include
25 #include
26 #include
27 #include
28
29 #define IR_GPIO GPIOH(10) //External interrupt pin
30
31 static struct input_dev *btn_input;
32 static int irqno;
33
34 irqreturn_t button_interrupt(int irq, void * dev_id)
35 {
36 int value;
37 printk("--------------^_^ %s--------------------- ",__FUNCTION__);
38
39 //Get key data
40 value = gpio_get_value(IR_GPIO);
41 if(value){
42 //Release
43 //Report data to users
44 //Parameter 1-Which device reports the data
45 //Parameter 2-Which type of data to report
46 //Parameter 3-Key value reported
47 //Parameter 4-State of the button
48 //input_report_key(btn_input, KEY_DOWN, 0);
49 input_event(btn_input, EV_KEY, KEY_DOWN, 0);
50 input_sync(btn_input); //Every time data is reported, synchronization/end must be called
51 }else{
52 //Press
53 input_report_key(btn_input, KEY_DOWN, 1);
54 input_sync(btn_input);
55 }
56 return IRQ_HANDLED;
57 }
58
59
60 static int __init simple_input_btn_init(void)
61 {
62 int ret;
63 printk("--------------^_^ %s--------------------- ",__FUNCTION__);
64
65 // 1. Allocate space for an input device object
66 btn_input = input_allocate_device();
67 if (btn_input == NULL) {
68 printk(KERN_ERR "input_allocate_device error ");
69 return -ENOMEM;
70 }
71
72 // 2. Initialize the input device object
73 //Which type of data the current device can generate------EV_KEY means to generate key data
74 btn_input->evbit[0] = BIT_MASK(EV_KEY);
75
76 //Which key can be generated-----For example: Can generate the next key: KEYD_DOWN,KEY_ESC
77 btn_input->keybit[BIT_WORD(KEY_DOWN)] = BIT_MASK(KEY_DOWN);
78
79 //3, register the input device object
80 ret = input_register_device(btn_input);
81 if (ret != 0) {
82 printk(KERN_ERR "input_register_device error! ");
83 goto err_free_input;
84 }
85
86 //4, the initialization of the hardware
87 irqno = gpio_to_irq(IR_GPIO);
88 ret = request_irq(irqno, button_interrupt, IRQF_TRIGGER_FALLING| IRQF_TRIGGER_RISING," key_input_eint1" , NULL);
89 if(ret != 0){
90 printk(KERN_ERR "request_irq error! ");
91 goto err_unregistger;
92 }
93 return 0;
94
95 err_unregistger:
96 input_unregister_device(btn_input);
97
98 err_free_input:
99 input_free_device(btn_input);
100 return ret;
101 }
102
103
104 static void __exit simple_input_btn_exit(void)
105 {
106 free_irq(irqno, NULL);
107 input_unregister_device(btn_input);
108 input_free_device(btn_input);
109 }
110
111
112 module_init(simple_input_btn_init);
113 module_exit(simple_input_btn_exit);
114 MODULE_LICENSE("GPL");

 1 #include 

2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8
9 int main(void)
10 {
11 int ret;
12 int fd;
13 struct input_event event;
14
15 fd = open("/dev/input/event4",O_RDWR);
16 if(fd < 0){
17 perror("open");
18 exit(1);
19 }
20 printf("open success ! ");
21
22 while(1){
23 ret = read(fd,&event,sizeof(event));
24 if(ret < 0){
25 perror("read");
26 exit(1);
27 }
28 if(event.type == EV_KEY){
29 if(event.code == KEY_DOWN){
30 if(event.value){
31 //按下
32 printf("app---->KEY_DOWN pressed! ");
33 }else{
34 //松开
35 printf("app---->KEY_DOWN up! ");
36 }
37 }
38 }
39 }
40 return 0;
41 }

Leave a Comment

Your email address will not be published.