全志_input
全志_input
平台:全志A40I
源码:Android 7.1 Linux 3.10
input_drv.c
1 #include <linux/input.h> 2 #include <linux/module.h> 3 #include <linux/init.h> 4 #include <linux/interrupt.h> 5 #include <linux/gpio.h> 6 #include <linux/sys_config.h> 7 #include <linux/of_gpio.h> 8 #include <linux/fs.h> 9 #include <linux/device.h> 10 #include <linux/slab.h> 11 #include <linux/cdev.h> 12 #include <asm/io.h> 13 #include <asm/string.h> 14 #include <asm/uaccess.h> 15 #include <asm-generic/ioctl.h> 16 #include <linux/kernel.h> 17 #include <linux/platform_device.h> 18 #include <linux/list.h> 19 #include <linux/spinlock.h> 20 #include <linux/rwsem.h> 21 #include <linux/timer.h> 22 #include <linux/err.h> 23 #include <linux/ctype.h> 24 #include <linux/sysfs.h> 25 #include <linux/of.h> 26 #include <linux/miscdevice.h> 27 #include <linux/delay.h> 28 29 #define IR_GPIO GPIOH(10) //外部中断引脚 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---------------------\n",__FUNCTION__); 38 39 //获取按键的数据 40 value = gpio_get_value(IR_GPIO); 41 if(value){ 42 //松开 43 //将数据上报给用户 44 //参数1 - 哪个设备上报数据 45 //参数2 - 上报哪个类型的数据 46 //参数3 - 上报的键值 47 //参数4 - 按键的状态 48 //input_report_key(btn_input, KEY_DOWN, 0); 49 input_event(btn_input, EV_KEY, KEY_DOWN, 0); 50 input_sync(btn_input); //每次上报数据,必须要调用同步/结束 51 }else{ 52 //按下 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---------------------\n",__FUNCTION__); 64 65 // 1,分配一个 input device 对象的空间 66 btn_input = input_allocate_device(); 67 if (btn_input == NULL) { 68 printk(KERN_ERR "input_allocate_device error\n"); 69 return -ENOMEM; 70 } 71 72 // 2,初始化 input device 对象 73 //当前设备能够产生哪种数据类型 ------EV_KEY表示产生按键数据 74 btn_input->evbit[0] = BIT_MASK(EV_KEY); 75 76 //能够产生哪个按键 -----比如:能够产生下键:KEYD_DOWN,KEY_ESC 77 btn_input->keybit[BIT_WORD(KEY_DOWN)] = BIT_MASK(KEY_DOWN); 78 79 //3,注册 input device 对象 80 ret = input_register_device(btn_input); 81 if (ret != 0) { 82 printk(KERN_ERR "input_register_device error!\n"); 83 goto err_free_input; 84 } 85 86 //4,硬件的初始化 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!\n"); 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 <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/types.h> 5 #include <sys/stat.h> 6 #include <fcntl.h> 7 #include <linux/input.h> 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 !\n"); 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!\n"); 33 }else{ 34 //松开 35 printf("app---->KEY_DOWN up!\n"); 36 } 37 } 38 } 39 } 40 return 0; 41 }
Makefile:
测试:
success!
Stay hungry, stay foolish
待续。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!