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