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
005// THIS FILE WAS AUTO-GENERATED BY ./wpilibj/generate_hids.py. DO NOT MODIFY
006
007package edu.wpi.first.wpilibj;
008
009import edu.wpi.first.hal.HAL;
010import edu.wpi.first.util.sendable.Sendable;
011import edu.wpi.first.util.sendable.SendableBuilder;
012import edu.wpi.first.wpilibj.event.BooleanEvent;
013import edu.wpi.first.wpilibj.event.EventLoop;
014
015/**
016 * Handle input from Xbox controllers connected to the Driver Station.
017 *
018 * <p>This class handles Xbox input that comes from the Driver Station. Each time a value is
019 * requested the most recent value is returned. There is a single class instance for each controller
020 * and the mapping of ports to hardware buttons depends on the code in the Driver Station.
021 *
022 * <p>Only first party controllers from Microsoft are guaranteed to have the correct mapping, and
023 * only through the official NI DS. Sim is not guaranteed to have the same mapping, as well as any
024 * 3rd party controllers.
025 */
026public class XboxController extends GenericHID implements Sendable {
027  /** Represents a digital button on a XboxController. */
028  public enum Button {
029    /** A button. */
030    kA(1),
031    /** B button. */
032    kB(2),
033    /** X button. */
034    kX(3),
035    /** Y button. */
036    kY(4),
037    /** Left bumper button. */
038    kLeftBumper(5),
039    /** Right bumper button. */
040    kRightBumper(6),
041    /** Back button. */
042    kBack(7),
043    /** Start button. */
044    kStart(8),
045    /** Left stick button. */
046    kLeftStick(9),
047    /** Right stick button. */
048    kRightStick(10);
049
050    /** Button value. */
051    public final int value;
052
053    Button(int value) {
054      this.value = value;
055    }
056
057    /**
058     * Get the human-friendly name of the button, matching the relevant methods. This is done by
059     * stripping the leading `k`, and appending `Button`.
060     *
061     * <p>Primarily used for automated unit tests.
062     *
063     * @return the human-friendly name of the button.
064     */
065    @Override
066    public String toString() {
067      // Remove leading `k`
068      return this.name().substring(1) + "Button";
069    }
070  }
071
072  /** Represents an axis on an XboxController. */
073  public enum Axis {
074    /** Left X axis. */
075    kLeftX(0),
076    /** Right X axis. */
077    kRightX(4),
078    /** Left Y axis. */
079    kLeftY(1),
080    /** Right Y axis. */
081    kRightY(5),
082    /** Left trigger. */
083    kLeftTrigger(2),
084    /** Right trigger. */
085    kRightTrigger(3);
086
087    /** Axis value. */
088    public final int value;
089
090    Axis(int value) {
091      this.value = value;
092    }
093
094    /**
095     * Get the human-friendly name of the axis, matching the relevant methods. This is done by
096     * stripping the leading `k`, and appending `Axis` if the name ends with `Trigger`.
097     *
098     * <p>Primarily used for automated unit tests.
099     *
100     * @return the human-friendly name of the axis.
101     */
102    @Override
103    public String toString() {
104      var name = this.name().substring(1); // Remove leading `k`
105      if (name.endsWith("Trigger")) {
106        return name + "Axis";
107      }
108      return name;
109    }
110  }
111
112  /**
113   * Construct an instance of a controller.
114   *
115   * @param port The port index on the Driver Station that the controller is plugged into (0-5).
116   */
117  public XboxController(final int port) {
118    super(port);
119    HAL.reportUsage("HID", port, "XboxController");
120  }
121
122  /**
123   * Get the X axis value of left side of the controller. Right is positive.
124   *
125   * @return The axis value.
126   */
127  public double getLeftX() {
128    return getRawAxis(Axis.kLeftX.value);
129  }
130
131  /**
132   * Get the X axis value of right side of the controller. Right is positive.
133   *
134   * @return The axis value.
135   */
136  public double getRightX() {
137    return getRawAxis(Axis.kRightX.value);
138  }
139
140  /**
141   * Get the Y axis value of left side of the controller. Back is positive.
142   *
143   * @return The axis value.
144   */
145  public double getLeftY() {
146    return getRawAxis(Axis.kLeftY.value);
147  }
148
149  /**
150   * Get the Y axis value of right side of the controller. Back is positive.
151   *
152   * @return The axis value.
153   */
154  public double getRightY() {
155    return getRawAxis(Axis.kRightY.value);
156  }
157
158  /**
159   * Get the left trigger axis value of the controller. Note that this axis is bound to the
160   * range of [0, 1] as opposed to the usual [-1, 1].
161   *
162   * @return The axis value.
163   */
164  public double getLeftTriggerAxis() {
165    return getRawAxis(Axis.kLeftTrigger.value);
166  }
167
168  /**
169   * Constructs an event instance around the axis value of the left trigger. The returned trigger
170   * will be true when the axis value is greater than {@code threshold}.
171   *
172   * @param threshold the minimum axis value for the returned {@link BooleanEvent} to be true. This
173   *     value should be in the range [0, 1] where 0 is the unpressed state of the axis.
174   * @param loop the event loop instance to attach the event to.
175   * @return an event instance that is true when the left trigger's axis exceeds the provided
176   *     threshold, attached to the given event loop
177   */
178  public BooleanEvent leftTrigger(double threshold, EventLoop loop) {
179    return axisGreaterThan(Axis.kLeftTrigger.value, threshold, loop);
180  }
181
182  /**
183   * Constructs an event instance around the axis value of the left trigger. The returned trigger
184   * will be true when the axis value is greater than 0.5.
185   *
186   * @param loop the event loop instance to attach the event to.
187   * @return an event instance that is true when the left trigger's axis exceeds the provided
188   *     threshold, attached to the given event loop
189   */
190  public BooleanEvent leftTrigger(EventLoop loop) {
191    return leftTrigger(0.5, loop);
192  }
193
194  /**
195   * Get the right trigger axis value of the controller. Note that this axis is bound to the
196   * range of [0, 1] as opposed to the usual [-1, 1].
197   *
198   * @return The axis value.
199   */
200  public double getRightTriggerAxis() {
201    return getRawAxis(Axis.kRightTrigger.value);
202  }
203
204  /**
205   * Constructs an event instance around the axis value of the right trigger. The returned trigger
206   * will be true when the axis value is greater than {@code threshold}.
207   *
208   * @param threshold the minimum axis value for the returned {@link BooleanEvent} to be true. This
209   *     value should be in the range [0, 1] where 0 is the unpressed state of the axis.
210   * @param loop the event loop instance to attach the event to.
211   * @return an event instance that is true when the right trigger's axis exceeds the provided
212   *     threshold, attached to the given event loop
213   */
214  public BooleanEvent rightTrigger(double threshold, EventLoop loop) {
215    return axisGreaterThan(Axis.kRightTrigger.value, threshold, loop);
216  }
217
218  /**
219   * Constructs an event instance around the axis value of the right trigger. The returned trigger
220   * will be true when the axis value is greater than 0.5.
221   *
222   * @param loop the event loop instance to attach the event to.
223   * @return an event instance that is true when the right trigger's axis exceeds the provided
224   *     threshold, attached to the given event loop
225   */
226  public BooleanEvent rightTrigger(EventLoop loop) {
227    return rightTrigger(0.5, loop);
228  }
229
230  /**
231   * Read the value of the A button on the controller.
232   *
233   * @return The state of the button.
234   */
235  public boolean getAButton() {
236    return getRawButton(Button.kA.value);
237  }
238
239  /**
240   * Whether the A button was pressed since the last check.
241   *
242   * @return Whether the button was pressed since the last check.
243   */
244  public boolean getAButtonPressed() {
245    return getRawButtonPressed(Button.kA.value);
246  }
247
248  /**
249   * Whether the A button was released since the last check.
250   *
251   * @return Whether the button was released since the last check.
252   */
253  public boolean getAButtonReleased() {
254    return getRawButtonReleased(Button.kA.value);
255  }
256
257  /**
258   * Constructs an event instance around the A button's digital signal.
259   *
260   * @param loop the event loop instance to attach the event to.
261   * @return an event instance representing the A button's digital signal
262   *     attached to the given loop.
263   */
264  public BooleanEvent a(EventLoop loop) {
265    return button(Button.kA.value, loop);
266  }
267
268  /**
269   * Read the value of the B button on the controller.
270   *
271   * @return The state of the button.
272   */
273  public boolean getBButton() {
274    return getRawButton(Button.kB.value);
275  }
276
277  /**
278   * Whether the B button was pressed since the last check.
279   *
280   * @return Whether the button was pressed since the last check.
281   */
282  public boolean getBButtonPressed() {
283    return getRawButtonPressed(Button.kB.value);
284  }
285
286  /**
287   * Whether the B button was released since the last check.
288   *
289   * @return Whether the button was released since the last check.
290   */
291  public boolean getBButtonReleased() {
292    return getRawButtonReleased(Button.kB.value);
293  }
294
295  /**
296   * Constructs an event instance around the B button's digital signal.
297   *
298   * @param loop the event loop instance to attach the event to.
299   * @return an event instance representing the B button's digital signal
300   *     attached to the given loop.
301   */
302  public BooleanEvent b(EventLoop loop) {
303    return button(Button.kB.value, loop);
304  }
305
306  /**
307   * Read the value of the X button on the controller.
308   *
309   * @return The state of the button.
310   */
311  public boolean getXButton() {
312    return getRawButton(Button.kX.value);
313  }
314
315  /**
316   * Whether the X button was pressed since the last check.
317   *
318   * @return Whether the button was pressed since the last check.
319   */
320  public boolean getXButtonPressed() {
321    return getRawButtonPressed(Button.kX.value);
322  }
323
324  /**
325   * Whether the X button was released since the last check.
326   *
327   * @return Whether the button was released since the last check.
328   */
329  public boolean getXButtonReleased() {
330    return getRawButtonReleased(Button.kX.value);
331  }
332
333  /**
334   * Constructs an event instance around the X button's digital signal.
335   *
336   * @param loop the event loop instance to attach the event to.
337   * @return an event instance representing the X button's digital signal
338   *     attached to the given loop.
339   */
340  public BooleanEvent x(EventLoop loop) {
341    return button(Button.kX.value, loop);
342  }
343
344  /**
345   * Read the value of the Y button on the controller.
346   *
347   * @return The state of the button.
348   */
349  public boolean getYButton() {
350    return getRawButton(Button.kY.value);
351  }
352
353  /**
354   * Whether the Y button was pressed since the last check.
355   *
356   * @return Whether the button was pressed since the last check.
357   */
358  public boolean getYButtonPressed() {
359    return getRawButtonPressed(Button.kY.value);
360  }
361
362  /**
363   * Whether the Y button was released since the last check.
364   *
365   * @return Whether the button was released since the last check.
366   */
367  public boolean getYButtonReleased() {
368    return getRawButtonReleased(Button.kY.value);
369  }
370
371  /**
372   * Constructs an event instance around the Y button's digital signal.
373   *
374   * @param loop the event loop instance to attach the event to.
375   * @return an event instance representing the Y button's digital signal
376   *     attached to the given loop.
377   */
378  public BooleanEvent y(EventLoop loop) {
379    return button(Button.kY.value, loop);
380  }
381
382  /**
383   * Read the value of the left bumper button on the controller.
384   *
385   * @return The state of the button.
386   */
387  public boolean getLeftBumperButton() {
388    return getRawButton(Button.kLeftBumper.value);
389  }
390
391  /**
392   * Whether the left bumper button was pressed since the last check.
393   *
394   * @return Whether the button was pressed since the last check.
395   */
396  public boolean getLeftBumperButtonPressed() {
397    return getRawButtonPressed(Button.kLeftBumper.value);
398  }
399
400  /**
401   * Whether the left bumper button was released since the last check.
402   *
403   * @return Whether the button was released since the last check.
404   */
405  public boolean getLeftBumperButtonReleased() {
406    return getRawButtonReleased(Button.kLeftBumper.value);
407  }
408
409  /**
410   * Constructs an event instance around the left bumper button's digital signal.
411   *
412   * @param loop the event loop instance to attach the event to.
413   * @return an event instance representing the left bumper button's digital signal
414   *     attached to the given loop.
415   */
416  public BooleanEvent leftBumper(EventLoop loop) {
417    return button(Button.kLeftBumper.value, loop);
418  }
419
420  /**
421   * Read the value of the right bumper button on the controller.
422   *
423   * @return The state of the button.
424   */
425  public boolean getRightBumperButton() {
426    return getRawButton(Button.kRightBumper.value);
427  }
428
429  /**
430   * Whether the right bumper button was pressed since the last check.
431   *
432   * @return Whether the button was pressed since the last check.
433   */
434  public boolean getRightBumperButtonPressed() {
435    return getRawButtonPressed(Button.kRightBumper.value);
436  }
437
438  /**
439   * Whether the right bumper button was released since the last check.
440   *
441   * @return Whether the button was released since the last check.
442   */
443  public boolean getRightBumperButtonReleased() {
444    return getRawButtonReleased(Button.kRightBumper.value);
445  }
446
447  /**
448   * Constructs an event instance around the right bumper button's digital signal.
449   *
450   * @param loop the event loop instance to attach the event to.
451   * @return an event instance representing the right bumper button's digital signal
452   *     attached to the given loop.
453   */
454  public BooleanEvent rightBumper(EventLoop loop) {
455    return button(Button.kRightBumper.value, loop);
456  }
457
458  /**
459   * Read the value of the back button on the controller.
460   *
461   * @return The state of the button.
462   */
463  public boolean getBackButton() {
464    return getRawButton(Button.kBack.value);
465  }
466
467  /**
468   * Whether the back button was pressed since the last check.
469   *
470   * @return Whether the button was pressed since the last check.
471   */
472  public boolean getBackButtonPressed() {
473    return getRawButtonPressed(Button.kBack.value);
474  }
475
476  /**
477   * Whether the back button was released since the last check.
478   *
479   * @return Whether the button was released since the last check.
480   */
481  public boolean getBackButtonReleased() {
482    return getRawButtonReleased(Button.kBack.value);
483  }
484
485  /**
486   * Constructs an event instance around the back button's digital signal.
487   *
488   * @param loop the event loop instance to attach the event to.
489   * @return an event instance representing the back button's digital signal
490   *     attached to the given loop.
491   */
492  public BooleanEvent back(EventLoop loop) {
493    return button(Button.kBack.value, loop);
494  }
495
496  /**
497   * Read the value of the start button on the controller.
498   *
499   * @return The state of the button.
500   */
501  public boolean getStartButton() {
502    return getRawButton(Button.kStart.value);
503  }
504
505  /**
506   * Whether the start button was pressed since the last check.
507   *
508   * @return Whether the button was pressed since the last check.
509   */
510  public boolean getStartButtonPressed() {
511    return getRawButtonPressed(Button.kStart.value);
512  }
513
514  /**
515   * Whether the start button was released since the last check.
516   *
517   * @return Whether the button was released since the last check.
518   */
519  public boolean getStartButtonReleased() {
520    return getRawButtonReleased(Button.kStart.value);
521  }
522
523  /**
524   * Constructs an event instance around the start button's digital signal.
525   *
526   * @param loop the event loop instance to attach the event to.
527   * @return an event instance representing the start button's digital signal
528   *     attached to the given loop.
529   */
530  public BooleanEvent start(EventLoop loop) {
531    return button(Button.kStart.value, loop);
532  }
533
534  /**
535   * Read the value of the left stick button on the controller.
536   *
537   * @return The state of the button.
538   */
539  public boolean getLeftStickButton() {
540    return getRawButton(Button.kLeftStick.value);
541  }
542
543  /**
544   * Whether the left stick button was pressed since the last check.
545   *
546   * @return Whether the button was pressed since the last check.
547   */
548  public boolean getLeftStickButtonPressed() {
549    return getRawButtonPressed(Button.kLeftStick.value);
550  }
551
552  /**
553   * Whether the left stick button was released since the last check.
554   *
555   * @return Whether the button was released since the last check.
556   */
557  public boolean getLeftStickButtonReleased() {
558    return getRawButtonReleased(Button.kLeftStick.value);
559  }
560
561  /**
562   * Constructs an event instance around the left stick button's digital signal.
563   *
564   * @param loop the event loop instance to attach the event to.
565   * @return an event instance representing the left stick button's digital signal
566   *     attached to the given loop.
567   */
568  public BooleanEvent leftStick(EventLoop loop) {
569    return button(Button.kLeftStick.value, loop);
570  }
571
572  /**
573   * Read the value of the right stick button on the controller.
574   *
575   * @return The state of the button.
576   */
577  public boolean getRightStickButton() {
578    return getRawButton(Button.kRightStick.value);
579  }
580
581  /**
582   * Whether the right stick button was pressed since the last check.
583   *
584   * @return Whether the button was pressed since the last check.
585   */
586  public boolean getRightStickButtonPressed() {
587    return getRawButtonPressed(Button.kRightStick.value);
588  }
589
590  /**
591   * Whether the right stick button was released since the last check.
592   *
593   * @return Whether the button was released since the last check.
594   */
595  public boolean getRightStickButtonReleased() {
596    return getRawButtonReleased(Button.kRightStick.value);
597  }
598
599  /**
600   * Constructs an event instance around the right stick button's digital signal.
601   *
602   * @param loop the event loop instance to attach the event to.
603   * @return an event instance representing the right stick button's digital signal
604   *     attached to the given loop.
605   */
606  public BooleanEvent rightStick(EventLoop loop) {
607    return button(Button.kRightStick.value, loop);
608  }
609
610  /**
611   * Read the value of the left bumper (LB) button on the controller.
612   *
613   * @return The state of the button.
614   * @deprecated Use {@link getLeftBumperButton} instead. This function is deprecated for removal
615   *     to make function names consistent to allow the HID classes to be automatically generated.
616   */
617  @Deprecated(since = "2025", forRemoval = true)
618  public boolean getLeftBumper() {
619    return getRawButton(Button.kLeftBumper.value);
620  }
621
622  /**
623   * Read the value of the right bumper (RB) button on the controller.
624   *
625   * @return The state of the button.
626   * @deprecated Use {@link getRightBumperButton} instead. This function is deprecated for removal
627   *     to make function names consistent to allow the HID classes to be automatically generated.
628   */
629  @Deprecated(since = "2025", forRemoval = true)
630  public boolean getRightBumper() {
631    return getRawButton(Button.kRightBumper.value);
632  }
633
634  /**
635   * Whether the left bumper (LB) was pressed since the last check.
636   *
637   * @return Whether the button was pressed since the last check.
638   * @deprecated Use {@link getLeftBumperButtonPressed} instead. This function is deprecated for
639   *     removal to make function names consistent to allow the HID classes to be automatically
640   *     generated.
641   */
642  @Deprecated(since = "2025", forRemoval = true)
643  public boolean getLeftBumperPressed() {
644    return getRawButtonPressed(Button.kLeftBumper.value);
645  }
646
647  /**
648   * Whether the right bumper (RB) was pressed since the last check.
649   *
650   * @return Whether the button was pressed since the last check.
651   * @deprecated Use {@link getRightBumperButtonPressed} instead. This function is deprecated for
652   *     removal to make function names consistent to allow the HID classes to be automatically
653   *     generated.
654   */
655  @Deprecated(since = "2025", forRemoval = true)
656  public boolean getRightBumperPressed() {
657    return getRawButtonPressed(Button.kRightBumper.value);
658  }
659
660  /**
661   * Whether the left bumper (LB) was released since the last check.
662   *
663   * @return Whether the button was released since the last check.
664   * @deprecated Use {@link getLeftBumperButtonReleased} instead. This function is deprecated for
665   *     removal to make function names consistent to allow the HID classes to be automatically
666   *     generated.
667   */
668  @Deprecated(since = "2025", forRemoval = true)
669  public boolean getLeftBumperReleased() {
670    return getRawButtonReleased(Button.kLeftBumper.value);
671  }
672
673  /**
674   * Whether the right bumper (RB) was released since the last check.
675   *
676   * @return Whether the button was released since the last check.
677   * @deprecated Use {@link getRightBumperButtonReleased} instead. This function is deprecated for
678   *     removal to make function names consistent to allow the HID classes to be automatically
679   *     generated.
680   */
681  @Deprecated(since = "2025", forRemoval = true)
682  public boolean getRightBumperReleased() {
683    return getRawButtonReleased(Button.kRightBumper.value);
684  }
685
686  @Override
687  public void initSendable(SendableBuilder builder) {
688    builder.setSmartDashboardType("HID");
689    builder.publishConstString("ControllerType", "Xbox");
690    builder.addDoubleProperty("LeftTrigger Axis", this::getLeftTriggerAxis, null);
691    builder.addDoubleProperty("RightTrigger Axis", this::getRightTriggerAxis, null);
692    builder.addDoubleProperty("LeftX", this::getLeftX, null);
693    builder.addDoubleProperty("RightX", this::getRightX, null);
694    builder.addDoubleProperty("LeftY", this::getLeftY, null);
695    builder.addDoubleProperty("RightY", this::getRightY, null);
696    builder.addBooleanProperty("A", this::getAButton, null);
697    builder.addBooleanProperty("B", this::getBButton, null);
698    builder.addBooleanProperty("X", this::getXButton, null);
699    builder.addBooleanProperty("Y", this::getYButton, null);
700    builder.addBooleanProperty("LeftBumper", this::getLeftBumperButton, null);
701    builder.addBooleanProperty("RightBumper", this::getRightBumperButton, null);
702    builder.addBooleanProperty("Back", this::getBackButton, null);
703    builder.addBooleanProperty("Start", this::getStartButton, null);
704    builder.addBooleanProperty("LeftStick", this::getLeftStickButton, null);
705    builder.addBooleanProperty("RightStick", this::getRightStickButton, null);
706  }
707}