|
| 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: ReceiveCallback |
| 26 | + * Description: Shows how to set up a custom callback to process rumble or LED |
| 27 | + * data when the device receives a new USB packet from the host. |
| 28 | + * |
| 29 | + * WARNING: The callback is called from within the USB ISR. Keep it short! |
| 30 | + * |
| 31 | + */ |
| 32 | + |
| 33 | +#include <XInput.h> |
| 34 | + |
| 35 | +const uint8_t LED_Pin = LED_BUILTIN; |
| 36 | +const uint8_t Button_Pin = 6; |
| 37 | + |
| 38 | +void setup() { |
| 39 | + pinMode(Button_Pin, INPUT_PULLUP); // Set button pin as input w/ pullup |
| 40 | + |
| 41 | + pinMode(LED_Pin, OUTPUT); // Set LED pin as output |
| 42 | + digitalWrite(LED_Pin, LOW); // Turn LED off |
| 43 | + |
| 44 | + // Set callback function. Function must have a 'void' return type |
| 45 | + // and take a single uint8_t as an argument |
| 46 | + XInput.setReceiveCallback(rumbleCallback); |
| 47 | +} |
| 48 | + |
| 49 | +void loop() { |
| 50 | + boolean buttonState = digitalRead(Button_Pin); |
| 51 | + |
| 52 | + if(buttonState == LOW) { |
| 53 | + XInput.press(TRIGGER_RIGHT); |
| 54 | + } |
| 55 | + else { |
| 56 | + XInput.release(TRIGGER_RIGHT); |
| 57 | + } |
| 58 | + |
| 59 | + XInput.send(); |
| 60 | +} |
| 61 | + |
| 62 | +void rumbleCallback(uint8_t packetType) { |
| 63 | + // If we have an LED packet (0x01), do nothing |
| 64 | + if (packetType == (uint8_t) XInputReceiveType::LEDs) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // If we have a rumble packet (0x00), see our rumble data on the LED |
| 69 | + else if (packetType == (uint8_t) XInputReceiveType::Rumble) { |
| 70 | + uint8_t rumbleValue = XInput.getRumbleLeft() | XInput.getRumbleRight(); |
| 71 | + analogWrite(LED_Pin, rumbleValue); |
| 72 | + } |
| 73 | +} |
0 commit comments