Last active
May 23, 2019 02:37
-
-
Save kamicane/c387ca15370811adcd9271c57238fd93 to your computer and use it in GitHub Desktop.
[psx-controller-xinput] PSX controller adapter to XInput with Arduino pro micro #arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://bitbucket.org/kamicane/psxpad/src/master/ | |
#include <PSXPad.h> | |
// https://github.com/dmadison/ArduinoXInput | |
#include <XInput.h> | |
const int ATT_PIN = 9; | |
PSXPad* pad; | |
void setup() { | |
// # PSXPad | |
// setup PSXPad SPI | |
PSXPad::setup(); | |
// create new pad connected on ATT_PIN (and SPI) | |
pad = new PSXPad(ATT_PIN); | |
// # XInput | |
XInput.setTriggerRange(0, 255); | |
XInput.setJoystickRange(0, 255); | |
XInput.setAutoSend(false); // Wait for all controls before sending | |
XInput.begin(); | |
} | |
void loop() { | |
// read rumble values from XInput | |
byte rumbleLeft = XInput.getRumbleLeft(); | |
byte rumbleRight = XInput.getRumbleRight(); | |
// save rumble values on pad | |
pad->setMotorLeft(rumbleLeft); | |
pad->setMotorRight(rumbleRight); | |
// query psx gamepad button states, and send rumble values | |
pad->query(); | |
// # Gamepad mode | |
// PSX_MODE_UNKNOWN = disconnected (most likely) | |
// PSX_MODE_DS1 = analog ds1 | |
// PSX_MODE_DS2 = analog ds2 (supports individual analog buttons) | |
const PSXPad_Mode mode = pad->getMode(); | |
if (mode == PSX_MODE_UNKNOWN) { | |
XInput.releaseAll(); | |
return; | |
} | |
XInput.setButton(BUTTON_A, pad->getButton(PSX_INPUT_CROSS)); | |
XInput.setButton(BUTTON_B, pad->getButton(PSX_INPUT_CIRCLE)); | |
XInput.setButton(BUTTON_X, pad->getButton(PSX_INPUT_SQUARE)); | |
XInput.setButton(BUTTON_Y, pad->getButton(PSX_INPUT_TRIANGLE)); | |
XInput.setButton(BUTTON_LB, pad->getButton(PSX_INPUT_L1)); | |
XInput.setButton(BUTTON_RB, pad->getButton(PSX_INPUT_R1)); | |
XInput.setButton(BUTTON_BACK, pad->getButton(PSX_INPUT_SELECT)); | |
XInput.setButton(BUTTON_START, pad->getButton(PSX_INPUT_START)); | |
XInput.setButton(BUTTON_L3, pad->getButton(PSX_INPUT_L3)); | |
XInput.setButton(BUTTON_R3, pad->getButton(PSX_INPUT_R3)); | |
XInput.setDpad(pad->getButton(PSX_INPUT_UP), pad->getButton(PSX_INPUT_DOWN), pad->getButton(PSX_INPUT_LEFT), pad->getButton(PSX_INPUT_RIGHT)); | |
// the following conditionals are unnecessary: PSXPad handles modes internally. | |
// in this case, in DS1 or DIGITAL modes, the values are either 0x00 (OFF) or 0xFF (ON) | |
// if (mode == PSX_MODE_DS2) { | |
XInput.setTrigger(TRIGGER_LEFT, pad->getAnalog(PSX_INPUT_L2)); | |
XInput.setTrigger(TRIGGER_RIGHT, pad->getAnalog(PSX_INPUT_R2)); | |
//} else { | |
// XInput.setButton(TRIGGER_LEFT, pad->getButton(PSX_INPUT_L2)); | |
// XInput.setButton(TRIGGER_RIGHT, pad->getButton(PSX_INPUT_R2)); | |
//} | |
// the following conditionals are unnecessary: PSXPad handles modes internally. | |
// in this case, in DIGITAL mode, the value returned from getAxis is 0x7F (middle position) | |
// if (mode == PSX_MODE_DS1 || mode == PSX_MODE_DS2) { | |
XInput.setJoystick(JOY_LEFT, pad->getAxis(PSX_INPUT_LX), 255 - pad->getAxis(PSX_INPUT_LY)); | |
XInput.setJoystick(JOY_RIGHT, pad->getAxis(PSX_INPUT_RX), 255 - pad->getAxis(PSX_INPUT_RY)); | |
// } else { | |
// XInput.setJoystick(JOY_LEFT, 0x7f, 0x7f); | |
// XInput.setJoystick(JOY_RIGHT, 0x7f, 0x7f); | |
// } | |
XInput.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment