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