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ů.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 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 <string.h> // for memcpy()
  34. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. #ifdef MOUSE_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. // which buttons are currently pressed
  39. uint8_t usb_mouse_buttons_state=0;
  40. //#define DEFAULT_XRES 640
  41. //#define DEFAULT_YRES 480
  42. //#define DEFAULT_XRES 800
  43. //#define DEFAULT_YRES 600
  44. //#define DEFAULT_XRES 1024
  45. //#define DEFAULT_YRES 768
  46. //#define DEFAULT_XRES 1280
  47. //#define DEFAULT_YRES 720
  48. //#define DEFAULT_XRES 1280
  49. //#define DEFAULT_YRES 800
  50. #define DEFAULT_XRES 1366
  51. #define DEFAULT_YRES 768
  52. //#define DEFAULT_XRES 1440
  53. //#define DEFAULT_YRES 900
  54. //#define DEFAULT_XRES 1920
  55. //#define DEFAULT_YRES 1080
  56. //#define DEFAULT_XRES 2560
  57. //#define DEFAULT_YRES 1440
  58. //#define DEFAULT_XRES 2560
  59. //#define DEFAULT_YRES 1600
  60. //#define DEFAULT_XRES 2880
  61. //#define DEFAULT_YRES 1800
  62. //#define DEFAULT_XRES 3840
  63. //#define DEFAULT_YRES 2160
  64. //#define DEFAULT_XRES 7680
  65. //#define DEFAULT_YRES 4320
  66. #define DEFAULT_XSCALE ((0x80000000ul+DEFAULT_XRES/2)/DEFAULT_XRES)
  67. #define DEFAULT_YSCALE ((0x80000000ul+DEFAULT_YRES/2)/DEFAULT_YRES)
  68. static uint16_t usb_mouse_resolution_x=DEFAULT_XRES;
  69. static uint16_t usb_mouse_resolution_y=DEFAULT_YRES;
  70. static uint16_t usb_mouse_position_x=DEFAULT_XRES/2;
  71. static uint16_t usb_mouse_position_y=DEFAULT_YRES/2;
  72. static uint32_t usb_mouse_scale_x=DEFAULT_XSCALE;
  73. static uint32_t usb_mouse_scale_y=DEFAULT_YSCALE;
  74. static uint32_t usb_mouse_offset_x=DEFAULT_XSCALE/2-1;
  75. static uint32_t usb_mouse_offset_y=DEFAULT_YSCALE/2-1;
  76. #define TX_NUM 4
  77. #define TX_BUFSIZE 32
  78. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  79. DMAMEM static uint8_t txbuffer[TX_NUM * TX_BUFSIZE] __attribute__ ((aligned(32)));
  80. static uint8_t tx_head=0;
  81. #if MOUSE_SIZE > TX_BUFSIZE
  82. #error "Internal error, transmit buffer size is too small for mouse endpoint"
  83. #endif
  84. void usb_mouse_configure(void)
  85. {
  86. memset(tx_transfer, 0, sizeof(tx_transfer));
  87. tx_head = 0;
  88. usb_config_tx(MOUSE_ENDPOINT, MOUSE_SIZE, 0, NULL);
  89. }
  90. // Set the mouse buttons. To create a "click", 2 calls are needed,
  91. // one to push the button down and the second to release it
  92. int usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right, uint8_t back, uint8_t forward)
  93. {
  94. uint8_t mask=0;
  95. if (left) mask |= 1;
  96. if (middle) mask |= 4;
  97. if (right) mask |= 2;
  98. if (back) mask |= 8;
  99. if (forward) mask |= 16;
  100. usb_mouse_buttons_state = mask;
  101. return usb_mouse_move(0, 0, 0, 0);
  102. }
  103. static uint8_t transmit_previous_timeout=0;
  104. // When the PC isn't listening, how long do we wait before discarding data?
  105. #define TX_TIMEOUT_MSEC 30
  106. static int usb_mouse_transmit(const uint8_t *data, uint32_t len)
  107. {
  108. if (!usb_configuration) return -1;
  109. uint32_t head = tx_head;
  110. transfer_t *xfer = tx_transfer + head;
  111. uint32_t wait_begin_at = systick_millis_count;
  112. while (1) {
  113. uint32_t status = usb_transfer_status(xfer);
  114. if (!(status & 0x80)) {
  115. if (status & 0x68) {
  116. // TODO: what if status has errors???
  117. printf("ERROR status = %x, i=%d, ms=%u\n",
  118. status, tx_head, systick_millis_count);
  119. }
  120. transmit_previous_timeout = 0;
  121. break;
  122. }
  123. if (transmit_previous_timeout) return -1;
  124. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  125. // waited too long, assume the USB host isn't listening
  126. transmit_previous_timeout = 1;
  127. return -1;
  128. }
  129. if (!usb_configuration) return -1;
  130. yield();
  131. }
  132. delayNanoseconds(30); // TODO: why is status ready too soon?
  133. uint8_t *buffer = txbuffer + head * TX_BUFSIZE;
  134. memcpy(buffer, data, len);
  135. usb_prepare_transfer(xfer, buffer, len, 0);
  136. arm_dcache_flush_delete(buffer, TX_BUFSIZE);
  137. usb_transmit(MOUSE_ENDPOINT, xfer);
  138. if (++head >= TX_NUM) head = 0;
  139. tx_head = head;
  140. return 0;
  141. }
  142. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  143. int usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t horiz)
  144. {
  145. //printf("move\n");
  146. if (x == -128) x = -127;
  147. if (y == -128) y = -127;
  148. if (wheel == -128) wheel = -127;
  149. if (horiz == -128) horiz = -127;
  150. uint8_t buffer[6];
  151. buffer[0] = 1;
  152. buffer[1] = usb_mouse_buttons_state;
  153. buffer[2] = x;
  154. buffer[3] = y;
  155. buffer[4] = wheel;
  156. buffer[5] = horiz; // horizontal scroll
  157. return usb_mouse_transmit(buffer, 6);
  158. }
  159. int usb_mouse_position(uint16_t x, uint16_t y)
  160. {
  161. if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
  162. usb_mouse_position_x = x;
  163. if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
  164. usb_mouse_position_y = y;
  165. uint8_t buffer[5];
  166. buffer[0] = 2;
  167. uint32_t val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
  168. //printf("position: %u -> %u", usb_mouse_position_x, val32);
  169. buffer[1] = val32 >> 16;
  170. buffer[2] = val32 >> 24;
  171. val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
  172. //printf(", %u -> %u\n", usb_mouse_position_y, val32);
  173. buffer[3] = val32 >> 16;
  174. buffer[4] = val32 >> 24;
  175. return usb_mouse_transmit(buffer, 5);
  176. }
  177. void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
  178. {
  179. if (width < 128) width = 128;
  180. else if (width > 7680) width = 7680;
  181. if (height < 128) height = 128;
  182. else if (height > 7680) height = 7680;
  183. usb_mouse_resolution_x = width;
  184. usb_mouse_resolution_y = height;
  185. usb_mouse_position_x = width / 2;
  186. usb_mouse_position_y = height / 2;
  187. usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
  188. usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
  189. usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
  190. usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
  191. if (mac) {
  192. // ugly workaround for Mac's HID coordinate scaling:
  193. // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
  194. usb_mouse_offset_x += 161061273ul;
  195. usb_mouse_offset_y += 161061273ul;
  196. usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
  197. usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
  198. }
  199. }
  200. #endif // MOUSE_INTERFACE