com.qualcomm.robotcore.hardware

## Class Gamepad

- java.lang.Object
- - RobocolParsableBase
  - - com.qualcomm.robotcore.hardware.Gamepad

-     public class Gamepad
      extends RobocolParsableBase

  Monitor a hardware gamepad.

  The buttons, analog sticks, and triggers are represented a public member variables that can be read from or written to directly.

  Analog sticks are represented as floats that range from -1.0 to +1.0. They will be 0.0 while at rest. The horizontal axis is labeled x, and the vertical axis is labeled y.

  Triggers are represented as floats that range from 0.0 to 1.0. They will be at 0.0 while at rest.

  Buttons are boolean values. They will be true if the button is pressed, otherwise they will be false.

  The dpad is represented as 4 buttons, dpad_up, dpad_down, dpad_left, and dpad_right

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `Gamepad.GamepadCallback` Optional callback interface for monitoring changes due to MotionEvents and KeyEvents.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `boolean` | `a` button a
    `boolean` | `b` button b
    `boolean` | `back` button back
    `boolean` | `dpad_down` dpad down
    `boolean` | `dpad_left` dpad left
    `boolean` | `dpad_right` dpad right
    `boolean` | `dpad_up` dpad up
    `protected float` | `dpadThreshold` DPAD button will be considered pressed when the movement crosses this threshold
    `boolean` | `guide` button guide - often the large button in the middle of the controller.
    `int` | `id` ID assigned to this gamepad by the OS.
    `static int` | `ID_SYNTHETIC` A gamepad with a phantom id a synthetic one made up by the system
    `static int` | `ID_UNASSOCIATED` A gamepad with an ID equal to ID_UNASSOCIATED has not been associated with any device.
    `protected float` | `joystickDeadzone` If the motion value is less than the threshold, the controller will be considered at rest
    `boolean` | `left_bumper` button left bumper
    `boolean` | `left_stick_button` left stick button
    `float` | `left_stick_x` left analog stick horizontal axis
    `float` | `left_stick_y` left analog stick vertical axis
    `float` | `left_trigger` left trigger
    `boolean` | `right_bumper` button right bumper
    `boolean` | `right_stick_button` right stick button
    `float` | `right_stick_x` right analog stick horizontal axis
    `float` | `right_stick_y` right analog stick vertical axis
    `float` | `right_trigger` right trigger
    `boolean` | `start` button start
    `long` | `timestamp` Relative timestamp of the last time an event was detected
    `boolean` | `x` button x
    `boolean` | `y` button y

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Gamepad()`
    `Gamepad(Gamepad.GamepadCallback callback)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `atRest()` Are all analog sticks and triggers in their rest position?
    `protected void` | `callCallback()`
    `protected float` | `cleanMotionValues(float number)`
    `static void` | `clearWhitelistFilter()` Clear the device whitelist filter.
    `void` | `copy(Gamepad gamepad)` Copy the state of a gamepad into this gamepad
    `static void` | `enableWhitelistFilter(int vendorId, int productId)` Add a whitelist filter for a specific device vendor/product ID.
    `void` | `fromByteArray(byte[] byteArray)`
    `int` | `getGamepadId()`
    `MsgType` | `getRobocolMsgType()`
    `GamepadUser` | `getUser()`
    `static boolean` | `isGamepadDevice(int deviceId)` Does this device ID belong to a gamepad device?
    `protected boolean` | `pressed(android.view.KeyEvent event)`
    `void` | `refreshTimestamp()` Refreshes the Gamepad's timestamp to be the current time.
    `void` | `reset()` Reset this gamepad into its initial state
    `void` | `setGamepadId(int id)`
    `void` | `setJoystickDeadzone(float deadzone)` Set the joystick deadzone.
    `void` | `setTimestamp(long timestamp)` Sets the time at which this Gamepad last changed its state, in the `android.os.SystemClock#uptimeMillis` time base.
    `void` | `setUser(GamepadUser user)`
    `byte[]` | `toByteArray()`
    `java.lang.String` | `toString()` Display a summary of this gamepad, including the state of all buttons, analog sticks, and triggers
    `java.lang.String` | `type()` Get the type of gamepad as a String.
    `void` | `update(android.view.KeyEvent event)` Update the gamepad based on a KeyEvent
    `void` | `update(android.view.MotionEvent event)` Update the gamepad based on a MotionEvent

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### ID_UNASSOCIATED

          public static final int ID_UNASSOCIATED

      A gamepad with an ID equal to ID_UNASSOCIATED has not been associated with any device.
      See Also:
        Constant Field Values

    - #### ID_SYNTHETIC

          public static final int ID_SYNTHETIC

      A gamepad with a phantom id a synthetic one made up by the system
      See Also:
        Constant Field Values

    - #### left_stick_x

          public float left_stick_x

      left analog stick horizontal axis

    - #### left_stick_y

          public float left_stick_y

      left analog stick vertical axis

    - #### right_stick_x

          public float right_stick_x

      right analog stick horizontal axis

    - #### right_stick_y

          public float right_stick_y

      right analog stick vertical axis

    - #### dpad_up

          public boolean dpad_up

      dpad up

    - #### dpad_down

          public boolean dpad_down

      dpad down

    - #### dpad_left

          public boolean dpad_left

      dpad left

    - #### dpad_right

          public boolean dpad_right

      dpad right

    - #### a

          public boolean a

      button a

    - #### b

          public boolean b

      button b

    - #### x

          public boolean x

      button x

    - #### y

          public boolean y

      button y

    - #### guide

          public boolean guide

      button guide - often the large button in the middle of the controller. The OS may capture this button before it is sent to the app; in which case you'll never receive it.

    - #### start

          public boolean start

      button start

    - #### back

          public boolean back

      button back

    - #### left_bumper

          public boolean left_bumper

      button left bumper

    - #### right_bumper

          public boolean right_bumper

      button right bumper

    - #### left_stick_button

          public boolean left_stick_button

      left stick button

    - #### right_stick_button

          public boolean right_stick_button

      right stick button

    - #### left_trigger

          public float left_trigger

      left trigger

    - #### right_trigger

          public float right_trigger

      right trigger

    - #### id

          public int id

      ID assigned to this gamepad by the OS. This value can change each time the device is plugged in.

    - #### timestamp

          public long timestamp

      Relative timestamp of the last time an event was detected

    - #### dpadThreshold

          protected float dpadThreshold

      DPAD button will be considered pressed when the movement crosses this threshold

    - #### joystickDeadzone

          protected float joystickDeadzone

      If the motion value is less than the threshold, the controller will be considered at rest

  - ### Constructor Detail

    - #### Gamepad

          public Gamepad()

    - #### Gamepad

          public Gamepad(Gamepad.GamepadCallback callback)

  - ### Method Detail

    - #### getUser

          public GamepadUser getUser()

    - #### setUser

          public void setUser(GamepadUser user)

    - #### setGamepadId

          public void setGamepadId(int id)

    - #### getGamepadId

          public int getGamepadId()

    - #### setTimestamp

          public void setTimestamp(long timestamp)

      Sets the time at which this Gamepad last changed its state, in the `android.os.SystemClock#uptimeMillis` time base.

    - #### refreshTimestamp

          public void refreshTimestamp()

      Refreshes the Gamepad's timestamp to be the current time.

    - #### copy

          public void copy(Gamepad gamepad)
                    throws RobotCoreException

      Copy the state of a gamepad into this gamepad
      Parameters:
        `gamepad` - state to be copied from
      Throws:
        `RobotCoreException` - if the copy fails - gamepad will be in an unknown state if this exception is thrown

    - #### reset

          public void reset()

      Reset this gamepad into its initial state

    - #### setJoystickDeadzone

          public void setJoystickDeadzone(float deadzone)

      Set the joystick deadzone. Must be between 0 and 1.
      Parameters:
        `deadzone` - amount of joystick deadzone

    - #### update

          public void update(android.view.MotionEvent event)

      Update the gamepad based on a MotionEvent
      Parameters:
        `event` - motion event

    - #### update

          public void update(android.view.KeyEvent event)

      Update the gamepad based on a KeyEvent
      Parameters:
        `event` - key event

    - #### getRobocolMsgType

          public MsgType getRobocolMsgType()

    - #### toByteArray

          public byte[] toByteArray()
                             throws RobotCoreException

      Throws:
        `RobotCoreException`

    - #### fromByteArray

          public void fromByteArray(byte[] byteArray)
                             throws RobotCoreException

      Throws:
        `RobotCoreException`

    - #### atRest

          public boolean atRest()

      Are all analog sticks and triggers in their rest position?
      Returns:
        true if all analog sticks and triggers are at rest; otherwise false

    - #### type

          public java.lang.String type()

      Get the type of gamepad as a String. This method defaults to "Standard".
      Returns:
        gamepad type

    - #### toString

          public java.lang.String toString()

      Display a summary of this gamepad, including the state of all buttons, analog sticks, and triggers
      Returns:
        a summary

    - #### cleanMotionValues

          protected float cleanMotionValues(float number)

    - #### pressed

          protected boolean pressed(android.view.KeyEvent event)

    - #### callCallback

          protected void callCallback()

    - #### enableWhitelistFilter

          public static void enableWhitelistFilter(int vendorId,
                                                   int productId)

      Add a whitelist filter for a specific device vendor/product ID.

      This adds a whitelist to the gamepad detection method. If a device has been added to the whitelist, then only devices that match the given vendor ID and product ID will be considered gamepads. This method can be called multiple times to add multiple devices to the whitelist.

      If no whitelist entries have been added, then the default OS detection methods will be used.

      Parameters:
        `vendorId` - the vendor ID
        `productId` - the product ID

    - #### clearWhitelistFilter

          public static void clearWhitelistFilter()

      Clear the device whitelist filter.

    - #### isGamepadDevice

          public static boolean isGamepadDevice(int deviceId)

      Does this device ID belong to a gamepad device?
      Parameters:
        `deviceId` - device ID
      Returns:
        true, if gamepad device; false otherwise