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 圖。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM