umouse.c 4.0 KB

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