Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
HandbrakeJoystick.ino

Emulates the handbrake as a joystick over USB.

/*
* 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/>.
*/
// This example requires the Arduino Joystick Library
// Download Here: https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <SimRacing.h>
#include <Joystick.h>
const int Pin_Handbrake = A2;
SimRacing::Handbrake handbrake(Pin_Handbrake);
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
true, // include Z axis for the handbrake
false, false, 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() {
handbrake.begin(); // initialize handbrake pins
// if you have one, your calibration line should go here
Joystick.begin(false); // 'false' to disable auto-send
Joystick.setZAxisRange(0, ADC_Max);
updateJoystick(); // send initial state
}
void loop() {
handbrake.update();
if (handbrake.positionChanged() || AlwaysSend) {
updateJoystick();
}
}
void updateJoystick() {
int pos = handbrake.getPosition(0, ADC_Max);
Joystick.setZAxis(pos);
Joystick.sendState();
}
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.