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