Check if Joystick Button exists when requested and pass 0 and warn if it doesn't

Change-Id: I2194859ef8b263f1a20aba52ec154fb0a1fc8078
This commit is contained in:
Kevin O'Connor
2014-11-21 11:32:08 -05:00
parent 14a1e6ae8e
commit 7e5ed03d28
9 changed files with 41 additions and 26 deletions

View File

@@ -181,7 +181,6 @@ public class DriverStation implements RobotState.Interface {
// Get the status of all of the joysticks
for(byte stick = 0; stick < kJoystickPorts; stick++) {
m_joystickButtons[stick] = FRCNetworkCommunicationsLibrary.HALGetJoystickButtons(stick);
m_joystickAxes[stick] = FRCNetworkCommunicationsLibrary.HALGetJoystickAxes(stick);
m_joystickPOVs[stick] = FRCNetworkCommunicationsLibrary.HALGetJoystickPOVs(stick);
}
@@ -274,12 +273,20 @@ public class DriverStation implements RobotState.Interface {
* @param stick The joystick to read.
* @return The state of the buttons on the joystick.
*/
public int getStickButtons(final int stick) {
public boolean getStickButton(final int stick, byte button) {
if(stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
return (int)m_joystickButtons[stick];
ByteBuffer countBuffer = ByteBuffer.allocateDirect(1);
int buttons = FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)stick, countBuffer);
byte count = 0;
count = countBuffer.get();
if(button >= count) {
reportError("WARNING: Joystick Button " + button + " on port " + stick + " not available, check if controller is plugged in\n", false);
return false;
}
return ((0x1 << (button - 1)) & buttons) != 0;
}
/**

View File

@@ -276,7 +276,7 @@ public class Joystick extends GenericHID {
* @return The state of the button.
*/
public boolean getRawButton(final int button) {
return ((0x1 << (button - 1)) & m_ds.getStickButtons(m_port)) != 0;
return m_ds.getStickButton(m_port, (byte)button);
}
/**

View File

@@ -467,7 +467,7 @@ public class FRCNetworkCommunicationsLibrary extends JNIWrapper {
case 4:
return HALAllianceStationID.Blue2;
case 5:
return HALAllianceStationID.Blue3;
return HALAllianceStationID.Blue3;
default:
return null;
}