Advertisement
Guest User

Untitled

a guest
Sep 25th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. /*
  2. * Project Rocket League XInput Demo
  3. * @author David Madison
  4. * @link partsnotincluded.com/tutorials/how-to-emulate-an-xbox-controller-with-arduino-xinput
  5. * @license MIT - Copyright (c) 2019 David Madison
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. */
  26.  
  27. #include <XInput.h>
  28.  
  29. // Input Pins
  30. const uint8_t Pin_Joystick_X = A0; // Turn left/right, left
  31. const uint8_t Pin_Joystick_Y = A1; // look up/down, left
  32. const uint8_t Pin_Joystick_L_X = A2; // left joy Turn left/right, left
  33. const uint8_t Pin_Joystick_L_Y = A3; // left joy look up/down, left
  34. // const uint8_t Pin_Joystick_X = A0; // Turn left/right, RIGHT
  35. // const uint8_t Pin_Joystick_Y = A1; // look up/down, RIGHT
  36. const uint8_t Pin_ButtonA = 2; // Jump
  37. const uint8_t Pin_ButtonB = 3; // Jump
  38. const uint8_t Pin_ButtonX = 4; // Jump
  39. const uint8_t Pin_ButtonY = 5; // Jump
  40. const uint8_t Pin_DPAD_UP = 15; // up
  41. const uint8_t Pin_DPAD_DOWN = 14; // Jump
  42. const uint8_t Pin_DPAD_LEFT = 16; // Jump
  43. const uint8_t Pin_DPAD_RIGHT = 10; // Jump
  44. const uint8_t Pin_ButtonLB = 6;
  45. const uint8_t Pin_ButtonRB = 7;
  46. const uint8_t Pin_LT_TRIGGER = A8;
  47. const uint8_t Pin_RT_TRIGGER = A9;
  48.  
  49. const boolean UseTriggerButtons = true;
  50.  
  51. //const uint8_t Pin_TriggerR = 3; // Accelerate
  52. //const uint8_t Pin_TriggerL = 3; // Accelerate
  53.  
  54.  
  55. // Output Pins
  56. //const uint8_t Pin_LED = 10;
  57.  
  58. // Analog Input Range
  59. const int AnalogRead_Max = 1023; // 10-bit ADC
  60.  
  61. void setup() {
  62. // Set input pin modes
  63. pinMode(Pin_ButtonA, INPUT_PULLUP);
  64. pinMode(Pin_ButtonB, INPUT_PULLUP);
  65. pinMode(Pin_ButtonX, INPUT_PULLUP);
  66. pinMode(Pin_ButtonY, INPUT_PULLUP);
  67. pinMode(Pin_DPAD_UP, INPUT_PULLUP);
  68. pinMode(Pin_DPAD_DOWN, INPUT_PULLUP);
  69. pinMode(Pin_DPAD_LEFT, INPUT_PULLUP);
  70. pinMode(Pin_DPAD_RIGHT, INPUT_PULLUP);
  71. pinMode(Pin_ButtonLB, INPUT_PULLUP);
  72. pinMode(Pin_ButtonRB, INPUT_PULLUP);
  73.  
  74. if (UseTriggerButtons == true) {
  75. pinMode(Pin_LT_TRIGGER, INPUT_PULLUP);
  76. pinMode(Pin_RT_TRIGGER, INPUT_PULLUP);
  77. }
  78. // Set output pin mode
  79. // pinMode(Pin_LED, OUTPUT);
  80. // digitalWrite(Pin_LED, LOW); // Turn 'off'
  81.  
  82. // Setup library
  83. XInput.setRange(JOY_RIGHT, 0, 1023);
  84. XInput.setRange(JOY_LEFT, 0, 1023);
  85. XInput.setJoystick(JOY_RIGHT, 0, 0);
  86. XInput.setJoystick(JOY_LEFT, 0, 0);
  87. //XInput.setTriggerRange(0, 255);
  88. //XInput.setTrigger(TRIGGER_LEFT, 0);
  89. //XInput.setTrigger(TRIGGER_RIGHT, 0);
  90. //XInput.setTrigger(TRIGGER_LEFT, 1023);
  91. //XInput.setTrigger(TRIGGER_RIGHT, 1023);
  92. XInput.begin();
  93. }
  94.  
  95. void loop() {
  96. // Read pin states
  97. boolean pressA = !digitalRead(Pin_ButtonA);
  98. boolean pressB = !digitalRead(Pin_ButtonB);
  99. boolean pressX = !digitalRead(Pin_ButtonX);
  100. boolean pressY = !digitalRead(Pin_ButtonY);
  101. boolean pressDPAD_UP = !digitalRead(Pin_DPAD_UP);
  102. boolean pressDPAD_DOWN = !digitalRead(Pin_DPAD_DOWN);
  103. boolean pressDPAD_LEFT = !digitalRead(Pin_DPAD_LEFT);
  104. boolean pressDPAD_RIGHT = !digitalRead(Pin_DPAD_RIGHT);
  105. boolean pressLB = !digitalRead(Pin_ButtonLB);
  106. boolean pressRB = !digitalRead(Pin_ButtonRB);
  107. //boolean pressTrigger = !digitalRead(Pin_TriggerR);
  108. int joystickValue_X = analogRead(Pin_Joystick_X);
  109. int joystickValue_L_X = analogRead(Pin_Joystick_L_X);
  110. int joystickValue_Y = analogRead(Pin_Joystick_Y);
  111. int joystickValue_L_Y = analogRead(Pin_Joystick_L_Y);
  112. //int triggerLeft = analogRead(Pin_LT_TRIGGER);
  113. //int triggerRight = analogRead(Pin_RT_TRIGGER);
  114. // Set button and trigger states
  115.  
  116. XInput.setButton(BUTTON_A, pressA);
  117. XInput.setButton(BUTTON_B, pressB);
  118. XInput.setButton(BUTTON_X, pressX);
  119. XInput.setButton(BUTTON_Y, pressY);
  120. XInput.setButton(DPAD_UP, pressDPAD_UP);
  121. XInput.setButton(DPAD_DOWN, pressDPAD_DOWN);
  122. XInput.setButton(DPAD_LEFT, pressDPAD_LEFT);
  123. XInput.setButton(DPAD_RIGHT, pressDPAD_RIGHT);
  124. XInput.setButton(BUTTON_LB, pressLB);
  125. XInput.setButton(BUTTON_RB, pressRB);
  126. // XInput.setButton(TRIGGER_RIGHT, pressTrigger);
  127. XInput.setJoystick(JOY_LEFT, joystickValue_L_X, joystickValue_L_Y);
  128. XInput.setJoystick(JOY_RIGHT, joystickValue_X, joystickValue_Y); // move x, leave y centered
  129. if (UseTriggerButtons == true) {
  130. // Read trigger buttons
  131. boolean triggerLeft = !digitalRead(Pin_LT_TRIGGER);
  132. boolean triggerRight = !digitalRead(Pin_RT_TRIGGER);
  133.  
  134. // Set the triggers as if they were buttons
  135. XInput.setButton(TRIGGER_LEFT, triggerLeft);
  136. XInput.setButton(TRIGGER_RIGHT, triggerRight);
  137. }
  138.  
  139. //XInput.setTrigger(TRIGGER_LEFT, triggerLeft);
  140. // XInput.setTrigger(TRIGGER_RIGHT, triggerRight);
  141. // Get rumble value
  142. //uint16_t rumble = XInput.getRumble();
  143.  
  144. // If controller is rumbling, turn on LED
  145. //if (rumble > 0) {
  146. // digitalWrite(Pin_LED, HIGH);
  147. //}
  148. //else {
  149. //digitalWrite(Pin_LED, LOW);
  150. //}
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement