[wpilibj] Fix joystick button edge synchronisation (#2433)

This commit is contained in:
David Vo
2020-08-20 12:46:51 +10:00
committed by GitHub
parent e680ba85fa
commit 950bbd6dc2

View File

@@ -317,21 +317,19 @@ public class DriverStation {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
m_cacheDataMutex.lock();
try {
if (button > m_joystickButtons[stick].m_count) {
// Unlock early so error printing isn't locked.
m_cacheDataMutex.unlock();
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
if (button <= m_joystickButtons[stick].m_count) {
return (m_joystickButtons[stick].m_buttons & 1 << (button - 1)) != 0;
}
return (m_joystickButtons[stick].m_buttons & 1 << (button - 1)) != 0;
} finally {
if (m_cacheDataMutex.isHeldByCurrentThread()) {
m_cacheDataMutex.unlock();
}
m_cacheDataMutex.unlock();
}
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
return false;
}
/**
@@ -349,27 +347,25 @@ public class DriverStation {
if (stick < 0 || stick >= kJoystickPorts) {
throw new IllegalArgumentException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_cacheDataMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
m_cacheDataMutex.lock();
try {
if (button <= m_joystickButtons[stick].m_count) {
// If button was pressed, clear flag and return true
if ((m_joystickButtonsPressed[stick] & 1 << (button - 1)) != 0) {
m_joystickButtonsPressed[stick] &= ~(1 << (button - 1));
retVal = true;
return true;
} else {
retVal = false;
return false;
}
}
} finally {
m_cacheDataMutex.unlock();
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
return false;
}
/**
@@ -388,27 +384,25 @@ public class DriverStation {
if (stick < 0 || stick >= kJoystickPorts) {
throw new IllegalArgumentException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_cacheDataMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
m_cacheDataMutex.lock();
try {
if (button <= m_joystickButtons[stick].m_count) {
// If button was released, clear flag and return true
if ((m_joystickButtonsReleased[stick] & 1 << (button - 1)) != 0) {
m_joystickButtonsReleased[stick] &= ~(1 << (button - 1));
retVal = true;
return true;
} else {
retVal = false;
return false;
}
}
} finally {
m_cacheDataMutex.unlock();
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
return false;
}
/**
@@ -429,20 +423,16 @@ public class DriverStation {
m_cacheDataMutex.lock();
try {
if (axis >= m_joystickAxes[stick].m_count) {
// Unlock early so error printing isn't locked.
m_cacheDataMutex.unlock();
reportJoystickUnpluggedWarning("Joystick axis " + axis + " on port " + stick
+ " not available, check if controller is plugged in");
return 0.0;
if (axis < m_joystickAxes[stick].m_count) {
return m_joystickAxes[stick].m_axes[axis];
}
return m_joystickAxes[stick].m_axes[axis];
} finally {
if (m_cacheDataMutex.isHeldByCurrentThread()) {
m_cacheDataMutex.unlock();
}
m_cacheDataMutex.unlock();
}
reportJoystickUnpluggedWarning("Joystick axis " + axis + " on port " + stick
+ " not available, check if controller is plugged in");
return 0.0;
}
/**
@@ -460,20 +450,16 @@ public class DriverStation {
m_cacheDataMutex.lock();
try {
if (pov >= m_joystickPOVs[stick].m_count) {
// Unlock early so error printing isn't locked.
m_cacheDataMutex.unlock();
reportJoystickUnpluggedWarning("Joystick POV " + pov + " on port " + stick
+ " not available, check if controller is plugged in");
return -1;
if (pov < m_joystickPOVs[stick].m_count) {
return m_joystickPOVs[stick].m_povs[pov];
}
} finally {
if (m_cacheDataMutex.isHeldByCurrentThread()) {
m_cacheDataMutex.unlock();
}
m_cacheDataMutex.unlock();
}
return m_joystickPOVs[stick].m_povs[pov];
reportJoystickUnpluggedWarning("Joystick POV " + pov + " on port " + stick
+ " not available, check if controller is plugged in");
return -1;
}
/**