Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
HandbrakeJoystick.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_Handbrake = A2;
35
36SimRacing::Handbrake handbrake(Pin_Handbrake);
37
38Joystick_ Joystick(
39 JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
40 JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
41 0, // number of buttons (none)
42 0, // number of hat switches (none)
43 false, false, // no X and Y axes
44 true, // include Z axis for the handbrake
45 false, false, false, false, false, false, false, false); // no other axes
46
47const int ADC_Max = 1023; // max value of the analog inputs, 10-bit on AVR boards
48const bool AlwaysSend = false; // override the position checks, *always* send data constantly
49
50
51void setup() {
52 handbrake.begin(); // initialize handbrake pins
53
54 // if you have one, your calibration line should go here
55
56 Joystick.begin(false); // 'false' to disable auto-send
57 Joystick.setZAxisRange(0, ADC_Max);
58
59 updateJoystick(); // send initial state
60}
61
62void loop() {
63 handbrake.update();
64
65 if (handbrake.positionChanged() || AlwaysSend) {
66 updateJoystick();
67 }
68}
69
70void updateJoystick() {
71 int pos = handbrake.getPosition(0, ADC_Max);
72 Joystick.setZAxis(pos);
73
74 Joystick.sendState();
75}
Header file for the Sim Racing Library.
Interface with analog handbrakes that use hall effect sensors.
Definition SimRacing.h:688
virtual void begin()
Initializes the pin for reading from the handbrake.
long getPosition(long rMin=0, long rMax=100) const
Retrieves the buffered position for the handbrake axis, rescaled to a nominal range using the calibra...
bool positionChanged() const
Checks whether the handbrake's position has changed since the last update.
Definition SimRacing.h:736
virtual bool update()
Polls the handbrake to update its position.