Skip to content

Commit 128e0f7

Browse files
committed
Added missing controls to gamepad example
This is no longer a "Basic" example, so it's been renamed
1 parent d49eb6f commit 128e0f7

2 files changed

Lines changed: 187 additions & 77 deletions

File tree

examples/BasicGamepad/BasicGamepad.ino

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Project Arduino XInput Library
3+
* @author David Madison
4+
* @link github.com/dmadison/ArduinoXInput
5+
* @license MIT - Copyright (c) 2019 David Madison
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*
25+
* Example: GamepadPins
26+
* Description: Uses all of the available pin inputs to build a 'complete'
27+
* Xbox gamepad, with both analog joysticks, both triggers,
28+
* and all of the main buttons.
29+
*
30+
* * Joysticks should be your typical 10k dual potentiometers.
31+
* * Triggers can be either analog (pots) or digital (buttons).
32+
* Set the 'TriggerButtons' variable to change between the two.
33+
* * Buttons use the internal pull-ups and should be connected
34+
* directly to ground.
35+
*
36+
* These pins are designed around the Leonardo's layout. You
37+
* may need to change the pin numbers if you're using a
38+
* different board type
39+
*
40+
*/
41+
42+
#include <XInput.h>
43+
44+
// Range Setup
45+
const int ADC_Max = 1023; // 10 bit
46+
const boolean UsingTriggerButtons = true; // set to false if using analog triggers
47+
48+
// Joystick Pins
49+
const int Pin_LeftJoyX = A0;
50+
const int Pin_LeftJoyY = A1;
51+
const int Pin_RightJoyX = A2;
52+
const int Pin_RightJoyY = A3;
53+
54+
// Trigger Pins
55+
const int Pin_TriggerL = A4;
56+
const int Pin_TriggerR = A5;
57+
58+
// Button Pins
59+
const int Pin_ButtonA = 0;
60+
const int Pin_ButtonB = 1;
61+
const int Pin_ButtonX = 2;
62+
const int Pin_ButtonY = 3;
63+
64+
const int Pin_ButtonLB = 4;
65+
const int Pin_ButtonRB = 5;
66+
67+
const int Pin_ButtonBack = 6;
68+
const int Pin_ButtonStart = 7;
69+
70+
const int Pin_ButtonL3 = 8;
71+
const int Pin_ButtonR3 = 9;
72+
73+
// Directional Pad Pins
74+
const int Pin_DpadUp = 10;
75+
const int Pin_DpadDown = 11;
76+
const int Pin_DpadLeft = 12;
77+
const int Pin_DpadRight = 13;
78+
79+
void setup() {
80+
// If using buttons for the triggers, use internal pull-up resistors
81+
if (UsingTriggerButtons == true) {
82+
pinMode(Pin_TriggerL, INPUT_PULLUP);
83+
pinMode(Pin_TriggerR, INPUT_PULLUP);
84+
}
85+
// If using potentiometers for the triggers, set range
86+
else {
87+
XInput.setTriggerRange(0, ADC_Max);
88+
}
89+
90+
// Set buttons as inputs, using internal pull-up resistors
91+
pinMode(Pin_ButtonA, INPUT_PULLUP);
92+
pinMode(Pin_ButtonB, INPUT_PULLUP);
93+
pinMode(Pin_ButtonX, INPUT_PULLUP);
94+
pinMode(Pin_ButtonY, INPUT_PULLUP);
95+
96+
pinMode(Pin_ButtonLB, INPUT_PULLUP);
97+
pinMode(Pin_ButtonRB, INPUT_PULLUP);
98+
99+
pinMode(Pin_ButtonBack, INPUT_PULLUP);
100+
pinMode(Pin_ButtonStart, INPUT_PULLUP);
101+
102+
pinMode(Pin_ButtonL3, INPUT_PULLUP);
103+
pinMode(Pin_ButtonR3, INPUT_PULLUP);
104+
105+
pinMode(Pin_DpadUp, INPUT_PULLUP);
106+
pinMode(Pin_DpadDown, INPUT_PULLUP);
107+
pinMode(Pin_DpadLeft, INPUT_PULLUP);
108+
pinMode(Pin_DpadRight, INPUT_PULLUP);
109+
110+
// Set joystick range to the ADC max
111+
XInput.setJoystickRange(0, ADC_Max);
112+
}
113+
114+
void loop() {
115+
// Read pin values and store in variables
116+
// (Note the "!" to invert the state, because LOW = pressed)
117+
boolean buttonA = !digitalRead(Pin_ButtonA);
118+
boolean buttonB = !digitalRead(Pin_ButtonB);
119+
boolean buttonX = !digitalRead(Pin_ButtonX);
120+
boolean buttonY = !digitalRead(Pin_ButtonY);
121+
122+
boolean buttonLB = !digitalRead(Pin_ButtonLB);
123+
boolean buttonRB = !digitalRead(Pin_ButtonRB);
124+
125+
boolean buttonBack = !digitalRead(Pin_ButtonBack);
126+
boolean buttonStart = !digitalRead(Pin_ButtonStart);
127+
128+
boolean buttonL3 = !digitalRead(Pin_ButtonL3);
129+
boolean buttonR3 = !digitalRead(Pin_ButtonR3);
130+
131+
boolean dpadUp = !digitalRead(Pin_DpadUp);
132+
boolean dpadDown = !digitalRead(Pin_DpadDown);
133+
boolean dpadLeft = !digitalRead(Pin_DpadLeft);
134+
boolean dpadRight = !digitalRead(Pin_DpadRight);
135+
136+
// Set XInput buttons
137+
XInput.setButton(BUTTON_A, buttonA);
138+
XInput.setButton(BUTTON_B, buttonB);
139+
XInput.setButton(BUTTON_X, buttonX);
140+
XInput.setButton(BUTTON_Y, buttonY);
141+
142+
XInput.setButton(BUTTON_LB, buttonLB);
143+
XInput.setButton(BUTTON_RB, buttonRB);
144+
145+
XInput.setButton(BUTTON_BACK, buttonBack);
146+
XInput.setButton(BUTTON_START, buttonStart);
147+
148+
XInput.setButton(BUTTON_L3, buttonL3);
149+
XInput.setButton(BUTTON_R3, buttonR3);
150+
151+
// Set XInput DPAD values
152+
XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight);
153+
154+
// Set XInput trigger values
155+
if (UsingTriggerButtons == true) {
156+
// Read trigger buttons
157+
boolean triggerLeft = !digitalRead(Pin_TriggerL);
158+
boolean triggerRight = !digitalRead(Pin_TriggerR);
159+
160+
// Set the triggers as if they were buttons
161+
XInput.setButton(TRIGGER_LEFT, triggerLeft);
162+
XInput.setButton(TRIGGER_RIGHT, triggerRight);
163+
}
164+
else {
165+
// Read trigger potentiometer values
166+
int triggerLeft = analogRead(Pin_TriggerL);
167+
int triggerRight = analogRead(Pin_TriggerR);
168+
169+
// Set the trigger values as analog
170+
XInput.setTrigger(TRIGGER_LEFT, triggerLeft);
171+
XInput.setTrigger(TRIGGER_RIGHT, triggerRight);
172+
}
173+
174+
// Read analog joystick values
175+
int leftJoyX = analogRead(Pin_LeftJoyX);
176+
int leftJoyY = analogRead(Pin_LeftJoyY);
177+
178+
int rightJoyX = analogRead(Pin_RightJoyX);
179+
int rightJoyY = analogRead(Pin_RightJoyY);
180+
181+
// Set XInput joystick values
182+
XInput.setJoystick(JOY_LEFT, leftJoyX, leftJoyY);
183+
XInput.setJoystick(JOY_RIGHT, rightJoyX, rightJoyY);
184+
185+
// Send control data to the computer
186+
XInput.send();
187+
}

0 commit comments

Comments
 (0)