Tiny6410 + Linux2.6.38 + input子系统 + 按键中断模拟系统键盘输入的例程

来源:互联网 发布:windows开发ios app 编辑:程序博客网 时间:2024/06/10 16:10

Linux下使用按钮来模拟键盘中“Left","Right","Tab","Space"输入的例程,已经过本人测试,总结在此以供参考。

驱动程序代码:

/***********************************************************************************  Tiny6410 + Linux2.6.38 + 按钮中断驱动 + linux输入子系统 来使用按键模拟键盘输入的例程。 ***********************************************************************************/#include <linux/input.h>#include <linux/module.h>#include <linux/init.h>#include <asm/irq.h>#include <asm/io.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/irq.h>#include <asm/irq.h>#include <linux/interrupt.h>#include <asm/uaccess.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <linux/platform_device.h>#include <linux/cdev.h>#include <linux/miscdevice.h>#include <linux/device.h>#include <linux/gpio.h> #include <plat/gpio-cfg.h>#include <mach/gpio-bank-n.h>#include <mach/gpio-bank-l.h>#include <mach/map.h>static struct input_dev *button_dev; static irqreturn_t buttons_interrupt(int irq, void *dummy);static int button_open(struct input_dev *dev);static void button_close(struct input_dev *dev); struct button_irq_desc {int irq;          int number;         char *name;      };static struct button_irq_desc button_irqs [] = {{IRQ_EINT(11), 0, "KEY0"},    //IRQ_EINT(11)对应GPN11{IRQ_EINT(16), 1, "KEY1"},    //IRQ_EINT(16)对应GPL8{IRQ_EINT(17), 2, "KEY2"},    //IRQ_EINT(17)对应GPL9{IRQ_EINT(18), 3, "KEY3"},    //IRQ_EINT(18)对应GPL10};// 前面的处理方法和按钮中断驱动类似。static int button_open(struct input_dev *dev){int i;int err = 0;set_irq_type(IRQ_EINT(11), IRQ_TYPE_EDGE_BOTH);  set_irq_type(IRQ_EINT(16), IRQ_TYPE_EDGE_BOTH);  set_irq_type(IRQ_EINT(17), IRQ_TYPE_EDGE_BOTH);      set_irq_type(IRQ_EINT(18), IRQ_TYPE_EDGE_BOTH);  for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++){if (button_irqs[i].irq < 0) continue;err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQF_SAMPLE_RANDOM, button_irqs[i].name, (void *)&button_irqs[i]);if (err) break;} if (err) {i--;for (; i >= 0; i--) {if (button_irqs[i].irq < 0) continue;disable_irq(button_irqs[i].irq);free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);}return -EBUSY;} return 0;} static void button_close(struct input_dev *dev){int i; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) continue;disable_irq(button_irqs[i].irq);free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);} } static irqreturn_t buttons_interrupt(int irq, void *dummy){input_report_key(button_dev, KEY_LEFT, !(readl(S3C64XX_GPNDAT)&(1<<11)));input_report_key(button_dev, KEY_RIGHT, !(readl(S3C64XX_GPLDAT)&(1<<8)));input_report_key(button_dev, KEY_MENU, !(readl(S3C64XX_GPLDAT)&(1<<9)));input_report_key(button_dev, KEY_SPACE, !(readl(S3C64XX_GPLDAT)&(1<<10)));    //将对应的事件报告给input子系统。input_sync(button_dev);return IRQ_HANDLED;} static int __init button_init(void){int error;struct input_dev *input_dev;printk("smdk6410 my_keyboard module start/n");  input_dev = input_allocate_device();  if(!input_dev)  {  printk(KERN_ERR "Unable to allocate the input device!!/n");  return -ENOMEM;  }  button_dev = input_dev;  set_bit(EV_KEY, button_dev->evbit);      // 通过set_bit()告诉子系统支持哪些事件。set_bit(KEY_LEFT, button_dev->keybit);set_bit(KEY_RIGHT, button_dev->keybit);set_bit(KEY_MENU, button_dev->keybit);set_bit(KEY_SPACE, button_dev->keybit);button_dev->name = "buttons_tyc";button_dev->dev.init_name = "input_tyc";button_dev->open = button_open;button_dev->close = button_close;printk("input device has allocated/n");error = input_register_device(button_dev);if (error){printk(KERN_ERR "button.c: Failed to register device\n");input_free_device(button_dev);return error;}printk("register device has success\n");return 0; } static void __exit button_exit(void){input_unregister_device(button_dev); } module_init(button_init);module_exit(button_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("modelsim");

测试程序源码:

/***********************************按键中断 + input子系统 测试程序。***********************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/select.h>#include <sys/time.h>#include <errno.h>#include <linux/input.h> int main(void){int fd;int key_value,i=0,count; struct input_event ev_key;fd = open("/dev/input/event1", 666);    //这儿有可能是event0、event2、event3....if (fd < 0) {perror("open device buttons");exit(1);} for (;;) {count = read(fd,&ev_key,sizeof(struct input_event)); for(i=0; i<(int)count/sizeof(struct input_event); i++)if(EV_KEY==ev_key.type)printf("type:%d,code:%d,value:%d\n", ev_key.type,ev_key.code,ev_key.value); if(EV_SYN==ev_key.type)printf("syn event\n\n");} close(fd);return 0;}

在此,感谢原作者的分享!

0 0
原创粉丝点击