|
| 1 | +/* |
| 2 | + * Project Teensy XInput Library |
| 3 | + * @author David Madison |
| 4 | + * @link github.com/dmadison/TeensyXInput |
| 5 | + * @license MIT - Copyright (c) 2018 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: BasicGamepad |
| 26 | + * Description: Sets up a basic NES-style gamepad using the XInput library, |
| 27 | + * and shows how to use the class functions. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | +#include <XInput.h> |
| 32 | + |
| 33 | +// Directional Pad Pins |
| 34 | +const int Pin_DpadUp = 0; |
| 35 | +const int Pin_DpadDown = 1; |
| 36 | +const int Pin_DpadLeft = 2; |
| 37 | +const int Pin_DpadRight = 3; |
| 38 | + |
| 39 | +// Button Pins |
| 40 | +const int Pin_ButtonA = 4; |
| 41 | +const int Pin_ButtonB = 5; |
| 42 | + |
| 43 | +// Note: Buttons should be connected from the sensor pin (above) to ground |
| 44 | +// when pressed |
| 45 | + |
| 46 | +void setup() { |
| 47 | + // Set buttons as inputs, using internal pull-up resistors |
| 48 | + pinMode(Pin_DpadUp, INPUT_PULLUP); |
| 49 | + pinMode(Pin_DpadDown, INPUT_PULLUP); |
| 50 | + pinMode(Pin_DpadLeft, INPUT_PULLUP); |
| 51 | + pinMode(Pin_DpadRight, INPUT_PULLUP); |
| 52 | + |
| 53 | + pinMode(Pin_ButtonA, INPUT_PULLUP); |
| 54 | + pinMode(Pin_ButtonB, INPUT_PULLUP); |
| 55 | +} |
| 56 | + |
| 57 | +void loop() { |
| 58 | + // Read pin values and store in variables |
| 59 | + // (Note the "!" to invert the state, because LOW = pressed) |
| 60 | + boolean dpadUp = !digitalRead(Pin_DpadUp); |
| 61 | + boolean dpadDown = !digitalRead(Pin_DpadDown); |
| 62 | + boolean dpadLeft = !digitalRead(Pin_DpadLeft); |
| 63 | + boolean dpadRight = !digitalRead(Pin_DpadRight); |
| 64 | + |
| 65 | + boolean buttonA = !digitalRead(Pin_ButtonA); |
| 66 | + boolean buttonB = !digitalRead(Pin_ButtonB); |
| 67 | + |
| 68 | + // Set XInput DPAD values |
| 69 | + XInput.setDpad(dpadUp, dpadDown, dpadLeft, dpadRight); |
| 70 | + |
| 71 | + // Set XInput buttons |
| 72 | + XInput.setButton(BUTTON_A, buttonA); |
| 73 | + XInput.setButton(BUTTON_B, buttonB); |
| 74 | + |
| 75 | + // Send control data to the computer |
| 76 | + XInput.send(); |
| 77 | + |
| 78 | + // Receive data (player number, rumble motors, etc.) |
| 79 | + XInput.receive(); |
| 80 | +} |
0 commit comments