input 子系统①

app代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
    struct input_event event;

	fd = open("/dev/event0",O_RDWR);
	if(fd < 0)
	{
		perror("open");
		exit(1);
	}
	while(1)
	{
       ret = read(fd, &event, sizeof(struct input_event));//read得到一个input_device结构体判断产生的事件
	   if(ret < 0)
	   {
		   perror("read");
		   exit(1);
	   }
	   if(event.type == EV_KEY)//从类型————》到事件--》到确定的值。
	   {
			if(event.code == KEY_DOWN)
			{
				if(event.value)
					printf("app按下---->按下\n");
				else
					printf("app松开---->按下\n");
			}
	   }
	}
	close(fd);
	return 0;
}

input_devices层:

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>

static struct input_dev *button_dev;

static irqreturn_t button_interrupt(int irq, void *dummy)
{
    int value;
	value = gpio_get_value(S5PV210_GPH0(1));//获取gpio值不需要请求和释放
	if(value)
	{//松开
		//void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
		input_event(button_dev, EV_KEY, KEY_DOWN, 0);//包含在input_report_key中
		input_sync(button_dev);//表示上报完毕
	}
	else//按下
	{
		input_report_key(button_dev,KEY_DOWN,1);
		input_sync(button_dev);//表示上报完毕
	}
    return IRQ_HANDLED;//中断处理函数的返回值
}

static int __init button_init(void)
{
    int error;
	//1.申请一个input_device对象的空间
    button_dev = input_allocate_device();
    if (!button_dev) 
	{
        printk(KERN_ERR "input_allocate_device error\n");
        return -ENOMEM;
    }
	//2.初始化input_device对象
	button_dev->name = "s5pv210_input_btn";
	button_dev->phys = "btn/s5pv210";
	button_dev->uniq = "btn/s5pv210_input";
	button_dev->id.bustype = BUS_I2C;
	button_dev->id.product = 0x1122;
	button_dev->id.vendor = 0x1234;
	button_dev->id.version = 0x0001;
	
	//可以产生哪种类型的数据
    button_dev->evbit[0] = BIT_MASK(EV_KEY);//32位每一个位代表一种类型的数据
	//button_dev->evbit[0] |= (1<<1);
	//__set_bit(EV_ABS,button_dev->evbit);
	
	//可以产生这种类型下面的哪个数据
        button_dev->keybit[BIT_WORD(KEY_DOWN)] = BIT_MASK(KEY_DOWN);
	//button_dev->keybit[108/32] |= 1<<(108%32);
	//__set_bit(ABS_X,button_dev->keybit);
	
    //3.注册input_device对象
    error = input_register_device(button_dev);
    if (error)
	{
        printk(KERN_ERR "input_register_device error\n");
        goto err_free_dev;
    }
    //4.硬件初始化--请求中断
    if (request_irq(IRQ_EINT(1), button_interrupt, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "button", NULL)) {
        printk(KERN_ERR "request_irq error\n");
        error= -EBUSY;
		goto err_unregister_dev;
    }
    return 0;
	err_unregister_dev:
		input_unregister_device(button_dev);//取消注册devices
	err_free_dev:
	    input_free_device(button_dev);//释放申请的对象空间
	return error;
}

static void __exit button_exit(void)
{
	 free_irq(IRQ_EINT(1), NULL);
	 input_unregister_device(button_dev);
	 input_free_device(button_dev);
}

module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值