Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
PedalsJoystick.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
34const int Pin_Gas = A2;
35const int Pin_Brake = A1;
36const int Pin_Clutch = A0;
37
38SimRacing::LogitechPedals pedals(Pin_Gas, Pin_Brake, Pin_Clutch);
39//SimRacing::LogitechPedals pedals(PEDAL_SHIELD_V1_PINS);
40
41Joystick_ Joystick(
42 JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
43 JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
44 0, // number of buttons (none)
45 0, // number of hat switches (none)
46 false, false, // no X and Y axes
47 pedals.hasPedal(SimRacing::Clutch), // include Z axis for the clutch pedal
48 pedals.hasPedal(SimRacing::Brake), // include Rx axis for the brake pedal
49 pedals.hasPedal(SimRacing::Gas), // include Ry axis for the gas pedal
50 false, false, false, false, false, false); // no other axes
51
52const int ADC_Max = 1023; // max value of the analog inputs, 10-bit on AVR boards
53const bool AlwaysSend = false; // override the position checks, *always* send data constantly
54
55
56void setup() {
57 pedals.begin(); // initialize pedal pins
58
59 // if you have one, your calibration line should go here
60
61 Joystick.begin(false); // 'false' to disable auto-send
62
63 Joystick.setZAxisRange(0, ADC_Max);
64 Joystick.setRxAxisRange(0, ADC_Max);
65 Joystick.setRyAxisRange(0, ADC_Max);
66
67 updateJoystick(); // send initial state
68}
69
70void loop() {
71 pedals.update();
72
73 if (pedals.positionChanged() || AlwaysSend) {
74 updateJoystick();
75 }
76}
77
78void updateJoystick() {
79 if (pedals.hasPedal(SimRacing::Gas)) {
80 int gasPedal = pedals.getPosition(SimRacing::Gas, 0, ADC_Max);
81 Joystick.setRyAxis(gasPedal);
82 }
83
84 if (pedals.hasPedal(SimRacing::Brake)) {
85 int brakePedal = pedals.getPosition(SimRacing::Brake, 0, ADC_Max);
86 Joystick.setRxAxis(brakePedal);
87 }
88
89 if (pedals.hasPedal(SimRacing::Clutch)) {
90 int clutchPedal = pedals.getPosition(SimRacing::Clutch, 0, ADC_Max);
91 Joystick.setZAxis(clutchPedal);
92 }
93
94 Joystick.sendState();
95}
Header file for the Sim Racing Library.
Interface with the Logitech pedals (Gas, Brake, and Clutch)
Definition SimRacing.h:760
bool hasPedal(PedalID pedal) const
Checks if a given pedal is present in the class.
bool positionChanged() const
Checks whether the current pedal positions have changed since the last update.
Definition SimRacing.h:351
virtual bool update()
Perform a poll of the hardware to refresh the class state.
virtual void begin()
Initialize the hardware (if necessary)
long getPosition(PedalID pedal, long rMin=0, long rMax=100) const
Retrieves the buffered position for the pedal, rescaled to a nominal range using the calibration valu...