Skip to content

Commit 45179d5

Browse files
authored
Merge pull request #14 from dmadison/api-example
USB API Example
2 parents e454767 + dbce6d3 commit 45179d5

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

extras/API-Demo/API-Demo.ino

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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: API Demo
26+
* Description: Tests for the functionality of the XInputUSB API
27+
* without using the Arduino XInput library. Useful
28+
* for testing boards packages without the library
29+
* as a factor.
30+
*
31+
* Tests the following XInputUSB functions:
32+
* * static boolean connected(void)
33+
* * static uint8_t available(void)
34+
* * static int send(const void *buffer, uint8_t nbytes)
35+
* * static int recv(void *buffer, uint8_t nbytes)
36+
* * static void setRecvCallback(void(*callback)(void))
37+
*
38+
* If the example is running properly, the controller's
39+
* 'A' button should toggle on and off every 2 seconds
40+
* and the built-in LED should turn on when rumble data
41+
* is received.
42+
*/
43+
44+
#ifndef USB_XINPUT
45+
#error "USB_XINPUT not defined! No XInput API detected"
46+
#else
47+
48+
uint8_t txData[20] = { 0x00, 0x14, 0x00 };
49+
boolean buttonState = false;
50+
51+
void setup() {
52+
pinMode(LED_BUILTIN, OUTPUT);
53+
digitalWrite(LED_BUILTIN, LOW);
54+
55+
while (!XInputUSB::connected()) {} // wait for connection
56+
XInputUSB::setRecvCallback(receiveCallback);
57+
}
58+
59+
void loop() {
60+
buttonState = !buttonState;
61+
setButtonA(txData, buttonState);
62+
XInputUSB::send(txData, sizeof(txData));
63+
64+
delay(1000);
65+
}
66+
67+
void setButtonA(uint8_t * ptr, boolean state) {
68+
const uint8_t ButtonIndex = 3;
69+
const uint8_t ButtonPosition = 4;
70+
71+
if (state == true) {
72+
ptr[ButtonIndex] |= (1 << ButtonPosition);
73+
}
74+
else {
75+
ptr[ButtonIndex] &= ~(1 << ButtonPosition);
76+
}
77+
}
78+
79+
void receiveCallback() {
80+
if (XInputUSB::available() > 0) {
81+
uint8_t rxData[8] = { 0x00 };
82+
const int success = XInputUSB::recv(rxData, sizeof(rxData));
83+
84+
if (success > 5 && rxData[0] == 0x00) { // rumble packet
85+
boolean rumbling = (rxData[3] > 0 || rxData[4] > 0 );
86+
digitalWrite(LED_BUILTIN, rumbling);
87+
}
88+
}
89+
}
90+
91+
#endif

extras/XInputUSB_API.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Here is the C++ API header in all of its glory:
1919

2020
class XInputUSB {
2121
public:
22-
static bool connected(void);
22+
static boolean connected(void);
2323
static uint8_t available(void);
2424
static int send(const void *buffer, uint8_t nbytes);
2525
static int recv(void *buffer, uint8_t nbytes);
@@ -33,7 +33,7 @@ Note the `USB_XINPUT` preprocessor definition. This definition is what tells the
3333
3434
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:
3535
36-
### bool connected(void)
36+
### boolean connected(void)
3737
3838
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`.
3939
@@ -84,3 +84,5 @@ Here are my two API implementations for reference:
8484
8585
* [Arduino AVR Boards](https://github.com/dmadison/ArduinoXInput_AVR/blob/master/cores/arduino/xinput/USB_XInput_API.cpp)
8686
* [Teensy 3 Boards](https://github.com/dmadison/ArduinoXInput_Teensy/blob/master/teensy/avr/cores/teensy3/usb_xinput.c)
87+
88+
I've also included a test sketch for validating an XInputUSB API implementation without the library, which you can find [here](API-Demo/API-Demo.ino).

0 commit comments

Comments
 (0)