-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamepad.ino
177 lines (134 loc) · 5.09 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <XInput.h>
// Setup
const boolean UseLeftJoystick = true; // set to true to enable left joystick
const boolean InvertLeftYAxis = true; // set to true to use inverted left joy Y
const boolean InvertLeftXAxis = true;
const boolean UseRightJoystick = false; // set to true to enable right joystick
const boolean InvertRightYAxis = false; // set to true to use inverted right joy Y
const boolean UseTriggerButtons = false; // set to false if using analog triggers
const int ADC_Max = 1023; // 10 bit
// Joystick Pins
const int Pin_LeftJoyX = A0;
const int Pin_LeftJoyY = A1;
const int Pin_RightJoyX = A2;
const int Pin_RightJoyY = A3;
const int Pin_ButtonL3 = 0;
const int Pin_ButtonR3 = 1;
// Button Pins
const int Pin_ButtonA = 5;
const int Pin_ButtonB = 4;
const int Pin_ButtonX = 3;
const int Pin_ButtonY = 2;
const int Pin_ButtonLB = 6;
const int Pin_ButtonRB = 7;
const int Pin_ButtonBACK = 8;
const int Pin_ButtonSTART = 9;
const int Pin_DpadUP = 10;
const int Pin_DpadDOWN = 11;
const int Pin_DpadLEFT = 12;
const int Pin_DpadRIGHT = 13;
// Trigger Pins
const int Pin_TriggerL = A4;
const int Pin_TriggerR = A5;
void setup() {
// Set buttons as inputs, using internal pull-up resistors
if (UseTriggerButtons == true) {
pinMode(Pin_TriggerL, INPUT_PULLUP);
pinMode(Pin_TriggerR, INPUT_PULLUP);
}
// If using potentiometers for the triggers, set range
else {
XInput.setTriggerRange(0, ADC_Max);
}
pinMode(Pin_ButtonA, INPUT_PULLUP);
pinMode(Pin_ButtonB, INPUT_PULLUP);
pinMode(Pin_ButtonX, INPUT_PULLUP);
pinMode(Pin_ButtonY, INPUT_PULLUP);
pinMode(Pin_ButtonLB, INPUT_PULLUP);
pinMode(Pin_ButtonRB, INPUT_PULLUP);
pinMode(Pin_ButtonBACK, INPUT_PULLUP);
pinMode(Pin_ButtonSTART, INPUT_PULLUP);
pinMode(Pin_ButtonL3, INPUT_PULLUP);
pinMode(Pin_ButtonR3, INPUT_PULLUP);
pinMode(Pin_DpadUP, INPUT_PULLUP);
pinMode(Pin_DpadDOWN, INPUT_PULLUP);
pinMode(Pin_DpadLEFT, INPUT_PULLUP);
pinMode(Pin_DpadRIGHT, INPUT_PULLUP);
XInput.setJoystickRange(0, ADC_Max); // Set joystick range to the ADC
XInput.setAutoSend(false); // Wait for all controls before sending
XInput.begin();
}
void loop() {
// Read pin values and store in variables
// (Note the "!" to invert the state, because LOW = pressed)
boolean buttonA = !digitalRead(Pin_ButtonA);
boolean buttonB = !digitalRead(Pin_ButtonB);
boolean buttonX = !digitalRead(Pin_ButtonX);
boolean buttonY = !digitalRead(Pin_ButtonY);
boolean buttonLB = !digitalRead(Pin_ButtonLB);
boolean buttonRB = !digitalRead(Pin_ButtonRB);
boolean buttonBack = !digitalRead(Pin_ButtonBack);
boolean buttonStart = !digitalRead(Pin_ButtonStart);
boolean buttonL3 = !digitalRead(Pin_ButtonL3);
boolean buttonR3 = !digitalRead(Pin_ButtonR3);
boolean dpadUp = !digitalRead(Pin_DpadUP);
boolean dpadDown = !digitalRead(Pin_DpadDOWN);
boolean dpadLeft = !digitalRead(Pin_DpadLEFT);
boolean dpadRight = !digitalRead(Pin_DpadRIGHT);
// Set XInput buttons
XInput.setButton(BUTTON_A, buttonA);
XInput.setButton(BUTTON_B, buttonB);
XInput.setButton(BUTTON_X, buttonX);
XInput.setButton(BUTTON_Y, buttonY);
XInput.setButton(BUTTON_LB, buttonLB);
XInput.setButton(BUTTON_RB, buttonRB);
XInput.setButton(BUTTON_BACK, buttonBack);
XInput.setButton(BUTTON_START, buttonStart);
XInput.setButton(BUTTON_L3, buttonL3);
XInput.setButton(BUTTON_R3, buttonR3);
// Set Dpad Values
XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight);
// Set Trigger values
if (UseTriggerButtons == true) {
// Read trigger buttons
boolean triggerLeft = !digitalRead(Pin_TriggerL);
boolean triggerRight = !digitalRead(Pin_TriggerR);
// Set the triggers as if they were buttons
XInput.setButton(TRIGGER_LEFT, triggerLeft);
XInput.setButton(TRIGGER_RIGHT, triggerRight);
}
else {
// Read trigger potentiometer values
int triggerLeft = analogRead(Pin_TriggerL);
int triggerRight = analogRead(Pin_TriggerR);
// Set the trigger values as analog
XInput.setTrigger(TRIGGER_LEFT, triggerLeft);
XInput.setTrigger(TRIGGER_RIGHT, triggerRight);
}
// Set left joystick
if (UseLeftJoystick == true) {
int leftJoyX = analogRead(Pin_LeftJoyX);
int leftJoyY = analogRead(Pin_LeftJoyY);
// White lie here... most generic joysticks are typically
// inverted by default. If the "Invert" variable is false
// then we need to do this transformation.
if (InvertLeftYAxis == false) {
leftJoyY = ADC_Max - leftJoyY;
}
if (InvertLeftXAxis == true) {
leftJoyX = ADC_Max - leftJoyX;
}
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
}
// Set right joystick
if (UseRightJoystick == true) {
int rightJoyX = analogRead(Pin_RightJoyX);
int rightJoyY = analogRead(Pin_RightJoyY);
if (InvertRightYAxis == false) {
rightJoyY = ADC_Max - rightJoyY;
}
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
}
// Send control data to the computer
XInput.send();
}