-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxinput-gamepad.ino
59 lines (49 loc) · 1.7 KB
/
xinput-gamepad.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SNES-2-USB (XInput)
// ===================
//
// SNESpad example demonstrating
// SNES to XInput USB controller.
//
#include <SNESpad.h>
#include <XInput.h> // https://github.com/dmadison/ArduinoXInput#compatible-boards
// Pins for SNES controller
#define CLOCK_PIN 5
#define LATCH_PIN 6
#define DATA0_PIN 7
#define DATA1_PIN 8
#define IOSEL_PIN 9
SNESpad * snes = new SNESpad(CLOCK_PIN, LATCH_PIN, DATA0_PIN, DATA1_PIN, IOSEL_PIN);
void setup() {
// initialize snes controller reading
snes->begin(); // init snes gpio
snes->start(); // init snes read
// initialize X-input output
XInput.setRange(JOY_LEFT, 0, 255);
XInput.setAutoSend(false); // Wait for all controls before sending
XInput.begin();
}
void loop() {
// read SNES controller button state
snes->poll();
// map SNES controller button state
XInput.setButton(BUTTON_A, snes->buttonB);
XInput.setButton(BUTTON_B, snes->buttonA);
XInput.setButton(BUTTON_X, snes->buttonY);
XInput.setButton(BUTTON_Y, snes->buttonX);
XInput.setButton(BUTTON_LB, snes->buttonL);
XInput.setButton(BUTTON_RB, snes->buttonR);
XInput.setButton(BUTTON_BACK, snes->buttonSelect);
XInput.setButton(BUTTON_START, snes->buttonStart);
XInput.setDpad(snes->directionUp, snes->directionDown, snes->directionLeft, snes->directionRight);
if (snes->type == SNES_PAD_MOUSE) {
// maps mouse movement to analog
XInput.setJoystickX(JOY_LEFT, snes->mouseX, false);
XInput.setJoystickY(JOY_LEFT, snes->mouseY, true);
// maps mouse movement to d-pad
// int mouseX = snes->mouseX-127;
// int mouseY = snes->mouseY-127;
// XInput.setDpad(mouseY < 0, mouseY > 0, mouseX < 0, mouseX > 0);
}
// send xinput values to device
XInput.send();
}