RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Gamepad.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.driverstation;
6
7import java.util.EnumSet;
8import java.util.Objects;
9import org.wpilib.driverstation.GenericHID.HIDType;
10import org.wpilib.driverstation.GenericHID.RumbleType;
11import org.wpilib.driverstation.GenericHID.SupportedOutput;
12import org.wpilib.driverstation.internal.DriverStationBackend;
13import org.wpilib.event.BooleanEvent;
14import org.wpilib.event.EventLoop;
15import org.wpilib.math.util.MathUtil;
16import org.wpilib.telemetry.TelemetryLoggable;
17import org.wpilib.telemetry.TelemetryTable;
18import org.wpilib.util.UsageReporting;
19
27public class Gamepad implements HIDDevice, TelemetryLoggable {
28 private static final double MAX_DEADBAND = Math.nextDown(1.0);
29
31 public enum Button {
33 FACE_DOWN(0, "FaceDownButton"),
35 FACE_RIGHT(1, "FaceRightButton"),
37 FACE_LEFT(2, "FaceLeftButton"),
39 FACE_UP(3, "FaceUpButton"),
41 BACK(4, "BackButton"),
43 GUIDE(5, "GuideButton"),
45 START(6, "StartButton"),
47 LEFT_STICK(7, "LeftStickButton"),
49 RIGHT_STICK(8, "RightStickButton"),
51 LEFT_BUMPER(9, "LeftBumperButton"),
53 RIGHT_BUMPER(10, "RightBumperButton"),
55 DPAD_UP(11, "DpadUpButton"),
57 DPAD_DOWN(12, "DpadDownButton"),
59 DPAD_LEFT(13, "DpadLeftButton"),
61 DPAD_RIGHT(14, "DpadRightButton"),
63 MISC_1(15, "Misc1Button"),
65 RIGHT_PADDLE_1(16, "RightPaddle1Button"),
67 LEFT_PADDLE_1(17, "LeftPaddle1Button"),
69 RIGHT_PADDLE_2(18, "RightPaddle2Button"),
71 LEFT_PADDLE_2(19, "LeftPaddle2Button"),
73 TOUCHPAD(20, "TouchpadButton"),
75 MISC_2(21, "Misc2Button"),
77 MISC_3(22, "Misc3Button"),
79 MISC_4(23, "Misc4Button"),
81 MISC_5(24, "Misc5Button"),
83 MISC_6(25, "Misc6Button");
84
86 public final int value;
87
88 private final String m_name;
89
90 Button(int value, String name) {
91 this.value = value;
92 this.m_name = name;
93 }
94
102 @Override
103 public String toString() {
104 return m_name;
105 }
106 }
107
109 public enum Axis {
111 LEFT_X(0, "LeftX"),
113 LEFT_Y(1, "LeftY"),
115 RIGHT_X(2, "RightX"),
117 RIGHT_Y(3, "RightY"),
119 LEFT_TRIGGER(4, "LeftTriggerAxis"),
121 RIGHT_TRIGGER(5, "RightTriggerAxis");
122
124 public final int value;
125
126 private final String m_name;
127
128 Axis(int value, String name) {
129 this.value = value;
130 this.m_name = name;
131 }
132
140 @Override
141 public String toString() {
142 return m_name;
143 }
144 }
145
146 private double m_leftXDeadband = 0.1;
147 private double m_leftYDeadband = 0.1;
148 private double m_rightXDeadband = 0.1;
149 private double m_rightYDeadband = 0.1;
150 private double m_leftTriggerDeadband = 0.01;
151 private double m_rightTriggerDeadband = 0.01;
152
153 private static double clampDeadband(double deadband) {
154 if (Double.isNaN(deadband)) {
155 return 0.0;
156 }
157 return Math.clamp(deadband, 0.0, MAX_DEADBAND);
158 }
159
160 private final GenericHID m_hid;
161
167 @Override
169 return m_hid;
170 }
171
177 public Gamepad(final int port) {
178 this(DriverStation.getGenericHID(port));
179 }
180
186 public Gamepad(final GenericHID hid) {
187 m_hid = Objects.requireNonNull(hid, "Provided HID object cannot be null");
188 UsageReporting.reportUsage("HID", hid.getPort(), "Gamepad");
189 }
190
198 public void setLeftXDeadband(double deadband) {
199 m_leftXDeadband = clampDeadband(deadband);
200 }
201
209 public void setLeftYDeadband(double deadband) {
210 m_leftYDeadband = clampDeadband(deadband);
211 }
212
220 public void setRightXDeadband(double deadband) {
221 m_rightXDeadband = clampDeadband(deadband);
222 }
223
231 public void setRightYDeadband(double deadband) {
232 m_rightYDeadband = clampDeadband(deadband);
233 }
234
242 public void setLeftTriggerDeadband(double deadband) {
243 m_leftTriggerDeadband = clampDeadband(deadband);
244 }
245
253 public void setRightTriggerDeadband(double deadband) {
254 m_rightTriggerDeadband = clampDeadband(deadband);
255 }
256
264 public double getLeftX() {
265 return MathUtil.applyDeadband(getAxis(Axis.LEFT_X), m_leftXDeadband);
266 }
267
275 public double getLeftY() {
276 return MathUtil.applyDeadband(getAxis(Axis.LEFT_Y), m_leftYDeadband);
277 }
278
286 public double getRightX() {
287 return MathUtil.applyDeadband(getAxis(Axis.RIGHT_X), m_rightXDeadband);
288 }
289
297 public double getRightY() {
298 return MathUtil.applyDeadband(getAxis(Axis.RIGHT_Y), m_rightYDeadband);
299 }
300
309 public double getLeftTrigger() {
310 return MathUtil.applyDeadband(getAxis(Axis.LEFT_TRIGGER), m_leftTriggerDeadband);
311 }
312
323 public BooleanEvent leftTrigger(double threshold, EventLoop loop) {
324 return axisGreaterThan(Axis.LEFT_TRIGGER, threshold, loop);
325 }
326
336 return leftTrigger(0.5, loop);
337 }
338
347 public double getRightTrigger() {
348 return MathUtil.applyDeadband(getAxis(Axis.RIGHT_TRIGGER), m_rightTriggerDeadband);
349 }
350
361 public BooleanEvent rightTrigger(double threshold, EventLoop loop) {
362 return axisGreaterThan(Axis.RIGHT_TRIGGER, threshold, loop);
363 }
364
374 return rightTrigger(0.5, loop);
375 }
376
382 public boolean getFaceDownButton() {
383 return getButton(Button.FACE_DOWN);
384 }
385
391 public boolean getFaceDownButtonPressed() {
393 }
394
400 public boolean getFaceDownButtonReleased() {
402 }
403
412 return button(Button.FACE_DOWN, loop);
413 }
414
420 public boolean getFaceRightButton() {
422 }
423
429 public boolean getFaceRightButtonPressed() {
431 }
432
438 public boolean getFaceRightButtonReleased() {
440 }
441
450 return button(Button.FACE_RIGHT, loop);
451 }
452
458 public boolean getFaceLeftButton() {
459 return getButton(Button.FACE_LEFT);
460 }
461
467 public boolean getFaceLeftButtonPressed() {
469 }
470
476 public boolean getFaceLeftButtonReleased() {
478 }
479
488 return button(Button.FACE_LEFT, loop);
489 }
490
496 public boolean getFaceUpButton() {
497 return getButton(Button.FACE_UP);
498 }
499
505 public boolean getFaceUpButtonPressed() {
507 }
508
514 public boolean getFaceUpButtonReleased() {
516 }
517
526 return button(Button.FACE_UP, loop);
527 }
528
534 public boolean getBackButton() {
535 return getButton(Button.BACK);
536 }
537
543 public boolean getBackButtonPressed() {
545 }
546
552 public boolean getBackButtonReleased() {
554 }
555
564 return button(Button.BACK, loop);
565 }
566
572 public boolean getGuideButton() {
573 return getButton(Button.GUIDE);
574 }
575
581 public boolean getGuideButtonPressed() {
583 }
584
590 public boolean getGuideButtonReleased() {
592 }
593
602 return button(Button.GUIDE, loop);
603 }
604
610 public boolean getStartButton() {
611 return getButton(Button.START);
612 }
613
619 public boolean getStartButtonPressed() {
621 }
622
628 public boolean getStartButtonReleased() {
630 }
631
640 return button(Button.START, loop);
641 }
642
648 public boolean getLeftStickButton() {
650 }
651
657 public boolean getLeftStickButtonPressed() {
659 }
660
666 public boolean getLeftStickButtonReleased() {
668 }
669
678 return button(Button.LEFT_STICK, loop);
679 }
680
686 public boolean getRightStickButton() {
688 }
689
695 public boolean getRightStickButtonPressed() {
697 }
698
707
716 return button(Button.RIGHT_STICK, loop);
717 }
718
724 public boolean getLeftBumperButton() {
726 }
727
733 public boolean getLeftBumperButtonPressed() {
735 }
736
745
754 return button(Button.LEFT_BUMPER, loop);
755 }
756
762 public boolean getRightBumperButton() {
764 }
765
774
783
792 return button(Button.RIGHT_BUMPER, loop);
793 }
794
800 public boolean getDpadUpButton() {
801 return getButton(Button.DPAD_UP);
802 }
803
809 public boolean getDpadUpButtonPressed() {
811 }
812
818 public boolean getDpadUpButtonReleased() {
820 }
821
830 return button(Button.DPAD_UP, loop);
831 }
832
838 public boolean getDpadDownButton() {
839 return getButton(Button.DPAD_DOWN);
840 }
841
847 public boolean getDpadDownButtonPressed() {
849 }
850
856 public boolean getDpadDownButtonReleased() {
858 }
859
868 return button(Button.DPAD_DOWN, loop);
869 }
870
876 public boolean getDpadLeftButton() {
877 return getButton(Button.DPAD_LEFT);
878 }
879
885 public boolean getDpadLeftButtonPressed() {
887 }
888
894 public boolean getDpadLeftButtonReleased() {
896 }
897
906 return button(Button.DPAD_LEFT, loop);
907 }
908
914 public boolean getDpadRightButton() {
916 }
917
923 public boolean getDpadRightButtonPressed() {
925 }
926
932 public boolean getDpadRightButtonReleased() {
934 }
935
944 return button(Button.DPAD_RIGHT, loop);
945 }
946
952 public boolean getMisc1Button() {
953 return getButton(Button.MISC_1);
954 }
955
961 public boolean getMisc1ButtonPressed() {
963 }
964
970 public boolean getMisc1ButtonReleased() {
972 }
973
982 return button(Button.MISC_1, loop);
983 }
984
990 public boolean getRightPaddle1Button() {
992 }
993
1002
1011
1020 return button(Button.RIGHT_PADDLE_1, loop);
1021 }
1022
1028 public boolean getLeftPaddle1Button() {
1030 }
1031
1039 }
1040
1049
1058 return button(Button.LEFT_PADDLE_1, loop);
1059 }
1060
1066 public boolean getRightPaddle2Button() {
1068 }
1069
1078
1087
1096 return button(Button.RIGHT_PADDLE_2, loop);
1097 }
1098
1104 public boolean getLeftPaddle2Button() {
1106 }
1107
1115 }
1116
1125
1134 return button(Button.LEFT_PADDLE_2, loop);
1135 }
1136
1142 public boolean getTouchpadButton() {
1143 return getButton(Button.TOUCHPAD);
1144 }
1145
1151 public boolean getTouchpadButtonPressed() {
1153 }
1154
1160 public boolean getTouchpadButtonReleased() {
1162 }
1163
1172 return button(Button.TOUCHPAD, loop);
1173 }
1174
1180 public boolean getMisc2Button() {
1181 return getButton(Button.MISC_2);
1182 }
1183
1189 public boolean getMisc2ButtonPressed() {
1191 }
1192
1198 public boolean getMisc2ButtonReleased() {
1200 }
1201
1210 return button(Button.MISC_2, loop);
1211 }
1212
1218 public boolean getMisc3Button() {
1219 return getButton(Button.MISC_3);
1220 }
1221
1227 public boolean getMisc3ButtonPressed() {
1229 }
1230
1236 public boolean getMisc3ButtonReleased() {
1238 }
1239
1248 return button(Button.MISC_3, loop);
1249 }
1250
1256 public boolean getMisc4Button() {
1257 return getButton(Button.MISC_4);
1258 }
1259
1265 public boolean getMisc4ButtonPressed() {
1267 }
1268
1274 public boolean getMisc4ButtonReleased() {
1276 }
1277
1286 return button(Button.MISC_4, loop);
1287 }
1288
1294 public boolean getMisc5Button() {
1295 return getButton(Button.MISC_5);
1296 }
1297
1303 public boolean getMisc5ButtonPressed() {
1305 }
1306
1312 public boolean getMisc5ButtonReleased() {
1314 }
1315
1324 return button(Button.MISC_5, loop);
1325 }
1326
1332 public boolean getMisc6Button() {
1333 return getButton(Button.MISC_6);
1334 }
1335
1341 public boolean getMisc6ButtonPressed() {
1343 }
1344
1350 public boolean getMisc6ButtonReleased() {
1352 }
1353
1362 return button(Button.MISC_6, loop);
1363 }
1364
1374 public boolean getButton(Button button) {
1375 return m_hid.getRawButton(button.value);
1376 }
1377
1389 return m_hid.getRawButtonPressed(button.value);
1390 }
1391
1403 return m_hid.getRawButtonReleased(button.value);
1404 }
1405
1414 return m_hid.button(button.value, loop);
1415 }
1416
1423 public double getAxis(Axis axis) {
1424 return m_hid.getRawAxis(axis.value);
1425 }
1426
1436 public BooleanEvent axisLessThan(Axis axis, double threshold, EventLoop loop) {
1437 return m_hid.axisLessThan(axis.value, threshold, loop);
1438 }
1439
1450 public BooleanEvent axisGreaterThan(Axis axis, double threshold, EventLoop loop) {
1451 return m_hid.axisGreaterThan(axis.value, threshold, loop);
1452 }
1453
1459 public int getAxesAvailable() {
1460 return m_hid.getAxesAvailable();
1461 }
1462
1468 public long getButtonsAvailable() {
1469 return m_hid.getButtonsAvailable();
1470 }
1471
1477 public boolean isConnected() {
1478 return m_hid.isConnected();
1479 }
1480
1487 return m_hid.getGamepadType();
1488 }
1489
1495 public EnumSet<SupportedOutput> getSupportedOutputs() {
1496 return m_hid.getSupportedOutputs();
1497 }
1498
1504 public String getName() {
1505 return m_hid.getName();
1506 }
1507
1516 public void setLeds(int r, int g, int b) {
1517 m_hid.setLeds(r, g, b);
1518 }
1519
1527 public void setRumble(RumbleType type, double value) {
1528 m_hid.setRumble(type, value);
1529 }
1530
1538 public boolean getTouchpadFingerAvailable(int touchpad, int finger) {
1539 return m_hid.getTouchpadFingerAvailable(touchpad, finger);
1540 }
1541
1550 return m_hid.getTouchpadFinger(touchpad, finger);
1551 }
1552
1553 private double getAxisForTelemetry(Axis axis) {
1554 return DriverStationBackend.getStickAxisIfAvailable(m_hid.getPort(), axis.value).orElse(0.0);
1555 }
1556
1557 @Override
1558 public void logTo(TelemetryTable table) {
1559 table.log("LeftTrigger", getAxisForTelemetry(Axis.LEFT_TRIGGER));
1560 table.log("RightTrigger", getAxisForTelemetry(Axis.RIGHT_TRIGGER));
1561 table.log("LeftX", getAxisForTelemetry(Axis.LEFT_X));
1562 table.log("LeftY", getAxisForTelemetry(Axis.LEFT_Y));
1563 table.log("RightX", getAxisForTelemetry(Axis.RIGHT_X));
1564 table.log("RightY", getAxisForTelemetry(Axis.RIGHT_Y));
1565
1566 long buttons = DriverStationBackend.getStickButtons(m_hid.getPort());
1567 table.log("FaceDown", (buttons & (1L << Button.FACE_DOWN.value)) != 0);
1568 table.log("FaceRight", (buttons & (1L << Button.FACE_RIGHT.value)) != 0);
1569 table.log("FaceLeft", (buttons & (1L << Button.FACE_LEFT.value)) != 0);
1570 table.log("FaceUp", (buttons & (1L << Button.FACE_UP.value)) != 0);
1571 table.log("Back", (buttons & (1L << Button.BACK.value)) != 0);
1572 table.log("Guide", (buttons & (1L << Button.GUIDE.value)) != 0);
1573 table.log("Start", (buttons & (1L << Button.START.value)) != 0);
1574 table.log("LeftStick", (buttons & (1L << Button.LEFT_STICK.value)) != 0);
1575 table.log("RightStick", (buttons & (1L << Button.RIGHT_STICK.value)) != 0);
1576 table.log("LeftBumper", (buttons & (1L << Button.LEFT_BUMPER.value)) != 0);
1577 table.log("RightBumper", (buttons & (1L << Button.RIGHT_BUMPER.value)) != 0);
1578 table.log("DpadUp", (buttons & (1L << Button.DPAD_UP.value)) != 0);
1579 table.log("DpadDown", (buttons & (1L << Button.DPAD_DOWN.value)) != 0);
1580 table.log("DpadLeft", (buttons & (1L << Button.DPAD_LEFT.value)) != 0);
1581 table.log("DpadRight", (buttons & (1L << Button.DPAD_RIGHT.value)) != 0);
1582 table.log("Misc1", (buttons & (1L << Button.MISC_1.value)) != 0);
1583 table.log("RightPaddle1", (buttons & (1L << Button.RIGHT_PADDLE_1.value)) != 0);
1584 table.log("LeftPaddle1", (buttons & (1L << Button.LEFT_PADDLE_1.value)) != 0);
1585 table.log("RightPaddle2", (buttons & (1L << Button.RIGHT_PADDLE_2.value)) != 0);
1586 table.log("LeftPaddle2", (buttons & (1L << Button.LEFT_PADDLE_2.value)) != 0);
1587 table.log("Touchpad", (buttons & (1L << Button.TOUCHPAD.value)) != 0);
1588 table.log("Misc2", (buttons & (1L << Button.MISC_2.value)) != 0);
1589 table.log("Misc3", (buttons & (1L << Button.MISC_3.value)) != 0);
1590 table.log("Misc4", (buttons & (1L << Button.MISC_4.value)) != 0);
1591 table.log("Misc5", (buttons & (1L << Button.MISC_5.value)) != 0);
1592 table.log("Misc6", (buttons & (1L << Button.MISC_6.value)) != 0);
1593 }
1594
1595 @Override
1596 public String getTelemetryType() {
1597 return "HID:Gamepad";
1598 }
1599}
static GenericHID getGenericHID(int port)
BooleanEvent rightBumper(EventLoop loop)
Definition Gamepad.java:791
TouchpadFinger getTouchpadFinger(int touchpad, int finger)
BooleanEvent dpadDown(EventLoop loop)
Definition Gamepad.java:867
BooleanEvent leftStick(EventLoop loop)
Definition Gamepad.java:677
void setLeds(int r, int g, int b)
Gamepad(final GenericHID hid)
Definition Gamepad.java:186
BooleanEvent leftTrigger(double threshold, EventLoop loop)
Definition Gamepad.java:323
BooleanEvent faceDown(EventLoop loop)
Definition Gamepad.java:411
BooleanEvent dpadRight(EventLoop loop)
Definition Gamepad.java:943
BooleanEvent rightPaddle2(EventLoop loop)
BooleanEvent leftPaddle1(EventLoop loop)
boolean getButtonPressed(Button button)
BooleanEvent misc2(EventLoop loop)
boolean getTouchpadFingerAvailable(int touchpad, int finger)
void setLeftXDeadband(double deadband)
Definition Gamepad.java:198
BooleanEvent guide(EventLoop loop)
Definition Gamepad.java:601
BooleanEvent misc5(EventLoop loop)
BooleanEvent misc1(EventLoop loop)
Definition Gamepad.java:981
void setRightYDeadband(double deadband)
Definition Gamepad.java:231
BooleanEvent rightTrigger(EventLoop loop)
Definition Gamepad.java:373
BooleanEvent faceRight(EventLoop loop)
Definition Gamepad.java:449
boolean getButton(Button button)
BooleanEvent axisLessThan(Axis axis, double threshold, EventLoop loop)
BooleanEvent leftBumper(EventLoop loop)
Definition Gamepad.java:753
BooleanEvent rightStick(EventLoop loop)
Definition Gamepad.java:715
void logTo(TelemetryTable table)
BooleanEvent misc3(EventLoop loop)
BooleanEvent misc6(EventLoop loop)
BooleanEvent start(EventLoop loop)
Definition Gamepad.java:639
BooleanEvent dpadUp(EventLoop loop)
Definition Gamepad.java:829
BooleanEvent leftTrigger(EventLoop loop)
Definition Gamepad.java:335
BooleanEvent rightPaddle1(EventLoop loop)
BooleanEvent touchpad(EventLoop loop)
EnumSet< SupportedOutput > getSupportedOutputs()
BooleanEvent rightTrigger(double threshold, EventLoop loop)
Definition Gamepad.java:361
boolean getButtonReleased(Button button)
BooleanEvent faceUp(EventLoop loop)
Definition Gamepad.java:525
void setLeftYDeadband(double deadband)
Definition Gamepad.java:209
void setLeftTriggerDeadband(double deadband)
Definition Gamepad.java:242
BooleanEvent faceLeft(EventLoop loop)
Definition Gamepad.java:487
BooleanEvent button(Button button, EventLoop loop)
BooleanEvent dpadLeft(EventLoop loop)
Definition Gamepad.java:905
BooleanEvent axisGreaterThan(Axis axis, double threshold, EventLoop loop)
BooleanEvent leftPaddle2(EventLoop loop)
void setRightTriggerDeadband(double deadband)
Definition Gamepad.java:253
void setRumble(RumbleType type, double value)
BooleanEvent back(EventLoop loop)
Definition Gamepad.java:563
void setRightXDeadband(double deadband)
Definition Gamepad.java:220
BooleanEvent misc4(EventLoop loop)
static OptionalDouble getStickAxisIfAvailable(int stick, int axis)
static double applyDeadband(double value, double deadband, double maxMagnitude)
Definition MathUtil.java:59
static void reportUsage(String resource, String data)