選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

usb_joystick.h 4.4KB

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