Emulates the Logitech G25 shifter as a joystick over USB.
#include <Joystick.h>
const int Pin_ShifterX = A0;
const int Pin_ShifterY = A2;
const int Pin_ShifterLatch = 5;
const int Pin_ShifterClock = 6;
const int Pin_ShifterData = 7;
const int Pin_ShifterLED =
SimRacing::UnusedPin;
const int Pin_ShifterDetect = SimRacing::UnusedPin;
Pin_ShifterX, Pin_ShifterY,
Pin_ShifterLatch, Pin_ShifterClock, Pin_ShifterData,
Pin_ShifterLED, Pin_ShifterDetect
);
const bool SendAnalogAxis = false;
const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 };
const int NumGears = sizeof(Gears) / sizeof(Gears[0]);
const ShifterButton Buttons[] = {
ShifterButton::BUTTON_SOUTH,
ShifterButton::BUTTON_EAST,
ShifterButton::BUTTON_WEST,
ShifterButton::BUTTON_NORTH,
ShifterButton::BUTTON_1,
ShifterButton::BUTTON_2,
ShifterButton::BUTTON_3,
ShifterButton::BUTTON_4,
};
const int NumButtons = sizeof(Buttons) / sizeof(Buttons[0]);
const int ADC_Max = 1023;
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
NumGears + NumButtons + 2,
1,
SendAnalogAxis, SendAnalogAxis,
false, false, false, false, false, false, false, false, false);
void updateJoystick();
void setup() {
Joystick.begin(false);
Joystick.setXAxisRange(0, ADC_Max);
Joystick.setYAxisRange(ADC_Max, 0);
updateJoystick();
}
void loop() {
bool dataChanged = shifter.
update();
if (dataChanged || SendAnalogAxis == true) {
updateJoystick();
}
}
void updateJoystick() {
int currentButton = 0;
for (int i = 0; i < NumGears; i++) {
if (shifter.
getGear() == Gears[i]) {
Joystick.pressButton(currentButton);
}
else {
Joystick.releaseButton(currentButton);
}
currentButton++;
}
if (SendAnalogAxis == true) {
Joystick.setXAxis(x);
Joystick.setYAxis(y);
}
for (int i = 0; i < NumButtons; i++) {
bool state = shifter.getButton(Buttons[i]);
Joystick.setButton(currentButton, state);
currentButton++;
}
int angle = shifter.getDpadAngle();
Joystick.setHatSwitch(0, angle);
bool shiftUp = shifter.getShiftUp();
Joystick.setButton(currentButton, shiftUp);
currentButton++;
bool shiftDown = shifter.getShiftDown();
Joystick.setButton(currentButton, shiftDown);
currentButton++;
Joystick.sendState();
}
Header file for the Sim Racing Library.
virtual void begin()
Initializes the hardware pins for reading the gear states.
long getPosition(Axis ax, long rMin=AnalogInput::Min, long rMax=AnalogInput::Max) const
Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibratio...
Interface with the Logitech G25 shifter.
Button
Enumeration of button values.
bool update()
Perform a poll of the hardware to refresh the class state.
Gear getGear() const
Returns the currently selected gear.