Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project Rocket League XInput Demo
- * @author David Madison
- * @link partsnotincluded.com/tutorials/how-to-emulate-an-xbox-controller-with-arduino-xinput
- * @license MIT - Copyright (c) 2019 David Madison
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include <XInput.h>
- // Input Pins
- const uint8_t Pin_Joystick_X = A0; // Turn left/right, left
- const uint8_t Pin_Joystick_Y = A1; // look up/down, left
- const uint8_t Pin_Joystick_L_X = A2; // left joy Turn left/right, left
- const uint8_t Pin_Joystick_L_Y = A3; // left joy look up/down, left
- // const uint8_t Pin_Joystick_X = A0; // Turn left/right, RIGHT
- // const uint8_t Pin_Joystick_Y = A1; // look up/down, RIGHT
- const uint8_t Pin_ButtonA = 2; // Jump
- const uint8_t Pin_ButtonB = 3; // Jump
- const uint8_t Pin_ButtonX = 4; // Jump
- const uint8_t Pin_ButtonY = 5; // Jump
- const uint8_t Pin_DPAD_UP = 15; // up
- const uint8_t Pin_DPAD_DOWN = 14; // Jump
- const uint8_t Pin_DPAD_LEFT = 16; // Jump
- const uint8_t Pin_DPAD_RIGHT = 10; // Jump
- const uint8_t Pin_ButtonLB = 6;
- const uint8_t Pin_ButtonRB = 7;
- const uint8_t Pin_LT_TRIGGER = A8;
- const uint8_t Pin_RT_TRIGGER = A9;
- const boolean UseTriggerButtons = true;
- //const uint8_t Pin_TriggerR = 3; // Accelerate
- //const uint8_t Pin_TriggerL = 3; // Accelerate
- // Output Pins
- //const uint8_t Pin_LED = 10;
- // Analog Input Range
- const int AnalogRead_Max = 1023; // 10-bit ADC
- void setup() {
- // Set input pin modes
- pinMode(Pin_ButtonA, INPUT_PULLUP);
- pinMode(Pin_ButtonB, INPUT_PULLUP);
- pinMode(Pin_ButtonX, INPUT_PULLUP);
- pinMode(Pin_ButtonY, INPUT_PULLUP);
- pinMode(Pin_DPAD_UP, INPUT_PULLUP);
- pinMode(Pin_DPAD_DOWN, INPUT_PULLUP);
- pinMode(Pin_DPAD_LEFT, INPUT_PULLUP);
- pinMode(Pin_DPAD_RIGHT, INPUT_PULLUP);
- pinMode(Pin_ButtonLB, INPUT_PULLUP);
- pinMode(Pin_ButtonRB, INPUT_PULLUP);
- if (UseTriggerButtons == true) {
- pinMode(Pin_LT_TRIGGER, INPUT_PULLUP);
- pinMode(Pin_RT_TRIGGER, INPUT_PULLUP);
- }
- // Set output pin mode
- // pinMode(Pin_LED, OUTPUT);
- // digitalWrite(Pin_LED, LOW); // Turn 'off'
- // Setup library
- XInput.setRange(JOY_RIGHT, 0, 1023);
- XInput.setRange(JOY_LEFT, 0, 1023);
- XInput.setJoystick(JOY_RIGHT, 0, 0);
- XInput.setJoystick(JOY_LEFT, 0, 0);
- //XInput.setTriggerRange(0, 255);
- //XInput.setTrigger(TRIGGER_LEFT, 0);
- //XInput.setTrigger(TRIGGER_RIGHT, 0);
- //XInput.setTrigger(TRIGGER_LEFT, 1023);
- //XInput.setTrigger(TRIGGER_RIGHT, 1023);
- XInput.begin();
- }
- void loop() {
- // Read pin states
- boolean pressA = !digitalRead(Pin_ButtonA);
- boolean pressB = !digitalRead(Pin_ButtonB);
- boolean pressX = !digitalRead(Pin_ButtonX);
- boolean pressY = !digitalRead(Pin_ButtonY);
- boolean pressDPAD_UP = !digitalRead(Pin_DPAD_UP);
- boolean pressDPAD_DOWN = !digitalRead(Pin_DPAD_DOWN);
- boolean pressDPAD_LEFT = !digitalRead(Pin_DPAD_LEFT);
- boolean pressDPAD_RIGHT = !digitalRead(Pin_DPAD_RIGHT);
- boolean pressLB = !digitalRead(Pin_ButtonLB);
- boolean pressRB = !digitalRead(Pin_ButtonRB);
- //boolean pressTrigger = !digitalRead(Pin_TriggerR);
- int joystickValue_X = analogRead(Pin_Joystick_X);
- int joystickValue_L_X = analogRead(Pin_Joystick_L_X);
- int joystickValue_Y = analogRead(Pin_Joystick_Y);
- int joystickValue_L_Y = analogRead(Pin_Joystick_L_Y);
- //int triggerLeft = analogRead(Pin_LT_TRIGGER);
- //int triggerRight = analogRead(Pin_RT_TRIGGER);
- // Set button and trigger states
- XInput.setButton(BUTTON_A, pressA);
- XInput.setButton(BUTTON_B, pressB);
- XInput.setButton(BUTTON_X, pressX);
- XInput.setButton(BUTTON_Y, pressY);
- XInput.setButton(DPAD_UP, pressDPAD_UP);
- XInput.setButton(DPAD_DOWN, pressDPAD_DOWN);
- XInput.setButton(DPAD_LEFT, pressDPAD_LEFT);
- XInput.setButton(DPAD_RIGHT, pressDPAD_RIGHT);
- XInput.setButton(BUTTON_LB, pressLB);
- XInput.setButton(BUTTON_RB, pressRB);
- // XInput.setButton(TRIGGER_RIGHT, pressTrigger);
- XInput.setJoystick(JOY_LEFT, joystickValue_L_X, joystickValue_L_Y);
- XInput.setJoystick(JOY_RIGHT, joystickValue_X, joystickValue_Y); // move x, leave y centered
- if (UseTriggerButtons == true) {
- // Read trigger buttons
- boolean triggerLeft = !digitalRead(Pin_LT_TRIGGER);
- boolean triggerRight = !digitalRead(Pin_RT_TRIGGER);
- // Set the triggers as if they were buttons
- XInput.setButton(TRIGGER_LEFT, triggerLeft);
- XInput.setButton(TRIGGER_RIGHT, triggerRight);
- }
- //XInput.setTrigger(TRIGGER_LEFT, triggerLeft);
- // XInput.setTrigger(TRIGGER_RIGHT, triggerRight);
- // Get rumble value
- //uint16_t rumble = XInput.getRumble();
- // If controller is rumbling, turn on LED
- //if (rumble > 0) {
- // digitalWrite(Pin_LED, HIGH);
- //}
- //else {
- //digitalWrite(Pin_LED, LOW);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement