Skip to content

Commit 4a8d454

Browse files
committed
Improvement: external dinput pedals support
1 parent 6d24057 commit 4a8d454

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

Source/DSAdvance/DSAdvance.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#include <atlstr.h>
1616
#include <dbt.h>
1717
#include <chrono>
18+
#include <mmsystem.h>
1819

1920
#pragma comment(lib, "winmm.lib")
2021

2122
void ArduinoRead()
2223
{
2324
DWORD bytesRead;
2425

25-
while (AppStatus.ExternalPedalsConnected) {
26+
while (AppStatus.ExternalPedalsArduinoConnected) {
2627
ReadFile(hSerial, &PedalsValues, sizeof(PedalsValues), &bytesRead, 0);
2728

2829
if (PedalsValues[0] > 1.0 || PedalsValues[0] < 0 || PedalsValues[1] > 1.0 || PedalsValues[1] < 0)
@@ -438,8 +439,10 @@ void MainTextUpdate() {
438439
printf_s(" Emulation: Keyboard and mouse (%s).\n Change profiles with \"ALT + Up | Down\" or \"PS + DPAD Up | Down\".\n", KMProfiles[ProfileIndex].c_str());
439440
printf(" Press \"ALT + Q | Left | Right\" or \"PS + DPAD Left | Right\" to switch emulation.\n");
440441

441-
if (AppStatus.ExternalPedalsConnected)
442-
printf(" External pedals connected.\n");
442+
if (AppStatus.ExternalPedalsDInputConnected)
443+
printf(" External pedals (DInput) connected.\n");
444+
if (AppStatus.ExternalPedalsArduinoConnected)
445+
printf(" External pedals (Arduino) connected.\n");
443446

444447
if (AppStatus.AimMode == AimMouseMode) printf(" AIM mode = mouse"); else printf(" AIM mode = mouse-joystick");
445448
printf(", press \"ALT + A\" or \"PS + R1\" to switch.\n");
@@ -501,7 +504,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
501504

502505
int main(int argc, char **argv)
503506
{
504-
SetConsoleTitle("DSAdvance 0.9.5");
507+
SetConsoleTitle("DSAdvance 0.9.6");
505508

506509
WNDCLASS AppWndClass = {};
507510
AppWndClass.lpfnWndProc = WindowProc;
@@ -560,8 +563,31 @@ int main(int argc, char **argv)
560563
int MicCustomKey = KeyNameToKeyCode(IniFile.ReadString("Gamepad", "MicCustomKey", "NONE"));
561564
if (MicCustomKey == 0) AppStatus.ScreenshotMode = ScreenShotXboxGameBarMode; // If not set, then hide this mode
562565
else ScreenShotKey = MicCustomKey;
563-
566+
567+
// External pedals
564568
int ExternalPedalsCOMPort = IniFile.ReadInteger("ExternalPedals", "COMPort", 0);
569+
570+
JOYINFOEX joyInfo;
571+
JOYCAPS joyCaps;
572+
joyInfo.dwFlags = JOY_RETURNALL;
573+
joyInfo.dwSize = sizeof(joyInfo);
574+
int JoyIndex = JOYSTICKID1;
575+
576+
if (IniFile.ReadBoolean("ExternalPedals", "DInput", false)) {
577+
if (joyGetPosEx(JOYSTICKID1, &joyInfo) == JOYERR_NOERROR)
578+
if (joyGetDevCaps(JoyIndex, &joyCaps, sizeof(joyCaps)) == JOYERR_NOERROR && joyCaps.wNumButtons == 16) { // DualSense - 15, DigiJoy - 16
579+
JoyIndex = JOYSTICKID1;
580+
AppStatus.ExternalPedalsDInputConnected = true;
581+
ExternalPedalsCOMPort = 0;
582+
}
583+
584+
if (AppStatus.ExternalPedalsDInputConnected == false && joyGetPosEx(JOYSTICKID2, &joyInfo) == JOYERR_NOERROR) {
585+
JoyIndex = JOYSTICKID2;
586+
AppStatus.ExternalPedalsDInputConnected = true;
587+
ExternalPedalsCOMPort = 0;
588+
}
589+
}
590+
565591
if (ExternalPedalsCOMPort != 0) {
566592
char sPortName[32];
567593
sprintf_s(sPortName, "\\\\.\\COM%d", ExternalPedalsCOMPort);
@@ -582,7 +608,7 @@ int main(int argc, char **argv)
582608

583609
if (SetCommState(hSerial, &dcbSerialParams))
584610
{
585-
AppStatus.ExternalPedalsConnected = true;
611+
AppStatus.ExternalPedalsArduinoConnected = true;
586612
PurgeComm(hSerial, PURGE_TXCLEAR | PURGE_RXCLEAR);
587613
pArduinoReadThread = new std::thread(ArduinoRead);
588614
}
@@ -884,7 +910,14 @@ int main(int argc, char **argv)
884910
report.bLeftTrigger = DeadZoneAxis(InputState.lTrigger, CurGamepad.Triggers.DeadZoneLeft) * 255;
885911
report.bRightTrigger = DeadZoneAxis(InputState.rTrigger, CurGamepad.Triggers.DeadZoneRight) * 255;
886912

887-
if (AppStatus.ExternalPedalsConnected) {
913+
// External pedals
914+
if (AppStatus.ExternalPedalsDInputConnected && joyGetPosEx(JoyIndex, &joyInfo) == JOYERR_NOERROR) {
915+
if (DeadZoneAxis(InputState.lTrigger, CurGamepad.Triggers.DeadZoneLeft) == 0)
916+
report.bLeftTrigger = joyInfo.dwVpos / 256;
917+
if (DeadZoneAxis(InputState.rTrigger, CurGamepad.Triggers.DeadZoneRight) == 0)
918+
report.bRightTrigger = joyInfo.dwUpos / 256;
919+
920+
} else if (AppStatus.ExternalPedalsArduinoConnected) {
888921
if (DeadZoneAxis(InputState.lTrigger, CurGamepad.Triggers.DeadZoneLeft) == 0)
889922
report.bLeftTrigger = PedalsValues[0] * 255;
890923
if (DeadZoneAxis(InputState.rTrigger, CurGamepad.Triggers.DeadZoneRight) == 0)
@@ -1108,8 +1141,8 @@ int main(int argc, char **argv)
11081141
if (CurGamepad.HidHandle != NULL)
11091142
hid_close(CurGamepad.HidHandle);
11101143

1111-
if (AppStatus.ExternalPedalsConnected) {
1112-
AppStatus.ExternalPedalsConnected = false;
1144+
if (AppStatus.ExternalPedalsArduinoConnected) {
1145+
AppStatus.ExternalPedalsArduinoConnected = false;
11131146
pArduinoReadThread->join();
11141147
delete pArduinoReadThread;
11151148
pArduinoReadThread = nullptr;

Source/DSAdvance/DSAdvance.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ struct _AppStatus {
178178
bool ChangeModesWithoutPress = false;
179179
bool ShowBatteryStatus = false;
180180
int ScreenshotMode = 0;
181-
bool ExternalPedalsConnected = false;
181+
bool ExternalPedalsArduinoConnected = false;
182+
bool ExternalPedalsDInputConnected = false;
182183
struct _Gamepad
183184
{
184185
bool BTReset = true;

0 commit comments

Comments
 (0)