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.

usb_joystick.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef USBjoystick_h_
  31. #define USBjoystick_h_
  32. #include "usb_desc.h"
  33. #if defined(JOYSTICK_INTERFACE)
  34. #include <inttypes.h>
  35. // C language implementation
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. void usb_joystick_configure(void);
  40. int usb_joystick_send(void);
  41. extern uint32_t usb_joystick_data[(JOYSTICK_SIZE+3)/4];
  42. extern volatile uint8_t usb_configuration;
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. // C++ interface
  47. #ifdef __cplusplus
  48. class usb_joystick_class
  49. {
  50. public:
  51. void begin(void) { }
  52. void end(void) { }
  53. #if JOYSTICK_SIZE == 12
  54. void button(uint8_t button, bool val) {
  55. if (--button >= 32) return;
  56. if (val) usb_joystick_data[0] |= (1 << button);
  57. else usb_joystick_data[0] &= ~(1 << button);
  58. if (!manual_mode) usb_joystick_send();
  59. }
  60. void X(unsigned int val) {
  61. if (val > 1023) val = 1023;
  62. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFFFC00F) | (val << 4);
  63. if (!manual_mode) usb_joystick_send();
  64. }
  65. void Y(unsigned int val) {
  66. if (val > 1023) val = 1023;
  67. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFF003FFF) | (val << 14);
  68. if (!manual_mode) usb_joystick_send();
  69. }
  70. void position(unsigned int x, unsigned int y) {
  71. if (x > 1023) x = 1023;
  72. if (y > 1023) y = 1023;
  73. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFF00000)
  74. | (x << 4) | (y << 14);
  75. if (!manual_mode) usb_joystick_send();
  76. }
  77. void Z(unsigned int val) {
  78. if (val > 1023) val = 1023;
  79. usb_joystick_data[1] = (usb_joystick_data[1] & 0x00FFFFFF) | (val << 24);
  80. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFFFFFFC) | (val >> 8);
  81. if (!manual_mode) usb_joystick_send();
  82. }
  83. void Zrotate(unsigned int val) {
  84. if (val > 1023) val = 1023;
  85. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFFFF003) | (val << 2);
  86. if (!manual_mode) usb_joystick_send();
  87. }
  88. void sliderLeft(unsigned int val) {
  89. if (val > 1023) val = 1023;
  90. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFC00FFF) | (val << 12);
  91. if (!manual_mode) usb_joystick_send();
  92. }
  93. void sliderRight(unsigned int val) {
  94. if (val > 1023) val = 1023;
  95. usb_joystick_data[2] = (usb_joystick_data[2] & 0x003FFFFF) | (val << 22);
  96. if (!manual_mode) usb_joystick_send();
  97. }
  98. void slider(unsigned int val) {
  99. if (val > 1023) val = 1023;
  100. usb_joystick_data[2] = (usb_joystick_data[2] & 0x00000FFF)
  101. | (val << 12) | (val << 22);
  102. if (!manual_mode) usb_joystick_send();
  103. }
  104. inline void hat(int dir) {
  105. uint32_t val = 0;
  106. if (dir < 0) val = 15;
  107. else if (dir < 23) val = 0;
  108. else if (dir < 68) val = 1;
  109. else if (dir < 113) val = 2;
  110. else if (dir < 158) val = 3;
  111. else if (dir < 203) val = 4;
  112. else if (dir < 245) val = 5;
  113. else if (dir < 293) val = 6;
  114. else if (dir < 338) val = 7;
  115. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFFFFFF0) | val;
  116. if (!manual_mode) usb_joystick_send();
  117. }
  118. #elif JOYSTICK_SIZE == 64
  119. void button(unsigned int num, bool val) {
  120. if (--num >= 128) return;
  121. uint32_t *p = usb_joystick_data + (num >> 5);
  122. num &= 0x1F;
  123. if (val) *p |= (1 << num);
  124. else *p &= ~(1 << num);
  125. if (!manual_mode) usb_joystick_send();
  126. }
  127. void X(unsigned int position) { analog16(0, position); }
  128. void Y(unsigned int position) { analog16(1, position); }
  129. void Z(unsigned int position) { analog16(2, position); }
  130. void Xrotate(unsigned int position) { analog16(3, position); }
  131. void Yrotate(unsigned int position) { analog16(4, position); }
  132. void Zrotate(unsigned int position) { analog16(5, position); }
  133. void slider(unsigned int num, unsigned int position) {
  134. if (--num >= 17) return;
  135. analog16(num + 6, position);
  136. }
  137. inline void hat(unsigned int num, int angle) {
  138. uint32_t val=15;
  139. if (angle > 0 && angle < 23) val = 0;
  140. else if (angle < 68) val = 1;
  141. else if (angle < 113) val = 2;
  142. else if (angle < 158) val = 3;
  143. else if (angle < 203) val = 4;
  144. else if (angle < 245) val = 5;
  145. else if (angle < 293) val = 6;
  146. else if (angle < 338) val = 7;
  147. else if (angle < 360) val = 0;
  148. uint32_t *p = usb_joystick_data;
  149. switch(num) {
  150. case 1:
  151. p[15] = (p[15] & 0xFFF0FFFF) | (val << 16); break;
  152. case 2:
  153. p[15] = (p[15] & 0xFF0FFFFF) | (val << 20); break;
  154. case 3:
  155. p[15] = (p[15] & 0xF0FFFFFF) | (val << 24); break;
  156. case 4:
  157. p[15] = (p[15] & 0x0FFFFFFF) | (val << 28); break;
  158. default:
  159. return;
  160. }
  161. if (!manual_mode) usb_joystick_send();
  162. }
  163. #endif
  164. void useManualSend(bool mode) {
  165. manual_mode = mode;
  166. }
  167. void send_now(void) {
  168. usb_joystick_send();
  169. }
  170. private:
  171. static uint8_t manual_mode;
  172. #if JOYSTICK_SIZE == 64
  173. void analog16(unsigned int num, unsigned int value) {
  174. if (value > 0xFFFF) value = 0xFFFF;
  175. uint16_t *p = (uint16_t *)(&usb_joystick_data[4]);
  176. p[num] = value;
  177. if (!manual_mode) usb_joystick_send();
  178. }
  179. #endif
  180. };
  181. extern usb_joystick_class Joystick;
  182. #endif // __cplusplus
  183. #endif // JOYSTICK_INTERFACE
  184. #endif // USBjoystick_h_