Skip to content

Commit 3448fd9

Browse files
authored
Merge pull request #1 from dmadison/examples
Examples
2 parents 468f140 + 1957030 commit 3448fd9

4 files changed

Lines changed: 341 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Project Teensy XInput Library
3+
* @author David Madison
4+
* @link github.com/dmadison/TeensyXInput
5+
* @license MIT - Copyright (c) 2018 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: BasicGamepad
26+
* Description: Sets up a basic NES-style gamepad using the XInput library,
27+
* and shows how to use the class functions.
28+
*
29+
*/
30+
31+
#include <XInput.h>
32+
33+
// Directional Pad Pins
34+
const int Pin_DpadUp = 0;
35+
const int Pin_DpadDown = 1;
36+
const int Pin_DpadLeft = 2;
37+
const int Pin_DpadRight = 3;
38+
39+
// Button Pins
40+
const int Pin_ButtonA = 4;
41+
const int Pin_ButtonB = 5;
42+
43+
// Note: Buttons should be connected from the sensor pin (above) to ground
44+
// when pressed
45+
46+
void setup() {
47+
// Set buttons as inputs, using internal pull-up resistors
48+
pinMode(Pin_DpadUp, INPUT_PULLUP);
49+
pinMode(Pin_DpadDown, INPUT_PULLUP);
50+
pinMode(Pin_DpadLeft, INPUT_PULLUP);
51+
pinMode(Pin_DpadRight, INPUT_PULLUP);
52+
53+
pinMode(Pin_ButtonA, INPUT_PULLUP);
54+
pinMode(Pin_ButtonB, INPUT_PULLUP);
55+
}
56+
57+
void loop() {
58+
// Read pin values and store in variables
59+
// (Note the "!" to invert the state, because LOW = pressed)
60+
boolean dpadUp = !digitalRead(Pin_DpadUp);
61+
boolean dpadDown = !digitalRead(Pin_DpadDown);
62+
boolean dpadLeft = !digitalRead(Pin_DpadLeft);
63+
boolean dpadRight = !digitalRead(Pin_DpadRight);
64+
65+
boolean buttonA = !digitalRead(Pin_ButtonA);
66+
boolean buttonB = !digitalRead(Pin_ButtonB);
67+
68+
// Set XInput DPAD values
69+
XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight);
70+
71+
// Set XInput buttons
72+
XInput.setButton(BUTTON_A, buttonA);
73+
XInput.setButton(BUTTON_B, buttonB);
74+
75+
// Send control data to the computer
76+
XInput.send();
77+
78+
// Receive data (player number, rumble motors, etc.)
79+
XInput.receive();
80+
}

examples/Blink/Blink.ino

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Project Teensy XInput Library
3+
* @author David Madison
4+
* @link github.com/dmadison/TeensyXInput
5+
* @license MIT - Copyright (c) 2018 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: Blink
26+
* Description: Using XInput, presses and then releases the "A" button
27+
* every two seconds. Good for testing that the XInput
28+
* library is working correctly.
29+
*/
30+
31+
#include <XInput.h>
32+
33+
void setup() {
34+
35+
}
36+
37+
void loop() {
38+
XInput.press(BUTTON_A);
39+
XInput.send();
40+
delay(1000);
41+
42+
XInput.release(BUTTON_A);
43+
XInput.send();
44+
delay(1000);
45+
}

examples/TestAll/TestAll.ino

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Project Teensy XInput Library
3+
* @author David Madison
4+
* @link github.com/dmadison/TeensyXInput
5+
* @license MIT - Copyright (c) 2018 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: TestAll
26+
* Description: Automatically activate all possible XInput controls.
27+
* Useful to test that everything is functioning properly.
28+
*
29+
* WARNING: This will spam inputs! Ground pin '0' to stop.
30+
*
31+
*/
32+
33+
#include <XInput.h>
34+
35+
// Config Settings
36+
const unsigned long CycleTime = 5000; // ms
37+
const int SafetyPin = 0; // Ground this pin to prevent inputs
38+
39+
// Button Setup
40+
const int NumButtons = 10;
41+
const int Buttons[NumButtons] = {
42+
BUTTON_A,
43+
BUTTON_B,
44+
BUTTON_X,
45+
BUTTON_Y,
46+
BUTTON_LB,
47+
BUTTON_RB,
48+
BUTTON_BACK,
49+
BUTTON_START,
50+
BUTTON_L3,
51+
BUTTON_R3,
52+
};
53+
const unsigned long ButtonDuration = CycleTime / NumButtons;
54+
unsigned long buttonTimeLast = 0;
55+
int buttonID = 0;
56+
57+
// DPad Setup
58+
const int NumDirections = 4;
59+
const unsigned long DPadDuration = CycleTime / NumDirections;
60+
unsigned long dpadTimeLast = 0;
61+
int dpadPosition = 0;
62+
63+
// Triggers
64+
const int TriggerMax = 255; // uint8_t max
65+
const unsigned long TriggerDuration = CycleTime / (TriggerMax * 2); // Go up and down
66+
unsigned long triggerTimeLast = 0;
67+
uint8_t triggerVal = 0;
68+
boolean triggerDirection = 0;
69+
70+
// Joystick Setup
71+
const int JoyMax = 32767; // int16_t max
72+
const double angle_precision = (2 * PI) / (CycleTime / 4); // 4 because 250 Hz update rate
73+
double angle = 0.0;
74+
75+
void setup() {
76+
pinMode(SafetyPin, INPUT_PULLUP);
77+
}
78+
79+
void loop() {
80+
if (digitalRead(SafetyPin) == LOW) {
81+
return;
82+
}
83+
84+
unsigned long t = millis(); // Get timestamp for comparison
85+
86+
// DPad
87+
if (t - dpadTimeLast >= DPadDuration) { // If enough time has passed, change the dpad
88+
XInput.setDpad(dpadPosition == 0, dpadPosition == 1, dpadPosition == 2, dpadPosition == 3);
89+
90+
dpadPosition++; // Increment the dpad counter
91+
if (dpadPosition >= NumDirections) dpadPosition = 0; // Go back to 0 if we hit the limit
92+
dpadTimeLast = t; // Save time we last did this
93+
}
94+
95+
// Buttons
96+
if (t - buttonTimeLast >= ButtonDuration) { // If enough time has passed, change the button pressed
97+
for (int i = 0; i < NumButtons; i++) {
98+
XInput.release((XInputControl)Buttons[i]); // Relase all buttons
99+
}
100+
101+
XInput.press((XInputControl)Buttons[buttonID]); // Press the next button
102+
buttonID++; // Increment the button counter
103+
if (buttonID >= NumButtons) buttonID = 0; // Go back to 0 if we hit the limit
104+
105+
buttonTimeLast = t; // Save time we last did this
106+
}
107+
108+
// Triggers
109+
if (t - triggerTimeLast >= TriggerDuration) { // If enough time has passed, update the triggers
110+
XInput.setTrigger(TRIGGER_LEFT, triggerVal);
111+
XInput.setTrigger(TRIGGER_RIGHT, ~triggerVal); // Inverse
112+
113+
// Increment trigger value based on direction
114+
if (triggerDirection == 0) { triggerVal++; }
115+
else { triggerVal--; }
116+
117+
if (triggerVal == TriggerMax || triggerVal == 0) { // If we've hit the edge of the range
118+
triggerDirection = !triggerDirection; // Reverse direction
119+
}
120+
121+
triggerTimeLast = t; // Save time we last did this
122+
}
123+
124+
// Calculate joystick x/y values using trig
125+
int axis_x = sin(angle) * JoyMax;
126+
int axis_y = cos(angle) * JoyMax;
127+
128+
angle += angle_precision;
129+
if (angle >= 360) {
130+
angle -= 360;
131+
}
132+
133+
XInput.setJoystick(JOY_LEFT, axis_x, axis_y); // Clockwise
134+
XInput.setJoystick(JOY_RIGHT, -axis_x, axis_y); // Counter-clockwise
135+
136+
// Send values to PC
137+
XInput.send();
138+
139+
// Receive data from PC
140+
XInput.receive();
141+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Project Teensy XInput Library
3+
* @author David Madison
4+
* @link github.com/dmadison/TeensyXInput
5+
* @license MIT - Copyright (c) 2018 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: WiiClassicController
26+
* Description: Maps the inputs from a Wii Classic Controller to an emulated
27+
* Xbox 360 gamepad using XInput. Requires using the
28+
* NintendoExtensionCtrl library and an extension adapter.
29+
*/
30+
31+
#include <XInput.h>
32+
#include <NintendoExtensionCtrl.h> // Library for the Wii controller communication
33+
34+
ClassicController classic;
35+
36+
void setup() {
37+
classic.begin();
38+
39+
while (!classic.connect()) {
40+
delay(1000); // Controller not connected
41+
}
42+
}
43+
44+
void loop() {
45+
if(classic.update()) { // Get new data!
46+
XInput.setJoystick(JOY_LEFT, map((classic.leftJoyX() << 2), 0, 255, -32768, 32767), map((classic.leftJoyY() << 2), 0, 255, -32768, 32767));
47+
XInput.setJoystick(JOY_RIGHT, map((classic.rightJoyX() << 3), 0, 255, -32768, 32767), map((classic.rightJoyY() << 3), 0, 255, -32768, 32767));
48+
49+
XInput.setButton(BUTTON_A, classic.buttonB());
50+
XInput.setButton(BUTTON_B, classic.buttonA());
51+
XInput.setButton(BUTTON_X, classic.buttonY());
52+
XInput.setButton(BUTTON_Y, classic.buttonX());
53+
54+
XInput.setButton(BUTTON_START, classic.buttonPlus());
55+
XInput.setButton(BUTTON_BACK, classic.buttonMinus());
56+
XInput.setButton(BUTTON_LOGO, classic.buttonHome());
57+
58+
XInput.setDpad(classic.dpadUp(), classic.dpadDown(), classic.dpadLeft(), classic.dpadRight());
59+
60+
XInput.setTrigger(TRIGGER_LEFT, classic.triggerL() << 3);
61+
XInput.setTrigger(TRIGGER_RIGHT, classic.triggerR() << 3);
62+
63+
XInput.setButton(BUTTON_LB, classic.buttonZL());
64+
XInput.setButton(BUTTON_RB, classic.buttonZR());
65+
66+
// XInput.setButton(BUTTON_L3, classic.buttonZL()); // The Classic Controller doesn't have L3 and R3
67+
// XInput.setButton(BUTTON_R3, classic.buttonZR()); // but you can uncomment these to check that they work
68+
69+
XInput.send();
70+
XInput.receive();
71+
}
72+
else { // Data is bad :(
73+
classic.reconnect();
74+
}
75+
}

0 commit comments

Comments
 (0)