文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>轮循方式读取按键

轮循方式读取按键

时间:2007-01-09  来源:基地小头目

key。c

#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

//#include<unistd.h>

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<asm/hardware.h>
#include<linux/init.h>
#include<asm/delay.h>
#include<asm/io.h>
#include<asm/uaccess.h>
#include<linux/types.h>
#include<linux/config.h>
#include<linux/mm.h>
#include<linux/errno.h>
#include<asm/segment.h>

MODULE_LICENSE("GPL");

#define INPUT_MAJOR 200

#define NOKEY 0

#define name "inputkey"

static char key=NOKEY;

static devfs_handle_t devfs_keybd;



int keybd_open(struct inode *inode, struct file *filp)
{

printk("open ok\n");
set_gpio_ctrl(GPIO_MODE_IN|GPIO_F0);
return 0;

}

int keybd_read(struct file *fp, char * buf,size_t size,loff_t * offt)
{
   
  udelay(10000);
  if(read_gpio_bit(GPIO_F0)==0)
{
 key=1;
}
 put_user(key,buf);
 key = NOKEY;
 return 1;
}

int keybd_release(struct inode *inode, struct file *filp)
{
printk("release ok\n");
return 0;
}

static struct file_operations keybd_fops = {
open:keybd_open,
read:keybd_read,
release:keybd_release,
};

int __init keybd_init(void)
{
      int ret;
printk("*********************keybd_init**************\n");

ret=register_chrdev(INPUT_MAJOR,name,&keybd_fops);

     devfs_keybd = devfs_register(NULL,name, DEVFS_FL_DEFAULT,
      INPUT_MAJOR, 0,
       S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
        &keybd_fops, NULL);

printk("*********************keybd_init**************\n");
      return 0;
}

static void __exit keybd_exit(void)
{
     
devfs_unregister(devfs_keybd);
unregister_chrdev(INPUT_MAJOR,name);
printk("rmmod ok!\n");
}

module_init(keybd_init);
module_exit(keybd_exit);

test.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define DEVICE_GPIOTEST "/dev/inputkey"

int main()
{
int fd;
int val=-1;
char key=0;
 char count=0;
 int nbyte=1;
 char flag=0;
if((fd=open(DEVICE_GPIOTEST,O_RDONLY|O_NONBLOCK))<0)
{
perror("can not open device");
exit(1);
}
printf("ready for key \n");
while(1)
{

  

 read(fd,&key,nbyte);

  if(key!=0)
    {flag=1;}
  else if(flag==1)
{
     count++;
 flag=0;
  printf("hello word %d \n",count);

  printf("ready for key input \n");
 
}

  if(count>3)
{

  printf("close \n");
  close(fd);
  break;
}
}
}

排行榜 更多 +
rento大富翁手游

rento大富翁手游

休闲益智 下载
未来教育考试

未来教育考试

学习教育 下载
我要整个活

我要整个活

动作格斗 下载