001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package org.wpilib.driverstation; 006 007import org.wpilib.event.BooleanEvent; 008import org.wpilib.event.EventLoop; 009import org.wpilib.hardware.hal.HAL; 010import org.wpilib.util.sendable.Sendable; 011import org.wpilib.util.sendable.SendableBuilder; 012 013/** 014 * Handle input from Gamepad controllers connected to the Driver Station. 015 * 016 * <p>This class handles Gamepad input that comes from the Driver Station. Each time a value is 017 * requested the most recent value is returned. There is a single class instance for each controller 018 * and the mapping of ports to hardware buttons depends on the code in the Driver Station. 019 * 020 * <p>Only first party controllers from Generic are guaranteed to have the correct mapping, and only 021 * through the official NI DS. Sim is not guaranteed to have the same mapping, as well as any 3rd 022 * party controllers. 023 */ 024public class Gamepad extends GenericHID implements Sendable { 025 /** Represents a digital button on a Gamepad. */ 026 public enum Button { 027 /** South Face button. */ 028 kSouthFace(0), 029 /** East Face button. */ 030 kEastFace(1), 031 /** West Face button. */ 032 kWestFace(2), 033 /** North Face button. */ 034 kNorthFace(3), 035 /** Back button. */ 036 kBack(4), 037 /** Guide button. */ 038 kGuide(5), 039 /** Start button. */ 040 kStart(6), 041 /** Left stick button. */ 042 kLeftStick(7), 043 /** Right stick button. */ 044 kRightStick(8), 045 /** Left bumper button. */ 046 kLeftBumper(9), 047 /** Right bumper button. */ 048 kRightBumper(10), 049 /** D-pad up button. */ 050 kDpadUp(11), 051 /** D-pad down button. */ 052 kDpadDown(12), 053 /** D-pad left button. */ 054 kDpadLeft(13), 055 /** D-pad right button. */ 056 kDpadRight(14), 057 /** Miscellaneous 1 button. */ 058 kMisc1(15), 059 /** Right Paddle 1 button. */ 060 kRightPaddle1(16), 061 /** Left Paddle 1 button. */ 062 kLeftPaddle1(17), 063 /** Right Paddle 2 button. */ 064 kRightPaddle2(18), 065 /** Left Paddle 2 button. */ 066 kLeftPaddle2(19), 067 /** Touchpad button. */ 068 kTouchpad(20), 069 /** Miscellaneous 2 button. */ 070 kMisc2(21), 071 /** Miscellaneous 3 button. */ 072 kMisc3(22), 073 /** Miscellaneous 4 button. */ 074 kMisc4(23), 075 /** Miscellaneous 5 button. */ 076 kMisc5(24), 077 /** Miscellaneous 6 button. */ 078 kMisc6(25); 079 080 /** Button value. */ 081 public final int value; 082 083 Button(int value) { 084 this.value = value; 085 } 086 087 /** 088 * Get the human-friendly name of the button, matching the relevant methods. This is done by 089 * stripping the leading `k`, and appending `Button`. 090 * 091 * <p>Primarily used for automated unit tests. 092 * 093 * @return the human-friendly name of the button. 094 */ 095 @Override 096 public String toString() { 097 // Remove leading `k` 098 return this.name().substring(1) + "Button"; 099 } 100 } 101 102 /** Represents an axis on an Gamepad. */ 103 public enum Axis { 104 /** Left X axis. */ 105 kLeftX(0), 106 /** Left Y axis. */ 107 kLeftY(1), 108 /** Right X axis. */ 109 kRightX(2), 110 /** Right Y axis. */ 111 kRightY(3), 112 /** Left trigger. */ 113 kLeftTrigger(4), 114 /** Right trigger. */ 115 kRightTrigger(5); 116 117 /** Axis value. */ 118 public final int value; 119 120 Axis(int value) { 121 this.value = value; 122 } 123 124 /** 125 * Get the human-friendly name of the axis, matching the relevant methods. This is done by 126 * stripping the leading `k`, and appending `Axis` if the name ends with `Trigger`. 127 * 128 * <p>Primarily used for automated unit tests. 129 * 130 * @return the human-friendly name of the axis. 131 */ 132 @Override 133 public String toString() { 134 var name = this.name().substring(1); // Remove leading `k` 135 if (name.endsWith("Trigger")) { 136 return name + "Axis"; 137 } 138 return name; 139 } 140 } 141 142 /** 143 * Construct an instance of a controller. 144 * 145 * @param port The port index on the Driver Station that the controller is plugged into (0-5). 146 */ 147 public Gamepad(final int port) { 148 super(port); 149 HAL.reportUsage("HID", port, "Gamepad"); 150 } 151 152 /** 153 * Get the X axis value of left side of the controller. Right is positive. 154 * 155 * @return The axis value. 156 */ 157 public double getLeftX() { 158 return getRawAxis(Axis.kLeftX.value); 159 } 160 161 /** 162 * Get the Y axis value of left side of the controller. Back is positive. 163 * 164 * @return The axis value. 165 */ 166 public double getLeftY() { 167 return getRawAxis(Axis.kLeftY.value); 168 } 169 170 /** 171 * Get the X axis value of right side of the controller. Right is positive. 172 * 173 * @return The axis value. 174 */ 175 public double getRightX() { 176 return getRawAxis(Axis.kRightX.value); 177 } 178 179 /** 180 * Get the Y axis value of right side of the controller. Back is positive. 181 * 182 * @return The axis value. 183 */ 184 public double getRightY() { 185 return getRawAxis(Axis.kRightY.value); 186 } 187 188 /** 189 * Get the left trigger axis value of the controller. Note that this axis is bound to the range of 190 * [0, 1] as opposed to the usual [-1, 1]. 191 * 192 * @return The axis value. 193 */ 194 public double getLeftTriggerAxis() { 195 return getRawAxis(Axis.kLeftTrigger.value); 196 } 197 198 /** 199 * Constructs an event instance around the axis value of the left trigger. The returned trigger 200 * will be true when the axis value is greater than {@code threshold}. 201 * 202 * @param threshold the minimum axis value for the returned {@link BooleanEvent} to be true. This 203 * value should be in the range [0, 1] where 0 is the unpressed state of the axis. 204 * @param loop the event loop instance to attach the event to. 205 * @return an event instance that is true when the left trigger's axis exceeds the provided 206 * threshold, attached to the given event loop 207 */ 208 public BooleanEvent leftTrigger(double threshold, EventLoop loop) { 209 return axisGreaterThan(Axis.kLeftTrigger.value, threshold, loop); 210 } 211 212 /** 213 * Constructs an event instance around the axis value of the left trigger. The returned trigger 214 * will be true when the axis value is greater than 0.5. 215 * 216 * @param loop the event loop instance to attach the event to. 217 * @return an event instance that is true when the left trigger's axis exceeds the provided 218 * threshold, attached to the given event loop 219 */ 220 public BooleanEvent leftTrigger(EventLoop loop) { 221 return leftTrigger(0.5, loop); 222 } 223 224 /** 225 * Get the right trigger axis value of the controller. Note that this axis is bound to the range 226 * of [0, 1] as opposed to the usual [-1, 1]. 227 * 228 * @return The axis value. 229 */ 230 public double getRightTriggerAxis() { 231 return getRawAxis(Axis.kRightTrigger.value); 232 } 233 234 /** 235 * Constructs an event instance around the axis value of the right trigger. The returned trigger 236 * will be true when the axis value is greater than {@code threshold}. 237 * 238 * @param threshold the minimum axis value for the returned {@link BooleanEvent} to be true. This 239 * value should be in the range [0, 1] where 0 is the unpressed state of the axis. 240 * @param loop the event loop instance to attach the event to. 241 * @return an event instance that is true when the right trigger's axis exceeds the provided 242 * threshold, attached to the given event loop 243 */ 244 public BooleanEvent rightTrigger(double threshold, EventLoop loop) { 245 return axisGreaterThan(Axis.kRightTrigger.value, threshold, loop); 246 } 247 248 /** 249 * Constructs an event instance around the axis value of the right trigger. The returned trigger 250 * will be true when the axis value is greater than 0.5. 251 * 252 * @param loop the event loop instance to attach the event to. 253 * @return an event instance that is true when the right trigger's axis exceeds the provided 254 * threshold, attached to the given event loop 255 */ 256 public BooleanEvent rightTrigger(EventLoop loop) { 257 return rightTrigger(0.5, loop); 258 } 259 260 /** 261 * Read the value of the South Face button on the controller. 262 * 263 * @return The state of the button. 264 */ 265 public boolean getSouthFaceButton() { 266 return getRawButton(Button.kSouthFace.value); 267 } 268 269 /** 270 * Whether the South Face button was pressed since the last check. 271 * 272 * @return Whether the button was pressed since the last check. 273 */ 274 public boolean getSouthFaceButtonPressed() { 275 return getRawButtonPressed(Button.kSouthFace.value); 276 } 277 278 /** 279 * Whether the South Face button was released since the last check. 280 * 281 * @return Whether the button was released since the last check. 282 */ 283 public boolean getSouthFaceButtonReleased() { 284 return getRawButtonReleased(Button.kSouthFace.value); 285 } 286 287 /** 288 * Constructs an event instance around the South Face button's digital signal. 289 * 290 * @param loop the event loop instance to attach the event to. 291 * @return an event instance representing the South Face button's digital signal attached to the 292 * given loop. 293 */ 294 public BooleanEvent southFace(EventLoop loop) { 295 return button(Button.kSouthFace.value, loop); 296 } 297 298 /** 299 * Read the value of the East Face button on the controller. 300 * 301 * @return The state of the button. 302 */ 303 public boolean getEastFaceButton() { 304 return getRawButton(Button.kEastFace.value); 305 } 306 307 /** 308 * Whether the East Face button was pressed since the last check. 309 * 310 * @return Whether the button was pressed since the last check. 311 */ 312 public boolean getEastFaceButtonPressed() { 313 return getRawButtonPressed(Button.kEastFace.value); 314 } 315 316 /** 317 * Whether the East Face button was released since the last check. 318 * 319 * @return Whether the button was released since the last check. 320 */ 321 public boolean getEastFaceButtonReleased() { 322 return getRawButtonReleased(Button.kEastFace.value); 323 } 324 325 /** 326 * Constructs an event instance around the East Face button's digital signal. 327 * 328 * @param loop the event loop instance to attach the event to. 329 * @return an event instance representing the East Face button's digital signal attached to the 330 * given loop. 331 */ 332 public BooleanEvent eastFace(EventLoop loop) { 333 return button(Button.kEastFace.value, loop); 334 } 335 336 /** 337 * Read the value of the West Face button on the controller. 338 * 339 * @return The state of the button. 340 */ 341 public boolean getWestFaceButton() { 342 return getRawButton(Button.kWestFace.value); 343 } 344 345 /** 346 * Whether the West Face button was pressed since the last check. 347 * 348 * @return Whether the button was pressed since the last check. 349 */ 350 public boolean getWestFaceButtonPressed() { 351 return getRawButtonPressed(Button.kWestFace.value); 352 } 353 354 /** 355 * Whether the West Face button was released since the last check. 356 * 357 * @return Whether the button was released since the last check. 358 */ 359 public boolean getWestFaceButtonReleased() { 360 return getRawButtonReleased(Button.kWestFace.value); 361 } 362 363 /** 364 * Constructs an event instance around the West Face button's digital signal. 365 * 366 * @param loop the event loop instance to attach the event to. 367 * @return an event instance representing the West Face button's digital signal attached to the 368 * given loop. 369 */ 370 public BooleanEvent westFace(EventLoop loop) { 371 return button(Button.kWestFace.value, loop); 372 } 373 374 /** 375 * Read the value of the North Face button on the controller. 376 * 377 * @return The state of the button. 378 */ 379 public boolean getNorthFaceButton() { 380 return getRawButton(Button.kNorthFace.value); 381 } 382 383 /** 384 * Whether the North Face button was pressed since the last check. 385 * 386 * @return Whether the button was pressed since the last check. 387 */ 388 public boolean getNorthFaceButtonPressed() { 389 return getRawButtonPressed(Button.kNorthFace.value); 390 } 391 392 /** 393 * Whether the North Face button was released since the last check. 394 * 395 * @return Whether the button was released since the last check. 396 */ 397 public boolean getNorthFaceButtonReleased() { 398 return getRawButtonReleased(Button.kNorthFace.value); 399 } 400 401 /** 402 * Constructs an event instance around the North Face button's digital signal. 403 * 404 * @param loop the event loop instance to attach the event to. 405 * @return an event instance representing the North Face button's digital signal attached to the 406 * given loop. 407 */ 408 public BooleanEvent northFace(EventLoop loop) { 409 return button(Button.kNorthFace.value, loop); 410 } 411 412 /** 413 * Read the value of the Back button on the controller. 414 * 415 * @return The state of the button. 416 */ 417 public boolean getBackButton() { 418 return getRawButton(Button.kBack.value); 419 } 420 421 /** 422 * Whether the Back button was pressed since the last check. 423 * 424 * @return Whether the button was pressed since the last check. 425 */ 426 public boolean getBackButtonPressed() { 427 return getRawButtonPressed(Button.kBack.value); 428 } 429 430 /** 431 * Whether the Back button was released since the last check. 432 * 433 * @return Whether the button was released since the last check. 434 */ 435 public boolean getBackButtonReleased() { 436 return getRawButtonReleased(Button.kBack.value); 437 } 438 439 /** 440 * Constructs an event instance around the Back button's digital signal. 441 * 442 * @param loop the event loop instance to attach the event to. 443 * @return an event instance representing the Back button's digital signal attached to the given 444 * loop. 445 */ 446 public BooleanEvent back(EventLoop loop) { 447 return button(Button.kBack.value, loop); 448 } 449 450 /** 451 * Read the value of the Guide button on the controller. 452 * 453 * @return The state of the button. 454 */ 455 public boolean getGuideButton() { 456 return getRawButton(Button.kGuide.value); 457 } 458 459 /** 460 * Whether the Guide button was pressed since the last check. 461 * 462 * @return Whether the button was pressed since the last check. 463 */ 464 public boolean getGuideButtonPressed() { 465 return getRawButtonPressed(Button.kGuide.value); 466 } 467 468 /** 469 * Whether the Guide button was released since the last check. 470 * 471 * @return Whether the button was released since the last check. 472 */ 473 public boolean getGuideButtonReleased() { 474 return getRawButtonReleased(Button.kGuide.value); 475 } 476 477 /** 478 * Constructs an event instance around the Guide button's digital signal. 479 * 480 * @param loop the event loop instance to attach the event to. 481 * @return an event instance representing the Guide button's digital signal attached to the given 482 * loop. 483 */ 484 public BooleanEvent guide(EventLoop loop) { 485 return button(Button.kGuide.value, loop); 486 } 487 488 /** 489 * Read the value of the Start button on the controller. 490 * 491 * @return The state of the button. 492 */ 493 public boolean getStartButton() { 494 return getRawButton(Button.kStart.value); 495 } 496 497 /** 498 * Whether the Start button was pressed since the last check. 499 * 500 * @return Whether the button was pressed since the last check. 501 */ 502 public boolean getStartButtonPressed() { 503 return getRawButtonPressed(Button.kStart.value); 504 } 505 506 /** 507 * Whether the Start button was released since the last check. 508 * 509 * @return Whether the button was released since the last check. 510 */ 511 public boolean getStartButtonReleased() { 512 return getRawButtonReleased(Button.kStart.value); 513 } 514 515 /** 516 * Constructs an event instance around the Start button's digital signal. 517 * 518 * @param loop the event loop instance to attach the event to. 519 * @return an event instance representing the Start button's digital signal attached to the given 520 * loop. 521 */ 522 public BooleanEvent start(EventLoop loop) { 523 return button(Button.kStart.value, loop); 524 } 525 526 /** 527 * Read the value of the left stick button on the controller. 528 * 529 * @return The state of the button. 530 */ 531 public boolean getLeftStickButton() { 532 return getRawButton(Button.kLeftStick.value); 533 } 534 535 /** 536 * Whether the left stick button was pressed since the last check. 537 * 538 * @return Whether the button was pressed since the last check. 539 */ 540 public boolean getLeftStickButtonPressed() { 541 return getRawButtonPressed(Button.kLeftStick.value); 542 } 543 544 /** 545 * Whether the left stick button was released since the last check. 546 * 547 * @return Whether the button was released since the last check. 548 */ 549 public boolean getLeftStickButtonReleased() { 550 return getRawButtonReleased(Button.kLeftStick.value); 551 } 552 553 /** 554 * Constructs an event instance around the left stick button's digital signal. 555 * 556 * @param loop the event loop instance to attach the event to. 557 * @return an event instance representing the left stick button's digital signal attached to the 558 * given loop. 559 */ 560 public BooleanEvent leftStick(EventLoop loop) { 561 return button(Button.kLeftStick.value, loop); 562 } 563 564 /** 565 * Read the value of the right stick button on the controller. 566 * 567 * @return The state of the button. 568 */ 569 public boolean getRightStickButton() { 570 return getRawButton(Button.kRightStick.value); 571 } 572 573 /** 574 * Whether the right stick button was pressed since the last check. 575 * 576 * @return Whether the button was pressed since the last check. 577 */ 578 public boolean getRightStickButtonPressed() { 579 return getRawButtonPressed(Button.kRightStick.value); 580 } 581 582 /** 583 * Whether the right stick button was released since the last check. 584 * 585 * @return Whether the button was released since the last check. 586 */ 587 public boolean getRightStickButtonReleased() { 588 return getRawButtonReleased(Button.kRightStick.value); 589 } 590 591 /** 592 * Constructs an event instance around the right stick button's digital signal. 593 * 594 * @param loop the event loop instance to attach the event to. 595 * @return an event instance representing the right stick button's digital signal attached to the 596 * given loop. 597 */ 598 public BooleanEvent rightStick(EventLoop loop) { 599 return button(Button.kRightStick.value, loop); 600 } 601 602 /** 603 * Read the value of the right bumper button on the controller. 604 * 605 * @return The state of the button. 606 */ 607 public boolean getLeftBumperButton() { 608 return getRawButton(Button.kLeftBumper.value); 609 } 610 611 /** 612 * Whether the right bumper button was pressed since the last check. 613 * 614 * @return Whether the button was pressed since the last check. 615 */ 616 public boolean getLeftBumperButtonPressed() { 617 return getRawButtonPressed(Button.kLeftBumper.value); 618 } 619 620 /** 621 * Whether the right bumper button was released since the last check. 622 * 623 * @return Whether the button was released since the last check. 624 */ 625 public boolean getLeftBumperButtonReleased() { 626 return getRawButtonReleased(Button.kLeftBumper.value); 627 } 628 629 /** 630 * Constructs an event instance around the right bumper button's digital signal. 631 * 632 * @param loop the event loop instance to attach the event to. 633 * @return an event instance representing the right bumper button's digital signal attached to the 634 * given loop. 635 */ 636 public BooleanEvent leftBumper(EventLoop loop) { 637 return button(Button.kLeftBumper.value, loop); 638 } 639 640 /** 641 * Read the value of the right bumper button on the controller. 642 * 643 * @return The state of the button. 644 */ 645 public boolean getRightBumperButton() { 646 return getRawButton(Button.kRightBumper.value); 647 } 648 649 /** 650 * Whether the right bumper button was pressed since the last check. 651 * 652 * @return Whether the button was pressed since the last check. 653 */ 654 public boolean getRightBumperButtonPressed() { 655 return getRawButtonPressed(Button.kRightBumper.value); 656 } 657 658 /** 659 * Whether the right bumper button was released since the last check. 660 * 661 * @return Whether the button was released since the last check. 662 */ 663 public boolean getRightBumperButtonReleased() { 664 return getRawButtonReleased(Button.kRightBumper.value); 665 } 666 667 /** 668 * Constructs an event instance around the right bumper button's digital signal. 669 * 670 * @param loop the event loop instance to attach the event to. 671 * @return an event instance representing the right bumper button's digital signal attached to the 672 * given loop. 673 */ 674 public BooleanEvent rightBumper(EventLoop loop) { 675 return button(Button.kRightBumper.value, loop); 676 } 677 678 /** 679 * Read the value of the D-pad up button on the controller. 680 * 681 * @return The state of the button. 682 */ 683 public boolean getDpadUpButton() { 684 return getRawButton(Button.kDpadUp.value); 685 } 686 687 /** 688 * Whether the D-pad up button was pressed since the last check. 689 * 690 * @return Whether the button was pressed since the last check. 691 */ 692 public boolean getDpadUpButtonPressed() { 693 return getRawButtonPressed(Button.kDpadUp.value); 694 } 695 696 /** 697 * Whether the D-pad up button was released since the last check. 698 * 699 * @return Whether the button was released since the last check. 700 */ 701 public boolean getDpadUpButtonReleased() { 702 return getRawButtonReleased(Button.kDpadUp.value); 703 } 704 705 /** 706 * Constructs an event instance around the D-pad up button's digital signal. 707 * 708 * @param loop the event loop instance to attach the event to. 709 * @return an event instance representing the D-pad up button's digital signal attached to the 710 * given loop. 711 */ 712 public BooleanEvent dpadUp(EventLoop loop) { 713 return button(Button.kDpadUp.value, loop); 714 } 715 716 /** 717 * Read the value of the D-pad down button on the controller. 718 * 719 * @return The state of the button. 720 */ 721 public boolean getDpadDownButton() { 722 return getRawButton(Button.kDpadDown.value); 723 } 724 725 /** 726 * Whether the D-pad down button was pressed since the last check. 727 * 728 * @return Whether the button was pressed since the last check. 729 */ 730 public boolean getDpadDownButtonPressed() { 731 return getRawButtonPressed(Button.kDpadDown.value); 732 } 733 734 /** 735 * Whether the D-pad down button was released since the last check. 736 * 737 * @return Whether the button was released since the last check. 738 */ 739 public boolean getDpadDownButtonReleased() { 740 return getRawButtonReleased(Button.kDpadDown.value); 741 } 742 743 /** 744 * Constructs an event instance around the D-pad down button's digital signal. 745 * 746 * @param loop the event loop instance to attach the event to. 747 * @return an event instance representing the D-pad down button's digital signal attached to the 748 * given loop. 749 */ 750 public BooleanEvent dpadDown(EventLoop loop) { 751 return button(Button.kDpadDown.value, loop); 752 } 753 754 /** 755 * Read the value of the D-pad left button on the controller. 756 * 757 * @return The state of the button. 758 */ 759 public boolean getDpadLeftButton() { 760 return getRawButton(Button.kDpadLeft.value); 761 } 762 763 /** 764 * Whether the D-pad left button was pressed since the last check. 765 * 766 * @return Whether the button was pressed since the last check. 767 */ 768 public boolean getDpadLeftButtonPressed() { 769 return getRawButtonPressed(Button.kDpadLeft.value); 770 } 771 772 /** 773 * Whether the D-pad left button was released since the last check. 774 * 775 * @return Whether the button was released since the last check. 776 */ 777 public boolean getDpadLeftButtonReleased() { 778 return getRawButtonReleased(Button.kDpadLeft.value); 779 } 780 781 /** 782 * Constructs an event instance around the D-pad left button's digital signal. 783 * 784 * @param loop the event loop instance to attach the event to. 785 * @return an event instance representing the D-pad left button's digital signal attached to the 786 * given loop. 787 */ 788 public BooleanEvent dpadLeft(EventLoop loop) { 789 return button(Button.kDpadLeft.value, loop); 790 } 791 792 /** 793 * Read the value of the D-pad right button on the controller. 794 * 795 * @return The state of the button. 796 */ 797 public boolean getDpadRightButton() { 798 return getRawButton(Button.kDpadRight.value); 799 } 800 801 /** 802 * Whether the D-pad right button was pressed since the last check. 803 * 804 * @return Whether the button was pressed since the last check. 805 */ 806 public boolean getDpadRightButtonPressed() { 807 return getRawButtonPressed(Button.kDpadRight.value); 808 } 809 810 /** 811 * Whether the D-pad right button was released since the last check. 812 * 813 * @return Whether the button was released since the last check. 814 */ 815 public boolean getDpadRightButtonReleased() { 816 return getRawButtonReleased(Button.kDpadRight.value); 817 } 818 819 /** 820 * Constructs an event instance around the D-pad right button's digital signal. 821 * 822 * @param loop the event loop instance to attach the event to. 823 * @return an event instance representing the D-pad right button's digital signal attached to the 824 * given loop. 825 */ 826 public BooleanEvent dpadRight(EventLoop loop) { 827 return button(Button.kDpadRight.value, loop); 828 } 829 830 /** 831 * Read the value of the Miscellaneous 1 button on the controller. 832 * 833 * @return The state of the button. 834 */ 835 public boolean getMisc1Button() { 836 return getRawButton(Button.kMisc1.value); 837 } 838 839 /** 840 * Whether the Miscellaneous 1 button was pressed since the last check. 841 * 842 * @return Whether the button was pressed since the last check. 843 */ 844 public boolean getMisc1ButtonPressed() { 845 return getRawButtonPressed(Button.kMisc1.value); 846 } 847 848 /** 849 * Whether the Miscellaneous 1 button was released since the last check. 850 * 851 * @return Whether the button was released since the last check. 852 */ 853 public boolean getMisc1ButtonReleased() { 854 return getRawButtonReleased(Button.kMisc1.value); 855 } 856 857 /** 858 * Constructs an event instance around the Miscellaneous 1 button's digital signal. 859 * 860 * @param loop the event loop instance to attach the event to. 861 * @return an event instance representing the Miscellaneous 1 button's digital signal attached to 862 * the given loop. 863 */ 864 public BooleanEvent misc1(EventLoop loop) { 865 return button(Button.kMisc1.value, loop); 866 } 867 868 /** 869 * Read the value of the Right Paddle 1 button on the controller. 870 * 871 * @return The state of the button. 872 */ 873 public boolean getRightPaddle1Button() { 874 return getRawButton(Button.kRightPaddle1.value); 875 } 876 877 /** 878 * Whether the Right Paddle 1 button was pressed since the last check. 879 * 880 * @return Whether the button was pressed since the last check. 881 */ 882 public boolean getRightPaddle1ButtonPressed() { 883 return getRawButtonPressed(Button.kRightPaddle1.value); 884 } 885 886 /** 887 * Whether the Right Paddle 1 button was released since the last check. 888 * 889 * @return Whether the button was released since the last check. 890 */ 891 public boolean getRightPaddle1ButtonReleased() { 892 return getRawButtonReleased(Button.kRightPaddle1.value); 893 } 894 895 /** 896 * Constructs an event instance around the Right Paddle 1 button's digital signal. 897 * 898 * @param loop the event loop instance to attach the event to. 899 * @return an event instance representing the Right Paddle 1 button's digital signal attached to 900 * the given loop. 901 */ 902 public BooleanEvent rightPaddle1(EventLoop loop) { 903 return button(Button.kRightPaddle1.value, loop); 904 } 905 906 /** 907 * Read the value of the Left Paddle 1 button on the controller. 908 * 909 * @return The state of the button. 910 */ 911 public boolean getLeftPaddle1Button() { 912 return getRawButton(Button.kLeftPaddle1.value); 913 } 914 915 /** 916 * Whether the Left Paddle 1 button was pressed since the last check. 917 * 918 * @return Whether the button was pressed since the last check. 919 */ 920 public boolean getLeftPaddle1ButtonPressed() { 921 return getRawButtonPressed(Button.kLeftPaddle1.value); 922 } 923 924 /** 925 * Whether the Left Paddle 1 button was released since the last check. 926 * 927 * @return Whether the button was released since the last check. 928 */ 929 public boolean getLeftPaddle1ButtonReleased() { 930 return getRawButtonReleased(Button.kLeftPaddle1.value); 931 } 932 933 /** 934 * Constructs an event instance around the Left Paddle 1 button's digital signal. 935 * 936 * @param loop the event loop instance to attach the event to. 937 * @return an event instance representing the Left Paddle 1 button's digital signal attached to 938 * the given loop. 939 */ 940 public BooleanEvent leftPaddle1(EventLoop loop) { 941 return button(Button.kLeftPaddle1.value, loop); 942 } 943 944 /** 945 * Read the value of the Right Paddle 2 button on the controller. 946 * 947 * @return The state of the button. 948 */ 949 public boolean getRightPaddle2Button() { 950 return getRawButton(Button.kRightPaddle2.value); 951 } 952 953 /** 954 * Whether the Right Paddle 2 button was pressed since the last check. 955 * 956 * @return Whether the button was pressed since the last check. 957 */ 958 public boolean getRightPaddle2ButtonPressed() { 959 return getRawButtonPressed(Button.kRightPaddle2.value); 960 } 961 962 /** 963 * Whether the Right Paddle 2 button was released since the last check. 964 * 965 * @return Whether the button was released since the last check. 966 */ 967 public boolean getRightPaddle2ButtonReleased() { 968 return getRawButtonReleased(Button.kRightPaddle2.value); 969 } 970 971 /** 972 * Constructs an event instance around the Right Paddle 2 button's digital signal. 973 * 974 * @param loop the event loop instance to attach the event to. 975 * @return an event instance representing the Right Paddle 2 button's digital signal attached to 976 * the given loop. 977 */ 978 public BooleanEvent rightPaddle2(EventLoop loop) { 979 return button(Button.kRightPaddle2.value, loop); 980 } 981 982 /** 983 * Read the value of the Left Paddle 2 button on the controller. 984 * 985 * @return The state of the button. 986 */ 987 public boolean getLeftPaddle2Button() { 988 return getRawButton(Button.kLeftPaddle2.value); 989 } 990 991 /** 992 * Whether the Left Paddle 2 button was pressed since the last check. 993 * 994 * @return Whether the button was pressed since the last check. 995 */ 996 public boolean getLeftPaddle2ButtonPressed() { 997 return getRawButtonPressed(Button.kLeftPaddle2.value); 998 } 999 1000 /** 1001 * Whether the Left Paddle 2 button was released since the last check. 1002 * 1003 * @return Whether the button was released since the last check. 1004 */ 1005 public boolean getLeftPaddle2ButtonReleased() { 1006 return getRawButtonReleased(Button.kLeftPaddle2.value); 1007 } 1008 1009 /** 1010 * Constructs an event instance around the Left Paddle 2 button's digital signal. 1011 * 1012 * @param loop the event loop instance to attach the event to. 1013 * @return an event instance representing the Left Paddle 2 button's digital signal attached to 1014 * the given loop. 1015 */ 1016 public BooleanEvent leftPaddle2(EventLoop loop) { 1017 return button(Button.kLeftPaddle2.value, loop); 1018 } 1019 1020 /** 1021 * Read the value of the Touchpad button on the controller. 1022 * 1023 * @return The state of the button. 1024 */ 1025 public boolean getTouchpadButton() { 1026 return getRawButton(Button.kTouchpad.value); 1027 } 1028 1029 /** 1030 * Whether the Touchpad button was pressed since the last check. 1031 * 1032 * @return Whether the button was pressed since the last check. 1033 */ 1034 public boolean getTouchpadButtonPressed() { 1035 return getRawButtonPressed(Button.kTouchpad.value); 1036 } 1037 1038 /** 1039 * Whether the Touchpad button was released since the last check. 1040 * 1041 * @return Whether the button was released since the last check. 1042 */ 1043 public boolean getTouchpadButtonReleased() { 1044 return getRawButtonReleased(Button.kTouchpad.value); 1045 } 1046 1047 /** 1048 * Constructs an event instance around the Touchpad button's digital signal. 1049 * 1050 * @param loop the event loop instance to attach the event to. 1051 * @return an event instance representing the Touchpad button's digital signal attached to the 1052 * given loop. 1053 */ 1054 public BooleanEvent touchpad(EventLoop loop) { 1055 return button(Button.kTouchpad.value, loop); 1056 } 1057 1058 /** 1059 * Read the value of the Miscellaneous 2 button on the controller. 1060 * 1061 * @return The state of the button. 1062 */ 1063 public boolean getMisc2Button() { 1064 return getRawButton(Button.kMisc2.value); 1065 } 1066 1067 /** 1068 * Whether the Miscellaneous 2 button was pressed since the last check. 1069 * 1070 * @return Whether the button was pressed since the last check. 1071 */ 1072 public boolean getMisc2ButtonPressed() { 1073 return getRawButtonPressed(Button.kMisc2.value); 1074 } 1075 1076 /** 1077 * Whether the Miscellaneous 2 button was released since the last check. 1078 * 1079 * @return Whether the button was released since the last check. 1080 */ 1081 public boolean getMisc2ButtonReleased() { 1082 return getRawButtonReleased(Button.kMisc2.value); 1083 } 1084 1085 /** 1086 * Constructs an event instance around the Miscellaneous 2 button's digital signal. 1087 * 1088 * @param loop the event loop instance to attach the event to. 1089 * @return an event instance representing the Miscellaneous 2 button's digital signal attached to 1090 * the given loop. 1091 */ 1092 public BooleanEvent misc2(EventLoop loop) { 1093 return button(Button.kMisc2.value, loop); 1094 } 1095 1096 /** 1097 * Read the value of the Miscellaneous 3 button on the controller. 1098 * 1099 * @return The state of the button. 1100 */ 1101 public boolean getMisc3Button() { 1102 return getRawButton(Button.kMisc3.value); 1103 } 1104 1105 /** 1106 * Whether the Miscellaneous 3 button was pressed since the last check. 1107 * 1108 * @return Whether the button was pressed since the last check. 1109 */ 1110 public boolean getMisc3ButtonPressed() { 1111 return getRawButtonPressed(Button.kMisc3.value); 1112 } 1113 1114 /** 1115 * Whether the Miscellaneous 3 button was released since the last check. 1116 * 1117 * @return Whether the button was released since the last check. 1118 */ 1119 public boolean getMisc3ButtonReleased() { 1120 return getRawButtonReleased(Button.kMisc3.value); 1121 } 1122 1123 /** 1124 * Constructs an event instance around the Miscellaneous 3 button's digital signal. 1125 * 1126 * @param loop the event loop instance to attach the event to. 1127 * @return an event instance representing the Miscellaneous 3 button's digital signal attached to 1128 * the given loop. 1129 */ 1130 public BooleanEvent misc3(EventLoop loop) { 1131 return button(Button.kMisc3.value, loop); 1132 } 1133 1134 /** 1135 * Read the value of the Miscellaneous 4 button on the controller. 1136 * 1137 * @return The state of the button. 1138 */ 1139 public boolean getMisc4Button() { 1140 return getRawButton(Button.kMisc4.value); 1141 } 1142 1143 /** 1144 * Whether the Miscellaneous 4 button was pressed since the last check. 1145 * 1146 * @return Whether the button was pressed since the last check. 1147 */ 1148 public boolean getMisc4ButtonPressed() { 1149 return getRawButtonPressed(Button.kMisc4.value); 1150 } 1151 1152 /** 1153 * Whether the Miscellaneous 4 button was released since the last check. 1154 * 1155 * @return Whether the button was released since the last check. 1156 */ 1157 public boolean getMisc4ButtonReleased() { 1158 return getRawButtonReleased(Button.kMisc4.value); 1159 } 1160 1161 /** 1162 * Constructs an event instance around the Miscellaneous 4 button's digital signal. 1163 * 1164 * @param loop the event loop instance to attach the event to. 1165 * @return an event instance representing the Miscellaneous 4 button's digital signal attached to 1166 * the given loop. 1167 */ 1168 public BooleanEvent misc4(EventLoop loop) { 1169 return button(Button.kMisc4.value, loop); 1170 } 1171 1172 /** 1173 * Read the value of the Miscellaneous 5 button on the controller. 1174 * 1175 * @return The state of the button. 1176 */ 1177 public boolean getMisc5Button() { 1178 return getRawButton(Button.kMisc5.value); 1179 } 1180 1181 /** 1182 * Whether the Miscellaneous 5 button was pressed since the last check. 1183 * 1184 * @return Whether the button was pressed since the last check. 1185 */ 1186 public boolean getMisc5ButtonPressed() { 1187 return getRawButtonPressed(Button.kMisc5.value); 1188 } 1189 1190 /** 1191 * Whether the Miscellaneous 5 button was released since the last check. 1192 * 1193 * @return Whether the button was released since the last check. 1194 */ 1195 public boolean getMisc5ButtonReleased() { 1196 return getRawButtonReleased(Button.kMisc5.value); 1197 } 1198 1199 /** 1200 * Constructs an event instance around the Miscellaneous 5 button's digital signal. 1201 * 1202 * @param loop the event loop instance to attach the event to. 1203 * @return an event instance representing the Miscellaneous 5 button's digital signal attached to 1204 * the given loop. 1205 */ 1206 public BooleanEvent misc5(EventLoop loop) { 1207 return button(Button.kMisc5.value, loop); 1208 } 1209 1210 /** 1211 * Read the value of the Miscellaneous 6 button on the controller. 1212 * 1213 * @return The state of the button. 1214 */ 1215 public boolean getMisc6Button() { 1216 return getRawButton(Button.kMisc6.value); 1217 } 1218 1219 /** 1220 * Whether the Miscellaneous 6 button was pressed since the last check. 1221 * 1222 * @return Whether the button was pressed since the last check. 1223 */ 1224 public boolean getMisc6ButtonPressed() { 1225 return getRawButtonPressed(Button.kMisc6.value); 1226 } 1227 1228 /** 1229 * Whether the Miscellaneous 6 button was released since the last check. 1230 * 1231 * @return Whether the button was released since the last check. 1232 */ 1233 public boolean getMisc6ButtonReleased() { 1234 return getRawButtonReleased(Button.kMisc6.value); 1235 } 1236 1237 /** 1238 * Constructs an event instance around the Miscellaneous 6 button's digital signal. 1239 * 1240 * @param loop the event loop instance to attach the event to. 1241 * @return an event instance representing the Miscellaneous 6 button's digital signal attached to 1242 * the given loop. 1243 */ 1244 public BooleanEvent misc6(EventLoop loop) { 1245 return button(Button.kMisc6.value, loop); 1246 } 1247 1248 private double getAxisForSendable(int axis) { 1249 return DriverStation.getStickAxisIfAvailable(getPort(), axis).orElse(0.0); 1250 } 1251 1252 private boolean getButtonForSendable(int button) { 1253 return DriverStation.getStickButtonIfAvailable(getPort(), button).orElse(false); 1254 } 1255 1256 @Override 1257 public void initSendable(SendableBuilder builder) { 1258 builder.setSmartDashboardType("HID"); 1259 builder.publishConstString("ControllerType", "Gamepad"); 1260 builder.addDoubleProperty( 1261 "LeftTrigger Axis", () -> getAxisForSendable(Axis.kLeftTrigger.value), null); 1262 builder.addDoubleProperty( 1263 "RightTrigger Axis", () -> getAxisForSendable(Axis.kRightTrigger.value), null); 1264 builder.addDoubleProperty("LeftX", () -> getAxisForSendable(Axis.kLeftX.value), null); 1265 builder.addDoubleProperty("LeftY", () -> getAxisForSendable(Axis.kLeftY.value), null); 1266 builder.addDoubleProperty("RightX", () -> getAxisForSendable(Axis.kRightX.value), null); 1267 builder.addDoubleProperty("RightY", () -> getAxisForSendable(Axis.kRightY.value), null); 1268 builder.addBooleanProperty( 1269 "SouthFace", () -> getButtonForSendable(Button.kSouthFace.value), null); 1270 builder.addBooleanProperty( 1271 "EastFace", () -> getButtonForSendable(Button.kEastFace.value), null); 1272 builder.addBooleanProperty( 1273 "WestFace", () -> getButtonForSendable(Button.kWestFace.value), null); 1274 builder.addBooleanProperty( 1275 "NorthFace", () -> getButtonForSendable(Button.kNorthFace.value), null); 1276 builder.addBooleanProperty("Back", () -> getButtonForSendable(Button.kBack.value), null); 1277 builder.addBooleanProperty("Guide", () -> getButtonForSendable(Button.kGuide.value), null); 1278 builder.addBooleanProperty("Start", () -> getButtonForSendable(Button.kStart.value), null); 1279 builder.addBooleanProperty( 1280 "LeftStick", () -> getButtonForSendable(Button.kLeftStick.value), null); 1281 builder.addBooleanProperty( 1282 "RightStick", () -> getButtonForSendable(Button.kRightStick.value), null); 1283 builder.addBooleanProperty( 1284 "LeftBumper", () -> getButtonForSendable(Button.kLeftBumper.value), null); 1285 builder.addBooleanProperty( 1286 "RightBumper", () -> getButtonForSendable(Button.kRightBumper.value), null); 1287 builder.addBooleanProperty("DpadUp", () -> getButtonForSendable(Button.kDpadUp.value), null); 1288 builder.addBooleanProperty( 1289 "DpadDown", () -> getButtonForSendable(Button.kDpadDown.value), null); 1290 builder.addBooleanProperty( 1291 "DpadLeft", () -> getButtonForSendable(Button.kDpadLeft.value), null); 1292 builder.addBooleanProperty( 1293 "DpadRight", () -> getButtonForSendable(Button.kDpadRight.value), null); 1294 builder.addBooleanProperty("Misc1", () -> getButtonForSendable(Button.kMisc1.value), null); 1295 builder.addBooleanProperty( 1296 "RightPaddle1", () -> getButtonForSendable(Button.kRightPaddle1.value), null); 1297 builder.addBooleanProperty( 1298 "LeftPaddle1", () -> getButtonForSendable(Button.kLeftPaddle1.value), null); 1299 builder.addBooleanProperty( 1300 "RightPaddle2", () -> getButtonForSendable(Button.kRightPaddle2.value), null); 1301 builder.addBooleanProperty( 1302 "LeftPaddle2", () -> getButtonForSendable(Button.kLeftPaddle2.value), null); 1303 builder.addBooleanProperty( 1304 "Touchpad", () -> getButtonForSendable(Button.kTouchpad.value), null); 1305 builder.addBooleanProperty("Misc2", () -> getButtonForSendable(Button.kMisc2.value), null); 1306 builder.addBooleanProperty("Misc3", () -> getButtonForSendable(Button.kMisc3.value), null); 1307 builder.addBooleanProperty("Misc4", () -> getButtonForSendable(Button.kMisc4.value), null); 1308 builder.addBooleanProperty("Misc5", () -> getButtonForSendable(Button.kMisc5.value), null); 1309 builder.addBooleanProperty("Misc6", () -> getButtonForSendable(Button.kMisc6.value), null); 1310 } 1311}