你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux USB鼠标驱动注解及测试

[日期:2010-01-11] 来源:CSDN  作者:刘洪涛 [字体:大 中 小]

五、应用层测试代码编写

在应用层编写测试鼠标的测试程序,在我的系统中,鼠标设备为/dev/input/event3. 测试代码如下:

/*
         * usb_mouse_test.c
         *by lht
         */

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

int main (void)
         {
                  int fd,i,count;
                           struct input_event ev_mouse[2];
                  fd = open ("/dev/input/event3",O_RDWR);
                  if (fd < 0) {
                           printf ("fd open failed\n");
                           exit(0);
                  }
                  printf ("\nmouse opened, fd=%d\n",fd);
                           while(1)
                           {
                           printf("...............................................\n");
                                    count=read(fd, ev_mouse, sizeof(struct input_event));
                           for(i=0;i<(int)count/sizeof(struct input_event);i++)
                          {
                                      printf("type=%d\n",ev_mouse[i].type);
                                      if(EV_REL==ev_mouse[i].type)
                                      {
               printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
                                                    printf(" type:%d code:%d value:%d\n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
                                       }
                                      if(EV_KEY==ev_mouse[i].type)
                                      {
                                      printf("time:%ld.%d",ev_mouse[i].time.tv_sec,ev_mouse[i].time.tv_usec);
                                      printf(" type:%d code:%d value:%d\n",ev_mouse[i].type,ev_mouse[i].code,ev_mouse[i].value);
                                      }
                     }
                     }
               close (fd);
               return 0;
      }

运行结果如下:

Linux USB鼠标驱动注解及测试

根据type、code、value的值,可以判断出鼠标的状态,具体值参考include/linux/input.h

linux
相关资讯       Linux驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款