linux输入子系统(input subsystem)之按键输入和LED控制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux输入子系统(input subsystem)之按键输入和LED控制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8086字,纯文字阅读大概需要12分钟。
内容图文
实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭。
1.代码
input_subsys_drv.c
1 #include <linux/module.h> 2 #include <linux/version.h> 3 4 #include <linux/init.h> 5 #include <linux/fs.h> 6 #include <linux/interrupt.h> 7 #include <linux/irq.h> 8 #include <linux/sched.h> 9 #include <linux/pm.h> 10 #include <linux/sysctl.h> 11 #include <linux/proc_fs.h> 12 #include <linux/delay.h> 13 #include <linux/platform_device.h> 14 #include <linux/input.h> 15 #include <linux/irq.h> 16 17 #include <asm/gpio.h> 18 #include <asm/io.h> 19 #include <asm/arch/regs-gpio.h> 20 21 22struct pin_desc{ 23int irq; 24char *name; 25 unsigned int pin; 26 unsigned int key_val; 27}; 28 29struct pin_desc pins_desc[4] = { 30 {IRQ_EINT0, "S2", S3C2410_GPF0, KEY_L}, 31 {IRQ_EINT2, "S3", S3C2410_GPF2, KEY_S}, 32 {IRQ_EINT11, "S4", S3C2410_GPG3, KEY_ENTER}, 33 {IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT}, 34}; 35 36staticstruct input_dev *input_subsys_dev; 37staticstruct pin_desc *irq_pd; 38staticstruct timer_list buttons_timer; 39 40static irqreturn_t buttons_irq(int irq, void *dev_id) 41{ 42/* [cgw]: 按键IO发生边沿中断时重新设置定时间隔 43 * 用于按键消抖 44*/ 45 irq_pd = (struct pin_desc *)dev_id; 46 buttons_timer.data = irq_pd->pin; 47 mod_timer(&buttons_timer, jiffies+HZ/100); 48return IRQ_RETVAL(IRQ_HANDLED); 49} 50 51staticvoid buttons_timer_function(unsigned long data) 52{ 53struct pin_desc * pindesc = irq_pd; 54 unsigned int pinval; 55 56if (!pindesc) 57return; 58 59/* [cgw]: 获取按键IO状态 */ 60 pinval = s3c2410_gpio_getpin((unsigned int)data); 61 62/* [cgw]: 根据按键IO状态上报按键事件 */ 63if (pinval) 64 { 65/* [cgw]: 上报按键弹起 */ 66 input_report_key(input_subsys_dev, pindesc->key_val, 0); 67//input_sync(input_subsys_dev); 68 } 69else 70 { 71/* [cgw]: 上报按键按下 */ 72 input_report_key(input_subsys_dev, pindesc->key_val, 1); 73//input_sync(input_subsys_dev); 74 } 75 76//printk("timer occur!\n"); 77} 78 79 80staticint led_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) 81{ 82 printk("led event!\n"); 83 printk("value: 0x%x\n", value); 84 85/* [cgw]: 根据应用程序下发的LED控制事件 86 * 亮灭LED 87*/ 88//if (code == SND_BELL) { 89if (code == LED_MUTE) { 90if (value == 0xAA) { 91/* [cgw]: 点亮 */ 92 s3c2410_gpio_setpin(S3C2410_GPF4, 0); 93 } elseif (value == 0xEE) { 94/* [cgw]: 熄灭 */ 95 s3c2410_gpio_setpin(S3C2410_GPF4, 1); 96 } 97 98return0; 99 } 100101return -1; 102} 103104int input_subsys_open(struct input_dev *dev) 105{ 106int i, retval; 107108/* [cgw]: 设置按键IO为中断输入 */109 s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0); 110 s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2); 111 s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11); 112 s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19); 113114/* [cgw]: 设置LED IO为输出,初始为熄灭LED */115 s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP); 116 s3c2410_gpio_setpin(S3C2410_GPF4, 1); 117118 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP); 119 s3c2410_gpio_setpin(S3C2410_GPF5, 1); 120121 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP); 122 s3c2410_gpio_setpin(S3C2410_GPF5, 1); 123124/* [cgw]: 为按键IO分配中断线 */125for (i = 0; i < 4; i++) 126 { 127 retval = request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]); 128 } 129130 printk("input subsys open!\n"); 131132return0; 133} 134135136137staticint input_subsys_init(void) 138{ 139/* [cgw]: 分配一个输入设备 */140 input_subsys_dev = input_allocate_device(); 141 input_subsys_dev->name = "input_subsys_dev"; 142143/* [cgw]: 设置支持的事件类型 */144 set_bit(EV_KEY, input_subsys_dev->evbit); 145//set_bit(EV_REP, input_subsys_dev->evbit);146147 set_bit(EV_LED, input_subsys_dev->evbit); 148//set_bit(EV_SND, input_subsys_dev->evbit);149150/* [cgw]: 设置支持的事件码 */151 set_bit(KEY_L, input_subsys_dev->keybit); 152 set_bit(KEY_S, input_subsys_dev->keybit); 153 set_bit(KEY_ENTER, input_subsys_dev->keybit); 154 set_bit(KEY_LEFTSHIFT, input_subsys_dev->keybit); 155156 set_bit(LED_MUTE, input_subsys_dev->ledbit); 157//set_bit(SND_BELL, input_subsys_dev->sndbit);158159/* [cgw]: 分配输入设备的open方法,用户操作open(/dev/xxx, ...)时调用 */160 input_subsys_dev->open = input_subsys_open; 161/* [cgw]: 分配输入设备的event方法,用户在应用程序write()时 */162 input_subsys_dev->event = led_event; 163164/* [cgw]: 注册输入设备 */165 input_register_device(input_subsys_dev); 166167/* [cgw]: 初始化定时器,用于按键消抖 */168 init_timer(&buttons_timer); 169 buttons_timer.function = buttons_timer_function; 170 add_timer(&buttons_timer); 171172 printk("input subsys init!\n"); 173174return0; 175} 176177staticvoid input_subsys_exit(void) 178{ 179int i; 180181/* [cgw]: 释放按键IO中断 */182for (i = 0; i < 4; i++) 183 { 184 free_irq(pins_desc[i].irq, &pins_desc[i]); 185 } 186187/* [cgw]: 删除定时器 */188 del_timer(&buttons_timer); 189/* [cgw]: 注销输入设备 */190 input_unregister_device(input_subsys_dev); 191/* [cgw]: 释放输入设备内存空间 */192 input_free_device(input_subsys_dev); 193} 194195module_init(input_subsys_init); 196197module_exit(input_subsys_exit); 198199 MODULE_LICENSE("GPL");
input_subsys_test.c
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <stdio.h> 5 #include <poll.h> 6 #include <signal.h> 7 #include <sys/types.h> 8 #include <unistd.h> 9 #include <fcntl.h> 1011 #include <linux/input.h> 12131415int fd; 1617void my_signal_fun(int signum) 18{ 19struct input_event buttons_event, leds_event; 2021/* [cgw]: 异步通知产生时返回的数据 */22 read(fd, &buttons_event, sizeof(struct input_event)); 2324/* [cgw]: 打印事件类型,事件码,事件值 */25 printf("type: 0x%x code: 0x%x value: 0x%x\n", 26 buttons_event.type, 27 buttons_event.code, 28 buttons_event.value); 2930/* [cgw]: 返回的是KEY_L或KEY_S值 */31if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) { 32/* [cgw]: 按键弹起 */33if (buttons_event.value == 0) { 3435/* [cgw]: 构造一个EV_LED事件 */3637//leds_event.type = EV_SND;38 leds_event.type = EV_LED; 39//leds_event.code = SND_BELL;40 leds_event.code = LED_MUTE; 4142/* [cgw]: KEY_L和KEY_S控制LED的亮灭 */43if (buttons_event.code == KEY_L) { 44 leds_event.value = 0xAA; 45 } elseif (buttons_event.code == KEY_S) { 46 leds_event.value = 0xEE; 47 } 4849/* [cgw]: 发送LED控制事件 */50 write(fd, &leds_event, sizeof(struct input_event)); 5152 printf("led write!\n"); 53 } 54 } 55} 5657int main(int argc, char **argv) 58{ 59int Oflags; 6061/* [cgw]: 设置需要处理的信号SIGIO,即输入文件会请求一个SIGIO 62 * 信号,当有新数据到来这个信号会发给filp->f_owner进程 63*/64 signal(SIGIO, my_signal_fun); 6566 fd = open("/dev/event1", O_RDWR | O_NONBLOCK); 6768//printf("fd = 0x%x\n", fd);6970if (fd < 0) 71 { 72 printf("can‘t open!\n"); 73 } 7475/* [cgw]: 根据文件标识符fd,设置能够获得这个文件的进程(owner) 76 * getpid()获得当前进程ID 77*/78 fcntl(fd, F_SETOWN, getpid()); 7980/* [cgw]: 获得file->f_flags标志 */81 Oflags = fcntl(fd, F_GETFL); 8283/* [cgw]: 置位FASYNC使能异步通知 */84 fcntl(fd, F_SETFL, Oflags | FASYNC); 8586while (1) 87 { 88/* [cgw]: 休眠 */89 sleep(1000); 90 } 9192return0; 93 }
makefile
1 KERN_DIR = /work/system/linux-2.6.22.6 2 3all: 4 make -C $(KERN_DIR) M=`pwd` modules 5 6clean: 7 make -C $(KERN_DIR) M=`pwd` modules clean 8 rm -rf modules.order 910 obj-m += input_subsys_drv.o
2. 实验
2.1
安装驱动程序:
insmod input_subsys_drv.ko
1 # insmod input_subsys_drv.ko 2 input: input_subsys_dev as /class/input/input1 3 input subsys open! 4 input subsys init!
运行应用程序
./input_subsys_test
1 # ./input_subsys_test 2 type: 0x1 code: 0x26 value: 0x1 3 type: 0x1 code: 0x26 value: 0x0 4 led event! 5 value: 0xaa 6 led write! 7 type: 0x11 code: 0x7 value: 0xaa 8 type: 0x1 code: 0x1f value: 0x1 9 type: 0x1 code: 0x1f value: 0x010 led event! 11 value: 0xee12 led write! 13 type: 0x11 code: 0x7 value: 0xee
3. 现象分析
按一下按键KEY_L,终端输出:
type: 0x1 code: 0x26 value: 0x1 //按键按下 type: 0x1 code: 0x26 value: 0x0 //按键弹起 led event! //应用程序操作write, 发送LED控制事件,调用led_event() value: 0xaa //读到的LED亮的命令 led write! //返回应用程序继执行 type: 0x11 code: 0x7 value: 0xaa //因为应用程序发送了事件给驱动程序,驱动把这个事件作为一个输入事件(相当于按键输入事件),同样通过异步通知反馈到应用程序
The end!
原文:http://www.cnblogs.com/hackfun/p/5928428.html
内容总结
以上是互联网集市为您收集整理的linux输入子系统(input subsystem)之按键输入和LED控制全部内容,希望文章能够帮你解决linux输入子系统(input subsystem)之按键输入和LED控制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。