您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

193 行
6.2KB

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