Class PS4Controller

java.lang.Object
edu.wpi.first.wpilibj.GenericHID
edu.wpi.first.wpilibj.PS4Controller

public class PS4Controller
extends GenericHID
Handle input from PS4 controllers connected to the Driver Station.

This class handles PS4 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 controller and the mapping of ports to hardware buttons depends on the code in the Driver Station.

Only first party controllers from Sony are guaranteed to have the correct mapping, and only through the official NI DS. Sim is not guaranteed to have the same mapping, as well as any 3rd party controllers.

  • Constructor Details

    • PS4Controller

      public PS4Controller​(int port)
      Construct an instance of a device.
      Parameters:
      port - The port index on the Driver Station that the device is plugged into.
  • Method Details

    • getLeftX

      public double getLeftX()
      Get the X axis value of left side of the controller.
      Returns:
      the axis value.
    • getRightX

      public double getRightX()
      Get the X axis value of right side of the controller.
      Returns:
      the axis value.
    • getLeftY

      public double getLeftY()
      Get the Y axis value of left side of the controller.
      Returns:
      the axis value.
    • getRightY

      public double getRightY()
      Get the Y axis value of right side of the controller.
      Returns:
      the axis value.
    • getL2Axis

      public double getL2Axis()
      Get the L2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as opposed to the usual [-1, 1].
      Returns:
      the axis value.
    • getR2Axis

      public double getR2Axis()
      Get the R2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as opposed to the usual [-1, 1].
      Returns:
      the axis value.
    • getL2Button

      public boolean getL2Button()
      Read the value of the left trigger button on the controller.
      Returns:
      The state of the button.
    • getR2Button

      public boolean getR2Button()
      Read the value of the right trigger button on the controller.
      Returns:
      The state of the button.
    • getL2ButtonPressed

      public boolean getL2ButtonPressed()
      Whether the L2 button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getR2ButtonPressed

      public boolean getR2ButtonPressed()
      Whether the R2 button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getL2ButtonReleased

      public boolean getL2ButtonReleased()
      Whether the L2 button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • getR2ButtonReleased

      public boolean getR2ButtonReleased()
      Whether the R2 button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • L2

      public BooleanEvent L2​(EventLoop loop)
      Constructs an event instance around the L2 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the L2 button's digital signal attached to the given loop.
    • R2

      public BooleanEvent R2​(EventLoop loop)
      Constructs an event instance around the R2 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the R2 button's digital signal attached to the given loop.
    • getL1Button

      public boolean getL1Button()
      Read the value of the L1 button on the controller.
      Returns:
      The state of the button.
    • getR1Button

      public boolean getR1Button()
      Read the value of the R1 button on the controller.
      Returns:
      The state of the button.
    • getL1ButtonPressed

      public boolean getL1ButtonPressed()
      Whether the L1 button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getR1ButtonPressed

      public boolean getR1ButtonPressed()
      Whether the R1 button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getL1ButtonReleased

      public boolean getL1ButtonReleased()
      Whether the L1 button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • getR1ButtonReleased

      public boolean getR1ButtonReleased()
      Whether the R1 button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • L1

      public BooleanEvent L1​(EventLoop loop)
      Constructs an event instance around the L1 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the L1 button's digital signal attached to the given loop.
    • R1

      public BooleanEvent R1​(EventLoop loop)
      Constructs an event instance around the R1 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the R1 button's digital signal attached to the given loop.
    • getL3Button

      public boolean getL3Button()
      Read the value of the L3 button (pressing the left analog stick) on the controller.
      Returns:
      The state of the button.
    • getR3Button

      public boolean getR3Button()
      Read the value of the R3 button (pressing the right analog stick) on the controller.
      Returns:
      The state of the button.
    • getL3ButtonPressed

      public boolean getL3ButtonPressed()
      Whether the L3 (left stick) button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getR3ButtonPressed

      public boolean getR3ButtonPressed()
      Whether the R3 (right stick) button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getL3ButtonReleased

      public boolean getL3ButtonReleased()
      Whether the L3 (left stick) button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • getR3ButtonReleased

      public boolean getR3ButtonReleased()
      Whether the R3 (right stick) button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • L3

      public BooleanEvent L3​(EventLoop loop)
      Constructs an event instance around the L3 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the L3 button's digital signal attached to the given loop.
    • R3

      public BooleanEvent R3​(EventLoop loop)
      Constructs an event instance around the R3 button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the R3 button's digital signal attached to the given loop.
    • getSquareButton

      public boolean getSquareButton()
      Read the value of the Square button on the controller.
      Returns:
      The state of the button.
    • getSquareButtonPressed

      public boolean getSquareButtonPressed()
      Whether the Square button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getSquareButtonReleased

      public boolean getSquareButtonReleased()
      Whether the Square button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • square

      public BooleanEvent square​(EventLoop loop)
      Constructs an event instance around the square button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the square button's digital signal attached to the given loop.
    • getCrossButton

      public boolean getCrossButton()
      Read the value of the Cross button on the controller.
      Returns:
      The state of the button.
    • getCrossButtonPressed

      public boolean getCrossButtonPressed()
      Whether the Cross button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getCrossButtonReleased

      public boolean getCrossButtonReleased()
      Whether the Cross button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • cross

      public BooleanEvent cross​(EventLoop loop)
      Constructs an event instance around the cross button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the cross button's digital signal attached to the given loop.
    • getTriangleButton

      public boolean getTriangleButton()
      Read the value of the Triangle button on the controller.
      Returns:
      The state of the button.
    • getTriangleButtonPressed

      public boolean getTriangleButtonPressed()
      Whether the Triangle button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getTriangleButtonReleased

      public boolean getTriangleButtonReleased()
      Whether the Triangle button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • triangle

      public BooleanEvent triangle​(EventLoop loop)
      Constructs an event instance around the triangle button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the triangle button's digital signal attached to the given loop.
    • getCircleButton

      public boolean getCircleButton()
      Read the value of the Circle button on the controller.
      Returns:
      The state of the button.
    • getCircleButtonPressed

      public boolean getCircleButtonPressed()
      Whether the Circle button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getCircleButtonReleased

      public boolean getCircleButtonReleased()
      Whether the Circle button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • circle

      public BooleanEvent circle​(EventLoop loop)
      Constructs an event instance around the circle button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the circle button's digital signal attached to the given loop.
    • getShareButton

      public boolean getShareButton()
      Read the value of the share button on the controller.
      Returns:
      The state of the button.
    • getShareButtonPressed

      public boolean getShareButtonPressed()
      Whether the share button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getShareButtonReleased

      public boolean getShareButtonReleased()
      Whether the share button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • share

      public BooleanEvent share​(EventLoop loop)
      Constructs an event instance around the share button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the share button's digital signal attached to the given loop.
    • getPSButton

      public boolean getPSButton()
      Read the value of the PS button on the controller.
      Returns:
      The state of the button.
    • getPSButtonPressed

      public boolean getPSButtonPressed()
      Whether the PS button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getPSButtonReleased

      public boolean getPSButtonReleased()
      Whether the PS button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • PS

      public BooleanEvent PS​(EventLoop loop)
      Constructs an event instance around the PS button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the PS button's digital signal attached to the given loop.
    • getOptionsButton

      public boolean getOptionsButton()
      Read the value of the options button on the controller.
      Returns:
      The state of the button.
    • getOptionsButtonPressed

      public boolean getOptionsButtonPressed()
      Whether the options button was pressed since the last check.
      Returns:
      Whether the button was pressed since the last check.
    • getOptionsButtonReleased

      public boolean getOptionsButtonReleased()
      Whether the options button was released since the last check.
      Returns:
      Whether the button was released since the last check.
    • options

      public BooleanEvent options​(EventLoop loop)
      Constructs an event instance around the options button's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the options button's digital signal attached to the given loop.
    • getTouchpad

      public boolean getTouchpad()
      Read the value of the touchpad on the controller.
      Returns:
      The state of the touchpad.
    • getTouchpadPressed

      public boolean getTouchpadPressed()
      Whether the touchpad was pressed since the last check.
      Returns:
      Whether the touchpad was pressed since the last check.
    • getTouchpadReleased

      public boolean getTouchpadReleased()
      Whether the touchpad was released since the last check.
      Returns:
      Whether the touchpad was released since the last check.
    • touchpad

      public BooleanEvent touchpad​(EventLoop loop)
      Constructs an event instance around the touchpad's digital signal.
      Parameters:
      loop - the event loop instance to attach the event to.
      Returns:
      an event instance representing the touchpad's digital signal attached to the given loop.