|
| 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: TestAll |
| 26 | + * Description: Automatically activate all possible XInput controls. |
| 27 | + * Useful to test that everything is functioning properly. |
| 28 | + * |
| 29 | + * WARNING: This will spam inputs! Ground pin '0' to stop. |
| 30 | + * |
| 31 | + */ |
| 32 | + |
| 33 | +#include <XInput.h> |
| 34 | + |
| 35 | +// Config Settings |
| 36 | +const unsigned long CycleTime = 5000; // ms |
| 37 | +const int SafetyPin = 0; // Ground this pin to prevent inputs |
| 38 | + |
| 39 | +// Button Setup |
| 40 | +const int NumButtons = 10; |
| 41 | +const int Buttons[NumButtons] = { |
| 42 | + BUTTON_A, |
| 43 | + BUTTON_B, |
| 44 | + BUTTON_X, |
| 45 | + BUTTON_Y, |
| 46 | + BUTTON_LB, |
| 47 | + BUTTON_RB, |
| 48 | + BUTTON_BACK, |
| 49 | + BUTTON_START, |
| 50 | + BUTTON_L3, |
| 51 | + BUTTON_R3, |
| 52 | +}; |
| 53 | +const unsigned long ButtonDuration = CycleTime / NumButtons; |
| 54 | +unsigned long buttonTimeLast = 0; |
| 55 | +int buttonID = 0; |
| 56 | + |
| 57 | +// DPad Setup |
| 58 | +const int NumDirections = 4; |
| 59 | +const unsigned long DPadDuration = CycleTime / NumDirections; |
| 60 | +unsigned long dpadTimeLast = 0; |
| 61 | +int dpadPosition = 0; |
| 62 | + |
| 63 | +// Triggers |
| 64 | +const int TriggerMax = 255; // uint8_t max |
| 65 | +const unsigned long TriggerDuration = CycleTime / (TriggerMax * 2); // Go up and down |
| 66 | +unsigned long triggerTimeLast = 0; |
| 67 | +uint8_t triggerVal = 0; |
| 68 | +boolean triggerDirection = 0; |
| 69 | + |
| 70 | +// Joystick Setup |
| 71 | +const int JoyMax = 32767; // int16_t max |
| 72 | +const double angle_precision = (2 * PI) / (CycleTime / 4); // 4 because 250 Hz update rate |
| 73 | +double angle = 0.0; |
| 74 | + |
| 75 | +void setup() { |
| 76 | + pinMode(SafetyPin, INPUT_PULLUP); |
| 77 | +} |
| 78 | + |
| 79 | +void loop() { |
| 80 | + if (digitalRead(SafetyPin) == LOW) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + unsigned long t = millis(); // Get timestamp for comparison |
| 85 | + |
| 86 | + // DPad |
| 87 | + if (t - dpadTimeLast >= DPadDuration) { // If enough time has passed, change the dpad |
| 88 | + XInput.setDpad(dpadPosition == 0, dpadPosition == 1, dpadPosition == 2, dpadPosition == 3); |
| 89 | + |
| 90 | + dpadPosition++; // Increment the dpad counter |
| 91 | + if (dpadPosition >= NumDirections) dpadPosition = 0; // Go back to 0 if we hit the limit |
| 92 | + dpadTimeLast = t; // Save time we last did this |
| 93 | + } |
| 94 | + |
| 95 | + // Buttons |
| 96 | + if (t - buttonTimeLast >= ButtonDuration) { // If enough time has passed, change the button pressed |
| 97 | + for (int i = 0; i < NumButtons; i++) { |
| 98 | + XInput.release((XInputControl)Buttons[i]); // Relase all buttons |
| 99 | + } |
| 100 | + |
| 101 | + XInput.press((XInputControl)Buttons[buttonID]); // Press the next button |
| 102 | + buttonID++; // Increment the button counter |
| 103 | + if (buttonID >= NumButtons) buttonID = 0; // Go back to 0 if we hit the limit |
| 104 | + |
| 105 | + buttonTimeLast = t; // Save time we last did this |
| 106 | + } |
| 107 | + |
| 108 | + // Triggers |
| 109 | + if (t - triggerTimeLast >= TriggerDuration) { // If enough time has passed, update the triggers |
| 110 | + XInput.setTrigger(TRIGGER_LEFT, triggerVal); |
| 111 | + XInput.setTrigger(TRIGGER_RIGHT, ~triggerVal); // Inverse |
| 112 | + |
| 113 | + // Increment trigger value based on direction |
| 114 | + if (triggerDirection == 0) { triggerVal++; } |
| 115 | + else { triggerVal--; } |
| 116 | + |
| 117 | + if (triggerVal == TriggerMax || triggerVal == 0) { // If we've hit the edge of the range |
| 118 | + triggerDirection = !triggerDirection; // Reverse direction |
| 119 | + } |
| 120 | + |
| 121 | + triggerTimeLast = t; // Save time we last did this |
| 122 | + } |
| 123 | + |
| 124 | + // Calculate joystick x/y values using trig |
| 125 | + int axis_x = sin(angle) * JoyMax; |
| 126 | + int axis_y = cos(angle) * JoyMax; |
| 127 | + |
| 128 | + angle += angle_precision; |
| 129 | + if (angle >= 360) { |
| 130 | + angle -= 360; |
| 131 | + } |
| 132 | + |
| 133 | + XInput.setJoystick(JOY_LEFT, axis_x, axis_y); // Clockwise |
| 134 | + XInput.setJoystick(JOY_RIGHT, -axis_x, axis_y); // Counter-clockwise |
| 135 | + |
| 136 | + // Send values to PC |
| 137 | + XInput.send(); |
| 138 | + |
| 139 | + // Receive data from PC |
| 140 | + XInput.receive(); |
| 141 | +} |
0 commit comments