|
| 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 |
0 commit comments