diff --git a/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/driver/JamepadJoystickDriver.java b/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/driver/JamepadJoystickDriver.java index 3ad0189772..e08d38d318 100644 --- a/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/driver/JamepadJoystickDriver.java +++ b/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/driver/JamepadJoystickDriver.java @@ -37,6 +37,7 @@ public class JamepadJoystickDriver extends AbstractJoystickDriver { * Milliseconds to wait between reading joystick/gamepad values */ private static final int READ_DELAY_MILLISECONDS = 1; + private static final long CONNECT_DELAY_MILLISECONDS = 5000; private final ExecutorService joystickReadThread; private ControllerManager controllerManager; private JamepadJoystickDevice currentDevice; @@ -68,6 +69,8 @@ private void mainLoop() { LOGGER.info("readDataLoop returned - possible controller unplug/replug"); currentDevice = null; + joystickState.clear(); + notifyJoystickUpdated(); notifyDeviceChanged(); } else { try { @@ -102,7 +105,9 @@ private List getAvailableControllerButtons() { private void readDataLoop() { try { - while (isRunning && currentDevice.controller().isConnected()) { + // Wait for the controller to properly initialize to avoid getting junk data + Thread.sleep(CONNECT_DELAY_MILLISECONDS); + while (isRunning && currentDevice != null && currentDevice.controller().isConnected()) { readData(); Thread.sleep(READ_DELAY_MILLISECONDS); } diff --git a/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/model/JoystickState.java b/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/model/JoystickState.java index 14c6b60558..b3791818c9 100644 --- a/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/model/JoystickState.java +++ b/ugs-platform/ugs-platform-plugin-joystick/src/main/java/com/willwinder/ugs/nbp/joystick/model/JoystickState.java @@ -81,4 +81,9 @@ public void setDirty(boolean dirty) { public String toString() { return ToStringBuilder.reflectionToString(this); } + + public void clear() { + buttonsMap.keySet().forEach(action -> buttonsMap.put(action, false)); + axisMap.keySet().forEach(action -> axisMap.put(action, 0.0f)); + } }