xref: /nrf52832-nimble/rt-thread/components/drivers/usb/usbhost/class/umouse.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2012-01-03     Yi Qiu      first version
9  */
10 
11 #include <rtthread.h>
12 #include <drivers/usb_host.h>
13 #include "hid.h"
14 
15 #ifdef RT_USING_RTGUI
16 #include <rtgui/event.h>
17 #include <rtgui/rtgui_server.h>
18 #include "drv_lcd.h"
19 #endif
20 
21 #if defined(RT_USBH_HID) && defined(RT_USBH_HID_MOUSE)
22 static struct uprotocal mouse_protocal;
23 
24 #ifdef RT_USING_RTGUI
25 #define LKEY_PRESS 0x01
26 #define RKEY_PRESS 0x02
27 #define MKEY_PRESS 0x04
28 #define MOUSE_SCALING 0x02
29 
30 static rt_bool_t lkey_down=RT_FALSE;
31 //static rt_bool_t rkey_down=RT_FALSE;
32 //static rt_bool_t mkey_down=RT_FALSE;
33 static struct rtgui_event_mouse emouse;
34 #endif
35 
36 static rt_err_t rt_usbh_hid_mouse_callback(void* arg)
37 {
38     struct uhid* hid;
39 #ifdef RT_USING_RTGUI
40     rt_uint16_t xoffset=0;
41     rt_uint16_t yoffset=0;
42 #endif
43     hid = (struct uhid*)arg;
44 
45     RT_DEBUG_LOG(RT_DEBUG_USB, ("hid 0x%x 0x%x\n",
46                                 *(rt_uint32_t*)hid->buffer,
47                                 *(rt_uint32_t*)(&hid->buffer[4])));
48 #ifdef RT_USING_RTGUI
49     if(hid->buffer[1]!=0)
50     {
51         if(hid->buffer[1]>127)
52         {
53             xoffset=(256-hid->buffer[1])*MOUSE_SCALING;
54             if(emouse.x>xoffset)
55             {
56                 emouse.x-=xoffset;
57             }
58             else
59             {
60                 emouse.x=0;
61             }
62         }
63         else
64         {
65             xoffset=(hid->buffer[1])*MOUSE_SCALING;
66             if((emouse.x+xoffset)<480)
67             {
68                 emouse.x+=xoffset;
69             }
70             else
71             {
72                 emouse.x=480;
73             }
74         }
75     }
76     if(hid->buffer[2]!=0)
77     {
78 
79         if(hid->buffer[2]>127)
80         {
81             yoffset=(256-hid->buffer[2])*MOUSE_SCALING;
82             if(emouse.y>yoffset)
83             {
84                 emouse.y-=yoffset;
85             }
86             else
87             {
88                 emouse.y=0;
89             }
90         }
91         else
92         {
93             yoffset=hid->buffer[2]*MOUSE_SCALING;
94             if(emouse.y+yoffset<272)
95             {
96                 emouse.y+=yoffset;
97             }
98             else
99             {
100                 emouse.y=272;
101             }
102         }
103     }
104     if(xoffset!=0||yoffset!=0)
105     {
106         cursor_set_position(emouse.x,emouse.y);
107     }
108     if(hid->buffer[0]&LKEY_PRESS)
109     {
110         if(lkey_down==RT_FALSE)
111         {
112             // rt_kprintf("mouse left key press down\n");
113             emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN);
114             rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
115             lkey_down=RT_TRUE;
116         }
117     }
118     else if(lkey_down==RT_TRUE)
119     {
120         // rt_kprintf("mouse left key press up\n");
121         emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP);
122         rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
123         lkey_down=RT_FALSE;
124     }
125 #endif
126     return RT_EOK;
127 }
128 
129 static rt_err_t rt_usbh_hid_mouse_init(void* arg)
130 {
131     struct uintf* intf = (struct uintf*)arg;
132 
133     RT_ASSERT(intf != RT_NULL);
134 
135     rt_usbh_hid_set_protocal(intf, 0);
136 
137     rt_usbh_hid_set_idle(intf, 10, 0);
138 
139     RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb mouse\n"));
140 #ifdef RT_USING_RTGUI
141     RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
142     emouse.wid = RT_NULL;
143     cursor_display(RT_TRUE);
144 #endif
145     return RT_EOK;
146 }
147 
148 /**
149  * This function will define the hid mouse protocal, it will be register to the protocal list.
150  *
151  * @return the keyboard protocal structure.
152  */
153 uprotocal_t rt_usbh_hid_protocal_mouse(void)
154 {
155     mouse_protocal.pro_id = USB_HID_MOUSE;
156 
157     mouse_protocal.init = rt_usbh_hid_mouse_init;
158     mouse_protocal.callback = rt_usbh_hid_mouse_callback;
159 
160     return &mouse_protocal;
161 }
162 
163 #endif
164 
165