umouse.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #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. static struct uprotocal mouse_protocal;
  20. #ifdef RT_USING_RTGUI
  21. #define LKEY_PRESS 0x01
  22. #define RKEY_PRESS 0x02
  23. #define MKEY_PRESS 0x04
  24. #define MOUSE_SCALING 0x02
  25. static rt_bool_t lkey_down=RT_FALSE;
  26. //static rt_bool_t rkey_down=RT_FALSE;
  27. //static rt_bool_t mkey_down=RT_FALSE;
  28. static struct rtgui_event_mouse emouse;
  29. #endif
  30. static rt_err_t rt_usbh_hid_mouse_callback(void* arg)
  31. {
  32. struct uhid* hid;
  33. #ifdef RT_USING_RTGUI
  34. rt_uint16_t xoffset=0;
  35. rt_uint16_t yoffset=0;
  36. #endif
  37. hid = (struct uhid*)arg;
  38. RT_DEBUG_LOG(RT_DEBUG_USB, ("hid 0x%x 0x%x\n",
  39. *(rt_uint32_t*)hid->buffer,
  40. *(rt_uint32_t*)(&hid->buffer[4])));
  41. #ifdef RT_USING_RTGUI
  42. if(hid->buffer[1]!=0)
  43. {
  44. if(hid->buffer[1]>127)
  45. {
  46. xoffset=(256-hid->buffer[1])*MOUSE_SCALING;
  47. if(emouse.x>xoffset)
  48. {
  49. emouse.x-=xoffset;
  50. }
  51. else
  52. {
  53. emouse.x=0;
  54. }
  55. }
  56. else
  57. {
  58. xoffset=(hid->buffer[1])*MOUSE_SCALING;
  59. if((emouse.x+xoffset)<480)
  60. {
  61. emouse.x+=xoffset;
  62. }
  63. else
  64. {
  65. emouse.x=480;
  66. }
  67. }
  68. }
  69. if(hid->buffer[2]!=0)
  70. {
  71. if(hid->buffer[2]>127)
  72. {
  73. yoffset=(256-hid->buffer[2])*MOUSE_SCALING;
  74. if(emouse.y>yoffset)
  75. {
  76. emouse.y-=yoffset;
  77. }
  78. else
  79. {
  80. emouse.y=0;
  81. }
  82. }
  83. else
  84. {
  85. yoffset=hid->buffer[2]*MOUSE_SCALING;
  86. if(emouse.y+yoffset<272)
  87. {
  88. emouse.y+=yoffset;
  89. }
  90. else
  91. {
  92. emouse.y=272;
  93. }
  94. }
  95. }
  96. if(xoffset!=0||yoffset!=0)
  97. {
  98. cursor_set_position(emouse.x,emouse.y);
  99. }
  100. if(hid->buffer[0]&LKEY_PRESS)
  101. {
  102. if(lkey_down==RT_FALSE)
  103. {
  104. // rt_kprintf("mouse left key press down\n");
  105. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN);
  106. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  107. lkey_down=RT_TRUE;
  108. }
  109. }
  110. else if(lkey_down==RT_TRUE)
  111. {
  112. // rt_kprintf("mouse left key press up\n");
  113. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP);
  114. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  115. lkey_down=RT_FALSE;
  116. }
  117. #endif
  118. return RT_EOK;
  119. }
  120. static rt_err_t rt_usbh_hid_mouse_init(void* arg)
  121. {
  122. struct uintf* intf = (struct uintf*)arg;
  123. RT_ASSERT(intf != RT_NULL);
  124. rt_usbh_hid_set_protocal(intf, 0);
  125. rt_usbh_hid_set_idle(intf, 10, 0);
  126. RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb mouse\n"));
  127. #ifdef RT_USING_RTGUI
  128. RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  129. emouse.wid = RT_NULL;
  130. cursor_display(RT_TRUE);
  131. #endif
  132. return RT_EOK;
  133. }
  134. /**
  135. * This function will define the hid mouse protocal, it will be register to the protocal list.
  136. *
  137. * @return the keyboard protocal structure.
  138. */
  139. uprotocal_t rt_usbh_hid_protocal_mouse(void)
  140. {
  141. mouse_protocal.pro_id = USB_HID_MOUSE;
  142. mouse_protocal.init = rt_usbh_hid_mouse_init;
  143. mouse_protocal.callback = rt_usbh_hid_mouse_callback;
  144. return &mouse_protocal;
  145. }
  146. #endif