Skip to content

Commit d33a0b0

Browse files
committed
API Demo: blink LED on rumble receive
Boards without built-in LEDs be damned. Turning on an LED (now via rumble data) is by far the easiest way to test that the receive data functions are working properly.
1 parent 4ef6741 commit d33a0b0

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

extras/API-Demo/API-Demo.ino

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@
3636
* * static void setRecvCallback(void(*callback)(void))
3737
*
3838
* If the example is running properly, the controller's
39-
* 'A' button should toggle on and off every 2 seconds.
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.
4042
*/
4143

4244
#ifndef USB_XINPUT
4345
#error "USB_XINPUT not defined! No XInput API detected"
4446
#else
4547

4648
uint8_t txData[20] = { 0x00, 0x14, 0x00 };
47-
uint8_t rxData[8] = { 0x00 };
48-
4949
boolean buttonState = false;
5050

5151
void setup() {
52+
pinMode(LED_BUILTIN, OUTPUT);
53+
digitalWrite(LED_BUILTIN, LOW);
54+
5255
while (!XInputUSB::connected()) {} // wait for connection
5356
XInputUSB::setRecvCallback(receiveCallback);
5457
}
@@ -75,7 +78,13 @@ void setButtonA(uint8_t * ptr, boolean state) {
7578

7679
void receiveCallback() {
7780
if (XInputUSB::available() > 0) {
78-
XInputUSB::recv(rxData, sizeof(rxData));
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+
}
7988
}
8089
}
8190

0 commit comments

Comments
 (0)