Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.1KB

  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 == 96000000
  91. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  92. #elif F_CPU == 48000000
  93. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  94. #elif F_CPU == 24000000
  95. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  96. #endif
  97. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  98. int usb_mouse_move(int8_t x, int8_t y, int8_t wheel)
  99. {
  100. uint32_t wait_count=0, val32;
  101. usb_packet_t *tx_packet;
  102. uint16_t newval;
  103. if (x == -128) x = -127;
  104. if (y == -128) y = -127;
  105. if (wheel == -128) wheel = -127;
  106. if (x > 0) {
  107. newval = usb_mouse_position_x + x;
  108. if (newval >= usb_mouse_resolution_x) newval = usb_mouse_resolution_x - 1;
  109. usb_mouse_position_x = newval;
  110. } else if (x < 0) {
  111. newval = usb_mouse_position_x + x;
  112. if (newval & 0x8000) newval = 0;
  113. usb_mouse_position_x = newval;
  114. }
  115. if (y > 0) {
  116. newval = usb_mouse_position_y + y;
  117. if (newval >= usb_mouse_resolution_y) newval = usb_mouse_resolution_y - 1;
  118. usb_mouse_position_y = newval;
  119. } else if (y < 0) {
  120. newval = usb_mouse_position_y + y;
  121. if (newval & 0x8000) newval = 0;
  122. usb_mouse_position_y = newval;
  123. }
  124. while (1) {
  125. if (!usb_configuration) {
  126. return -1;
  127. }
  128. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  129. tx_packet = usb_malloc();
  130. if (tx_packet) break;
  131. }
  132. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  133. transmit_previous_timeout = 1;
  134. return -1;
  135. }
  136. yield();
  137. }
  138. transmit_previous_timeout = 0;
  139. *(tx_packet->buf) = usb_mouse_buttons_state;
  140. val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
  141. //serial_print("move:");
  142. //serial_phex16(usb_mouse_position_x);
  143. //serial_print("->");
  144. //serial_phex32(val32);
  145. *(tx_packet->buf + 1) = val32 >> 16;
  146. *(tx_packet->buf + 2) = val32 >> 24;
  147. val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
  148. //serial_print(",");
  149. //serial_phex16(usb_mouse_position_y);
  150. //serial_print("->");
  151. //serial_phex32(val32);
  152. //serial_print("\n");
  153. *(tx_packet->buf + 3) = val32 >> 16;
  154. *(tx_packet->buf + 4) = val32 >> 24;
  155. *(tx_packet->buf + 5) = wheel;
  156. tx_packet->len = 6;
  157. usb_tx(MOUSE_ENDPOINT, tx_packet);
  158. return 0;
  159. }
  160. int usb_mouse_position(uint16_t x, uint16_t y)
  161. {
  162. if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
  163. usb_mouse_position_x = x;
  164. if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
  165. usb_mouse_position_y = y;
  166. return usb_mouse_move(0, 0, 0);
  167. }
  168. void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
  169. {
  170. if (width < 128) width = 128;
  171. else if (width > 7680) width = 7680;
  172. if (height < 128) height = 128;
  173. else if (height > 7680) height = 7680;
  174. usb_mouse_resolution_x = width;
  175. usb_mouse_resolution_y = height;
  176. usb_mouse_position_x = width / 2;
  177. usb_mouse_position_y = height / 2;
  178. usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
  179. usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
  180. usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
  181. usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
  182. if (mac) {
  183. // ugly workaround for Mac's HID coordinate scaling:
  184. // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
  185. usb_mouse_offset_x += 161061273ul;
  186. usb_mouse_offset_y += 161061273ul;
  187. usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
  188. usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
  189. }
  190. }
  191. #endif // MOUSE_INTERFACE