zoukankan      html  css  js  c++  java
  • 输入子系统编程实验

    使用input输入子系统

    1.分配input_dev

    2.设置

    3.注册

    4.硬件相关操作(有数据产生时,需要调用input_event来上报,给input_handle  最终调用到input_handler 中的event函数)

    input_dev结构体

       unsigned long evbit[NBITS(EV_MAX)];  
        unsigned long keybit[NBITS(KEY_MAX)];
        unsigned long relbit[NBITS(REL_MAX)];
        unsigned long absbit[NBITS(ABS_MAX)];
        unsigned long mscbit[NBITS(MSC_MAX)];
        unsigned long ledbit[NBITS(LED_MAX)];
        unsigned long sndbit[NBITS(SND_MAX)];
        unsigned long ffbit[NBITS(FF_MAX)];
        unsigned long swbit[NBITS(SW_MAX)];

    入口函数:

    static int buttons_init(void)
    {
        int i;
        /*1.分配一个input_dev结构体*/
        buttons_dev = input_allocate_device();//可以通过判断返回值查看是否分配成功
    
        /*2.配置*/
        /*2.1能够产生那一类事件*/
        set_bit(EV_KEY, buttons_dev->evbit);
        set_bit(EV_REP, buttons_dev->evbit);//支持重复
        /*2.2能够产生EV_KEY中的哪些事件 L,S,ENTER,LEFTSHIFT*/
        set_bit(KEY_L,buttons_dev->keybit);
        set_bit(KEY_S,buttons_dev->keybit);
        set_bit(KEY_ENTER,buttons_dev->keybit);
        set_bit(KEY_LEFTSHIFT,buttons_dev->keybit);
        /*3.注册*/
        input_register_device(buttons_dev);//调用input_register_device注册buttons_dev
        /*4.硬件操作*/
        /*4.1设置定时器*/
        init_timer(&buttons_timer);//初始化定时器
        buttons_timer.function = buttons_timer_function;//设置事件到了处理函数
        add_timer(&buttons_timer);//添加定时器到内核
        /*4.2注册4个中断*/
        for(i=0;i<4;i++)
            {    /*                中断号               中断处理函数            触发方式            名字                设备ID       */
                request_irq(pins_desc[i].irq,  buttons_irq , IRQT_BOTHEDGE,  pins_desc[i].name, &pins_desc[i]);
            }
        
        
        return 0;
    }

    出口函数:

    /*出口函数*/
    static void buttons_exti(void)
    {
        int i;
        /*释放中断*/
        for(i=0;i<4;i++)
            {
                free_irq(pins_desc[i].irq,&pins_desc[i]);
            }
        del_timer(&buttons_timer);//删除定时器
        input_unregister_device(buttons_dev);//input_unregister_device卸载掉buttons_dev
        input_free_device(buttons_dev);//释放掉分配的空间
    }

    按键中断:

    //中断处理函数
    static irqreturn_t buttons_irq(int irq, void *dev_id)
    {
        irq_pd = (struct pin_desc *)dev_id;
        //修改定时器的超时时间  HZ = 100  jiffies 当前时钟  10ms后进入中断处理函数
        mod_timer(&buttons_timer, jiffies+HZ/100);//jiffies  是一个全局变量  系统每隔10ms就会产生一个系统时钟中断 过100个10ms进入中断  
        return IRQ_RETVAL(IRQ_HANDLED);
    }

    ls -l /dev/event*  查看驱动事件

    hexdump /dev/event1  十六进制显示(open(/dev/event1) read())


    # hexdump /dev/event1

         秒             微秒     type     code     value
    0000000 0052 0000 354a 000c 0001 001f 0001 0000
    0000010 0052 0000 e0df 000e 0001 001f 0000 0000
    0000020 0054 0000 8901 0000 0001 001f 0001 0000
    0000030 0054 0000 0d75 0003 0001 001f 0000 0000

     使用exec命令 替代掉键盘

    exec </dev/tty1  exec 0</dev/tty1

    关于exec 命令参考

    https://www.cnblogs.com/xiaofeiIDO/p/7301327.html

  • 相关阅读:
    Linux日志不记录问题
    Centos下yum安装PHP
    centos yum update kernel
    oh-my-zsh主题
    centos 6.6 使用tomcat6部署solr5.3.1
    Nginx manifest 实现 HTML5 Application Cache
    -bash: /bin/rm: Argument list too long
    linux mysql-5.6.26 安装
    LVM 管理减少swap分区空间增加到根分区
    Linux 使用iftop命令查看服务器流量
  • 原文地址:https://www.cnblogs.com/hjxzjp/p/10562087.html
Copyright © 2011-2022 走看看