输入子系统代码

来源:互联网 发布:mt4软件购买 编辑:程序博客网 时间:2024/05/08 20:03

参考韦东山老师的代码:

/****************************************************************filename: 8th.c*description:开发板按下按键S2 S3 S5 ,在cat /dev/tty1中显示字母ls*exec 0</dev/tty1 按下S2 立马显示l,S3立马显示s, S2 S3 S5显示该目录下的文件*author:    xyc*create time:2014/6/18*version:  1*modify info:****************************************************************/#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/irq.h>#include <linux/input.h>#include <linux/interrupt.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/io.h>#include <asm/arch/regs-gpio.h>#include <asm/hardware.h>#include <linux/poll.h>static struct input_dev *buttons_dev;static struct pin_desc *irq_pd =NULL;struct pin_desc{unsigned int irq;char *name;unsigned int pin;unsigned int key_val;};struct pin_desc pins_desc[3] = {{IRQ_EINT0,  "S2", S3C2410_GPF0, KEY_L},{IRQ_EINT2,  "S3", S3C2410_GPF2, KEY_S},{IRQ_EINT19,"S5", S3C2410_GPG11, KEY_ENTER},};static struct timer_list buttons_timer;/*第一步:定义一个定时器结构体buttons_timer*/static irqreturn_t buttons_irq(int irq, void *dev_id){/*保存键值*/irq_pd = (struct pin_desc *)dev_id;/*第三步:修改定时器,消抖动,最后一次时调用超时函数  *buttons_timer_function *修改定时器10ms,按键有抖动,而抖动时间肯定小于10ms,所以 *第一次抖动修改定时器,定时器延后抖动发生 的10ms *第N-1次抖动修改定时器,定时器延后抖动发生 的10ms *最后一次(第N次)抖动发生,定时器修改到抖动发生后10ms *N次抖动后10ms,即N次的定时器超时时间10ms到后,调用定时器 *处理函数buttons_timer_function */mod_timer(&buttons_timer, jiffies+HZ/100);return IRQ_RETVAL(IRQ_HANDLED);}static void buttons_timer_function(unsigned date){struct pin_desc * pindesc = irq_pd;unsigned int pinval;/*add_timer添加时就产生了定时器超时,所以需要判断*/if(!pindesc) return;pinval = s3c2410_gpio_getpin(pindesc->pin);if (pinval){/*input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) *value =1时松开时,ls显示在串口中,前提exec 0</dev/tty1 *value的值松开时为0,按下时为1也可以,为2表示重复类事件 *value=0时,按下,ls字母显示在串口中, *所以如果你需要按下时就显示字母,就设置value =1 *//* 松开 */input_event(buttons_dev, EV_KEY, pindesc->key_val, 0);input_sync(buttons_dev);}else{/* 按下 */input_event(buttons_dev, EV_KEY, pindesc->key_val, 1);input_sync(buttons_dev);}}static int buttons_init(void){/* 1. 分配一个intput_dev结构体 */int error=0, i;for( i=0; i<3; i++){if( request_irq( (&pins_desc[i]) ->irq, buttons_irq, IRQT_BOTHEDGE, (&pins_desc[i]) ->name, &pins_desc[i])){printk(KERN_ERR"button.c: Can't allocate irq %d\n",  pins_desc[i] .irq);return -EBUSY;}}buttons_dev = input_allocate_device();if (!buttons_dev)goto err_free_irq;/* 2. 设置 *//* 2.1. 设置为按键类事件 */set_bit(EV_KEY, buttons_dev->evbit);set_bit(EV_REP,   buttons_dev->evbit);/* 2.2. 产生按键KEY_L, KEY_S, KEY_ENTER */set_bit(KEY_L,    buttons_dev->keybit);set_bit(KEY_S,    buttons_dev->keybit);set_bit(KEY_ENTER, buttons_dev->keybit);/* 3. 注册input_dev结构体 */error = input_register_device(buttons_dev);if (error) {printk(KERN_ERR "Unable to register gpio-keys input device\n");goto err_free_dev;}/* 4. 硬件相关操作 */init_timer(&buttons_timer);/*初始化定时器结构体成员function,function为定时器超时后调用的函数buttons_timer_function*/buttons_timer.function = buttons_timer_function;/*注册定时器并运行*/add_timer(&buttons_timer);return 0;err_free_dev:input_free_device(buttons_dev);err_free_irq:for( i=0; i<3; i++){free_irq((&pins_desc[i])->irq,  &pins_desc[i]);}return error;}static void buttons_exit(void){int i;for( i=0; i<3; i++){free_irq((&pins_desc[i])->irq,  &pins_desc[i]);}del_timer(&buttons_timer);input_unregister_device(buttons_dev);input_free_device(buttons_dev);}module_init(buttons_init);module_exit(buttons_exit);MODULE_LICENSE("GPL");

input_event(buttons_dev, EV_KEY, pindesc->key_val, 0);

相当于:

input_report_key(buttons_dev, pindesc->key_val, 0);

测试:
1. 
hexdump /dev/event1  (open(/dev/event1), read(), )
           秒        微秒    类  code    value
0000000 0bb2 0000 0e48 000c 0001 0026 0001 0000
0000010 0bb2 0000 0e54 000c 0000 0000 0000 0000
0000020 0bb2 0000 5815 000e 0001 0026 0000 0000
0000030 0bb2 0000 581f 000e 0000 0000 0000 0000


2. 如果没有启动QT:
//author : xyc 此驱动的输入设备是/dev/tty1, /dev/tty1本来就只是输入设备
cat /dev/tty1
按:s2,s3,s4
就可以得到ls


或者:
exec 0</dev/tty1
串口可以接收开发板按键ls+enter
然后可以使用按键来输入


0 0
原创粉丝点击