Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
ShiftJoystick.ino
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) 2022 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
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// Set this option to 'true' to send the shifter's X/Y position
35// as a joystick. This is not needed for most games.
36const bool SendAnalogAxis = false;
37
38// Set this option to 'true' to send the raw state of the reverse
39// trigger as its own button. This is not needed for any racing
40// games, but can be useful for custom controller purposes.
41const bool SendReverseRaw = false;
42
43const int Pin_ShifterX = A0;
44const int Pin_ShifterY = A2;
45const int Pin_ShifterRev = 2;
46
47SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);
48//SimRacing::LogitechShifter shifter(SHIFTER_SHIELD_V1_PINS);
49
50const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 };
51const int NumGears = sizeof(Gears) / sizeof(Gears[0]);
52
53const int ADC_Max = 1023; // 10-bit on AVR
54
55Joystick_ Joystick(
56 JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
57 JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
58 NumGears + SendReverseRaw, // number of buttons (7 gears: reverse and 1-6)
59 0, // number of hat switches (none)
60 SendAnalogAxis, SendAnalogAxis, // include X and Y axes for analog output, if set above
61 false, false, false, false, false, false, false, false, false); // no other axes
62
63void updateJoystick(); // forward-declared function for non-Arduino environments
64
65
66void setup() {
67 shifter.begin();
68
69 // if you have one, your calibration line should go here
70
71 Joystick.begin(false); // 'false' to disable auto-send
72 Joystick.setXAxisRange(0, ADC_Max);
73 Joystick.setYAxisRange(ADC_Max, 0); // invert axis so 'up' is up
74
75 updateJoystick(); // send initial state
76}
77
78void loop() {
79 shifter.update();
80
81 if (SendAnalogAxis == true || shifter.gearChanged()) {
82 updateJoystick();
83 }
84}
85
86void updateJoystick() {
87 // set the buttons corresponding to the gears
88 for (int i = 0; i < NumGears; i++) {
89 if (shifter.getGear() == Gears[i]) {
90 Joystick.pressButton(i);
91 }
92 else {
93 Joystick.releaseButton(i);
94 }
95 }
96
97 // set the analog axes (if the option is set)
98 if (SendAnalogAxis == true) {
99 int x = shifter.getPosition(SimRacing::X, 0, ADC_Max);
100 int y = shifter.getPosition(SimRacing::Y, 0, ADC_Max);
101 Joystick.setXAxis(x);
102 Joystick.setYAxis(y);
103 }
104
105 // set the reverse button (if the option is set)
106 if (SendReverseRaw == true) {
107 bool reverseState = shifter.getReverseButton();
108 Joystick.setButton(NumGears, reverseState); // "NumGears" is the 0-indexed max gear + 1
109 }
110
111 Joystick.sendState();
112}
Header file for the Sim Racing Library.
bool getReverseButton() const
Checks the current state of the "reverse" button at the bottom of the shift column.
virtual bool update()
Polls the hardware to update the current gear state.
virtual void begin()
Initializes the hardware pins for reading the gear states.
long getPosition(Axis ax, long rMin=AnalogInput::Min, long rMax=AnalogInput::Max) const
Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibratio...
Interface with the Logitech Driving Force shifter.
Definition SimRacing.h:787
int8_t getGear() const
Returns the currently selected gear.
Definition SimRacing.h:475
bool gearChanged() const
Checks whether the current gear has changed since the last update.
Definition SimRacing.h:519