Added functions for detecting button press and release events (#626)

I also shuffled around the HID interfaces to be more intuitive, deprecated some
Joystick and XboxController member functions, and deprecated the JoystickBase
and GamepadBase classes.

Supersedes #89.
This commit is contained in:
Tyler Veness
2017-10-27 21:45:56 -07:00
committed by Peter Johnson
parent c33fca34e9
commit 21585f70a8
17 changed files with 1334 additions and 724 deletions

View File

@@ -81,6 +81,11 @@ public class DriverStation implements RobotState.Interface {
private HALJoystickAxes[] m_joystickAxesCache = new HALJoystickAxes[kJoystickPorts];
private HALJoystickPOVs[] m_joystickPOVsCache = new HALJoystickPOVs[kJoystickPorts];
private HALJoystickButtons[] m_joystickButtonsCache = new HALJoystickButtons[kJoystickPorts];
// Joystick button rising/falling edge flags
HALJoystickButtons[] m_joystickButtonsPressed = new HALJoystickButtons[kJoystickPorts];
HALJoystickButtons[] m_joystickButtonsReleased = new HALJoystickButtons[kJoystickPorts];
// preallocated byte buffer for button count
private ByteBuffer m_buttonCountBuffer = ByteBuffer.allocateDirect(1);
@@ -126,6 +131,9 @@ public class DriverStation implements RobotState.Interface {
m_joystickButtonsCache[i] = new HALJoystickButtons();
m_joystickAxesCache[i] = new HALJoystickAxes(HAL.kMaxJoystickAxes);
m_joystickPOVsCache[i] = new HALJoystickPOVs(HAL.kMaxJoystickPOVs);
m_joystickButtonsPressed[i].m_buttons = 0;
m_joystickButtonsReleased[i].m_buttons = 0;
}
m_controlWordMutex = new Object();
@@ -188,6 +196,89 @@ public class DriverStation implements RobotState.Interface {
HAL.sendError(isError, code, false, error, locString, printTrace ? traceString : "", true);
}
/**
* The state of one joystick button. Button indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return The state of the joystick button.
*/
public boolean getStickButton(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
retVal = (m_joystickButtons[stick].m_buttons & 1 << (button - 1)) != 0;
}
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
}
/**
* Whether one joystick button was pressed since the last check. Button indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return Whether the joystick button was pressed since the last check.
*/
boolean getStickButtonPressed(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
// If button was pressed, clear flag and return true
if ((m_joystickButtonsPressed[stick].m_buttons & 1 << (button - 1)) != 0) {
m_joystickButtonsPressed[stick].m_buttons &= ~(1 << (button - 1));
return true;
} else {
return false;
}
}
/**
* Whether one joystick button was released since the last check. Button indexes
* begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return Whether the joystick button was released since the last check.
*/
boolean getStickButtonReleased(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
// If button was released, clear flag and return true
if ((m_joystickButtonsReleased[stick].m_buttons & 1 << (button - 1)) != 0) {
m_joystickButtonsReleased[stick].m_buttons &= ~(1 << (button - 1));
return true;
} else {
return false;
}
}
/**
* Get the value of the axis on a joystick. This depends on the mapping of the joystick connected
* to the specified port.
@@ -266,38 +357,6 @@ public class DriverStation implements RobotState.Interface {
}
}
/**
* The state of one joystick button. Button indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return The state of the joystick button.
*/
public boolean getStickButton(final int stick, byte button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
retVal = ((0x1 << (button - 1)) & m_joystickButtons[stick].m_buttons) != 0;
}
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
}
/**
* Returns the number of axes on a given joystick port.
*
@@ -717,6 +776,16 @@ public class DriverStation implements RobotState.Interface {
// lock joystick mutex to swap cache data
synchronized (m_joystickMutex) {
for (int i = 0; i < kJoystickPorts; i++) {
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
m_joystickButtonsPressed[i].m_buttons |=
~m_joystickButtons[i].m_buttons & m_joystickButtonsCache[i].m_buttons;
// If buttons were pressed and aren't now, set flags in m_buttonsReleased
m_joystickButtonsReleased[i].m_buttons |=
m_joystickButtons[i].m_buttons & ~m_joystickButtonsCache[i].m_buttons;
}
// move cache to actual data
HALJoystickAxes[] currentAxes = m_joystickAxes;
m_joystickAxes = m_joystickAxesCache;

View File

@@ -9,7 +9,10 @@ package edu.wpi.first.wpilibj;
/**
* Gamepad Interface.
*
* @deprecated Inherit directly from GenericHID instead.
*/
@Deprecated
public abstract class GamepadBase extends GenericHID {
public GamepadBase(int port) {
super(port);

View File

@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* GenericHID Interface.
*/
@@ -59,9 +61,14 @@ public abstract class GenericHID {
}
}
private DriverStation m_ds;
private final int m_port;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
public GenericHID(int port) {
m_ds = DriverStation.getInstance();
m_port = port;
}
@@ -100,20 +107,49 @@ public abstract class GenericHID {
public abstract double getY(Hand hand);
/**
* Get the raw axis.
* Get the button value (starting at button 1).
*
* @param which index of the axis
* @return the raw value of the selected axis
* <p>The buttons are returned in a single 16 bit value with one bit representing the state of
* each button. The appropriate button is returned as a boolean value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
*/
public abstract double getRawAxis(int which);
public boolean getRawButton(int button) {
return m_ds.getStickButton(m_port, (byte) button);
}
/**
* Is the given button pressed.
* Whether the button was pressed since the last check. Button indexes begin at
* 1.
*
* @param button which button number
* @return true if the button is pressed
* @param button The button index, beginning at 1.
* @return Whether the button was pressed since the last check.
*/
public abstract boolean getRawButton(int button);
public boolean getRawButtonPressed(int button) {
return m_ds.getStickButtonPressed(m_port, (byte) button);
}
/**
* Whether the button was released since the last check. Button indexes begin at
* 1.
*
* @param button The button index, beginning at 1.
* @return Whether the button was released since the last check.
*/
public boolean getRawButtonReleased(int button) {
return m_ds.getStickButtonReleased(m_port, button);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(int axis) {
return m_ds.getStickAxis(m_port, axis);
}
/**
* Get the angle in degrees of a POV on the HID.
@@ -124,16 +160,63 @@ public abstract class GenericHID {
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
public abstract int getPOV(int pov);
public int getPOV(int pov) {
return m_ds.getStickPOV(m_port, pov);
}
public int getPOV() {
return getPOV(0);
}
/**
* Get the number of axes for the HID.
*
* @return the number of axis for the current HID
*/
public int getAxisCount() {
return m_ds.getStickAxisCount(m_port);
}
/**
* For the current HID, return the number of POVs.
*/
public abstract int getPOVCount();
public int getPOVCount() {
return m_ds.getStickPOVCount(m_port);
}
/**
* For the current HID, return the number of buttons.
*/
public int getButtonCount() {
return m_ds.getStickButtonCount(m_port);
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(m_port)];
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
public String getName() {
return m_ds.getJoystickName(m_port);
}
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return m_ds.getJoystickAxisType(m_port, axis);
}
/**
* Get the port number of the HID.
@@ -144,34 +227,26 @@ public abstract class GenericHID {
return m_port;
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
public abstract HIDType getType();
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
public abstract String getName();
/**
* Set a single HID output value for the HID.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
public abstract void setOutput(int outputNumber, boolean value);
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set all HID output values for the HID.
*
* @param value The 32 bit output value (1 bit for each output)
*/
public abstract void setOutputs(int value);
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set the rumble output for the HID. The DS currently supports 2 rumble values, left rumble and
@@ -180,5 +255,17 @@ public abstract class GenericHID {
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
public abstract void setRumble(RumbleType type, double value);
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -11,26 +11,25 @@ import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* Handle input from standard Joysticks connected to the Driver Station. This class handles standard
* input that comes from the Driver Station. Each time a value is requested the most recent value is
* returned. There is a single class instance for each joystick and the mapping of ports to hardware
* buttons depends on the code in the Driver Station.
* Handle input from standard Joysticks connected to the Driver Station.
*
* <p>This class handles standard input that comes from the Driver Station. Each time a value is
* requested the most recent value is returned. There is a single class instance for each joystick
* and the mapping of ports to hardware buttons depends on the code in the Driver Station.
*/
public class Joystick extends JoystickBase {
public class Joystick extends GenericHID {
static final byte kDefaultXAxis = 0;
static final byte kDefaultYAxis = 1;
static final byte kDefaultZAxis = 2;
static final byte kDefaultTwistAxis = 2;
static final byte kDefaultThrottleAxis = 3;
static final int kDefaultTriggerButton = 1;
static final int kDefaultTopButton = 2;
static final byte kMinNumAxes = 4;
/**
* Represents an analog axis on a joystick.
*/
public enum AxisType {
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4), kNumAxis(5);
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4);
@SuppressWarnings("MemberName")
public final int value;
@@ -41,10 +40,10 @@ public class Joystick extends JoystickBase {
}
/**
* Represents a digital button on the JoyStick.
* Represents a digital button on a joystick.
*/
public enum ButtonType {
kTrigger(0), kTop(1), kNumButton(2);
kTrigger(1), kTop(2);
@SuppressWarnings("MemberName")
public final int value;
@@ -54,12 +53,35 @@ public class Joystick extends JoystickBase {
}
}
private final DriverStation m_ds;
/**
* Represents a digital button on a joystick.
*/
private enum Button {
kTrigger(1), kTop(2);
@SuppressWarnings("MemberName")
public final int value;
Button(int value) {
this.value = value;
}
}
/**
* Represents an analog axis on a joystick.
*/
private enum Axis {
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4);
@SuppressWarnings("MemberName")
public final int value;
Axis(int value) {
this.value = value;
}
}
private final byte[] m_axes;
private final byte[] m_buttons;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
@@ -68,36 +90,131 @@ public class Joystick extends JoystickBase {
* @param port The port on the Driver Station that the joystick is plugged into.
*/
public Joystick(final int port) {
this(port, AxisType.kNumAxis.value, ButtonType.kNumButton.value);
super(port);
m_axes[AxisType.kX.value] = kDefaultXAxis;
m_axes[AxisType.kY.value] = kDefaultYAxis;
m_axes[AxisType.kZ.value] = kDefaultZAxis;
m_axes[AxisType.kTwist.value] = kDefaultTwistAxis;
m_axes[AxisType.kThrottle.value] = kDefaultThrottleAxis;
m_axes = new byte[Math.max(getAxisCount(), kMinNumAxes)];
m_buttons[ButtonType.kTrigger.value] = kDefaultTriggerButton;
m_buttons[ButtonType.kTop.value] = kDefaultTopButton;
m_axes[Axis.kX.value] = kDefaultXAxis;
m_axes[Axis.kY.value] = kDefaultYAxis;
m_axes[Axis.kZ.value] = kDefaultZAxis;
m_axes[Axis.kTwist.value] = kDefaultTwistAxis;
m_axes[Axis.kThrottle.value] = kDefaultThrottleAxis;
HAL.report(tResourceType.kResourceType_Joystick, port);
}
/**
* Protected version of the constructor to be called by sub-classes.
* Set the channel associated with the X axis.
*
* <p>This constructor allows the subclass to configure the number of constants for axes and
* buttons.
*
* @param port The port on the Driver Station that the joystick is plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
* @param channel The channel to set the axis to.
*/
protected Joystick(int port, int numAxisTypes, int numButtonTypes) {
super(port);
public void setXChannel(int channel) {
m_axes[Axis.kX.value] = (byte) channel;
}
m_ds = DriverStation.getInstance();
m_axes = new byte[numAxisTypes];
m_buttons = new byte[numButtonTypes];
/**
* Set the channel associated with the Y axis.
*
* @param channel The channel to set the axis to.
*/
public void setYChannel(int channel) {
m_axes[Axis.kY.value] = (byte) channel;
}
/**
* Set the channel associated with the Z axis.
*
* @param channel The channel to set the axis to.
*/
public void setZChannel(int channel) {
m_axes[Axis.kZ.value] = (byte) channel;
}
/**
* Set the channel associated with the throttle axis.
*
* @param channel The channel to set the axis to.
*/
public void setThrottleChannel(int channel) {
m_axes[Axis.kThrottle.value] = (byte) channel;
}
/**
* Set the channel associated with the twist axis.
*
* @param channel The channel to set the axis to.
*/
public void setTwistChannel(int channel) {
m_axes[Axis.kTwist.value] = (byte) channel;
}
/**
* Set the channel associated with a specified axis.
*
* @deprecated Use the more specific axis channel setter functions.
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
@Deprecated
public void setAxisChannel(AxisType axis, int channel) {
m_axes[axis.value] = (byte) channel;
}
/**
* Get the channel currently associated with the X axis.
*
* @return The channel for the axis.
*/
public int getXChannel() {
return m_axes[Axis.kX.value];
}
/**
* Get the channel currently associated with the Y axis.
*
* @return The channel for the axis.
*/
public int getYChannel() {
return m_axes[Axis.kY.value];
}
/**
* Get the channel currently associated with the Z axis.
*
* @return The channel for the axis.
*/
public int getZChannel() {
return m_axes[Axis.kZ.value];
}
/**
* Get the channel currently associated with the twist axis.
*
* @return The channel for the axis.
*/
public int getTwistChannel() {
return m_axes[Axis.kTwist.value];
}
/**
* Get the channel currently associated with the throttle axis.
*
* @return The channel for the axis.
*/
public int getThrottleChannel() {
return m_axes[Axis.kThrottle.value];
}
/**
* Get the channel currently associated with the specified axis.
*
* @deprecated Use the more specific axis channel getter functions.
* @param axis The axis to look up the channel for.
* @return The channel for the axis.
*/
@Deprecated
public int getAxisChannel(AxisType axis) {
return m_axes[axis.value];
}
/**
@@ -109,7 +226,7 @@ public class Joystick extends JoystickBase {
*/
@Override
public final double getX(Hand hand) {
return getRawAxis(m_axes[AxisType.kX.value]);
return getRawAxis(m_axes[Axis.kX.value]);
}
/**
@@ -121,12 +238,16 @@ public class Joystick extends JoystickBase {
*/
@Override
public final double getY(Hand hand) {
return getRawAxis(m_axes[AxisType.kY.value]);
return getRawAxis(m_axes[Axis.kY.value]);
}
@Override
public final double getZ(Hand hand) {
return getRawAxis(m_axes[AxisType.kZ.value]);
/**
* Get the z position of the HID.
*
* @return the z position
*/
public double getZ() {
return getRawAxis(m_axes[Axis.kZ.value]);
}
/**
@@ -136,7 +257,7 @@ public class Joystick extends JoystickBase {
* @return The Twist value of the joystick.
*/
public double getTwist() {
return getRawAxis(m_axes[AxisType.kTwist.value]);
return getRawAxis(m_axes[Axis.kTwist.value]);
}
/**
@@ -146,17 +267,7 @@ public class Joystick extends JoystickBase {
* @return The Throttle value of the joystick.
*/
public double getThrottle() {
return getRawAxis(m_axes[AxisType.kThrottle.value]);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(getPort(), axis);
return getRawAxis(m_axes[Axis.kThrottle.value]);
}
/**
@@ -165,9 +276,11 @@ public class Joystick extends JoystickBase {
* <p>This is for cases where the joystick axis is returned programmatically, otherwise one of the
* previous functions would be preferable (for example getX()).
*
* @deprecated Use the more specific axis getter functions.
* @param axis The axis to read.
* @return The value of the axis.
*/
@Deprecated
public double getAxis(final AxisType axis) {
switch (axis) {
case kX:
@@ -186,80 +299,57 @@ public class Joystick extends JoystickBase {
}
/**
* For the current joystick, return the number of axis.
* Read the state of the trigger on the joystick.
*
* @return The state of the trigger.
*/
public int getAxisCount() {
return m_ds.getStickAxisCount(getPort());
public boolean getTrigger() {
return getRawButton(Button.kTrigger.value);
}
/**
* Read the state of the trigger on the joystick.
* Whether the trigger was pressed since the last check.
*
* <p>Look up which button has been assigned to the trigger and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the trigger.
* @return Whether the button was pressed since the last check.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTrigger(Hand hand) {
return getRawButton(m_buttons[ButtonType.kTrigger.value]);
public boolean getTriggerPressed() {
return getRawButtonPressed(Button.kTrigger.value);
}
/**
* Whether the trigger was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getTriggerReleased() {
return getRawButtonReleased(Button.kTrigger.value);
}
/**
* Read the state of the top button on the joystick.
*
* <p>Look up which button has been assigned to the top and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the top button.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTop(Hand hand) {
return getRawButton(m_buttons[ButtonType.kTop.value]);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
public boolean getTop() {
return getRawButton(Button.kTop.value);
}
/**
* This is not supported for the Joystick. This method is only here to complete the GenericHID
* interface.
* Whether the top button was pressed since the last check.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the bumper (always false)
* @return Whether the button was pressed since the last check.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getBumper(Hand hand) {
return false;
public boolean getTopPressed() {
return getRawButtonPressed(Button.kTop.value);
}
/**
* Get the button value (starting at button 1).
* Whether the top button was released since the last check.
*
* <p>The appropriate button is returned as a boolean value.
*
* @param button The button number to be read (starting at 1).
* @return The state of the button.
* @return Whether the button was released since the last check.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* For the current joystick, return the number of buttons.
*/
public int getButtonCount() {
return m_ds.getStickButtonCount(getPort());
public boolean getTopReleased() {
return getRawButtonReleased(Button.kTop.value);
}
/**
@@ -267,18 +357,13 @@ public class Joystick extends JoystickBase {
*
* <p>The button type will be looked up in the list of buttons and then read.
*
* @deprecated Use Button enum values instead of ButtonType.
* @param button The type of button to read.
* @return The state of the button.
*/
@Deprecated
public boolean getButton(ButtonType button) {
switch (button) {
case kTrigger:
return getTrigger();
case kTop:
return getTop();
default:
return false;
}
return getRawButton(button.value);
}
/**
@@ -308,89 +393,4 @@ public class Joystick extends JoystickBase {
public double getDirectionDegrees() {
return Math.toDegrees(getDirectionRadians());
}
/**
* Get the channel currently associated with the specified axis.
*
* @param axis The axis to look up the channel for.
* @return The channel fr the axis.
*/
public int getAxisChannel(AxisType axis) {
return m_axes[axis.value];
}
/**
* Set the channel associated with a specified axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
public void setAxisChannel(AxisType axis, int channel) {
m_axes[axis.value] = (byte) channel;
}
/**
* Get the value of isXbox for the current joystick.
*
* @return A boolean that is true if the controller is an xbox controller.
*/
public boolean getIsXbox() {
return m_ds.getJoystickIsXbox(getPort());
}
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return m_ds.getJoystickAxisType(getPort(), axis);
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -9,7 +9,10 @@ package edu.wpi.first.wpilibj;
/**
* JoystickBase Interface.
*
* @deprecated Inherit directly from GenericHID instead.
*/
@Deprecated
public abstract class JoystickBase extends GenericHID {
public JoystickBase(int port) {
super(port);
@@ -40,52 +43,4 @@ public abstract class JoystickBase extends GenericHID {
* @return the throttle value
*/
public abstract double getThrottle();
/**
* Is the trigger pressed.
*
* @return true if pressed
*/
public final boolean getTrigger() {
return getTrigger(Hand.kRight);
}
/**
* Is the trigger pressed.
*
* @param hand which hand
* @return true if the trigger for the given hand is pressed
*/
public abstract boolean getTrigger(Hand hand);
/**
* Is the top button pressed.
*
* @return true if the top button is pressed
*/
public final boolean getTop() {
return getTop(Hand.kRight);
}
/**
* Is the top button pressed.
*
* @param hand which hand
* @return true if hte top button for the given hand is pressed
*/
public abstract boolean getTop(Hand hand);
public abstract int getPOV(int pov);
public abstract int getPOVCount();
public abstract HIDType getType();
public abstract String getName();
public abstract void setOutput(int outputNumber, boolean value);
public abstract void setOutputs(int value);
public abstract void setRumble(RumbleType type, double value);
}

View File

@@ -17,11 +17,29 @@ import edu.wpi.first.wpilibj.hal.HAL;
* requested the most recent value is returned. There is a single class instance for each controller
* and the mapping of ports to hardware buttons depends on the code in the Driver Station.
*/
public class XboxController extends GamepadBase {
private DriverStation m_ds;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
public class XboxController extends GenericHID {
/**
* Represents a digital button on an XboxController.
*/
private enum Button {
kBumperLeft(5),
kBumperRight(6),
kStickLeft(9),
kStickRight(10),
kA(1),
kB(2),
kX(3),
kY(4),
kBack(7),
kStart(8);
@SuppressWarnings("MemberName")
private int value;
Button(int value) {
this.value = value;
}
}
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
@@ -31,7 +49,6 @@ public class XboxController extends GamepadBase {
*/
public XboxController(final int port) {
super(port);
m_ds = DriverStation.getInstance();
// HAL.report(tResourceType.kResourceType_XboxController, port);
HAL.report(tResourceType.kResourceType_Joystick, port);
@@ -67,69 +84,6 @@ public class XboxController extends GamepadBase {
}
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(getPort(), axis);
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
@Override
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(5);
} else {
return getRawButton(6);
}
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the trigger (always false)
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTrigger(Hand hand) {
return false;
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the top button (always false)
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTop(Hand hand) {
return false;
}
/**
* Get the button value (starting at button 1).
*
* <p>The appropriate button is returned as a boolean value.
*
* @param button The button number to be read (starting at 1).
* @return The state of the button.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* Get the trigger axis value of the controller.
*
@@ -145,39 +99,45 @@ public class XboxController extends GamepadBase {
}
/**
* Read the value of the A button on the controller.
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
public boolean getAButton() {
return getRawButton(1);
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(Button.kBumperLeft.value);
} else {
return getRawButton(Button.kBumperRight.value);
}
}
/**
* Read the value of the B button on the controller.
* Whether the bumper was pressed since the last check.
*
* @return The state of the button.
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
public boolean getBButton() {
return getRawButton(2);
public boolean getBumperPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kBumperLeft.value);
} else {
return getRawButtonPressed(Button.kBumperRight.value);
}
}
/**
* Read the value of the X button on the controller.
* Whether the bumper was released since the last check.
*
* @return The state of the button.
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
public boolean getXButton() {
return getRawButton(3);
}
/**
* Read the value of the Y button on the controller.
*
* @return The state of the button.
*/
public boolean getYButton() {
return getRawButton(4);
public boolean getBumperReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kBumperLeft.value);
} else {
return getRawButtonReleased(Button.kBumperRight.value);
}
}
/**
@@ -186,22 +146,175 @@ public class XboxController extends GamepadBase {
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
@Override
public boolean getStickButton(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(9);
return getRawButton(Button.kStickLeft.value);
} else {
return getRawButton(10);
return getRawButton(Button.kStickRight.value);
}
}
/**
* Whether the stick button was pressed since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
public boolean getStickButtonPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kStickLeft.value);
} else {
return getRawButtonPressed(Button.kStickRight.value);
}
}
/**
* Whether the stick button was released since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
public boolean getStickButtonReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kStickLeft.value);
} else {
return getRawButtonReleased(Button.kStickRight.value);
}
}
/**
* Read the value of the A button on the controller.
*
* @return The state of the button.
*/
public boolean getAButton() {
return getRawButton(Button.kA.value);
}
/**
* Whether the A button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getAButtonPressed() {
return getRawButtonPressed(Button.kA.value);
}
/**
* Whether the A button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getAButtonReleased() {
return getRawButtonReleased(Button.kA.value);
}
/**
* Read the value of the B button on the controller.
*
* @return The state of the button.
*/
public boolean getBButton() {
return getRawButton(Button.kB.value);
}
/**
* Whether the B button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getBButtonPressed() {
return getRawButtonPressed(Button.kB.value);
}
/**
* Whether the B button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getBButtonReleased() {
return getRawButtonReleased(Button.kB.value);
}
/**
* Read the value of the X button on the controller.
*
* @return The state of the button.
*/
public boolean getXButton() {
return getRawButton(Button.kX.value);
}
/**
* Whether the X button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getXButtonPressed() {
return getRawButtonPressed(Button.kX.value);
}
/**
* Whether the X button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getXButtonReleased() {
return getRawButtonReleased(Button.kX.value);
}
/**
* Read the value of the Y button on the controller.
*
* @return The state of the button.
*/
public boolean getYButton() {
return getRawButton(Button.kY.value);
}
/**
* Whether the Y button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getYButtonPressed() {
return getRawButtonPressed(Button.kY.value);
}
/**
* Whether the Y button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getYButtonReleased() {
return getRawButtonReleased(Button.kY.value);
}
/**
* Read the value of the back button on the controller.
*
* @return The state of the button.
*/
public boolean getBackButton() {
return getRawButton(7);
return getRawButton(Button.kBack.value);
}
/**
* Whether the back button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getBackButtonPressed() {
return getRawButtonPressed(Button.kBack.value);
}
/**
* Whether the back button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getBackButtonReleased() {
return getRawButtonReleased(Button.kBack.value);
}
/**
@@ -210,53 +323,24 @@ public class XboxController extends GamepadBase {
* @return The state of the button.
*/
public boolean getStartButton() {
return getRawButton(8);
return getRawButton(Button.kStart.value);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
/**
* Whether the start button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getStartButtonPressed() {
return getRawButtonPressed(Button.kStart.value);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
}
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
/**
* Whether the start button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getStartButtonReleased() {
return getRawButtonReleased(Button.kStart.value);
}
}