public class Gamepad
extends RobocolParsableBase
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 codes KEYCODE_BUTTON_SELECT and KEYCODE_BACK are both be handled as a "back" button event. Older Android devices (Kit Kat) map a Logitech F310 "back" button press to a KEYCODE_BUTTON_SELECT event. Newer Android devices (Marshmallow or greater) map this "back" button press to a KEYCODE_BACK event. Also, the REV Robotics Gamepad (REV-31-1159) has a "select" button instead of a "back" button on the gamepad.
The dpad is represented as 4 buttons, dpad_up, dpad_down, dpad_left, and dpad_right
Modifier and Type | Class and Description |
---|---|
static interface |
Gamepad.GamepadCallback
Optional callback interface for monitoring changes due to MotionEvents and KeyEvents.
|
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 and Description |
---|
Gamepad() |
Gamepad(Gamepad.GamepadCallback callback) |
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
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
|
public static final int ID_UNASSOCIATED
public static final int ID_SYNTHETIC
public float left_stick_x
public float left_stick_y
public float right_stick_x
public float right_stick_y
public boolean dpad_up
public boolean dpad_down
public boolean dpad_left
public boolean dpad_right
public boolean a
public boolean b
public boolean x
public boolean y
public boolean guide
public boolean start
public boolean back
public boolean left_bumper
public boolean right_bumper
public boolean left_stick_button
public boolean right_stick_button
public float left_trigger
public float right_trigger
public int id
public long timestamp
protected float dpadThreshold
protected float joystickDeadzone
public Gamepad()
public Gamepad(Gamepad.GamepadCallback callback)
public GamepadUser getUser()
public void setUser(GamepadUser user)
public void setGamepadId(int id)
public int getGamepadId()
public void setTimestamp(long timestamp)
SystemClock.uptimeMillis()
time base.public void refreshTimestamp()
public void copy(Gamepad gamepad) throws RobotCoreException
gamepad
- state to be copied fromRobotCoreException
- if the copy fails - gamepad will be in an unknown
state if this exception is thrownpublic void reset()
public void setJoystickDeadzone(float deadzone)
deadzone
- amount of joystick deadzonepublic void update(android.view.MotionEvent event)
event
- motion eventpublic void update(android.view.KeyEvent event)
event
- key eventpublic MsgType getRobocolMsgType()
public byte[] toByteArray() throws RobotCoreException
RobotCoreException
public void fromByteArray(byte[] byteArray) throws RobotCoreException
RobotCoreException
public boolean atRest()
public java.lang.String type()
public java.lang.String toString()
protected float cleanMotionValues(float number)
protected boolean pressed(android.view.KeyEvent event)
protected void callCallback()
public static void enableWhitelistFilter(int vendorId, int productId)
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.
vendorId
- the vendor IDproductId
- the product IDpublic static void clearWhitelistFilter()
public static boolean isGamepadDevice(int deviceId)
deviceId
- device ID