Skip to content

Commit 2796936

Browse files
authored
Merge pull request #11 from dmadison/documentation
Documentation
2 parents 4038334 + c2ae381 commit 2796936

File tree

13 files changed

+260
-49
lines changed

13 files changed

+260
-49
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 David Madison
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
# Arduino XInput Library
1+
# Arduino XInput Library
2+
3+
This library lets you easily emulate an Xbox 360 controller using a USB-capable Arduino microcontroller.
4+
5+
## Getting Started
6+
7+
```cpp
8+
void setup() {
9+
XInput.begin();
10+
}
11+
12+
void loop() {
13+
XInput.press(BUTTON_A);
14+
delay(1000);
15+
16+
XInput.release(BUTTON_A);
17+
delay(1000);
18+
}
19+
```
20+
21+
Before the library will work, you must install a compatible boards file that contains the XInput USB descriptors, otherwise the microcontroller won't behave like an XInput device. **This is not optional**. See the [compatible boards](#compatible-boards) section below for more information.
22+
23+
After installing a compatible boards package, you must then [download and install the library](https://www.arduino.cc/en/guide/libraries). Once the XInput library is installed, open up the Arduino IDE and load an example sketch, located in `File -> Examples -> XInput` (I suggest trying the 'Blink' sketch first). Double-check that you have the correct XInput board selected in the 'Tools' menu, then upload the sketch to your microcontroller.
24+
25+
On Windows, you can test that the sketch is working properly by opening up the joystick control panel ([joy.cpl](https://support.microsoft.com/en-us/help/831361/how-to-troubleshoot-game-controllers-in-microsoft-games)) or by using [GamepadViewer.com](https://gamepadviewer.com/?p=1). If you uploaded the XInput 'Blink' example, the #1 button ('A') should be slowly turning on and off.
26+
27+
## Control Surfaces
28+
29+
The library gives you access to the following controls available on the Xbox 360 controller:
30+
* 10 + 1 Digital Buttons
31+
* 2 Analog Joysticks (16 bit)
32+
* 2 Analog Triggers (8 bit)
33+
* 1 Four-Way Directional Pad (D-Pad)
34+
35+
The library also processes received data, so you can read the status of the controller's 2 rumble motors (8-bit), the assigned player number (1-4), and the index of the current LED animation. Data is sent and received automatically over USB.
36+
37+
## Compatible Boards
38+
39+
To function as an XInput device, you *must* use a compatible boards package with XInput USB descriptors. **This is not optional**. Without these descriptors the library will only function in "debug" mode and the microcontroller will not behave as an XInput device.
40+
41+
The following boards packages are available:
42+
43+
* #### [Arduino AVR Core Boards](https://www.github.com/dmadison/ArduinoXInput_AVR)
44+
Modifies the Arduino AVR core to emulate an XInput device. Includes support for the Arduino Leonardo, Micro, Yun, and more.
45+
46+
* #### [SparkFun AVR Boards](https://www.github.com/dmadison/ArduinoXInput_SparkFun)
47+
Provides support for the MaKey MaKey, Pro Micro, Fio, Qduino Mini, and LilyPad USB Plus. Requires the XInput AVR Core boards.
48+
49+
* #### [Teensy 3 Boards](https://www.github.com/dmadison/ArduinoXInput_Teensy)
50+
Includes an 'XInput' USB mode for the Teensy 3.1, 3.2, 3.5, 3.6, and LC microcontrollers. Requires a preexisting Teensyduino installation.
51+
52+
For a complete list of available packages and compatible boards see [the 'supported boards' file](extras/SupportedBoards.md). For information on how to add support for another Arduino-compatible board with native USB support, see [the documentation on the USB API](extras/XInputUSB_API.md).
53+
54+
## License
55+
56+
This library is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT). See the [LICENSE](LICENSE) file for more information.

examples/Blink/Blink.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <XInput.h>
3232

3333
void setup() {
34-
34+
XInput.begin();
3535
}
3636

3737
void loop() {

examples/GamepadPins/GamepadPins.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ void setup() {
114114
pinMode(Pin_DpadLeft, INPUT_PULLUP);
115115
pinMode(Pin_DpadRight, INPUT_PULLUP);
116116

117-
// Set joystick range to the ADC max
118-
XInput.setJoystickRange(0, ADC_Max);
119-
117+
XInput.setJoystickRange(0, ADC_Max); // Set joystick range to the ADC
120118
XInput.setAutoSend(false); // Wait for all controls before sending
119+
120+
XInput.begin();
121121
}
122122

123123
void loop() {

examples/ReceiveCallback/ReceiveCallback.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void setup() {
4444
// Set callback function. Function must have a 'void' return type
4545
// and take a single uint8_t as an argument
4646
XInput.setReceiveCallback(rumbleCallback);
47+
48+
XInput.begin();
4749
}
4850

4951
void loop() {

examples/SimulateAll/SimulateAll.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ double angle = 0.0;
7575
void setup() {
7676
pinMode(SafetyPin, INPUT_PULLUP);
7777
XInput.setAutoSend(false); // Wait for all controls before sending
78+
79+
XInput.begin();
7880
}
7981

8082
void loop() {

examples/WiiClassicController/WiiClassicController.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void setup() {
4343

4444
XInput.setAutoSend(false); // Wait for all controls before sending
4545

46+
XInput.begin();
47+
4648
while (!classic.connect()) {
4749
delay(1000); // Controller not connected
4850
}

extras/SupportedBoards.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Supported Boards
2+
3+
Here is a complete list of all of the currently supported boards. Each header links to the respective repository where those board files are hosted. You *must* use an XInput specific board package for the library to function properly.
4+
5+
## [Arduino AVR Core Boards](https://github.com/dmadison/ArduinoXInput_AVR/)
6+
7+
* [Adafruit Circuit Playground 32u4](https://www.adafruit.com/product/3000)
8+
* [Arduino Esplora](https://store.arduino.cc/usa/arduino-esplora)
9+
* [Arduino Industrial 101](https://store.arduino.cc/usa/arduino-industrial-101)
10+
* [Arduino Leonardo](https://store.arduino.cc/usa/leonardo)
11+
* [Arduino Leonardo ETH](https://store.arduino.cc/usa/arduino-leonardo-eth)
12+
* [Arduino Micro](https://store.arduino.cc/usa/arduino-micro)
13+
* [Arduino Robot Control / Motor](https://store.arduino.cc/usa/arduino-robot)
14+
* [Arduino Yún](https://store.arduino.cc/usa/arduino-yun)
15+
* [Arduino Yún Mini](https://store.arduino.cc/usa/arduino-yun-mini)
16+
* [LilyPad Arduino USB](https://www.sparkfun.com/products/12049)
17+
* [Linino One](https://store.arduino.cc/usa/linino-one)
18+
19+
## [SparkFun AVR Boards](https://github.com/dmadison/ArduinoXInput_Sparkfun)
20+
(Note: requires the XInput AVR core)
21+
22+
* [MaKey MaKey](https://www.sparkfun.com/products/11511)
23+
* [Pro Micro 3.3V](https://www.sparkfun.com/products/10999)
24+
* [Pro Micro 5V](https://www.sparkfun.com/products/11098)
25+
* [Fio v3](https://www.sparkfun.com/products/11520)
26+
* [Qduino Mini](https://www.sparkfun.com/products/13614)
27+
* [LilyPad USB Plus](https://www.sparkfun.com/products/14346)
28+
29+
## [Teensy 3 Boards](https://github.com/dmadison/ArduinoXInput_Teensy/)
30+
31+
* [Teensy 3.6](https://www.pjrc.com/store/teensy36.html)
32+
* [Teensy 3.5](https://www.pjrc.com/store/teensy35.html)
33+
* [Teensy 3.1](https://www.pjrc.com/store/teensy31.html) / [3.2](https://www.pjrc.com/store/teensy32.html)
34+
* [Teensy LC](https://www.pjrc.com/teensy/teensyLC.html)

extras/XInputUSB_API.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Using the XInputUSB API
2+
3+
The Arduino XInput implementation is broken up into three separate components, of which this library is just one part:
4+
5+
1. **The Library**, which is user-facing and contains lots of syntactic sugar to make it easy to manipulate the USB data.
6+
2. **The Backend**, which handles the USB communication itself including endpoint memory management.
7+
3. **The USB API**, which provides a standardized interface between these two components.
8+
9+
The goal of this document is to describe how the USB API works so that you can use it to build XInput support for other board types while maintaining compatibility with this library.
10+
11+
---
12+
13+
## Overview
14+
15+
Here is the C++ API header in all of its glory:
16+
17+
```cpp
18+
#define USB_XINPUT
19+
20+
class XInputUSB {
21+
public:
22+
static bool connected(void);
23+
static uint8_t available(void);
24+
static int send(const void *buffer, uint8_t nbytes);
25+
static int recv(void *buffer, uint8_t nbytes);
26+
static void setRecvCallback(void(*callback)(void));
27+
};
28+
```
29+
30+
Note the `USB_XINPUT` preprocessor definition. This definition is what tells the library that the API is present and that it can send data over USB.
31+
32+
## Functions
33+
34+
The API consists of five core static functions of the `XInputUSB` class that pass the USB data back and forth from the bus to the library. Here is the breakdown:
35+
36+
### bool connected(void)
37+
38+
The `connected` function returns the state of the USB interface with respect to XInput. If the device is connected to a host and functional this should return `true`. If the device is disconnected this should return `false`.
39+
40+
### uint8_t available(void)
41+
42+
The `available` function returns the number of bytes currently stored in the USB data banks for the received control surface data. If there are no bytes available this function returns 0.
43+
44+
### int send(const void *buffer, uint8_t nbytes)
45+
46+
The `send` function is used to send control surface data to the host over USB. It takes two arguments:
47+
1. A pointer to the start of an array of data to be sent
48+
2. The number of bytes to be sent
49+
50+
The function then blocks until the data is sent, and returns the number of bytes that were transmitted. If an error occurs, the function returns -1.
51+
52+
### int recv(void *buffer, uint8_t nbytes)
53+
54+
The `recv` function is used to receive control surface data from the host. It takes two arguments:
55+
1. A pointer to the start of a buffer to store the data
56+
2. The number of bytes available in the buffer
57+
58+
The function should move any received data from the USB data banks into the user-provided buffer.
59+
60+
The function returns how many bytes were copied into the provided buffer. If no bytes are copied the function returns 0. If an error occurs the function should return -1.
61+
62+
### void setRecvCallback(void(*callback)(void))
63+
64+
The `setRecvCallback` function is used to set the function callback for received data. It takes one argument, a function pointer with no arguments and a 'void' return type. This function pointer should be invoked whenever a new control surface data packet has been received.
65+
66+
The API does not specify the storage for this callback pointer.
67+
68+
## Building a Board Implementation
69+
70+
These functions make up the API that allows the library to communicate over USB. However, the lower level USB implementation itself is up to the developer's discretion. To be detected by the Windows driver as an XInput device, your board needs to define the following, taken from an existing XInput product:
71+
72+
* VID / PID
73+
* Device Descriptor
74+
* Configuration Descriptor
75+
* String Descriptors
76+
77+
This library is built around the official Xbox 360 wired controller's data layout. You can find these descriptors and an explanation of what they mean [here](http://www.partsnotincluded.com/reverse-engineering/understanding-the-xbox-360-wired-controllers-usb-data). If you're emulating another device the layout of the data packet may be different and need to be modified by your implementation.
78+
79+
Once the board is detected by the host as an XInput device, define the USB API per the information above. Then fire up the library and try it out!
80+
81+
## Examples
82+
83+
Here are my two API implementations for reference:
84+
85+
* [Arduino AVR Boards](https://github.com/dmadison/ArduinoXInput_AVR/blob/master/cores/arduino/xinput/USB_XInput_API.cpp)
86+
* [Teensy 3 Boards](https://github.com/dmadison/ArduinoXInput_Teensy/blob/master/teensy/avr/cores/teensy3/usb_xinput.c)

keywords.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
XInput KEYWORD1
1111

1212
# Classes
13-
XInputGamepad KEYWORD1
13+
XInputController KEYWORD1
1414

1515
# Enums
1616
XInputControl KEYWORD1
@@ -21,6 +21,10 @@ XInputLEDPattern KEYWORD1
2121
# Methods and Functions (KEYWORD2)
2222
#######################################
2323

24+
# Setup
25+
begin KEYWORD2
26+
reset KEYWORD2
27+
2428
# Set Control Data
2529
press KEYWORD2
2630
release KEYWORD2
@@ -61,7 +65,6 @@ setJoystickRange KEYWORD2
6165
setRange KEYWORD2
6266

6367
# Other
64-
reset KEYWORD2
6568
printDebug KEYWORD2
6669

6770
#######################################

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=XInput
2-
version=0.0.1
2+
version=1.0.0
33
author=David Madison
44
maintainer=David Madison
55
sentence=Library for emulating an Xbox controller over USB.

0 commit comments

Comments
 (0)