2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-03-15 22:54:23 -04:00
|
|
|
/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2018-04-29 23:56:00 -07:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2018-09-20 21:59:46 -07:00
|
|
|
import edu.wpi.first.hal.HAL;
|
2018-05-24 00:31:04 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* GenericHID Interface.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
public abstract class GenericHID {
|
2016-11-18 23:05:37 -08:00
|
|
|
/**
|
|
|
|
|
* Represents a rumble output on the JoyStick.
|
|
|
|
|
*/
|
|
|
|
|
public enum RumbleType {
|
|
|
|
|
kLeftRumble, kRightRumble
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum HIDType {
|
|
|
|
|
kUnknown(-1),
|
|
|
|
|
kXInputUnknown(0),
|
|
|
|
|
kXInputGamepad(1),
|
|
|
|
|
kXInputWheel(2),
|
|
|
|
|
kXInputArcadeStick(3),
|
|
|
|
|
kXInputFlightStick(4),
|
|
|
|
|
kXInputDancePad(5),
|
|
|
|
|
kXInputGuitar(6),
|
|
|
|
|
kXInputGuitar2(7),
|
|
|
|
|
kXInputDrumKit(8),
|
|
|
|
|
kXInputGuitar3(11),
|
|
|
|
|
kXInputArcadePad(19),
|
|
|
|
|
kHIDJoystick(20),
|
|
|
|
|
kHIDGamepad(21),
|
|
|
|
|
kHIDDriving(22),
|
|
|
|
|
kHIDFlight(23),
|
|
|
|
|
kHID1stPerson(24);
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("MemberName")
|
|
|
|
|
public final int value;
|
2018-06-03 10:00:53 -07:00
|
|
|
@SuppressWarnings("PMD.UseConcurrentHashMap")
|
2018-04-29 23:56:00 -07:00
|
|
|
private static final Map<Integer, HIDType> map = new HashMap<>();
|
2016-11-18 23:05:37 -08:00
|
|
|
|
2017-10-17 21:47:55 -07:00
|
|
|
HIDType(int value) {
|
2016-11-18 23:05:37 -08:00
|
|
|
this.value = value;
|
|
|
|
|
}
|
2018-04-29 23:56:00 -07:00
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
for (HIDType hidType : HIDType.values()) {
|
|
|
|
|
map.put(hidType.value, hidType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HIDType of(int value) {
|
2018-06-03 10:00:53 -07:00
|
|
|
return map.get(value);
|
2018-04-29 23:56:00 -07:00
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
|
|
|
|
* Which hand the Human Interface Device is associated with.
|
|
|
|
|
*/
|
2016-07-13 23:39:58 -07:00
|
|
|
public enum Hand {
|
|
|
|
|
kLeft(0), kRight(1);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-20 12:07:40 -04:00
|
|
|
@SuppressWarnings("MemberName")
|
2015-06-25 15:07:55 -04:00
|
|
|
public final int value;
|
2014-10-17 14:46:25 -04:00
|
|
|
|
2017-10-17 21:47:55 -07:00
|
|
|
Hand(int value) {
|
2015-06-25 15:07:55 -04:00
|
|
|
this.value = value;
|
2014-10-17 14:46:25 -04:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
private DriverStation m_ds;
|
2016-11-18 23:05:37 -08:00
|
|
|
private final int m_port;
|
2017-10-27 21:45:56 -07:00
|
|
|
private int m_outputs;
|
|
|
|
|
private short m_leftRumble;
|
|
|
|
|
private short m_rightRumble;
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
public GenericHID(int port) {
|
2017-10-27 21:45:56 -07:00
|
|
|
m_ds = DriverStation.getInstance();
|
2016-11-18 23:05:37 -08:00
|
|
|
m_port = port;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Get the x position of the HID.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return the x position of the HID
|
|
|
|
|
*/
|
|
|
|
|
public final double getX() {
|
|
|
|
|
return getX(Hand.kRight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Get the x position of HID.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param hand which hand, left or right
|
|
|
|
|
* @return the x position
|
|
|
|
|
*/
|
|
|
|
|
public abstract double getX(Hand hand);
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Get the y position of the HID.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return the y position
|
|
|
|
|
*/
|
|
|
|
|
public final double getY() {
|
|
|
|
|
return getY(Hand.kRight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Get the y position of the HID.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param hand which hand, left or right
|
|
|
|
|
* @return the y position
|
|
|
|
|
*/
|
|
|
|
|
public abstract double getY(Hand hand);
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Get the button value (starting at button 1).
|
|
|
|
|
*
|
|
|
|
|
* <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.
|
|
|
|
|
*
|
2020-03-15 22:54:23 -04:00
|
|
|
* <p>This method returns true if the button is being held down at the time
|
|
|
|
|
* that this method is being called.
|
|
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param button The button number to be read (starting at 1)
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
|
|
|
|
public boolean getRawButton(int button) {
|
|
|
|
|
return m_ds.getStickButton(m_port, (byte) button);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the button was pressed since the last check. Button indexes begin at
|
|
|
|
|
* 1.
|
|
|
|
|
*
|
2020-03-15 22:54:23 -04:00
|
|
|
* <p>This method returns true if the button went from not pressed to held down
|
|
|
|
|
* since the last time this method was called. This is useful if you only
|
|
|
|
|
* want to call a function once when you press the button.
|
|
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param button The button index, beginning at 1.
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
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.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2020-03-15 22:54:23 -04:00
|
|
|
* <p>This method returns true if the button went from held down to not pressed
|
|
|
|
|
* since the last time this method was called. This is useful if you only
|
|
|
|
|
* want to call a function once when you release the button.
|
|
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param button The button index, beginning at 1.
|
|
|
|
|
* @return Whether the button was released since the last check.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public boolean getRawButtonReleased(int button) {
|
|
|
|
|
return m_ds.getStickButtonReleased(m_port, button);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Get the value of the axis.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param axis The axis to read, starting at 0.
|
|
|
|
|
* @return The value of the axis.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public double getRawAxis(int axis) {
|
|
|
|
|
return m_ds.getStickAxis(m_port, axis);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Get the angle in degrees of a POV on the HID.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* <p>The POV angles start at 0 in the up direction, and increase clockwise (eg right is 90,
|
|
|
|
|
* upper-left is 315).
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @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.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public int getPOV(int pov) {
|
|
|
|
|
return m_ds.getStickPOV(m_port, pov);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
public int getPOV() {
|
|
|
|
|
return getPOV(0);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* For the current HID, return the number of POVs.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public int getPOVCount() {
|
|
|
|
|
return m_ds.getStickPOVCount(m_port);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* For the current HID, return the number of buttons.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public int getButtonCount() {
|
|
|
|
|
return m_ds.getStickButtonCount(m_port);
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2020-11-13 14:11:10 -05:00
|
|
|
/**
|
|
|
|
|
* Get if the HID is connected.
|
|
|
|
|
*
|
|
|
|
|
* @return true if the HID is connected
|
|
|
|
|
*/
|
|
|
|
|
public boolean isConnected() {
|
|
|
|
|
return m_ds.isJoystickConnected(m_port);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Get the type of the HID.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @return the type of the HID.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public HIDType getType() {
|
2018-04-29 23:56:00 -07:00
|
|
|
return HIDType.of(m_ds.getJoystickType(m_port));
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Get the name of the HID.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @return the name of the HID.
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @return The port number of the HID.
|
|
|
|
|
*/
|
|
|
|
|
public int getPort() {
|
|
|
|
|
return m_port;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Set a single HID output value for the HID.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @param outputNumber The index of the output to set (1-32)
|
|
|
|
|
* @param value The value to set the output to
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
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);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Set all HID output values for the HID.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @param value The 32 bit output value (1 bit for each output)
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
public void setOutputs(int value) {
|
|
|
|
|
m_outputs = value;
|
|
|
|
|
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-11-18 23:05:37 -08:00
|
|
|
* Set the rumble output for the HID. The DS currently supports 2 rumble values, left rumble and
|
|
|
|
|
* right rumble.
|
2016-05-20 12:07:40 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @param type Which rumble value to set
|
|
|
|
|
* @param value The normalized value (0 to 1) to set the rumble to
|
2015-06-25 15:07:55 -04:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
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);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|