Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

244 lines
7.7KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_mouse.h"
  32. #include "core_pins.h" // for yield()
  33. #include "HardwareSerial.h"
  34. #include <string.h> // for memcpy()
  35. #ifdef MOUSE_INTERFACE // defined by usb_dev.h -> usb_desc.h
  36. // which buttons are currently pressed
  37. uint8_t usb_mouse_buttons_state=0;
  38. //#define DEFAULT_XRES 640
  39. //#define DEFAULT_YRES 480
  40. //#define DEFAULT_XRES 800
  41. //#define DEFAULT_YRES 600
  42. //#define DEFAULT_XRES 1024
  43. //#define DEFAULT_YRES 768
  44. //#define DEFAULT_XRES 1280
  45. //#define DEFAULT_YRES 720
  46. //#define DEFAULT_XRES 1280
  47. //#define DEFAULT_YRES 800
  48. #define DEFAULT_XRES 1366
  49. #define DEFAULT_YRES 768
  50. //#define DEFAULT_XRES 1440
  51. //#define DEFAULT_YRES 900
  52. //#define DEFAULT_XRES 1920
  53. //#define DEFAULT_YRES 1080
  54. //#define DEFAULT_XRES 2560
  55. //#define DEFAULT_YRES 1440
  56. //#define DEFAULT_XRES 2560
  57. //#define DEFAULT_YRES 1600
  58. //#define DEFAULT_XRES 2880
  59. //#define DEFAULT_YRES 1800
  60. //#define DEFAULT_XRES 3840
  61. //#define DEFAULT_YRES 2160
  62. //#define DEFAULT_XRES 7680
  63. //#define DEFAULT_YRES 4320
  64. #define DEFAULT_XSCALE ((0x80000000ul+DEFAULT_XRES/2)/DEFAULT_XRES)
  65. #define DEFAULT_YSCALE ((0x80000000ul+DEFAULT_YRES/2)/DEFAULT_YRES)
  66. static uint16_t usb_mouse_resolution_x=DEFAULT_XRES;
  67. static uint16_t usb_mouse_resolution_y=DEFAULT_YRES;
  68. static uint16_t usb_mouse_position_x=DEFAULT_XRES/2;
  69. static uint16_t usb_mouse_position_y=DEFAULT_YRES/2;
  70. static uint32_t usb_mouse_scale_x=DEFAULT_XSCALE;
  71. static uint32_t usb_mouse_scale_y=DEFAULT_YSCALE;
  72. static uint32_t usb_mouse_offset_x=DEFAULT_XSCALE/2-1;
  73. static uint32_t usb_mouse_offset_y=DEFAULT_YSCALE/2-1;
  74. // Set the mouse buttons. To create a "click", 2 calls are needed,
  75. // one to push the button down and the second to release it
  76. int usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right)
  77. {
  78. uint8_t mask=0;
  79. if (left) mask |= 1;
  80. if (middle) mask |= 4;
  81. if (right) mask |= 2;
  82. usb_mouse_buttons_state = mask;
  83. return usb_mouse_move(0, 0, 0);
  84. }
  85. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  86. #define TX_PACKET_LIMIT 3
  87. static uint8_t transmit_previous_timeout=0;
  88. // When the PC isn't listening, how long do we wait before discarding data?
  89. #define TX_TIMEOUT_MSEC 30
  90. #if F_CPU == 168000000
  91. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  92. #elif F_CPU == 144000000
  93. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  94. #elif F_CPU == 120000000
  95. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  96. #elif F_CPU == 96000000
  97. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  98. #elif F_CPU == 72000000
  99. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  100. #elif F_CPU == 48000000
  101. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  102. #elif F_CPU == 24000000
  103. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  104. #endif
  105. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  106. int usb_mouse_move(int8_t x, int8_t y, int8_t wheel)
  107. {
  108. uint32_t wait_count=0;
  109. usb_packet_t *tx_packet;
  110. //serial_print("move");
  111. //serial_print("\n");
  112. if (x == -128) x = -127;
  113. if (y == -128) y = -127;
  114. if (wheel == -128) wheel = -127;
  115. while (1) {
  116. if (!usb_configuration) {
  117. return -1;
  118. }
  119. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  120. tx_packet = usb_malloc();
  121. if (tx_packet) break;
  122. }
  123. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  124. transmit_previous_timeout = 1;
  125. return -1;
  126. }
  127. yield();
  128. }
  129. transmit_previous_timeout = 0;
  130. *(tx_packet->buf + 0) = 1;
  131. *(tx_packet->buf + 1) = usb_mouse_buttons_state;
  132. *(tx_packet->buf + 2) = x;
  133. *(tx_packet->buf + 3) = y;
  134. *(tx_packet->buf + 4) = wheel;
  135. tx_packet->len = 5;
  136. usb_tx(MOUSE_ENDPOINT, tx_packet);
  137. return 0;
  138. }
  139. int usb_mouse_position(uint16_t x, uint16_t y)
  140. {
  141. uint32_t wait_count=0, val32;
  142. usb_packet_t *tx_packet;
  143. if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
  144. usb_mouse_position_x = x;
  145. if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
  146. usb_mouse_position_y = y;
  147. while (1) {
  148. if (!usb_configuration) {
  149. return -1;
  150. }
  151. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  152. tx_packet = usb_malloc();
  153. if (tx_packet) break;
  154. }
  155. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  156. transmit_previous_timeout = 1;
  157. return -1;
  158. }
  159. yield();
  160. }
  161. transmit_previous_timeout = 0;
  162. *(tx_packet->buf + 0) = 2;
  163. val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
  164. //serial_print("position:");
  165. //serial_phex16(usb_mouse_position_x);
  166. //serial_print("->");
  167. //serial_phex32(val32);
  168. *(tx_packet->buf + 1) = val32 >> 16;
  169. *(tx_packet->buf + 2) = val32 >> 24;
  170. val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
  171. //serial_print(",");
  172. //serial_phex16(usb_mouse_position_y);
  173. //serial_print("->");
  174. //serial_phex32(val32);
  175. //serial_print("\n");
  176. *(tx_packet->buf + 3) = val32 >> 16;
  177. *(tx_packet->buf + 4) = val32 >> 24;
  178. tx_packet->len = 5;
  179. usb_tx(MOUSE_ENDPOINT, tx_packet);
  180. return 0;
  181. }
  182. void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
  183. {
  184. if (width < 128) width = 128;
  185. else if (width > 7680) width = 7680;
  186. if (height < 128) height = 128;
  187. else if (height > 7680) height = 7680;
  188. usb_mouse_resolution_x = width;
  189. usb_mouse_resolution_y = height;
  190. usb_mouse_position_x = width / 2;
  191. usb_mouse_position_y = height / 2;
  192. usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
  193. usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
  194. usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
  195. usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
  196. if (mac) {
  197. // ugly workaround for Mac's HID coordinate scaling:
  198. // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
  199. usb_mouse_offset_x += 161061273ul;
  200. usb_mouse_offset_y += 161061273ul;
  201. usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
  202. usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
  203. }
  204. }
  205. #endif // MOUSE_INTERFACE