Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

236 lines
7.4KB

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