umouse.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }else
  55. {
  56. emouse.x=0;
  57. }
  58. }else
  59. {
  60. xoffset=(hid->buffer[1])*MOUSE_SCALING;
  61. if((emouse.x+xoffset)<480)
  62. {
  63. emouse.x+=xoffset;
  64. }else
  65. {
  66. emouse.x=480;
  67. }
  68. }
  69. }
  70. if(hid->buffer[2]!=0)
  71. {
  72. if(hid->buffer[2]>127)
  73. {
  74. yoffset=(256-hid->buffer[2])*MOUSE_SCALING;
  75. if(emouse.y>yoffset)
  76. {
  77. emouse.y-=yoffset;
  78. }else
  79. {
  80. emouse.y=0;
  81. }
  82. }else
  83. {
  84. yoffset=hid->buffer[2]*MOUSE_SCALING;
  85. if(emouse.y+yoffset<272)
  86. {
  87. emouse.y+=yoffset;
  88. }else
  89. {
  90. emouse.y=272;
  91. }
  92. }
  93. }
  94. if(xoffset!=0||yoffset!=0)
  95. {
  96. cursor_set_position(emouse.x,emouse.y);
  97. }
  98. if(hid->buffer[0]&LKEY_PRESS)
  99. {
  100. if(lkey_down==RT_FALSE){
  101. // rt_kprintf("mouse left key press down\n");
  102. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN);
  103. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  104. lkey_down=RT_TRUE;
  105. }
  106. }else if(lkey_down==RT_TRUE)
  107. {
  108. // rt_kprintf("mouse left key press up\n");
  109. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP);
  110. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  111. lkey_down=RT_FALSE;
  112. }
  113. #endif
  114. return RT_EOK;
  115. }
  116. static rt_err_t rt_usbh_hid_mouse_init(void* arg)
  117. {
  118. struct uintf* intf = (struct uintf*)arg;
  119. RT_ASSERT(intf != RT_NULL);
  120. rt_usbh_hid_set_protocal(intf, 0);
  121. rt_usbh_hid_set_idle(intf, 10, 0);
  122. RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb mouse\n"));
  123. #ifdef RT_USING_RTGUI
  124. RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  125. emouse.wid = RT_NULL;
  126. cursor_display(RT_TRUE);
  127. #endif
  128. return RT_EOK;
  129. }
  130. /**
  131. * This function will define the hid mouse protocal, it will be register to the protocal list.
  132. *
  133. * @return the keyboard protocal structure.
  134. */
  135. uprotocal_t rt_usbh_hid_protocal_mouse(void)
  136. {
  137. mouse_protocal.pro_id = USB_HID_MOUSE;
  138. mouse_protocal.init = rt_usbh_hid_mouse_init;
  139. mouse_protocal.callback = rt_usbh_hid_mouse_callback;
  140. return &mouse_protocal;
  141. }
  142. #endif