-
Notifications
You must be signed in to change notification settings - Fork 7
/
PedalsJoystick.ino
95 lines (76 loc) · 3.13 KB
/
PedalsJoystick.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Project Sim Racing Library for Arduino
* @author David Madison
* @link github.com/dmadison/Sim-Racing-Arduino
* @license LGPLv3 - Copyright (c) 2022 David Madison
*
* This file is part of the Sim Racing Library for Arduino.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @details Emulates the pedals as a joystick over USB.
* @example PedalsJoystick.ino
*/
// This example requires the Arduino Joystick Library
// Download Here: https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <SimRacing.h>
#include <Joystick.h>
const int Pin_Gas = A2;
const int Pin_Brake = A1;
const int Pin_Clutch = A0;
SimRacing::LogitechPedals pedals(Pin_Gas, Pin_Brake, Pin_Clutch);
//SimRacing::LogitechPedals pedals(PEDAL_SHIELD_V1_PINS);
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
0, // number of buttons (none)
0, // number of hat switches (none)
false, false, // no X and Y axes
pedals.hasPedal(SimRacing::Clutch), // include Z axis for the clutch pedal
pedals.hasPedal(SimRacing::Brake), // include Rx axis for the brake pedal
pedals.hasPedal(SimRacing::Gas), // include Ry axis for the gas pedal
false, false, false, false, false, false); // no other axes
const int ADC_Max = 1023; // max value of the analog inputs, 10-bit on AVR boards
const bool AlwaysSend = false; // override the position checks, *always* send data constantly
void setup() {
pedals.begin(); // initialize pedal pins
// if you have one, your calibration line should go here
Joystick.begin(false); // 'false' to disable auto-send
Joystick.setZAxisRange(0, ADC_Max);
Joystick.setRxAxisRange(0, ADC_Max);
Joystick.setRyAxisRange(0, ADC_Max);
updateJoystick(); // send initial state
}
void loop() {
pedals.update();
if (pedals.positionChanged() || AlwaysSend) {
updateJoystick();
}
}
void updateJoystick() {
if (pedals.hasPedal(SimRacing::Gas)) {
int gasPedal = pedals.getPosition(SimRacing::Gas, 0, ADC_Max);
Joystick.setRyAxis(gasPedal);
}
if (pedals.hasPedal(SimRacing::Brake)) {
int brakePedal = pedals.getPosition(SimRacing::Brake, 0, ADC_Max);
Joystick.setRxAxis(brakePedal);
}
if (pedals.hasPedal(SimRacing::Clutch)) {
int clutchPedal = pedals.getPosition(SimRacing::Clutch, 0, ADC_Max);
Joystick.setZAxis(clutchPedal);
}
Joystick.sendState();
}