diff --git a/Android/app/src/main/java/com/example/androidsteering/FragmentConnection.java b/Android/app/src/main/java/com/example/androidsteering/FragmentConnection.java index 72e6fd4..aeaa958 100644 --- a/Android/app/src/main/java/com/example/androidsteering/FragmentConnection.java +++ b/Android/app/src/main/java/com/example/androidsteering/FragmentConnection.java @@ -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(); diff --git a/Android/app/src/main/java/com/example/androidsteering/FragmentControlAlter.java b/Android/app/src/main/java/com/example/androidsteering/FragmentControlAlter.java index d1433e2..a1123ee 100644 --- a/Android/app/src/main/java/com/example/androidsteering/FragmentControlAlter.java +++ b/Android/app/src/main/java/com/example/androidsteering/FragmentControlAlter.java @@ -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); diff --git a/Android/app/src/main/java/com/example/androidsteering/FragmentControlDefault.java b/Android/app/src/main/java/com/example/androidsteering/FragmentControlDefault.java index b5eaca9..65ab0ad 100644 --- a/Android/app/src/main/java/com/example/androidsteering/FragmentControlDefault.java +++ b/Android/app/src/main/java/com/example/androidsteering/FragmentControlDefault.java @@ -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); - } } diff --git a/Android/app/src/main/java/com/example/androidsteering/FragmentControlPad.java b/Android/app/src/main/java/com/example/androidsteering/FragmentControlPad.java index 1c0d090..63057fa 100644 --- a/Android/app/src/main/java/com/example/androidsteering/FragmentControlPad.java +++ b/Android/app/src/main/java/com/example/androidsteering/FragmentControlPad.java @@ -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); diff --git a/Android/app/src/main/java/com/example/androidsteering/MainActivity.java b/Android/app/src/main/java/com/example/androidsteering/MainActivity.java index 7e625c5..a550159 100644 --- a/Android/app/src/main/java/com/example/androidsteering/MainActivity.java +++ b/Android/app/src/main/java/com/example/androidsteering/MainActivity.java @@ -340,6 +340,7 @@ public void connectionButtonOnClick(View view) { ((Button)view).setText(R.string.buttonConnect); } + else ((Button)view).setText(R.string.buttonDisconnect); }); }); threadDisconnect.start(); @@ -368,6 +369,7 @@ public void connectionButtonOnClick(View view) } ((Button)view).setText(R.string.buttonDisconnect); } + else ((Button)view).setText(R.string.buttonConnect); }); }); threadConnect.start(); diff --git a/Windows/README.md b/Windows/README.md index 847ec2e..422a390 100644 --- a/Windows/README.md +++ b/Windows/README.md @@ -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 ------ diff --git a/Windows/SteeringWheel/Connection.cs b/Windows/SteeringWheel/Connection.cs index 914bf56..3060bc4 100644 --- a/Windows/SteeringWheel/Connection.cs +++ b/Windows/SteeringWheel/Connection.cs @@ -18,7 +18,7 @@ namespace SteeringWheel /// public class SharedBuffer { - private const int MAX_SIZE = 20; + private const int MAX_SIZE = 5; private readonly List buffer = new List(); public void AddData(bool v1, int v2, float v3) { @@ -175,7 +175,7 @@ private void ConnectBluetooth() isConnectionAllowed = true; BluetoothClient tmp = null; bthTargetDeviceID = null; - while (isConnectionAllowed) + while (isConnectionAllowed && bthListener != null) { try { @@ -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 @@ -595,7 +610,7 @@ public void Disconnect() { try { - bthListener.Server.Dispose(); + if(bthListener.Server != null) bthListener.Server.Dispose(); bthListener.Stop(); bthListener = null; } diff --git a/Windows/SteeringWheel/Controller.cs b/Windows/SteeringWheel/Controller.cs index 27fcca0..b0faf9b 100644 --- a/Windows/SteeringWheel/Controller.cs +++ b/Windows/SteeringWheel/Controller.cs @@ -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; @@ -125,7 +125,7 @@ public void Destroy() { if (!updateThread.Join(MAX_WAIT_TIME)) updateThread.Abort(); } - joystick.ResetAll(); + ResetVJoy(); joystick.RelinquishVJD(joystickID); } @@ -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); @@ -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; @@ -341,6 +341,22 @@ private void SetupVJoy() } } + /// + /// reset vjoy controller axis + /// + 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); + } + /// /// check necessary components according to xbox controller /// @@ -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); @@ -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;