zoukankan      html  css  js  c++  java
  • 全志_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---------------------
    ",__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---------------------
    ",__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
    ");
     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!
    ");
     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!
    ");
     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 !
    ");
    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 }

    Makefile:

     

    测试:

    success!

  • 相关阅读:
    超级钢琴 2010年NOI
    vijos P1375 大整数(高精不熟的一定要做!)
    COGS 445. [HAOI2010]最长公共子序列
    系统升级
    mariabd mysql升级mariadb
    mysql view 视图
    mysql 杂
    mysql主从复制
    DNS迭代查询与递归查询的区别
    Python 中 str 和 repr 的区别
  • 原文地址:https://www.cnblogs.com/panda-w/p/11624922.html
Copyright © 2011-2022 走看看