Skip to content

Commit 98ea8e9

Browse files
committed
Add library-less USB API demo
For testing boards packages without having to use the library. Included in the 'extras' folder so as not to confuse end-users.
1 parent 51f727d commit 98ea8e9

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

extras/API-Demo/API-Demo.ino

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
*/
41+
42+
#ifndef USB_XINPUT
43+
#error "USB_XINPUT not defined! No XInput API detected"
44+
#else
45+
46+
uint8_t txData[20] = { 0x00, 0x14, 0x00 };
47+
uint8_t rxData[8] = { 0x00 };
48+
49+
boolean buttonState = false;
50+
51+
void setup() {
52+
while (!XInputUSB::connected()) {} // wait for connection
53+
XInputUSB::setRecvCallback(receiveCallback);
54+
}
55+
56+
void loop() {
57+
buttonState = !buttonState;
58+
setButtonA(txData, buttonState);
59+
XInputUSB::send(txData, sizeof(txData));
60+
61+
delay(1000);
62+
}
63+
64+
void setButtonA(uint8_t * ptr, boolean state) {
65+
const uint8_t ButtonIndex = 3;
66+
const uint8_t ButtonPosition = 4;
67+
68+
if (state == true) {
69+
ptr[ButtonIndex] |= (1 << ButtonPosition);
70+
}
71+
else {
72+
ptr[ButtonIndex] &= ~(1 << ButtonPosition);
73+
}
74+
}
75+
76+
void receiveCallback() {
77+
if (XInputUSB::available() > 0) {
78+
XInputUSB::recv(rxData, sizeof(rxData));
79+
}
80+
}
81+
82+
#endif

0 commit comments

Comments
 (0)