Skip to content

Commit

Permalink
fix vjoy init axis bug & reduce delay
Browse files Browse the repository at this point in the history
  • Loading branch information
teamclouday committed Aug 21, 2021
1 parent 059574a commit 591c5c5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
assert activity != null;
ConnectionMode mode = activity.getConnectionMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
assert activity != null;
Button buttonLT = activity.findViewById(R.id.buttonLT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,4 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
{
return inflater.inflate(R.layout.frag_control_default, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
assert activity != null;
Button buttonLT = activity.findViewById(R.id.buttonLTPad);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public void connectionButtonOnClick(View view)
{
((Button)view).setText(R.string.buttonConnect);
}
else ((Button)view).setText(R.string.buttonDisconnect);
});
});
threadDisconnect.start();
Expand Down Expand Up @@ -368,6 +369,7 @@ public void connectionButtonOnClick(View view)
}
((Button)view).setText(R.string.buttonDisconnect);
}
else ((Button)view).setText(R.string.buttonConnect);
});
});
threadConnect.start();
Expand Down
2 changes: 1 addition & 1 deletion Windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Used to configure controller buttons and axis
Overflowing values are clamped
* Steering (Pitch)
* Real range is `(-90, 90)`, with resting mode in range `(-2, 2)`
* Interpreted range is `(-60, 60)`
* Interpreted range is `(-45, 45)`
Overflowing values are clamped

------
Expand Down
25 changes: 20 additions & 5 deletions Windows/SteeringWheel/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace SteeringWheel
/// </summary>
public class SharedBuffer
{
private const int MAX_SIZE = 20;
private const int MAX_SIZE = 5;
private readonly List<MotionData> buffer = new List<MotionData>();
public void AddData(bool v1, int v2, float v3)
{
Expand Down Expand Up @@ -175,7 +175,7 @@ private void ConnectBluetooth()
isConnectionAllowed = true;
BluetoothClient tmp = null;
bthTargetDeviceID = null;
while (isConnectionAllowed)
while (isConnectionAllowed && bthListener != null)
{
try
{
Expand Down Expand Up @@ -509,8 +509,23 @@ private bool TestClient(BluetoothClient client)
}
if (!stream.CanRead || !stream.CanWrite) return false;
// check received integer
if (stream.Read(receivedPack, 0, receivedPack.Length) <= 0) return false;
else if (DecodeInt(receivedPack) != DEVICE_CHECK_EXPECTED) return false;
int offset = 0;
do
{
int size = stream.Read(receivedPack, offset, receivedPack.Length - offset);
if (size <= 0)
{
Debug.WriteLine("[Connection] TestClient invalid received size (" + size + ")");
return false;
}
offset += size;
} while (offset < 4);
int decoded = DecodeInt(receivedPack);
if (decoded != DEVICE_CHECK_EXPECTED)
{
Debug.WriteLine("[Connection] TestClient decoded number not expected (" + decoded + ")");
return false;
}
// send back integer for verification
stream.Write(sentPack, 0, sentPack.Length);
// save valid target client ID
Expand Down Expand Up @@ -595,7 +610,7 @@ public void Disconnect()
{
try
{
bthListener.Server.Dispose();
if(bthListener.Server != null) bthListener.Server.Dispose();
bthListener.Stop();
bthListener = null;
}
Expand Down
38 changes: 27 additions & 11 deletions Windows/SteeringWheel/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Controller
private Thread updateThread;
private readonly int MAX_WAIT_TIME = 1500;
private bool isProcessAllowed = false;
private readonly float CAP_Steering = 60.0f; //treat max angle as 60 even though it can reach 90
private readonly float CAP_Steering = 45.0f; //treat max angle as 60 even though it can reach 90
private readonly float CAP_AccForward = 80.0f;
private readonly float CAP_AccBackward = 10.0f;

Expand Down Expand Up @@ -125,7 +125,7 @@ public void Destroy()
{
if (!updateThread.Join(MAX_WAIT_TIME)) updateThread.Abort();
}
joystick.ResetAll();
ResetVJoy();
joystick.RelinquishVJD(joystickID);
}

Expand Down Expand Up @@ -221,8 +221,9 @@ private void SetupUpdate()
{
if (!joystick.UpdateVJD(joystickID, ref joyReport))
{
AddLog("Failed to update vJoy controller state");
// AddLog("Failed to update vJoy controller state");
Debug.WriteLine("[Controller] updateThread failed to update VJD");
joystick.AcquireVJD(joystickID);
}
}
Thread.Sleep(updateInterval);
Expand Down Expand Up @@ -325,9 +326,8 @@ private void SetupVJoy()
{
joystickID = i;
joystick.GetVJDAxisMax(joystickID, HID_USAGES.HID_USAGE_X, ref axisMax);
joystick.ResetAll();
joystick.SetAxis((int)(axisMax / 2.0f), joystickID, HID_USAGES.HID_USAGE_X);
joyReport.bDevice = (byte)joystickID;
ResetVJoy();
AddLog("vJoy valid device found\nID = " + joystickID);
Debug.WriteLine("[Controller] SetupVJoy find valid device ID = " + joystickID);
vJoyInitialized = true;
Expand All @@ -341,6 +341,22 @@ private void SetupVJoy()
}
}

/// <summary>
/// reset vjoy controller axis
/// </summary>
private void ResetVJoy()
{
joystick.ResetAll();
joyReport.bHats = 0xFFFFFFFF;
joyReport.AxisX = (int)(axisMax / 2.0f);
joyReport.AxisXRot = (int)(axisMax / 2.0f);
joyReport.AxisY = (int)(axisMax / 2.0f);
joyReport.AxisYRot = (int)(axisMax / 2.0f);
joyReport.AxisZ = 0;
joyReport.AxisZRot = 0;
joystick.UpdateVJD(joystickID, ref joyReport);
}

/// <summary>
/// check necessary components according to xbox controller
/// </summary>
Expand Down Expand Up @@ -410,13 +426,13 @@ public void TriggerControl(ControlAxis axis)
joyReport.AxisX = 0;
break;
case ControlAxis.XRot:
joyReport.AxisXRot = (int)(axisMax / 2);
joyReport.AxisXRot = 0;
break;
case ControlAxis.Y:
joyReport.AxisY = (int)(axisMax / 2);
joyReport.AxisY = 0;
break;
case ControlAxis.YRot:
joyReport.AxisYRot = (int)(axisMax / 2);
joyReport.AxisYRot = 0;
break;
case ControlAxis.Z:
joyReport.AxisZ = (int)(axisMax / 2);
Expand All @@ -441,13 +457,13 @@ public void TriggerControl(ControlAxis axis)
joyReport.AxisX = (int)(axisMax / 2);
break;
case ControlAxis.XRot:
joyReport.AxisXRot = 0;
joyReport.AxisXRot = (int)(axisMax / 2);
break;
case ControlAxis.Y:
joyReport.AxisY = 0;
joyReport.AxisY = (int)(axisMax / 2);
break;
case ControlAxis.YRot:
joyReport.AxisYRot = 0;
joyReport.AxisYRot = (int)(axisMax / 2);
break;
case ControlAxis.Z:
joyReport.AxisZ = 0;
Expand Down

0 comments on commit 591c5c5

Please sign in to comment.