/************************************************************************************/
buttons_input.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct input_dev *buttons_dev;
static struct timer_list button_timer;
static struct pins_desc * pin_pd = NULL;
struct pins_desc {
int irq;
int pin;
int keyval;
char *name;
};
static struct pins_desc pins_desc[6] = {
{IRQ_EINT8, S3C2410_GPG(0), KEY_L, "S1"},
{IRQ_EINT11, S3C2410_GPG(3), KEY_S, "S2"},
{IRQ_EINT13, S3C2410_GPG(5), KEY_ENTER, "S3"},
{IRQ_EINT14, S3C2410_GPG(6), KEY_C, "S4"},
{IRQ_EINT15, S3C2410_GPG(7), KEY_D, "S5"},
{IRQ_EINT19, S3C2410_GPG(11), KEY_A, "S6"},
};
static irqreturn_t bottons_handler(int irq, void *dev_id)
{
pin_pd = (struct pins_desc *)dev_id;
mod_timer(&button_timer, jiffies + HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static void buttons_fun(unsigned long data)
{
struct pins_desc * desc =pin_pd;
int key;
if(!pin_pd)
return;
key = s3c2410_gpio_getpin(desc->pin);
if(key)
// 上报事件,最后一个参数,松开时0,按下为1;
input_event(buttons_dev, EV_KEY, pin_pd->keyval, 0);
else
input_event(buttons_dev, EV_KEY, pin_pd->keyval, 1);
// 上报一个同步时间
input_sync(buttons_dev);
}
static __init int bottons_init(void)
{
int i;
/* 分配一个input_dev结构 */
buttons_dev = input_allocate_device();
/* 设置 input_dev结构*/
// 设置能产生那类时间
set_bit(EV_KEY, buttons_dev->evbit);
set_bit(EV_KEY, buttons_dev->evbit);
// 设置能产生这类事件中的那些时间
set_bit(KEY_L, buttons_dev->keybit);
set_bit(KEY_S, buttons_dev->keybit);
set_bit(KEY_ENTER, buttons_dev->keybit);
set_bit(KEY_C, buttons_dev->keybit);
set_bit(KEY_D, buttons_dev->keybit);
set_bit(KEY_A, buttons_dev->keybit);
/* 注册input_dev结构 */
input_register_device(buttons_dev);
// 注册中断
for(i = 0; i < 6; i++)
request_irq(pins_desc[i].irq, bottons_handler, IRQ_TYPE_EDGE_BOTH, pins_desc[i].name, &pins_desc[i]);
// 初始化定时器
init_timer(&button_timer);
// 设置超时函数
button_timer.function = buttons_fun;
// 注册定时器
add_timer(&button_timer);
return 0;
}
static __exit void bottons_exit(void)
{
int i;
// 删除定时器
del_timer(&button_timer);
// 注销input_dev
input_unregister_device(buttons_dev);
// 释放input_dev结构体
input_free_device(buttons_dev);
// 注销中断
for(i = 0; i < 6; i++)
free_irq(pins_desc[i].irq, &pins_desc[i]);
}
module_init(bottons_init);
module_exit(bottons_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("");
/*********************************************************************************/
/*********************************************************************************/
input_test.c
#include
#include
#include
#include
#include
#include
static struct input_event key_event;
int main(int argc,char **argv)
{
int fd;
fd = open("/dev/event1",O_RDWR);
if(fd<0)
{
printf("cannot open !\n");
return -1;
}
while(1)
{
// 读取输入子系统的时,读出来的是一个struct input_event结构
read(fd, &key_event,sizeof(key_event));
//printf("wake up!! \n");
printf("timer.tv_sec = %d, timer.tv_usec = %d, type = %d, code = %d, value = %d\n",
key_event.time.tv_sec, key_event.time.tv_usec, key_event.type, key_event.code,key_event.value);
}
close(fd);
return 0;
}
/*********************************************************************************/
阅读(2367) | 评论(0) | 转发(0) |