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 PS4 controllers connected to the Driver Station.
017 *
018 * <p>This class handles PS4 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 Sony 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 PS4Controller extends GenericHID implements Sendable {
027  /** Represents a digital button on a PS4Controller. */
028  public enum Button {
029    /** Square button. */
030    kSquare(1),
031    /** Cross button. */
032    kCross(2),
033    /** Circle button. */
034    kCircle(3),
035    /** Triangle button. */
036    kTriangle(4),
037    /** Left trigger 1 button. */
038    kL1(5),
039    /** Right trigger 1 button. */
040    kR1(6),
041    /** Left trigger 2 button. */
042    kL2(7),
043    /** Right trigger 2 button. */
044    kR2(8),
045    /** Share button. */
046    kShare(9),
047    /** Options button. */
048    kOptions(10),
049    /** L3 (left stick) button. */
050    kL3(11),
051    /** R3 (right stick) button. */
052    kR3(12),
053    /** PlayStation button. */
054    kPS(13),
055    /** Touchpad button. */
056    kTouchpad(14);
057
058    /** Button value. */
059    public final int value;
060
061    Button(int value) {
062      this.value = value;
063    }
064
065    /**
066     * Get the human-friendly name of the button, matching the relevant methods. This is done by
067     * stripping the leading `k`, and appending `Button`.
068     *
069     * <p>Primarily used for automated unit tests.
070     *
071     * @return the human-friendly name of the button.
072     */
073    @Override
074    public String toString() {
075      // Remove leading `k`
076      return this.name().substring(1) + "Button";
077    }
078  }
079
080  /** Represents an axis on an PS4Controller. */
081  public enum Axis {
082    /** Left X axis. */
083    kLeftX(0),
084    /** Left Y axis. */
085    kLeftY(1),
086    /** Right X axis. */
087    kRightX(2),
088    /** Right Y axis. */
089    kRightY(5),
090    /** Left trigger 2. */
091    kL2(3),
092    /** Right trigger 2. */
093    kR2(4);
094
095    /** Axis value. */
096    public final int value;
097
098    Axis(int value) {
099      this.value = value;
100    }
101
102    /**
103     * Get the human-friendly name of the axis, matching the relevant methods. This is done by
104     * stripping the leading `k`, and appending `Axis` if the name ends with `2`.
105     *
106     * <p>Primarily used for automated unit tests.
107     *
108     * @return the human-friendly name of the axis.
109     */
110    @Override
111    public String toString() {
112      var name = this.name().substring(1); // Remove leading `k`
113      if (name.endsWith("2")) {
114        return name + "Axis";
115      }
116      return name;
117    }
118  }
119
120  /**
121   * Construct an instance of a controller.
122   *
123   * @param port The port index on the Driver Station that the controller is plugged into (0-5).
124   */
125  public PS4Controller(final int port) {
126    super(port);
127    HAL.reportUsage("HID", port, "PS4Controller");
128  }
129
130  /**
131   * Get the X axis value of left side of the controller. Right is positive.
132   *
133   * @return The axis value.
134   */
135  public double getLeftX() {
136    return getRawAxis(Axis.kLeftX.value);
137  }
138
139  /**
140   * Get the Y axis value of left side of the controller. Back is positive.
141   *
142   * @return The axis value.
143   */
144  public double getLeftY() {
145    return getRawAxis(Axis.kLeftY.value);
146  }
147
148  /**
149   * Get the X axis value of right side of the controller. Right is positive.
150   *
151   * @return The axis value.
152   */
153  public double getRightX() {
154    return getRawAxis(Axis.kRightX.value);
155  }
156
157  /**
158   * Get the Y axis value of right side of the controller. Back is positive.
159   *
160   * @return The axis value.
161   */
162  public double getRightY() {
163    return getRawAxis(Axis.kRightY.value);
164  }
165
166  /**
167   * Get the left trigger 2 axis value of the controller. Note that this axis is bound to the
168   * range of [0, 1] as opposed to the usual [-1, 1].
169   *
170   * @return The axis value.
171   */
172  public double getL2Axis() {
173    return getRawAxis(Axis.kL2.value);
174  }
175
176  /**
177   * Get the right trigger 2 axis value of the controller. Note that this axis is bound to the
178   * range of [0, 1] as opposed to the usual [-1, 1].
179   *
180   * @return The axis value.
181   */
182  public double getR2Axis() {
183    return getRawAxis(Axis.kR2.value);
184  }
185
186  /**
187   * Read the value of the square button on the controller.
188   *
189   * @return The state of the button.
190   */
191  public boolean getSquareButton() {
192    return getRawButton(Button.kSquare.value);
193  }
194
195  /**
196   * Whether the square button was pressed since the last check.
197   *
198   * @return Whether the button was pressed since the last check.
199   */
200  public boolean getSquareButtonPressed() {
201    return getRawButtonPressed(Button.kSquare.value);
202  }
203
204  /**
205   * Whether the square button was released since the last check.
206   *
207   * @return Whether the button was released since the last check.
208   */
209  public boolean getSquareButtonReleased() {
210    return getRawButtonReleased(Button.kSquare.value);
211  }
212
213  /**
214   * Constructs an event instance around the square button's digital signal.
215   *
216   * @param loop the event loop instance to attach the event to.
217   * @return an event instance representing the square button's digital signal
218   *     attached to the given loop.
219   */
220  public BooleanEvent square(EventLoop loop) {
221    return button(Button.kSquare.value, loop);
222  }
223
224  /**
225   * Read the value of the cross button on the controller.
226   *
227   * @return The state of the button.
228   */
229  public boolean getCrossButton() {
230    return getRawButton(Button.kCross.value);
231  }
232
233  /**
234   * Whether the cross button was pressed since the last check.
235   *
236   * @return Whether the button was pressed since the last check.
237   */
238  public boolean getCrossButtonPressed() {
239    return getRawButtonPressed(Button.kCross.value);
240  }
241
242  /**
243   * Whether the cross button was released since the last check.
244   *
245   * @return Whether the button was released since the last check.
246   */
247  public boolean getCrossButtonReleased() {
248    return getRawButtonReleased(Button.kCross.value);
249  }
250
251  /**
252   * Constructs an event instance around the cross button's digital signal.
253   *
254   * @param loop the event loop instance to attach the event to.
255   * @return an event instance representing the cross button's digital signal
256   *     attached to the given loop.
257   */
258  public BooleanEvent cross(EventLoop loop) {
259    return button(Button.kCross.value, loop);
260  }
261
262  /**
263   * Read the value of the circle button on the controller.
264   *
265   * @return The state of the button.
266   */
267  public boolean getCircleButton() {
268    return getRawButton(Button.kCircle.value);
269  }
270
271  /**
272   * Whether the circle button was pressed since the last check.
273   *
274   * @return Whether the button was pressed since the last check.
275   */
276  public boolean getCircleButtonPressed() {
277    return getRawButtonPressed(Button.kCircle.value);
278  }
279
280  /**
281   * Whether the circle button was released since the last check.
282   *
283   * @return Whether the button was released since the last check.
284   */
285  public boolean getCircleButtonReleased() {
286    return getRawButtonReleased(Button.kCircle.value);
287  }
288
289  /**
290   * Constructs an event instance around the circle button's digital signal.
291   *
292   * @param loop the event loop instance to attach the event to.
293   * @return an event instance representing the circle button's digital signal
294   *     attached to the given loop.
295   */
296  public BooleanEvent circle(EventLoop loop) {
297    return button(Button.kCircle.value, loop);
298  }
299
300  /**
301   * Read the value of the triangle button on the controller.
302   *
303   * @return The state of the button.
304   */
305  public boolean getTriangleButton() {
306    return getRawButton(Button.kTriangle.value);
307  }
308
309  /**
310   * Whether the triangle button was pressed since the last check.
311   *
312   * @return Whether the button was pressed since the last check.
313   */
314  public boolean getTriangleButtonPressed() {
315    return getRawButtonPressed(Button.kTriangle.value);
316  }
317
318  /**
319   * Whether the triangle button was released since the last check.
320   *
321   * @return Whether the button was released since the last check.
322   */
323  public boolean getTriangleButtonReleased() {
324    return getRawButtonReleased(Button.kTriangle.value);
325  }
326
327  /**
328   * Constructs an event instance around the triangle button's digital signal.
329   *
330   * @param loop the event loop instance to attach the event to.
331   * @return an event instance representing the triangle button's digital signal
332   *     attached to the given loop.
333   */
334  public BooleanEvent triangle(EventLoop loop) {
335    return button(Button.kTriangle.value, loop);
336  }
337
338  /**
339   * Read the value of the left trigger 1 button on the controller.
340   *
341   * @return The state of the button.
342   */
343  public boolean getL1Button() {
344    return getRawButton(Button.kL1.value);
345  }
346
347  /**
348   * Whether the left trigger 1 button was pressed since the last check.
349   *
350   * @return Whether the button was pressed since the last check.
351   */
352  public boolean getL1ButtonPressed() {
353    return getRawButtonPressed(Button.kL1.value);
354  }
355
356  /**
357   * Whether the left trigger 1 button was released since the last check.
358   *
359   * @return Whether the button was released since the last check.
360   */
361  public boolean getL1ButtonReleased() {
362    return getRawButtonReleased(Button.kL1.value);
363  }
364
365  /**
366   * Constructs an event instance around the left trigger 1 button's digital signal.
367   *
368   * @param loop the event loop instance to attach the event to.
369   * @return an event instance representing the left trigger 1 button's digital signal
370   *     attached to the given loop.
371   */
372  public BooleanEvent L1(EventLoop loop) {
373    return button(Button.kL1.value, loop);
374  }
375
376  /**
377   * Read the value of the right trigger 1 button on the controller.
378   *
379   * @return The state of the button.
380   */
381  public boolean getR1Button() {
382    return getRawButton(Button.kR1.value);
383  }
384
385  /**
386   * Whether the right trigger 1 button was pressed since the last check.
387   *
388   * @return Whether the button was pressed since the last check.
389   */
390  public boolean getR1ButtonPressed() {
391    return getRawButtonPressed(Button.kR1.value);
392  }
393
394  /**
395   * Whether the right trigger 1 button was released since the last check.
396   *
397   * @return Whether the button was released since the last check.
398   */
399  public boolean getR1ButtonReleased() {
400    return getRawButtonReleased(Button.kR1.value);
401  }
402
403  /**
404   * Constructs an event instance around the right trigger 1 button's digital signal.
405   *
406   * @param loop the event loop instance to attach the event to.
407   * @return an event instance representing the right trigger 1 button's digital signal
408   *     attached to the given loop.
409   */
410  public BooleanEvent R1(EventLoop loop) {
411    return button(Button.kR1.value, loop);
412  }
413
414  /**
415   * Read the value of the left trigger 2 button on the controller.
416   *
417   * @return The state of the button.
418   */
419  public boolean getL2Button() {
420    return getRawButton(Button.kL2.value);
421  }
422
423  /**
424   * Whether the left trigger 2 button was pressed since the last check.
425   *
426   * @return Whether the button was pressed since the last check.
427   */
428  public boolean getL2ButtonPressed() {
429    return getRawButtonPressed(Button.kL2.value);
430  }
431
432  /**
433   * Whether the left trigger 2 button was released since the last check.
434   *
435   * @return Whether the button was released since the last check.
436   */
437  public boolean getL2ButtonReleased() {
438    return getRawButtonReleased(Button.kL2.value);
439  }
440
441  /**
442   * Constructs an event instance around the left trigger 2 button's digital signal.
443   *
444   * @param loop the event loop instance to attach the event to.
445   * @return an event instance representing the left trigger 2 button's digital signal
446   *     attached to the given loop.
447   */
448  public BooleanEvent L2(EventLoop loop) {
449    return button(Button.kL2.value, loop);
450  }
451
452  /**
453   * Read the value of the right trigger 2 button on the controller.
454   *
455   * @return The state of the button.
456   */
457  public boolean getR2Button() {
458    return getRawButton(Button.kR2.value);
459  }
460
461  /**
462   * Whether the right trigger 2 button was pressed since the last check.
463   *
464   * @return Whether the button was pressed since the last check.
465   */
466  public boolean getR2ButtonPressed() {
467    return getRawButtonPressed(Button.kR2.value);
468  }
469
470  /**
471   * Whether the right trigger 2 button was released since the last check.
472   *
473   * @return Whether the button was released since the last check.
474   */
475  public boolean getR2ButtonReleased() {
476    return getRawButtonReleased(Button.kR2.value);
477  }
478
479  /**
480   * Constructs an event instance around the right trigger 2 button's digital signal.
481   *
482   * @param loop the event loop instance to attach the event to.
483   * @return an event instance representing the right trigger 2 button's digital signal
484   *     attached to the given loop.
485   */
486  public BooleanEvent R2(EventLoop loop) {
487    return button(Button.kR2.value, loop);
488  }
489
490  /**
491   * Read the value of the share button on the controller.
492   *
493   * @return The state of the button.
494   */
495  public boolean getShareButton() {
496    return getRawButton(Button.kShare.value);
497  }
498
499  /**
500   * Whether the share button was pressed since the last check.
501   *
502   * @return Whether the button was pressed since the last check.
503   */
504  public boolean getShareButtonPressed() {
505    return getRawButtonPressed(Button.kShare.value);
506  }
507
508  /**
509   * Whether the share button was released since the last check.
510   *
511   * @return Whether the button was released since the last check.
512   */
513  public boolean getShareButtonReleased() {
514    return getRawButtonReleased(Button.kShare.value);
515  }
516
517  /**
518   * Constructs an event instance around the share button's digital signal.
519   *
520   * @param loop the event loop instance to attach the event to.
521   * @return an event instance representing the share button's digital signal
522   *     attached to the given loop.
523   */
524  public BooleanEvent share(EventLoop loop) {
525    return button(Button.kShare.value, loop);
526  }
527
528  /**
529   * Read the value of the options button on the controller.
530   *
531   * @return The state of the button.
532   */
533  public boolean getOptionsButton() {
534    return getRawButton(Button.kOptions.value);
535  }
536
537  /**
538   * Whether the options button was pressed since the last check.
539   *
540   * @return Whether the button was pressed since the last check.
541   */
542  public boolean getOptionsButtonPressed() {
543    return getRawButtonPressed(Button.kOptions.value);
544  }
545
546  /**
547   * Whether the options button was released since the last check.
548   *
549   * @return Whether the button was released since the last check.
550   */
551  public boolean getOptionsButtonReleased() {
552    return getRawButtonReleased(Button.kOptions.value);
553  }
554
555  /**
556   * Constructs an event instance around the options button's digital signal.
557   *
558   * @param loop the event loop instance to attach the event to.
559   * @return an event instance representing the options button's digital signal
560   *     attached to the given loop.
561   */
562  public BooleanEvent options(EventLoop loop) {
563    return button(Button.kOptions.value, loop);
564  }
565
566  /**
567   * Read the value of the L3 (left stick) button on the controller.
568   *
569   * @return The state of the button.
570   */
571  public boolean getL3Button() {
572    return getRawButton(Button.kL3.value);
573  }
574
575  /**
576   * Whether the L3 (left stick) button was pressed since the last check.
577   *
578   * @return Whether the button was pressed since the last check.
579   */
580  public boolean getL3ButtonPressed() {
581    return getRawButtonPressed(Button.kL3.value);
582  }
583
584  /**
585   * Whether the L3 (left stick) button was released since the last check.
586   *
587   * @return Whether the button was released since the last check.
588   */
589  public boolean getL3ButtonReleased() {
590    return getRawButtonReleased(Button.kL3.value);
591  }
592
593  /**
594   * Constructs an event instance around the L3 (left stick) button's digital signal.
595   *
596   * @param loop the event loop instance to attach the event to.
597   * @return an event instance representing the L3 (left stick) button's digital signal
598   *     attached to the given loop.
599   */
600  public BooleanEvent L3(EventLoop loop) {
601    return button(Button.kL3.value, loop);
602  }
603
604  /**
605   * Read the value of the R3 (right stick) button on the controller.
606   *
607   * @return The state of the button.
608   */
609  public boolean getR3Button() {
610    return getRawButton(Button.kR3.value);
611  }
612
613  /**
614   * Whether the R3 (right stick) button was pressed since the last check.
615   *
616   * @return Whether the button was pressed since the last check.
617   */
618  public boolean getR3ButtonPressed() {
619    return getRawButtonPressed(Button.kR3.value);
620  }
621
622  /**
623   * Whether the R3 (right stick) button was released since the last check.
624   *
625   * @return Whether the button was released since the last check.
626   */
627  public boolean getR3ButtonReleased() {
628    return getRawButtonReleased(Button.kR3.value);
629  }
630
631  /**
632   * Constructs an event instance around the R3 (right stick) button's digital signal.
633   *
634   * @param loop the event loop instance to attach the event to.
635   * @return an event instance representing the R3 (right stick) button's digital signal
636   *     attached to the given loop.
637   */
638  public BooleanEvent R3(EventLoop loop) {
639    return button(Button.kR3.value, loop);
640  }
641
642  /**
643   * Read the value of the PlayStation button on the controller.
644   *
645   * @return The state of the button.
646   */
647  public boolean getPSButton() {
648    return getRawButton(Button.kPS.value);
649  }
650
651  /**
652   * Whether the PlayStation button was pressed since the last check.
653   *
654   * @return Whether the button was pressed since the last check.
655   */
656  public boolean getPSButtonPressed() {
657    return getRawButtonPressed(Button.kPS.value);
658  }
659
660  /**
661   * Whether the PlayStation button was released since the last check.
662   *
663   * @return Whether the button was released since the last check.
664   */
665  public boolean getPSButtonReleased() {
666    return getRawButtonReleased(Button.kPS.value);
667  }
668
669  /**
670   * Constructs an event instance around the PlayStation button's digital signal.
671   *
672   * @param loop the event loop instance to attach the event to.
673   * @return an event instance representing the PlayStation button's digital signal
674   *     attached to the given loop.
675   */
676  public BooleanEvent PS(EventLoop loop) {
677    return button(Button.kPS.value, loop);
678  }
679
680  /**
681   * Read the value of the touchpad button on the controller.
682   *
683   * @return The state of the button.
684   */
685  public boolean getTouchpadButton() {
686    return getRawButton(Button.kTouchpad.value);
687  }
688
689  /**
690   * Whether the touchpad button was pressed since the last check.
691   *
692   * @return Whether the button was pressed since the last check.
693   */
694  public boolean getTouchpadButtonPressed() {
695    return getRawButtonPressed(Button.kTouchpad.value);
696  }
697
698  /**
699   * Whether the touchpad button was released since the last check.
700   *
701   * @return Whether the button was released since the last check.
702   */
703  public boolean getTouchpadButtonReleased() {
704    return getRawButtonReleased(Button.kTouchpad.value);
705  }
706
707  /**
708   * Constructs an event instance around the touchpad button's digital signal.
709   *
710   * @param loop the event loop instance to attach the event to.
711   * @return an event instance representing the touchpad button's digital signal
712   *     attached to the given loop.
713   */
714  public BooleanEvent touchpad(EventLoop loop) {
715    return button(Button.kTouchpad.value, loop);
716  }
717
718  /**
719   * Read the value of the touchpad on the controller.
720   *
721   * @return The state of the touchpad.
722   * @deprecated Use {@link getTouchpadButton} instead. This function is deprecated for removal to
723   *     make function names consistent to allow the HID classes to be automatically generated.
724   */
725  @Deprecated(since = "2025", forRemoval = true)
726  public boolean getTouchpad() {
727    return getRawButton(Button.kTouchpad.value);
728  }
729
730  /**
731   * Whether the touchpad was pressed since the last check.
732   *
733   * @return Whether the touchpad was pressed since the last check.
734   * @deprecated Use {@link getTouchpadButtonPressed} instead. This function is deprecated for
735   *     removal to make function names consistent to allow the HID classes to be automatically
736   *     generated.
737   */
738  @Deprecated(since = "2025", forRemoval = true)
739  public boolean getTouchpadPressed() {
740    return getRawButtonPressed(Button.kTouchpad.value);
741  }
742
743  /**
744   * Whether the touchpad was released since the last check.
745   *
746   * @return Whether the touchpad was released since the last check.
747   * @deprecated Use {@link getTouchpadButtonReleased} instead. This function is deprecated for
748   *     removal to make function names consistent to allow the HID classes to be automatically
749   *     generated.
750   */
751  @Deprecated(since = "2025", forRemoval = true)
752  public boolean getTouchpadReleased() {
753    return getRawButtonReleased(Button.kTouchpad.value);
754  }
755
756  @Override
757  public void initSendable(SendableBuilder builder) {
758    builder.setSmartDashboardType("HID");
759    builder.publishConstString("ControllerType", "PS4");
760    builder.addDoubleProperty("L2 Axis", this::getL2Axis, null);
761    builder.addDoubleProperty("R2 Axis", this::getR2Axis, null);
762    builder.addDoubleProperty("LeftX", this::getLeftX, null);
763    builder.addDoubleProperty("LeftY", this::getLeftY, null);
764    builder.addDoubleProperty("RightX", this::getRightX, null);
765    builder.addDoubleProperty("RightY", this::getRightY, null);
766    builder.addBooleanProperty("Square", this::getSquareButton, null);
767    builder.addBooleanProperty("Cross", this::getCrossButton, null);
768    builder.addBooleanProperty("Circle", this::getCircleButton, null);
769    builder.addBooleanProperty("Triangle", this::getTriangleButton, null);
770    builder.addBooleanProperty("L1", this::getL1Button, null);
771    builder.addBooleanProperty("R1", this::getR1Button, null);
772    builder.addBooleanProperty("L2", this::getL2Button, null);
773    builder.addBooleanProperty("R2", this::getR2Button, null);
774    builder.addBooleanProperty("Share", this::getShareButton, null);
775    builder.addBooleanProperty("Options", this::getOptionsButton, null);
776    builder.addBooleanProperty("L3", this::getL3Button, null);
777    builder.addBooleanProperty("R3", this::getR3Button, null);
778    builder.addBooleanProperty("PS", this::getPSButton, null);
779    builder.addBooleanProperty("Touchpad", this::getTouchpadButton, null);
780  }
781}