Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

usb_joystick.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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[3];
  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. void button(uint8_t button, bool val) {
  52. if (--button >= 32) return;
  53. if (val) usb_joystick_data[0] |= (1 << button);
  54. else usb_joystick_data[0] &= ~(1 << button);
  55. if (!manual_mode) usb_joystick_send();
  56. }
  57. void X(unsigned int val) {
  58. if (val > 1023) val = 1023;
  59. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFFFC00F) | (val << 4);
  60. if (!manual_mode) usb_joystick_send();
  61. }
  62. void Y(unsigned int val) {
  63. if (val > 1023) val = 1023;
  64. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFF003FFF) | (val << 14);
  65. if (!manual_mode) usb_joystick_send();
  66. }
  67. void position(unsigned int x, unsigned int y) {
  68. if (x > 1023) x = 1023;
  69. if (y > 1023) y = 1023;
  70. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFF00000)
  71. | (x << 4) | (y << 14);
  72. if (!manual_mode) usb_joystick_send();
  73. }
  74. void Z(unsigned int val) {
  75. if (val > 1023) val = 1023;
  76. usb_joystick_data[1] = (usb_joystick_data[1] & 0x00FFFFFF) | (val << 24);
  77. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFFFFFFC) | (val >> 8);
  78. if (!manual_mode) usb_joystick_send();
  79. }
  80. void Zrotate(unsigned int val) {
  81. if (val > 1023) val = 1023;
  82. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFFFF003) | (val << 2);
  83. if (!manual_mode) usb_joystick_send();
  84. }
  85. void sliderLeft(unsigned int val) {
  86. if (val > 1023) val = 1023;
  87. usb_joystick_data[2] = (usb_joystick_data[2] & 0xFFC00FFF) | (val << 12);
  88. if (!manual_mode) usb_joystick_send();
  89. }
  90. void sliderRight(unsigned int val) {
  91. if (val > 1023) val = 1023;
  92. usb_joystick_data[2] = (usb_joystick_data[2] & 0x003FFFFF) | (val << 22);
  93. if (!manual_mode) usb_joystick_send();
  94. }
  95. void slider(unsigned int val) {
  96. if (val > 1023) val = 1023;
  97. usb_joystick_data[2] = (usb_joystick_data[2] & 0x00000FFF)
  98. | (val << 12) | (val << 22);
  99. if (!manual_mode) usb_joystick_send();
  100. }
  101. inline void hat(int dir) {
  102. uint32_t val;
  103. if (dir < 0) val = 15;
  104. else if (dir < 23) val = 0;
  105. else if (dir < 68) val = 1;
  106. else if (dir < 113) val = 2;
  107. else if (dir < 158) val = 3;
  108. else if (dir < 203) val = 4;
  109. else if (dir < 245) val = 5;
  110. else if (dir < 293) val = 6;
  111. else if (dir < 338) val = 7;
  112. usb_joystick_data[1] = (usb_joystick_data[1] & 0xFFFFFFF0) | val;
  113. if (!manual_mode) usb_joystick_send();
  114. }
  115. void useManualSend(bool mode) {
  116. manual_mode = mode;
  117. }
  118. void send_now(void) {
  119. usb_joystick_send();
  120. }
  121. private:
  122. static uint8_t manual_mode;
  123. };
  124. extern usb_joystick_class Joystick;
  125. #endif // __cplusplus
  126. #endif // JOYSTICK_INTERFACE
  127. #endif // USBjoystick_h_