001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj2.command.button;
006
007import edu.wpi.first.wpilibj.PS4Controller;
008import edu.wpi.first.wpilibj.event.EventLoop;
009import edu.wpi.first.wpilibj2.command.CommandScheduler;
010
011/**
012 * A version of {@link PS4Controller} with {@link Trigger} factories for command-based.
013 *
014 * @see PS4Controller
015 */
016@SuppressWarnings("MethodName")
017public class CommandPS4Controller extends CommandGenericHID {
018  private final PS4Controller m_hid;
019
020  /**
021   * Construct an instance of a device.
022   *
023   * @param port The port index on the Driver Station that the device is plugged into.
024   */
025  public CommandPS4Controller(int port) {
026    super(port);
027    m_hid = new PS4Controller(port);
028  }
029
030  /**
031   * Get the underlying GenericHID object.
032   *
033   * @return the wrapped GenericHID object
034   */
035  @Override
036  public PS4Controller getHID() {
037    return m_hid;
038  }
039
040  /**
041   * Constructs an event instance around the L2 button's digital signal.
042   *
043   * @return an event instance representing the L2 button's digital signal attached to the {@link
044   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
045   */
046  public Trigger L2() {
047    return L2(CommandScheduler.getInstance().getDefaultButtonLoop());
048  }
049
050  /**
051   * Constructs an event instance around the L2 button's digital signal.
052   *
053   * @param loop the event loop instance to attach the event to.
054   * @return an event instance representing the L2 button's digital signal attached to the given
055   *     loop.
056   */
057  public Trigger L2(EventLoop loop) {
058    return m_hid.L2(loop).castTo(Trigger::new);
059  }
060
061  /**
062   * Constructs an event instance around the R2 button's digital signal.
063   *
064   * @return an event instance representing the R2 button's digital signal attached to the {@link
065   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
066   */
067  public Trigger R2() {
068    return R2(CommandScheduler.getInstance().getDefaultButtonLoop());
069  }
070
071  /**
072   * Constructs an event instance around the R2 button's digital signal.
073   *
074   * @param loop the event loop instance to attach the event to.
075   * @return an event instance representing the R2 button's digital signal attached to the given
076   *     loop.
077   */
078  public Trigger R2(EventLoop loop) {
079    return m_hid.R2(loop).castTo(Trigger::new);
080  }
081
082  /**
083   * Constructs an event instance around the L1 button's digital signal.
084   *
085   * @return an event instance representing the L1 button's digital signal attached to the {@link
086   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
087   */
088  public Trigger L1() {
089    return L1(CommandScheduler.getInstance().getDefaultButtonLoop());
090  }
091
092  /**
093   * Constructs an event instance around the L1 button's digital signal.
094   *
095   * @param loop the event loop instance to attach the event to.
096   * @return an event instance representing the L1 button's digital signal attached to the given
097   *     loop.
098   */
099  public Trigger L1(EventLoop loop) {
100    return m_hid.L1(loop).castTo(Trigger::new);
101  }
102
103  /**
104   * Constructs an event instance around the R1 button's digital signal.
105   *
106   * @return an event instance representing the R1 button's digital signal attached to the {@link
107   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
108   */
109  public Trigger R1() {
110    return R1(CommandScheduler.getInstance().getDefaultButtonLoop());
111  }
112
113  /**
114   * Constructs an event instance around the R1 button's digital signal.
115   *
116   * @param loop the event loop instance to attach the event to.
117   * @return an event instance representing the R1 button's digital signal attached to the given
118   *     loop.
119   */
120  public Trigger R1(EventLoop loop) {
121    return m_hid.R1(loop).castTo(Trigger::new);
122  }
123
124  /**
125   * Constructs an event instance around the L3 button's digital signal.
126   *
127   * @return an event instance representing the L3 button's digital signal attached to the {@link
128   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
129   */
130  public Trigger L3() {
131    return L3(CommandScheduler.getInstance().getDefaultButtonLoop());
132  }
133
134  /**
135   * Constructs an event instance around the L3 button's digital signal.
136   *
137   * @param loop the event loop instance to attach the event to.
138   * @return an event instance representing the L3 button's digital signal attached to the given
139   *     loop.
140   */
141  public Trigger L3(EventLoop loop) {
142    return m_hid.L3(loop).castTo(Trigger::new);
143  }
144
145  /**
146   * Constructs an event instance around the R3 button's digital signal.
147   *
148   * @return an event instance representing the R3 button's digital signal attached to the {@link
149   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
150   */
151  public Trigger R3() {
152    return R3(CommandScheduler.getInstance().getDefaultButtonLoop());
153  }
154
155  /**
156   * Constructs an event instance around the R3 button's digital signal.
157   *
158   * @param loop the event loop instance to attach the event to.
159   * @return an event instance representing the R3 button's digital signal attached to the given
160   *     loop.
161   */
162  public Trigger R3(EventLoop loop) {
163    return m_hid.R3(loop).castTo(Trigger::new);
164  }
165
166  /**
167   * Constructs an event instance around the square button's digital signal.
168   *
169   * @return an event instance representing the square button's digital signal attached to the
170   *     {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
171   */
172  public Trigger square() {
173    return square(CommandScheduler.getInstance().getDefaultButtonLoop());
174  }
175
176  /**
177   * Constructs an event instance around the square button's digital signal.
178   *
179   * @param loop the event loop instance to attach the event to.
180   * @return an event instance representing the square button's digital signal attached to the given
181   *     loop.
182   */
183  public Trigger square(EventLoop loop) {
184    return m_hid.square(loop).castTo(Trigger::new);
185  }
186
187  /**
188   * Constructs an event instance around the cross button's digital signal.
189   *
190   * @return an event instance representing the cross button's digital signal attached to the {@link
191   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
192   */
193  public Trigger cross() {
194    return cross(CommandScheduler.getInstance().getDefaultButtonLoop());
195  }
196
197  /**
198   * Constructs an event instance around the cross button's digital signal.
199   *
200   * @param loop the event loop instance to attach the event to.
201   * @return an event instance representing the cross button's digital signal attached to the given
202   *     loop.
203   */
204  public Trigger cross(EventLoop loop) {
205    return m_hid.cross(loop).castTo(Trigger::new);
206  }
207
208  /**
209   * Constructs an event instance around the triangle button's digital signal.
210   *
211   * @return an event instance representing the triangle button's digital signal attached to the
212   *     {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
213   */
214  public Trigger triangle() {
215    return triangle(CommandScheduler.getInstance().getDefaultButtonLoop());
216  }
217
218  /**
219   * Constructs an event instance around the triangle button's digital signal.
220   *
221   * @param loop the event loop instance to attach the event to.
222   * @return an event instance representing the triangle button's digital signal attached to the
223   *     given loop.
224   */
225  public Trigger triangle(EventLoop loop) {
226    return m_hid.triangle(loop).castTo(Trigger::new);
227  }
228
229  /**
230   * Constructs an event instance around the circle button's digital signal.
231   *
232   * @return an event instance representing the circle button's digital signal attached to the
233   *     {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
234   */
235  public Trigger circle() {
236    return circle(CommandScheduler.getInstance().getDefaultButtonLoop());
237  }
238
239  /**
240   * Constructs an event instance around the circle button's digital signal.
241   *
242   * @param loop the event loop instance to attach the event to.
243   * @return an event instance representing the circle button's digital signal attached to the given
244   *     loop.
245   */
246  public Trigger circle(EventLoop loop) {
247    return m_hid.circle(loop).castTo(Trigger::new);
248  }
249
250  /**
251   * Constructs an event instance around the share button's digital signal.
252   *
253   * @return an event instance representing the share button's digital signal attached to the {@link
254   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
255   */
256  public Trigger share() {
257    return share(CommandScheduler.getInstance().getDefaultButtonLoop());
258  }
259
260  /**
261   * Constructs an event instance around the share button's digital signal.
262   *
263   * @param loop the event loop instance to attach the event to.
264   * @return an event instance representing the share button's digital signal attached to the given
265   *     loop.
266   */
267  public Trigger share(EventLoop loop) {
268    return m_hid.share(loop).castTo(Trigger::new);
269  }
270
271  /**
272   * Constructs an event instance around the PS button's digital signal.
273   *
274   * @return an event instance representing the PS button's digital signal attached to the {@link
275   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
276   */
277  public Trigger PS() {
278    return PS(CommandScheduler.getInstance().getDefaultButtonLoop());
279  }
280
281  /**
282   * Constructs an event instance around the PS button's digital signal.
283   *
284   * @param loop the event loop instance to attach the event to.
285   * @return an event instance representing the PS button's digital signal attached to the given
286   *     loop.
287   */
288  public Trigger PS(EventLoop loop) {
289    return m_hid.PS(loop).castTo(Trigger::new);
290  }
291
292  /**
293   * Constructs an event instance around the options button's digital signal.
294   *
295   * @return an event instance representing the options button's digital signal attached to the
296   *     {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
297   */
298  public Trigger options() {
299    return options(CommandScheduler.getInstance().getDefaultButtonLoop());
300  }
301
302  /**
303   * Constructs an event instance around the options button's digital signal.
304   *
305   * @param loop the event loop instance to attach the event to.
306   * @return an event instance representing the options button's digital signal attached to the
307   *     given loop.
308   */
309  public Trigger options(EventLoop loop) {
310    return m_hid.options(loop).castTo(Trigger::new);
311  }
312
313  /**
314   * Constructs an event instance around the touchpad's digital signal.
315   *
316   * @return an event instance representing the touchpad's digital signal attached to the {@link
317   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
318   */
319  public Trigger touchpad() {
320    return touchpad(CommandScheduler.getInstance().getDefaultButtonLoop());
321  }
322
323  /**
324   * Constructs an event instance around the touchpad's digital signal.
325   *
326   * @param loop the event loop instance to attach the event to.
327   * @return an event instance representing the touchpad's digital signal attached to the given
328   *     loop.
329   */
330  public Trigger touchpad(EventLoop loop) {
331    return m_hid.touchpad(loop).castTo(Trigger::new);
332  }
333
334  /**
335   * Get the X axis value of left side of the controller.
336   *
337   * @return the axis value.
338   */
339  public double getLeftX() {
340    return m_hid.getLeftX();
341  }
342
343  /**
344   * Get the X axis value of right side of the controller.
345   *
346   * @return the axis value.
347   */
348  public double getRightX() {
349    return m_hid.getRightX();
350  }
351
352  /**
353   * Get the Y axis value of left side of the controller.
354   *
355   * @return the axis value.
356   */
357  public double getLeftY() {
358    return m_hid.getLeftY();
359  }
360
361  /**
362   * Get the Y axis value of right side of the controller.
363   *
364   * @return the axis value.
365   */
366  public double getRightY() {
367    return m_hid.getRightY();
368  }
369
370  /**
371   * Get the L2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as
372   * opposed to the usual [-1, 1].
373   *
374   * @return the axis value.
375   */
376  public double getL2Axis() {
377    return m_hid.getL2Axis();
378  }
379
380  /**
381   * Get the R2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as
382   * opposed to the usual [-1, 1].
383   *
384   * @return the axis value.
385   */
386  public double getR2Axis() {
387    return m_hid.getR2Axis();
388  }
389}