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.driverstation.internal.DriverStationBackend;
008import org.wpilib.event.BooleanEvent;
009import org.wpilib.event.EventLoop;
010import org.wpilib.hardware.hal.HAL;
011import org.wpilib.util.sendable.Sendable;
012import org.wpilib.util.sendable.SendableBuilder;
013
014/**
015 * Handle input from Gamepad controllers connected to the Driver Station.
016 *
017 * <p>This class handles Gamepad input that comes from the Driver Station. Each time a value is
018 * requested the most recent value is returned. There is a single class instance for each controller
019 * and the mapping of ports to hardware buttons depends on the code in the Driver Station.
020 *
021 * <p>Only first party controllers from Generic are guaranteed to have the correct mapping, and only
022 * through the official NI DS. Sim is not guaranteed to have the same mapping, as well as any 3rd
023 * party controllers.
024 */
025public class Gamepad extends GenericHID implements Sendable {
026  /** Represents a digital button on a Gamepad. */
027  public enum Button {
028    /** South Face button. */
029    SOUTH_FACE(0, "SouthFaceButton"),
030    /** East Face button. */
031    EAST_FACE(1, "EastFaceButton"),
032    /** West Face button. */
033    WEST_FACE(2, "WestFaceButton"),
034    /** North Face button. */
035    NORTH_FACE(3, "NorthFaceButton"),
036    /** Back button. */
037    BACK(4, "BackButton"),
038    /** Guide button. */
039    GUIDE(5, "GuideButton"),
040    /** Start button. */
041    START(6, "StartButton"),
042    /** Left stick button. */
043    LEFT_STICK(7, "LeftStickButton"),
044    /** Right stick button. */
045    RIGHT_STICK(8, "RightStickButton"),
046    /** Left bumper button. */
047    LEFT_BUMPER(9, "LeftBumperButton"),
048    /** Right bumper button. */
049    RIGHT_BUMPER(10, "RightBumperButton"),
050    /** D-pad up button. */
051    DPAD_UP(11, "DpadUpButton"),
052    /** D-pad down button. */
053    DPAD_DOWN(12, "DpadDownButton"),
054    /** D-pad left button. */
055    DPAD_LEFT(13, "DpadLeftButton"),
056    /** D-pad right button. */
057    DPAD_RIGHT(14, "DpadRightButton"),
058    /** Miscellaneous 1 button. */
059    MISC_1(15, "Misc1Button"),
060    /** Right Paddle 1 button. */
061    RIGHT_PADDLE_1(16, "RightPaddle1Button"),
062    /** Left Paddle 1 button. */
063    LEFT_PADDLE_1(17, "LeftPaddle1Button"),
064    /** Right Paddle 2 button. */
065    RIGHT_PADDLE_2(18, "RightPaddle2Button"),
066    /** Left Paddle 2 button. */
067    LEFT_PADDLE_2(19, "LeftPaddle2Button"),
068    /** Touchpad button. */
069    TOUCHPAD(20, "TouchpadButton"),
070    /** Miscellaneous 2 button. */
071    MISC_2(21, "Misc2Button"),
072    /** Miscellaneous 3 button. */
073    MISC_3(22, "Misc3Button"),
074    /** Miscellaneous 4 button. */
075    MISC_4(23, "Misc4Button"),
076    /** Miscellaneous 5 button. */
077    MISC_5(24, "Misc5Button"),
078    /** Miscellaneous 6 button. */
079    MISC_6(25, "Misc6Button");
080
081    /** Button value. */
082    public final int value;
083
084    private final String m_name;
085
086    Button(int value, String name) {
087      this.value = value;
088      this.m_name = name;
089    }
090
091    /**
092     * Get the human-friendly name of the button, matching the relevant methods.
093     *
094     * <p>Primarily used for automated unit tests.
095     *
096     * @return the human-friendly name of the button.
097     */
098    @Override
099    public String toString() {
100      return m_name;
101    }
102  }
103
104  /** Represents an axis on an Gamepad. */
105  public enum Axis {
106    /** Left X axis. */
107    LEFT_X(0, "LeftX"),
108    /** Left Y axis. */
109    LEFT_Y(1, "LeftY"),
110    /** Right X axis. */
111    RIGHT_X(2, "RightX"),
112    /** Right Y axis. */
113    RIGHT_Y(3, "RightY"),
114    /** Left trigger. */
115    LEFT_TRIGGER(4, "LeftTriggerAxis"),
116    /** Right trigger. */
117    RIGHT_TRIGGER(5, "RightTriggerAxis");
118
119    /** Axis value. */
120    public final int value;
121
122    private final String m_name;
123
124    Axis(int value, String name) {
125      this.value = value;
126      this.m_name = name;
127    }
128
129    /**
130     * Get the human-friendly name of the axis, matching the relevant methods.
131     *
132     * <p>Primarily used for automated unit tests.
133     *
134     * @return the human-friendly name of the axis.
135     */
136    @Override
137    public String toString() {
138      return m_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 getAxis(Axis.LEFT_X);
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 getAxis(Axis.LEFT_Y);
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 getAxis(Axis.RIGHT_X);
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 getAxis(Axis.RIGHT_Y);
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 getAxis(Axis.LEFT_TRIGGER);
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.LEFT_TRIGGER, 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 getAxis(Axis.RIGHT_TRIGGER);
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.RIGHT_TRIGGER, 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 getButton(Button.SOUTH_FACE);
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 getButtonPressed(Button.SOUTH_FACE);
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 getButtonReleased(Button.SOUTH_FACE);
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.SOUTH_FACE, 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 getButton(Button.EAST_FACE);
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 getButtonPressed(Button.EAST_FACE);
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 getButtonReleased(Button.EAST_FACE);
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.EAST_FACE, 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 getButton(Button.WEST_FACE);
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 getButtonPressed(Button.WEST_FACE);
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 getButtonReleased(Button.WEST_FACE);
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.WEST_FACE, 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 getButton(Button.NORTH_FACE);
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 getButtonPressed(Button.NORTH_FACE);
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 getButtonReleased(Button.NORTH_FACE);
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.NORTH_FACE, 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 getButton(Button.BACK);
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 getButtonPressed(Button.BACK);
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 getButtonReleased(Button.BACK);
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.BACK, 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 getButton(Button.GUIDE);
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 getButtonPressed(Button.GUIDE);
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 getButtonReleased(Button.GUIDE);
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.GUIDE, 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 getButton(Button.START);
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 getButtonPressed(Button.START);
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 getButtonReleased(Button.START);
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.START, 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 getButton(Button.LEFT_STICK);
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 getButtonPressed(Button.LEFT_STICK);
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 getButtonReleased(Button.LEFT_STICK);
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.LEFT_STICK, 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 getButton(Button.RIGHT_STICK);
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 getButtonPressed(Button.RIGHT_STICK);
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 getButtonReleased(Button.RIGHT_STICK);
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.RIGHT_STICK, 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 getButton(Button.LEFT_BUMPER);
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 getButtonPressed(Button.LEFT_BUMPER);
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 getButtonReleased(Button.LEFT_BUMPER);
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.LEFT_BUMPER, 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 getButton(Button.RIGHT_BUMPER);
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 getButtonPressed(Button.RIGHT_BUMPER);
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 getButtonReleased(Button.RIGHT_BUMPER);
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.RIGHT_BUMPER, 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 getButton(Button.DPAD_UP);
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 getButtonPressed(Button.DPAD_UP);
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 getButtonReleased(Button.DPAD_UP);
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.DPAD_UP, 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 getButton(Button.DPAD_DOWN);
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 getButtonPressed(Button.DPAD_DOWN);
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 getButtonReleased(Button.DPAD_DOWN);
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.DPAD_DOWN, 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 getButton(Button.DPAD_LEFT);
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 getButtonPressed(Button.DPAD_LEFT);
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 getButtonReleased(Button.DPAD_LEFT);
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.DPAD_LEFT, 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 getButton(Button.DPAD_RIGHT);
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 getButtonPressed(Button.DPAD_RIGHT);
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 getButtonReleased(Button.DPAD_RIGHT);
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.DPAD_RIGHT, 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 getButton(Button.MISC_1);
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 getButtonPressed(Button.MISC_1);
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 getButtonReleased(Button.MISC_1);
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.MISC_1, 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 getButton(Button.RIGHT_PADDLE_1);
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 getButtonPressed(Button.RIGHT_PADDLE_1);
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 getButtonReleased(Button.RIGHT_PADDLE_1);
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.RIGHT_PADDLE_1, 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 getButton(Button.LEFT_PADDLE_1);
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 getButtonPressed(Button.LEFT_PADDLE_1);
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 getButtonReleased(Button.LEFT_PADDLE_1);
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.LEFT_PADDLE_1, 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 getButton(Button.RIGHT_PADDLE_2);
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 getButtonPressed(Button.RIGHT_PADDLE_2);
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 getButtonReleased(Button.RIGHT_PADDLE_2);
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.RIGHT_PADDLE_2, 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 getButton(Button.LEFT_PADDLE_2);
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 getButtonPressed(Button.LEFT_PADDLE_2);
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 getButtonReleased(Button.LEFT_PADDLE_2);
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.LEFT_PADDLE_2, 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 getButton(Button.TOUCHPAD);
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 getButtonPressed(Button.TOUCHPAD);
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 getButtonReleased(Button.TOUCHPAD);
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.TOUCHPAD, 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 getButton(Button.MISC_2);
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 getButtonPressed(Button.MISC_2);
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 getButtonReleased(Button.MISC_2);
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.MISC_2, 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 getButton(Button.MISC_3);
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 getButtonPressed(Button.MISC_3);
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 getButtonReleased(Button.MISC_3);
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.MISC_3, 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 getButton(Button.MISC_4);
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 getButtonPressed(Button.MISC_4);
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 getButtonReleased(Button.MISC_4);
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.MISC_4, 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 getButton(Button.MISC_5);
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 getButtonPressed(Button.MISC_5);
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 getButtonReleased(Button.MISC_5);
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.MISC_5, 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 getButton(Button.MISC_6);
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 getButtonPressed(Button.MISC_6);
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 getButtonReleased(Button.MISC_6);
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.MISC_6, loop);
1246  }
1247
1248  /**
1249   * Get the button value (starting at button 1).
1250   *
1251   * <p>This method returns true if the button is being held down at the time that this method is
1252   * being called.
1253   *
1254   * @param button The button
1255   * @return The state of the button.
1256   */
1257  public boolean getButton(Button button) {
1258    return getRawButton(button.value);
1259  }
1260
1261  /**
1262   * Whether the button was pressed since the last check.
1263   *
1264   * <p>This method returns true if the button went from not pressed to held down since the last
1265   * time this method was called. This is useful if you only want to call a function once when you
1266   * press the button.
1267   *
1268   * @param button The button
1269   * @return Whether the button was pressed since the last check.
1270   */
1271  public boolean getButtonPressed(Button button) {
1272    return getRawButtonPressed(button.value);
1273  }
1274
1275  /**
1276   * Whether the button was released since the last check.
1277   *
1278   * <p>This method returns true if the button went from held down to not pressed since the last
1279   * time this method was called. This is useful if you only want to call a function once when you
1280   * release the button.
1281   *
1282   * @param button The button
1283   * @return Whether the button was released since the last check.
1284   */
1285  public boolean getButtonReleased(Button button) {
1286    return getRawButtonReleased(button.value);
1287  }
1288
1289  /**
1290   * Constructs an event instance around this button's digital signal.
1291   *
1292   * @param button the button
1293   * @param loop the event loop instance to attach the event to.
1294   * @return an event instance representing the button's digital signal attached to the given loop.
1295   */
1296  public BooleanEvent button(Button button, EventLoop loop) {
1297    return super.button(button.value, loop);
1298  }
1299
1300  /**
1301   * Get the value of the axis.
1302   *
1303   * @param axis The axis to read
1304   * @return The value of the axis.
1305   */
1306  public double getAxis(Axis axis) {
1307    return getRawAxis(axis.value);
1308  }
1309
1310  /**
1311   * Constructs an event instance that is true when the axis value is less than {@code threshold},
1312   * attached to the given loop.
1313   *
1314   * @param axis The axis to read, starting at 0
1315   * @param threshold The value below which this event should return true.
1316   * @param loop the event loop instance to attach the event to.
1317   * @return an event instance that is true when the axis value is less than the provided threshold.
1318   */
1319  public BooleanEvent axisLessThan(Axis axis, double threshold, EventLoop loop) {
1320    return super.axisLessThan(axis.value, threshold, loop);
1321  }
1322
1323  /**
1324   * Constructs an event instance that is true when the axis value is greater than {@code
1325   * threshold}, attached to the given loop.
1326   *
1327   * @param axis The axis to read, starting at 0
1328   * @param threshold The value above which this event should return true.
1329   * @param loop the event loop instance to attach the event to.
1330   * @return an event instance that is true when the axis value is greater than the provided
1331   *     threshold.
1332   */
1333  public BooleanEvent axisGreaterThan(Axis axis, double threshold, EventLoop loop) {
1334    return super.axisGreaterThan(axis.value, threshold, loop);
1335  }
1336
1337  private double getAxisForSendable(Axis axis) {
1338    return DriverStationBackend.getStickAxisIfAvailable(getPort(), axis.value).orElse(0.0);
1339  }
1340
1341  private boolean getButtonForSendable(Button button) {
1342    return DriverStationBackend.getStickButtonIfAvailable(getPort(), button.value).orElse(false);
1343  }
1344
1345  @Override
1346  public void initSendable(SendableBuilder builder) {
1347    builder.setSmartDashboardType("HID");
1348    builder.publishConstString("ControllerType", "Gamepad");
1349    builder.addDoubleProperty(
1350        "LeftTrigger Axis", () -> getAxisForSendable(Axis.LEFT_TRIGGER), null);
1351    builder.addDoubleProperty(
1352        "RightTrigger Axis", () -> getAxisForSendable(Axis.RIGHT_TRIGGER), null);
1353    builder.addDoubleProperty("LeftX", () -> getAxisForSendable(Axis.LEFT_X), null);
1354    builder.addDoubleProperty("LeftY", () -> getAxisForSendable(Axis.LEFT_Y), null);
1355    builder.addDoubleProperty("RightX", () -> getAxisForSendable(Axis.RIGHT_X), null);
1356    builder.addDoubleProperty("RightY", () -> getAxisForSendable(Axis.RIGHT_Y), null);
1357    builder.addBooleanProperty("SouthFace", () -> getButtonForSendable(Button.SOUTH_FACE), null);
1358    builder.addBooleanProperty("EastFace", () -> getButtonForSendable(Button.EAST_FACE), null);
1359    builder.addBooleanProperty("WestFace", () -> getButtonForSendable(Button.WEST_FACE), null);
1360    builder.addBooleanProperty("NorthFace", () -> getButtonForSendable(Button.NORTH_FACE), null);
1361    builder.addBooleanProperty("Back", () -> getButtonForSendable(Button.BACK), null);
1362    builder.addBooleanProperty("Guide", () -> getButtonForSendable(Button.GUIDE), null);
1363    builder.addBooleanProperty("Start", () -> getButtonForSendable(Button.START), null);
1364    builder.addBooleanProperty("LeftStick", () -> getButtonForSendable(Button.LEFT_STICK), null);
1365    builder.addBooleanProperty("RightStick", () -> getButtonForSendable(Button.RIGHT_STICK), null);
1366    builder.addBooleanProperty("LeftBumper", () -> getButtonForSendable(Button.LEFT_BUMPER), null);
1367    builder.addBooleanProperty(
1368        "RightBumper", () -> getButtonForSendable(Button.RIGHT_BUMPER), null);
1369    builder.addBooleanProperty("DpadUp", () -> getButtonForSendable(Button.DPAD_UP), null);
1370    builder.addBooleanProperty("DpadDown", () -> getButtonForSendable(Button.DPAD_DOWN), null);
1371    builder.addBooleanProperty("DpadLeft", () -> getButtonForSendable(Button.DPAD_LEFT), null);
1372    builder.addBooleanProperty("DpadRight", () -> getButtonForSendable(Button.DPAD_RIGHT), null);
1373    builder.addBooleanProperty("Misc1", () -> getButtonForSendable(Button.MISC_1), null);
1374    builder.addBooleanProperty(
1375        "RightPaddle1", () -> getButtonForSendable(Button.RIGHT_PADDLE_1), null);
1376    builder.addBooleanProperty(
1377        "LeftPaddle1", () -> getButtonForSendable(Button.LEFT_PADDLE_1), null);
1378    builder.addBooleanProperty(
1379        "RightPaddle2", () -> getButtonForSendable(Button.RIGHT_PADDLE_2), null);
1380    builder.addBooleanProperty(
1381        "LeftPaddle2", () -> getButtonForSendable(Button.LEFT_PADDLE_2), null);
1382    builder.addBooleanProperty("Touchpad", () -> getButtonForSendable(Button.TOUCHPAD), null);
1383    builder.addBooleanProperty("Misc2", () -> getButtonForSendable(Button.MISC_2), null);
1384    builder.addBooleanProperty("Misc3", () -> getButtonForSendable(Button.MISC_3), null);
1385    builder.addBooleanProperty("Misc4", () -> getButtonForSendable(Button.MISC_4), null);
1386    builder.addBooleanProperty("Misc5", () -> getButtonForSendable(Button.MISC_5), null);
1387    builder.addBooleanProperty("Misc6", () -> getButtonForSendable(Button.MISC_6), null);
1388  }
1389}