Skip to content

Commit

Permalink
Updated so it's only making a single Xinput driver call per cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwomey committed May 28, 2017
1 parent f6f8355 commit f4d42fc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Debug
Release
.vs
.DS_Store
26 changes: 24 additions & 2 deletions OrbiterSteamController/XinputController.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#include "XinputController.h"


XinputController::XinputController() {
init();
}


XinputController::~XinputController() {

}

// Creates "normalized" versions of analog state for use with Orbiter
void XinputController::update() {
updateState();
normState.LX = max(-1, (double)state.Gamepad.sThumbLX / 32767); // Returns from -1 to 1
normState.LY = max(-1, (double)state.Gamepad.sThumbLY / 32767); // Returns from -1 to 1
normState.RX = max(-1, (double)state.Gamepad.sThumbRX / 32767); // Returns from -1 to 1
normState.RY = max(-1, (double)state.Gamepad.sThumbRY / 32767); // Returns from -1 to 1
normState.RT = (double)state.Gamepad.bRightTrigger / 255; // Returns from 0 to 1
normState.LT = (double)state.Gamepad.bLeftTrigger / 255; // Returns from 0 to 1
}

void XinputController::init() {
setControllerNumber();
updateState();
Expand All @@ -20,57 +29,70 @@ void XinputController::updateState() {
XInputGetState(controllerNumber, &state);
}


// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getLX() {
updateState();
return state.Gamepad.sThumbLX;
}

// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getLY() {
updateState();
return state.Gamepad.sThumbLY;
}

// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getRX() {
updateState();
return state.Gamepad.sThumbRX;
}

// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getRY() {
updateState();
return state.Gamepad.sThumbRY;
}

// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getRT() {
updateState();
return state.Gamepad.bRightTrigger;
}

// Depreciated - use update() and XinputController.state instead (to avoid multiple Xinput calls)
int XinputController::getLT() {
updateState();
return state.Gamepad.bLeftTrigger;
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormLX() {
return max(-1, (double)getLX() / 32767); // Returns from -1 to 1
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormLY() {
return max(-1, (double)getLY() / 32767); // Returns from -1 to 1
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormRX() {
return max(-1, (double)getRX() / 32767); // Returns from -1 to 1
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormRY() {
return max(-1, (double)getRY() / 32767); // Returns from -1 to 1
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormRT() {
//return max(-1, (double)((getRT() - 128) / 127)); // Returns from -1 to 1
return (double)getRT() / 255; // Returns from 0 to 1
}

// Depreciated - use update() and XinputController.normState instead (to avoid multiple Xinput calls)
double XinputController::getNormLT() {
//return max(-1, (double)((getLT() - 128) / 127)); // Returns from -1 to 1
return (double)getLT() / 255; // Returns from 0 to 1
Expand Down
36 changes: 23 additions & 13 deletions OrbiterSteamController/XinputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
#include <Windows.h>
#include <xinput.h>

struct NormState {
double LX;
double LY;
double RX;
double RY;
double RT;
double LT;
};

class XinputController
{
int controllerNumber;
XINPUT_STATE state;

public:
XinputController();
~XinputController();

double getNormLX();
double getNormLY();
double getNormRX();
double getNormRY();
double getNormRT();
double getNormLT();

private:
void setControllerNumber();
void init();
void updateState();
Expand All @@ -28,5 +24,19 @@ class XinputController
int getRY();
int getRT();
int getLT();

public:
XINPUT_STATE state;
struct NormState normState;

XinputController();
~XinputController();
void update();
double getNormLX();
double getNormLY();
double getNormRX();
double getNormRY();
double getNormRT();
double getNormLT();
};

11 changes: 6 additions & 5 deletions OrbiterSteamController/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ DLLCLBK void InitModule(HINSTANCE hModule) {

DLLCLBK void opcPreStep(double simt, double simdt, double mjd) {
#ifdef DEBUG
sprintf(oapiDebugString(), "LX: %f LY: %f LT+RT: %f RX: %f RY: %f", controller.getNormLX(), controller.getNormLY(), controller.getNormRT() + (controller.getNormLT() * -1), controller.getNormRX(), controller.getNormRY());
sprintf(oapiDebugString(), "LX: %f LY: %f LT+RT: %f RX: %f RY: %f", controller.normState.LX, controller.normState.LY, controller.normState.RT + (controller.normState.LT * -1), controller.normState.RX, controller.normState.RY);
#endif

VESSEL* vessel = oapiGetFocusInterface();
vessel->SetControlSurfaceLevel(AIRCTRL_ELEVATOR, controller.getNormLY() * -1, false);
vessel->SetControlSurfaceLevel(AIRCTRL_AILERON, controller.getNormLX(), false);
vessel->SetControlSurfaceLevel(AIRCTRL_RUDDER, controller.getNormRT() + (controller.getNormLT() * -1), false);
controller.update();
vessel->SetControlSurfaceLevel(AIRCTRL_ELEVATOR, controller.normState.LY * -1, false);
vessel->SetControlSurfaceLevel(AIRCTRL_AILERON, controller.normState.LX, false);
vessel->SetControlSurfaceLevel(AIRCTRL_RUDDER, controller.normState.RT + (controller.normState.LT * -1), false);

// An experiment with controlling the view with the RS directly. Not really needed as you can bind either pad to a mouse with the right button held down
//oapiCameraSetCockpitDir((roundf(controller.getNormRX() * 1000) / 1000) * -1, roundf(controller.getNormRY() * 1000) / 1000, false);
//oapiCameraSetCockpitDir((roundf(controller.normState.RX * 1000) / 1000) * -1, roundf(controller.normState.RY * 1000) / 1000, false);
}

0 comments on commit f4d42fc

Please sign in to comment.