umouse.c 4.4 KB

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