Last active
May 23, 2019 02:38
-
-
Save kamicane/3cf61e1eb6f27fe8d41c0f12256e8208 to your computer and use it in GitHub Desktop.
[Sega Joystick to DirectInput] #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
#include <SegaController.h> | |
#include <Joystick.h> | |
#define USE_DEBUG 1 | |
// Controller DB9 pins (looking face-on to the end of the plug): | |
// | |
// 5 4 3 2 1 | |
// 9 8 7 6 | |
// Connect pin 5 to +5V and pin 8 to GND | |
// Connect the remaining pins to digital I/O pins (see below) | |
// Specify the Arduino pins that are connected to | |
// DB9 Pin 7, DB9 Pin 1, DB9 Pin 2, DB9 Pin 3, DB9 Pin 4, DB9 Pin 6, DB9 Pin 9 | |
// 07 01 02 03 04 06 09 | |
// 04 15 18 20 03 19 02 | |
SegaController controller(4, 15, 18, 20, 3, 19, 2); | |
Joystick_ gamepad(JOYSTICK_DEFAULT_REPORT_ID, | |
JOYSTICK_TYPE_GAMEPAD, 8, | |
1, | |
false, false, false, false, false, false, | |
false, false, false, false, false); | |
word currentState = 0; | |
word lastState = 0; | |
void setup () { | |
Serial.begin(9600); | |
gamepad.begin(false); | |
} | |
void loop () { | |
currentState = controller.getState(); | |
if (currentState != lastState) { | |
if (USE_DEBUG && Serial.available()) { | |
Serial.print((currentState & SC_CTL_ON) ? "+" : "-"); | |
Serial.print((currentState & SC_BTN_UP) ? "U" : "0"); | |
Serial.print((currentState & SC_BTN_DOWN) ? "D" : "0"); | |
Serial.print((currentState & SC_BTN_LEFT) ? "L" : "0"); | |
Serial.print((currentState & SC_BTN_RIGHT) ? "R" : "0"); | |
Serial.print((currentState & SC_BTN_START) ? "S" : "0"); | |
Serial.print((currentState & SC_BTN_A) ? "A" : "0"); | |
Serial.print((currentState & SC_BTN_B) ? "B" : "0"); | |
Serial.print((currentState & SC_BTN_C) ? "C" : "0"); | |
Serial.print((currentState & SC_BTN_X) ? "X" : "0"); | |
Serial.print((currentState & SC_BTN_Y) ? "Y" : "0"); | |
Serial.print((currentState & SC_BTN_Z) ? "Z" : "0"); | |
Serial.print((currentState & SC_BTN_MODE) ? "M" : "0"); | |
Serial.print("\n"); | |
} | |
if ((currentState & SC_BTN_UP) && (currentState & SC_BTN_RIGHT)) { | |
gamepad.setHatSwitch(0, 45); | |
} else if ((currentState & SC_BTN_UP) && (currentState & SC_BTN_LEFT)) { | |
gamepad.setHatSwitch(0, 315); | |
} else if ((currentState & SC_BTN_DOWN) && (currentState & SC_BTN_RIGHT)) { | |
gamepad.setHatSwitch(0, 135); | |
} else if ((currentState & SC_BTN_DOWN) && (currentState & SC_BTN_LEFT)) { | |
gamepad.setHatSwitch(0, 225); | |
} else if (currentState & SC_BTN_UP) { | |
gamepad.setHatSwitch(0, 0); | |
} else if (currentState & SC_BTN_DOWN) { | |
gamepad.setHatSwitch(0, 180); | |
} else if (currentState & SC_BTN_LEFT) { | |
gamepad.setHatSwitch(0, 270); | |
} else if (currentState & SC_BTN_RIGHT) { | |
gamepad.setHatSwitch(0, 90); | |
} else { | |
gamepad.setHatSwitch(0, JOYSTICK_HATSWITCH_RELEASE); | |
} | |
gamepad.setButton(0, (currentState & SC_BTN_A) ? 1 : 0); | |
gamepad.setButton(1, (currentState & SC_BTN_B) ? 1 : 0); | |
gamepad.setButton(2, (currentState & SC_BTN_C) ? 1 : 0); | |
gamepad.setButton(3, (currentState & SC_BTN_X) ? 1 : 0); | |
gamepad.setButton(4, (currentState & SC_BTN_Y) ? 1 : 0); | |
gamepad.setButton(5, (currentState & SC_BTN_Z) ? 1 : 0); | |
gamepad.setButton(6, (currentState & SC_BTN_START) ? 1 : 0); | |
gamepad.setButton(7, (currentState & SC_BTN_MODE) ? 1 : 0); | |
gamepad.sendState(); | |
lastState = currentState; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment