|
| 1 | +/* |
| 2 | + * Project Sim Racing Library for Arduino |
| 3 | + * @author David Madison |
| 4 | + * @link github.com/dmadison/Sim-Racing-Arduino |
| 5 | + * @license LGPLv3 - Copyright (c) 2024 David Madison |
| 6 | + * |
| 7 | + * This file is part of the Sim Racing Library for Arduino. |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | + /** |
| 24 | + * @details Emulates the Logitech G25 shifter as a joystick over USB. |
| 25 | + * @example LogitechShifterG25_Joystick.ino |
| 26 | + */ |
| 27 | + |
| 28 | +// This example requires the Arduino Joystick Library |
| 29 | +// Download Here: https://github.com/MHeironimus/ArduinoJoystickLibrary |
| 30 | + |
| 31 | +#include <SimRacing.h> |
| 32 | +#include <Joystick.h> |
| 33 | + |
| 34 | +const int Pin_ShifterX = A0; // DE-9 pin 4 |
| 35 | +const int Pin_ShifterY = A2; // DE-9 pin 8 |
| 36 | + |
| 37 | +const int Pin_ShifterLatch = 5; // DE-9 pin 3 |
| 38 | +const int Pin_ShifterClock = 6; // DE-9 pin 7 |
| 39 | +const int Pin_ShifterData = 7; // DE-9 pin 2 |
| 40 | + |
| 41 | +// These pins require extra resistors! If you have made the proper |
| 42 | +// connections, change the pin numbers to the ones you're using |
| 43 | +const int Pin_ShifterDetect = SimRacing::UnusedPin; // DE-9 pin 1, requires pull-down resistor |
| 44 | +const int Pin_ShifterLED = SimRacing::UnusedPin; // DE-9 pin 5, requires 100-120 Ohm series resistor |
| 45 | + |
| 46 | +SimRacing::LogitechShifterG25 shifter( |
| 47 | + Pin_ShifterX, Pin_ShifterY, |
| 48 | + Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData, |
| 49 | + Pin_ShifterDetect, Pin_ShifterLED |
| 50 | +); |
| 51 | + |
| 52 | +// Set this option to 'true' to send the shifter's X/Y position |
| 53 | +// as a joystick. This is not needed for most games. |
| 54 | +const bool SendAnalogAxis = false; |
| 55 | + |
| 56 | +const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 }; |
| 57 | +const int NumGears = sizeof(Gears) / sizeof(Gears[0]); |
| 58 | + |
| 59 | +using ShifterButton = SimRacing::LogitechShifterG25::Button; |
| 60 | +const ShifterButton Buttons[] = { |
| 61 | + ShifterButton::BUTTON_SOUTH, |
| 62 | + ShifterButton::BUTTON_EAST, |
| 63 | + ShifterButton::BUTTON_WEST, |
| 64 | + ShifterButton::BUTTON_NORTH, |
| 65 | + ShifterButton::BUTTON_1, |
| 66 | + ShifterButton::BUTTON_2, |
| 67 | + ShifterButton::BUTTON_3, |
| 68 | + ShifterButton::BUTTON_4, |
| 69 | +}; |
| 70 | +const int NumButtons = sizeof(Buttons) / sizeof(Buttons[0]); |
| 71 | + |
| 72 | +const int ADC_Max = 1023; // 10-bit on AVR |
| 73 | + |
| 74 | +Joystick_ Joystick( |
| 75 | + JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages) |
| 76 | + JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager |
| 77 | + NumGears + NumButtons + 2, // number of buttons (7 gears: reverse and 1-6, 8 buttons, 2 sequential gears) |
| 78 | + 1, // number of hat switches (1, the directional pad) |
| 79 | + SendAnalogAxis, SendAnalogAxis, // include X and Y axes for analog output, if set above |
| 80 | + false, false, false, false, false, false, false, false, false); // no other axes |
| 81 | + |
| 82 | +void updateJoystick(); // forward-declared function for non-Arduino environments |
| 83 | + |
| 84 | + |
| 85 | +void setup() { |
| 86 | + shifter.begin(); |
| 87 | + |
| 88 | + // if you have one, your calibration line should go here |
| 89 | + |
| 90 | + Joystick.begin(false); // 'false' to disable auto-send |
| 91 | + Joystick.setXAxisRange(0, ADC_Max); |
| 92 | + Joystick.setYAxisRange(ADC_Max, 0); // invert axis so 'up' is up |
| 93 | + |
| 94 | + updateJoystick(); // send initial state |
| 95 | +} |
| 96 | + |
| 97 | +void loop() { |
| 98 | + bool dataChanged = shifter.update(); |
| 99 | + |
| 100 | + if (dataChanged || SendAnalogAxis == true) { |
| 101 | + updateJoystick(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void updateJoystick() { |
| 106 | + // keep track of which button we're updating |
| 107 | + // in the joystick output |
| 108 | + int currentButton = 0; |
| 109 | + |
| 110 | + // set the buttons corresponding to the gears |
| 111 | + for (int i = 0; i < NumGears; i++) { |
| 112 | + if (shifter.getGear() == Gears[i]) { |
| 113 | + Joystick.pressButton(currentButton); |
| 114 | + } |
| 115 | + else { |
| 116 | + Joystick.releaseButton(currentButton); |
| 117 | + } |
| 118 | + |
| 119 | + currentButton++; |
| 120 | + } |
| 121 | + |
| 122 | + // set the analog axes (if the option is set) |
| 123 | + if (SendAnalogAxis == true) { |
| 124 | + int x = shifter.getPosition(SimRacing::X, 0, ADC_Max); |
| 125 | + int y = shifter.getPosition(SimRacing::Y, 0, ADC_Max); |
| 126 | + Joystick.setXAxis(x); |
| 127 | + Joystick.setYAxis(y); |
| 128 | + } |
| 129 | + |
| 130 | + // set the buttons |
| 131 | + for (int i = 0; i < NumButtons; i++) { |
| 132 | + bool state = shifter.getButton(Buttons[i]); |
| 133 | + Joystick.setButton(currentButton, state); |
| 134 | + |
| 135 | + currentButton++; |
| 136 | + } |
| 137 | + |
| 138 | + // set the hatswitch (directional pad) |
| 139 | + int angle = shifter.getDpadAngle(); |
| 140 | + Joystick.setHatSwitch(0, angle); |
| 141 | + |
| 142 | + // set the sequential shifting buttons |
| 143 | + bool shiftUp = shifter.getShiftUp(); |
| 144 | + Joystick.setButton(currentButton, shiftUp); |
| 145 | + currentButton++; |
| 146 | + |
| 147 | + bool shiftDown = shifter.getShiftDown(); |
| 148 | + Joystick.setButton(currentButton, shiftDown); |
| 149 | + currentButton++; |
| 150 | + |
| 151 | + // send the updated data via USB |
| 152 | + Joystick.sendState(); |
| 153 | +} |
0 commit comments