Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_mouse.c 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. * Modified by Jacob Alexander (2015-2016)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * 1. The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * 2. If the Software is incorporated into a build system that allows
  18. * selection among a list of target devices, then similar target
  19. * devices manufactured by PJRC.COM must be included in the list of
  20. * target devices and selectable in the same manner.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. #include <kll_defs.h>
  32. #if enableMouse_define == 1
  33. // ----- Includes -----
  34. // Compiler Includes
  35. #include <string.h> // for memcpy()
  36. // Project Includes
  37. #include <Lib/OutputLib.h>
  38. #include <print.h>
  39. // Local Includes
  40. #include "usb_dev.h"
  41. #include "usb_mouse.h"
  42. // ----- Defines -----
  43. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  44. #define TX_PACKET_LIMIT 3
  45. // When the PC isn't listening, how long do we wait before discarding data?
  46. #define TX_TIMEOUT_MSEC 30
  47. #if F_CPU == 168000000
  48. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  49. #elif F_CPU == 144000000
  50. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  51. #elif F_CPU == 120000000
  52. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  53. #elif F_CPU == 96000000
  54. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  55. #elif F_CPU == 72000000
  56. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  57. #elif F_CPU == 48000000
  58. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  59. #elif F_CPU == 24000000
  60. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  61. #endif
  62. //#define DEFAULT_XRES 640
  63. //#define DEFAULT_YRES 480
  64. //#define DEFAULT_XRES 800
  65. //#define DEFAULT_YRES 600
  66. //#define DEFAULT_XRES 1024
  67. //#define DEFAULT_YRES 768
  68. //#define DEFAULT_XRES 1280
  69. //#define DEFAULT_YRES 720
  70. //#define DEFAULT_XRES 1280
  71. //#define DEFAULT_YRES 800
  72. #define DEFAULT_XRES 1366
  73. #define DEFAULT_YRES 768
  74. //#define DEFAULT_XRES 1440
  75. //#define DEFAULT_YRES 900
  76. //#define DEFAULT_XRES 1920
  77. //#define DEFAULT_YRES 1080
  78. //#define DEFAULT_XRES 2560
  79. //#define DEFAULT_YRES 1440
  80. //#define DEFAULT_XRES 2560
  81. //#define DEFAULT_YRES 1600
  82. //#define DEFAULT_XRES 2880
  83. //#define DEFAULT_YRES 1800
  84. //#define DEFAULT_XRES 3840
  85. //#define DEFAULT_YRES 2160
  86. //#define DEFAULT_XRES 7680
  87. //#define DEFAULT_YRES 4320
  88. #define DEFAULT_XSCALE ((0x80000000ul+DEFAULT_XRES/2)/DEFAULT_XRES)
  89. #define DEFAULT_YSCALE ((0x80000000ul+DEFAULT_YRES/2)/DEFAULT_YRES)
  90. // ----- Variables -----
  91. static uint8_t transmit_previous_timeout = 0;
  92. // which buttons are currently pressed
  93. uint8_t usb_mouse_buttons_state = 0;
  94. static uint16_t usb_mouse_resolution_x = DEFAULT_XRES;
  95. static uint16_t usb_mouse_resolution_y = DEFAULT_YRES;
  96. static uint16_t usb_mouse_position_x = DEFAULT_XRES / 2;
  97. static uint16_t usb_mouse_position_y = DEFAULT_YRES / 2;
  98. static uint32_t usb_mouse_scale_x = DEFAULT_XSCALE;
  99. static uint32_t usb_mouse_scale_y = DEFAULT_YSCALE;
  100. static uint32_t usb_mouse_offset_x = DEFAULT_XSCALE / 2 - 1;
  101. static uint32_t usb_mouse_offset_y = DEFAULT_YSCALE / 2 - 1;
  102. // ----- Functions -----
  103. // Process pending mouse commands
  104. // XXX Missing mouse movement and wheels
  105. // Proper support will require KLL generation of the USB descriptors
  106. // Similar support will be required for joystick control
  107. void usb_mouse_send()
  108. {
  109. uint32_t wait_count = 0;
  110. usb_packet_t *tx_packet;
  111. // Wait till ready
  112. while ( 1 )
  113. {
  114. if ( !usb_configuration )
  115. {
  116. erro_print("USB not configured...");
  117. return;
  118. }
  119. // Attempt to acquire a USB packet for the mouse endpoint
  120. if ( usb_tx_packet_count( MOUSE_ENDPOINT ) < TX_PACKET_LIMIT )
  121. {
  122. tx_packet = usb_malloc();
  123. if ( tx_packet )
  124. break;
  125. }
  126. if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
  127. {
  128. transmit_previous_timeout = 1;
  129. warn_print("USB Transmit Timeout...");
  130. // Clear status and state
  131. USBMouse_Buttons = 0;
  132. USBMouse_Relative_x = 0;
  133. USBMouse_Relative_y = 0;
  134. USBMouse_Changed = 0;
  135. return;
  136. }
  137. yield();
  138. }
  139. transmit_previous_timeout = 0;
  140. // Prepare USB Mouse Packet
  141. // TODO Dynamically generate this code based on KLL requirements
  142. uint16_t *packet_data = (uint16_t*)(&tx_packet->buf[0]);
  143. packet_data[0] = USBMouse_Buttons;
  144. packet_data[1] = USBMouse_Relative_x;
  145. packet_data[2] = USBMouse_Relative_y;
  146. tx_packet->len = 6;
  147. usb_tx( MOUSE_ENDPOINT, tx_packet );
  148. // Clear status and state
  149. USBMouse_Buttons = 0;
  150. USBMouse_Relative_x = 0;
  151. USBMouse_Relative_y = 0;
  152. USBMouse_Changed = 0;
  153. }
  154. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
  155. int usb_mouse_move(int8_t x, int8_t y, int8_t wheel)
  156. {
  157. uint32_t wait_count=0;
  158. usb_packet_t *tx_packet;
  159. //serial_print("move");
  160. //serial_print("\n");
  161. if (x == -128) x = -127;
  162. if (y == -128) y = -127;
  163. if (wheel == -128) wheel = -127;
  164. while (1) {
  165. if (!usb_configuration) {
  166. return -1;
  167. }
  168. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  169. tx_packet = usb_malloc();
  170. if (tx_packet) break;
  171. }
  172. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  173. transmit_previous_timeout = 1;
  174. return -1;
  175. }
  176. yield();
  177. }
  178. transmit_previous_timeout = 0;
  179. *(tx_packet->buf + 0) = 1;
  180. *(tx_packet->buf + 1) = usb_mouse_buttons_state;
  181. *(tx_packet->buf + 2) = x;
  182. *(tx_packet->buf + 3) = y;
  183. *(tx_packet->buf + 4) = wheel;
  184. tx_packet->len = 5;
  185. usb_tx(MOUSE_ENDPOINT, tx_packet);
  186. return 0;
  187. }
  188. int usb_mouse_position(uint16_t x, uint16_t y)
  189. {
  190. uint32_t wait_count=0, val32;
  191. usb_packet_t *tx_packet;
  192. if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
  193. usb_mouse_position_x = x;
  194. if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
  195. usb_mouse_position_y = y;
  196. while (1) {
  197. if (!usb_configuration) {
  198. return -1;
  199. }
  200. if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
  201. tx_packet = usb_malloc();
  202. if (tx_packet) break;
  203. }
  204. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  205. transmit_previous_timeout = 1;
  206. return -1;
  207. }
  208. yield();
  209. }
  210. transmit_previous_timeout = 0;
  211. *(tx_packet->buf + 0) = 2;
  212. val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
  213. //serial_print("position:");
  214. //serial_phex16(usb_mouse_position_x);
  215. //serial_print("->");
  216. //serial_phex32(val32);
  217. *(tx_packet->buf + 1) = val32 >> 16;
  218. *(tx_packet->buf + 2) = val32 >> 24;
  219. val32 = usb_mouse_position_y * usb_mouse_scale_y + usb_mouse_offset_y;
  220. //serial_print(",");
  221. //serial_phex16(usb_mouse_position_y);
  222. //serial_print("->");
  223. //serial_phex32(val32);
  224. //serial_print("\n");
  225. *(tx_packet->buf + 3) = val32 >> 16;
  226. *(tx_packet->buf + 4) = val32 >> 24;
  227. tx_packet->len = 5;
  228. usb_tx(MOUSE_ENDPOINT, tx_packet);
  229. return 0;
  230. }
  231. void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac)
  232. {
  233. if (width < 128) width = 128;
  234. else if (width > 7680) width = 7680;
  235. if (height < 128) height = 128;
  236. else if (height > 7680) height = 7680;
  237. usb_mouse_resolution_x = width;
  238. usb_mouse_resolution_y = height;
  239. usb_mouse_position_x = width / 2;
  240. usb_mouse_position_y = height / 2;
  241. usb_mouse_scale_x = (0x80000000ul + (width >> 1)) / width;
  242. usb_mouse_scale_y = (0x80000000ul + (height >> 1)) / height;
  243. usb_mouse_offset_x = (usb_mouse_scale_x >> 1) - 1;
  244. usb_mouse_offset_y = (usb_mouse_scale_y >> 1) - 1;
  245. if (mac) {
  246. // ugly workaround for Mac's HID coordinate scaling:
  247. // http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
  248. usb_mouse_offset_x += 161061273ul;
  249. usb_mouse_offset_y += 161061273ul;
  250. usb_mouse_scale_x = (1825361101ul + (width >> 1)) / width;
  251. usb_mouse_scale_y = (1825361101ul + (height >> 1)) / height;
  252. }
  253. }
  254. #endif