Teensy 4.1 core updated for C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

usb_mouse.c 8.3KB

il y a 9 ans
il y a 9 ans
il y a 6 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #if F_CPU >= 20000000
  37. // which buttons are currently pressed
  38. uint8_t usb_mouse_buttons_state=0;
  39. //#define DEFAULT_XRES 640
  40. //#define DEFAULT_YRES 480
  41. //#define DEFAULT_XRES 800
  42. //#define DEFAULT_YRES 600
  43. //#define DEFAULT_XRES 1024
  44. //#define DEFAULT_YRES 768
  45. //#define DEFAULT_XRES 1280
  46. //#define DEFAULT_YRES 720
  47. //#define DEFAULT_XRES 1280
  48. //#define DEFAULT_YRES 800
  49. #define DEFAULT_XRES 1366
  50. #define DEFAULT_YRES 768
  51. //#define DEFAULT_XRES 1440
  52. //#define DEFAULT_YRES 900
  53. //#define DEFAULT_XRES 1920
  54. //#define DEFAULT_YRES 1080
  55. //#define DEFAULT_XRES 2560
  56. //#define DEFAULT_YRES 1440
  57. //#define DEFAULT_XRES 2560
  58. //#define DEFAULT_YRES 1600
  59. //#define DEFAULT_XRES 2880
  60. //#define DEFAULT_YRES 1800
  61. //#define DEFAULT_XRES 3840
  62. //#define DEFAULT_YRES 2160
  63. //#define DEFAULT_XRES 7680
  64. //#define DEFAULT_YRES 4320
  65. #define DEFAULT_XSCALE ((0x80000000ul+DEFAULT_XRES/2)/DEFAULT_XRES)
  66. #define DEFAULT_YSCALE ((0x80000000ul+DEFAULT_YRES/2)/DEFAULT_YRES)
  67. static uint16_t usb_mouse_resolution_x=DEFAULT_XRES;
  68. static uint16_t usb_mouse_resolution_y=DEFAULT_YRES;
  69. static uint16_t usb_mouse_position_x=DEFAULT_XRES/2;
  70. static uint16_t usb_mouse_position_y=DEFAULT_YRES/2;
  71. static uint32_t usb_mouse_scale_x=DEFAULT_XSCALE;
  72. static uint32_t usb_mouse_scale_y=DEFAULT_YSCALE;
  73. static uint32_t usb_mouse_offset_x=DEFAULT_XSCALE/2-1;
  74. static uint32_t usb_mouse_offset_y=DEFAULT_YSCALE/2-1;
  75. // Set the mouse buttons. To create a "click", 2 calls are needed,
  76. // one to push the button down and the second to release it
  77. int usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right, uint8_t back, uint8_t forward)
  78. {
  79. uint8_t mask=0;
  80. if (left) mask |= 1;
  81. if (middle) mask |= 4;
  82. if (right) mask |= 2;
  83. if (back) mask |= 8;
  84. if (forward) mask |= 16;
  85. usb_mouse_buttons_state = mask;
  86. return usb_mouse_move(0, 0, 0, 0);
  87. }
  88. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  89. #define TX_PACKET_LIMIT 3
  90. static uint8_t transmit_previous_timeout=0;
  91. // When the PC isn't listening, how long do we wait before discarding data?
  92. #define TX_TIMEOUT_MSEC 30
  93. #if F_CPU == 256000000
  94. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706)
  95. #elif F_CPU == 240000000
  96. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  97. #elif F_CPU == 216000000
  98. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  99. #elif F_CPU == 192000000
  100. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  101. #elif F_CPU == 180000000
  102. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  103. #elif F_CPU == 168000000
  104. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  105. #elif F_CPU == 144000000
  106. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  107. #elif F_CPU == 120000000
  108. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  109. #elif F_CPU == 96000000
  110. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  111. #elif F_CPU == 72000000
  112. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  113. #elif F_CPU == 48000000
  114. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  115. #elif F_CPU == 24000000
  116. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  117. #endif
  118. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  119. int usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t horiz)
  120. {
  121. uint32_t wait_count=0;
  122. usb_packet_t *tx_packet;
  123. //serial_print("move");
  124. //serial_print("\n");
  125. if (x == -128) x = -127;
  126. if (y == -128) y = -127;
  127. if (wheel == -128) wheel = -127;
  128. if (horiz == -128) horiz = -127;
  129. while (1) {
  130. if (!usb_configuration) {
  131. return -1;
  132. }
  133. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  134. tx_packet = usb_malloc();
  135. if (tx_packet) break;
  136. }
  137. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  138. transmit_previous_timeout = 1;
  139. return -1;
  140. }
  141. yield();
  142. }
  143. transmit_previous_timeout = 0;
  144. *(tx_packet->buf + 0) = 1;
  145. *(tx_packet->buf + 1) = usb_mouse_buttons_state;
  146. *(tx_packet->buf + 2) = x;
  147. *(tx_packet->buf + 3) = y;
  148. *(tx_packet->buf + 4) = wheel;
  149. *(tx_packet->buf + 5) = horiz; // horizontal scroll
  150. tx_packet->len = 6;
  151. usb_tx(MOUSE_ENDPOINT, tx_packet);
  152. return 0;
  153. }
  154. int usb_mouse_position(uint16_t x, uint16_t y)
  155. {
  156. uint32_t wait_count=0, val32;
  157. usb_packet_t *tx_packet;
  158. if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
  159. usb_mouse_position_x = x;
  160. if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
  161. usb_mouse_position_y = y;
  162. while (1) {
  163. if (!usb_configuration) {
  164. return -1;
  165. }
  166. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  167. tx_packet = usb_malloc();
  168. if (tx_packet) break;
  169. }
  170. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  171. transmit_previous_timeout = 1;
  172. return -1;
  173. }
  174. yield();
  175. }
  176. transmit_previous_timeout = 0;
  177. *(tx_packet->buf + 0) = 2;
  178. val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
  179. //serial_print("position:");
  180. //serial_phex16(usb_mouse_position_x);
  181. //serial_print("->");
  182. //serial_phex32(val32);
  183. *(tx_packet->buf + 1) = val32 >> 16;
  184. *(tx_packet->buf + 2) = val32 >> 24;
  185. val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
  186. //serial_print(",");
  187. //serial_phex16(usb_mouse_position_y);
  188. //serial_print("->");
  189. //serial_phex32(val32);
  190. //serial_print("\n");
  191. *(tx_packet->buf + 3) = val32 >> 16;
  192. *(tx_packet->buf + 4) = val32 >> 24;
  193. tx_packet->len = 5;
  194. usb_tx(MOUSE_ENDPOINT, tx_packet);
  195. return 0;
  196. }
  197. void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
  198. {
  199. if (width < 128) width = 128;
  200. else if (width > 7680) width = 7680;
  201. if (height < 128) height = 128;
  202. else if (height > 7680) height = 7680;
  203. usb_mouse_resolution_x = width;
  204. usb_mouse_resolution_y = height;
  205. usb_mouse_position_x = width / 2;
  206. usb_mouse_position_y = height / 2;
  207. usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
  208. usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
  209. usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
  210. usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
  211. if (mac) {
  212. // ugly workaround for Mac's HID coordinate scaling:
  213. // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
  214. usb_mouse_offset_x += 161061273ul;
  215. usb_mouse_offset_y += 161061273ul;
  216. usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
  217. usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
  218. }
  219. }
  220. #endif // F_CPU
  221. #endif // MOUSE_INTERFACE