Skip to content

Commit

Permalink
Wait for the controller to initialize before reading data to avoid ge…
Browse files Browse the repository at this point in the history
…tting junk (#2371)
  • Loading branch information
breiler committed Nov 20, 2023
1 parent 2a0cc11 commit 9c7eb3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,8 @@ private void mainLoop() {

LOGGER.info("readDataLoop returned - possible controller unplug/replug");
currentDevice = null;
joystickState.clear();
notifyJoystickUpdated();
notifyDeviceChanged();
} else {
try {
Expand Down Expand Up @@ -102,7 +105,9 @@ private List<ControllerButton> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 9c7eb3e

Please sign in to comment.