Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add steering wheel support #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Fino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Joystick_ Joystick(
19, 2, // Button Count, Hat Switch Count
true, true, false, // X, Y, Z
false, false, false, // Rx, Ry, Rz
true, true); // rudder, throttle
true, true, // rudder, throttle
false, false, false); // accelerator, brake, steering

void setup() {

Expand Down
56 changes: 53 additions & 3 deletions src/Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#define JOYSTICK_INCLUDE_RUDDER B00000001
#define JOYSTICK_INCLUDE_THROTTLE B00000010
#define JOYSTICK_INCLUDE_ACCELERATOR B00000100
#define JOYSTICK_INCLUDE_BRAKE B00001000
#define JOYSTICK_INCLUDE_STEERING B00010000

const float cutoff_freq_damper = 2.0; //Cutoff frequency in Hz
const float sampling_time_damper = 0.002; //Sampling time in seconds.
Expand All @@ -67,7 +69,10 @@ Joystick_::Joystick_(
bool includeRyAxis,
bool includeRzAxis,
bool includeRudder,
bool includeThrottle)
bool includeThrottle,
bool includeAccelerator,
bool includeBrake,
bool includeSteering)
{
// Set the USB HID Report ID
_hidReportId = hidReportId;
Expand All @@ -84,7 +89,10 @@ Joystick_::Joystick_(
_includeAxisFlags |= (includeRzAxis ? JOYSTICK_INCLUDE_RZ_AXIS : 0);
_includeSimulatorFlags = 0;
_includeSimulatorFlags |= (includeRudder ? JOYSTICK_INCLUDE_RUDDER : 0);
_includeSimulatorFlags |= (includeThrottle ? JOYSTICK_INCLUDE_THROTTLE : 0);
_includeSimulatorFlags |= (includeThrottle ? JOYSTICK_INCLUDE_THROTTLE : 0);
_includeSimulatorFlags |= (includeAccelerator ? JOYSTICK_INCLUDE_ACCELERATOR : 0);
_includeSimulatorFlags |= (includeBrake ? JOYSTICK_INCLUDE_BRAKE : 0);
_includeSimulatorFlags |= (includeSteering ? JOYSTICK_INCLUDE_STEERING : 0);

// Build Joystick HID Report Description

Expand All @@ -105,7 +113,10 @@ Joystick_::Joystick_(
+ (includeRzAxis == true);

uint8_t simulationCount = (includeRudder == true)
+ (includeThrottle == true);
+ (includeThrottle == true)
+ (includeAccelerator == true)
+ (includeBrake == true)
+ (includeSteering == true);

static uint8_t tempHidReportDescriptor[150];
int hidReportDescriptorSize = 0;
Expand Down Expand Up @@ -406,6 +417,24 @@ Joystick_::Joystick_(
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBB;
}

if (includeAccelerator == true) {
// USAGE (Accelerator)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC4;
}

if (includeBrake == true) {
// USAGE (Brake)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC5;
}

if (includeSteering == true) {
// USAGE (Steering)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC8;
}

// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
Expand Down Expand Up @@ -446,6 +475,9 @@ Joystick_::Joystick_(
_zAxisRotation = 0;
_throttle = 0;
_rudder = 0;
_accelerator = 0;
_brake = 0;
_steering = 0;
for (int index = 0; index < JOYSTICK_HATSWITCH_COUNT_MAXIMUM; index++)
{
_hatSwitchValues[index] = JOYSTICK_HATSWITCH_RELEASE;
Expand Down Expand Up @@ -892,6 +924,21 @@ void Joystick_::setThrottle(int16_t value)
_throttle = value;
if (_autoSendState) sendState();
}
void Joystick_::setAccelerator(int16_t value)
{
_accelerator = value;
if (_autoSendState) sendState();
}
void Joystick_::setBrake(int16_t value)
{
_brake = value;
if (_autoSendState) sendState();
}
void Joystick_::setSteering(int16_t value)
{
_steering = value;
if (_autoSendState) sendState();
}

void Joystick_::setHatSwitch(int8_t hatSwitchIndex, int16_t value)
{
Expand Down Expand Up @@ -988,6 +1035,9 @@ void Joystick_::sendState()
// Set Simulation Values
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_RUDDER, _rudder, _rudderMinimum, _rudderMaximum, &(data[index]));
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_THROTTLE, _throttle, _throttleMinimum, _throttleMaximum, &(data[index]));
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_ACCELERATOR, _accelerator, _acceleratorMinimum, _acceleratorMaximum, &(data[index]));
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_BRAKE, _brake, _brakeMinimum, _brakeMaximum, &(data[index]));
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_STEERING, _steering, _steeringMinimum, _steeringMaximum, &(data[index]));

DynamicHID().SendReport(_hidReportId, data, _hidReportSize);
}
Expand Down
32 changes: 31 additions & 1 deletion src/Joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Joystick_
int16_t _zAxisRotation;
int16_t _throttle;
int16_t _rudder;
int16_t _accelerator;
int16_t _brake;
int16_t _steering;
int16_t _hatSwitchValues[JOYSTICK_HATSWITCH_COUNT_MAXIMUM];
uint8_t *_buttonValues = NULL;

Expand All @@ -128,6 +131,12 @@ class Joystick_
int16_t _rudderMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
int16_t _throttleMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
int16_t _throttleMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
int16_t _acceleratorMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
int16_t _acceleratorMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
int16_t _brakeMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
int16_t _brakeMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
int16_t _steeringMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
int16_t _steeringMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;

uint8_t _hidReportId;
uint8_t _hidReportSize;
Expand Down Expand Up @@ -171,7 +180,10 @@ class Joystick_
bool includeRyAxis = true,
bool includeRzAxis = true,
bool includeRudder = true,
bool includeThrottle = true);
bool includeThrottle = true,
bool includeAccelerator = true,
bool includeBrake = true,
bool includeSteering = true);

void begin(bool initAutoSendState = true);
void end();
Expand Down Expand Up @@ -216,6 +228,21 @@ class Joystick_
_throttleMinimum = minimum;
_throttleMaximum = maximum;
}
inline void setAcceleratorRange(int16_t minimum, int16_t maximum)
{
_acceleratorMinimum = minimum;
_acceleratorMaximum = maximum;
}
inline void setBrakeRange(int16_t minimum, int16_t maximum)
{
_brakeMinimum = minimum;
_brakeMaximum = maximum;
}
inline void setSteeringRange(int16_t minimum, int16_t maximum)
{
_steeringMinimum = minimum;
_steeringMaximum = maximum;
}

// Set Axis Values
void setXAxis(int16_t value);
Expand All @@ -228,6 +255,9 @@ class Joystick_
// Set Simuation Values
void setRudder(int16_t value);
void setThrottle(int16_t value);
void setAccelerator(int16_t value);
void setBrake(int16_t value);
void setSteering(int16_t value);

void setButton(uint8_t button, uint8_t value);
void pressButton(uint8_t button);
Expand Down