linux 读取 USB HID鼠标坐标和点击 在 LCD上显示

首先要,编译内核时启用了 USB HID 设备。启用了 鼠标 。

在开发板上插入usb 时会有如下提示。

可以看到,多了一个 mouse0 和 eventX 打出来的是我的 联想鼠标。

1, 在 终端打印出坐杯

测试代码:

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 //author:ningci dev
 6 //date: 2017-05-04 15:39
 7 #define MOUSE_DEV "/dev/input/mouse0"
 8 
 9 static int postion_x;
10 static int postion_y;
11 static int mouse_fd;
12 
13 int main(int argc, char **argv)
14 {
15     mouse_fd = open(MOUSE_DEV, O_RDONLY);
16     if(-1 == mouse_fd)
17     {
18         printf("mouse cat't open %s \n", MOUSE_DEV);
19         return -1;
20     }
21     while(1)
22     {
23         unsigned char buf[3];
24         if(read(mouse_fd, buf, sizeof(buf)))
25         {
26             postion_x = buf[1];
27             postion_y = buf[2];
28             printf("x:%d y:%d \n", postion_x,  postion_y);
29         }
30     }
31     return 0;
32 }

测试结果: 上下移动时

左右移动时

测试按键的结果 (打印 buf[0] )

左键 0x9
松开 0x8

右键 0xa
松开 0x8

中键 0xc
松开 0x8

好了,可以开始写转换作标的函数了。

测试正常后的源码

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 
 6 /**
 7  * author:ningci dev
 8  * date: 2017-05-04 15:58
 9  */
10 #define MOUSE_DEV "/dev/input/mouse0"
11 
12 //定义步进
13 #define MOVE_STEP 10
14 static int postion_x;
15 static int postion_y;
16 static int mouse_fd;
17 
18 int main(int argc, char **argv)
19 {
20     //限定最大 X  Y 
21     int max_x = 300;
22     int max_y = 300;
23     
24     mouse_fd = open(MOUSE_DEV, O_RDONLY);
25     if(-1 == mouse_fd)
26     {
27         printf("mouse cat't open %s \n", MOUSE_DEV);
28         return -1;
29     }
30     while(1)
31     {
32         unsigned char buf[3];
33         if(read(mouse_fd, buf, sizeof(buf)))
34         {
35             /**
36              * 原理 当不为0 时说明鼠标在移动,经测试发现,值为 12 或 255 254 所以这里取比10小就是减少
37              */
38             //X 向右移动时变大 没问题
39             if(0 < buf[1])
40             {
41                 postion_x += (10 > buf[1]) ? MOVE_STEP : (0 - MOVE_STEP);
42             }
43             //Y 向下移动时变小 需要反转
44             if(0 < buf[2])
45             {
46                 postion_y += (10 > buf[2]) ? (0 - MOVE_STEP) : MOVE_STEP;
47             }
48             postion_x = (1 > postion_x) ? 0 : postion_x;
49             postion_y = (1 > postion_y) ? 0 : postion_y;
50             postion_x = (max_x < postion_x) ? max_x : postion_x;
51             postion_y = (max_y < postion_y) ? max_y : postion_y;
52             
53             printf("x:%d y:%d \n", postion_x,  postion_y);
54         }
55     }
56     return 0;
57 }

 

2,在 lcd 显示鼠标键头。

正常的鼠标指针,肯定是这个样子,(下图中是在 ps 中)。 除了指针外,是透明的。

 

因为现在,还没有添加 png 图片解析功能,所以先使用 BMP 图片,效果就是,指针外带有一个白外框。

 

重新修正,添加功能后 gif 图。

posted @ 2017-05-04 16:01  宁次  阅读(4883)  评论(1编辑  收藏  举报