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.Joystick;
008import edu.wpi.first.wpilibj.event.EventLoop;
009import edu.wpi.first.wpilibj2.command.CommandScheduler;
010
011/**
012 * A version of {@link Joystick} with {@link Trigger} factories for command-based.
013 *
014 * @see Joystick
015 */
016public class CommandJoystick extends CommandGenericHID {
017  private final Joystick m_hid;
018
019  /**
020   * Construct an instance of a controller.
021   *
022   * @param port The port index on the Driver Station that the controller is plugged into.
023   */
024  public CommandJoystick(int port) {
025    super(port);
026    m_hid = new Joystick(port);
027  }
028
029  /**
030   * Get the underlying GenericHID object.
031   *
032   * @return the wrapped GenericHID object
033   */
034  @Override
035  public Joystick getHID() {
036    return m_hid;
037  }
038
039  /**
040   * Constructs an event instance around the trigger button's digital signal.
041   *
042   * @return an event instance representing the trigger button's digital signal attached to the
043   *     {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
044   * @see #trigger(EventLoop)
045   */
046  public Trigger trigger() {
047    return trigger(CommandScheduler.getInstance().getDefaultButtonLoop());
048  }
049
050  /**
051   * Constructs an event instance around the trigger button's digital signal.
052   *
053   * @param loop the event loop instance to attach the event to.
054   * @return an event instance representing the trigger button's digital signal attached to the
055   *     given loop.
056   */
057  public Trigger trigger(EventLoop loop) {
058    return m_hid.trigger(loop).castTo(Trigger::new);
059  }
060
061  /**
062   * Constructs an event instance around the top button's digital signal.
063   *
064   * @return an event instance representing the top button's digital signal attached to the {@link
065   *     CommandScheduler#getDefaultButtonLoop() default scheduler button loop}.
066   * @see #top(EventLoop)
067   */
068  public Trigger top() {
069    return top(CommandScheduler.getInstance().getDefaultButtonLoop());
070  }
071
072  /**
073   * Constructs an event instance around the top button's digital signal.
074   *
075   * @param loop the event loop instance to attach the event to.
076   * @return an event instance representing the top button's digital signal attached to the given
077   *     loop.
078   */
079  public Trigger top(EventLoop loop) {
080    return m_hid.top(loop).castTo(Trigger::new);
081  }
082
083  /**
084   * Set the channel associated with the X axis.
085   *
086   * @param channel The channel to set the axis to.
087   */
088  public void setXChannel(int channel) {
089    m_hid.setXChannel(channel);
090  }
091
092  /**
093   * Set the channel associated with the Y axis.
094   *
095   * @param channel The channel to set the axis to.
096   */
097  public void setYChannel(int channel) {
098    m_hid.setYChannel(channel);
099  }
100
101  /**
102   * Set the channel associated with the Z axis.
103   *
104   * @param channel The channel to set the axis to.
105   */
106  public void setZChannel(int channel) {
107    m_hid.setZChannel(channel);
108  }
109
110  /**
111   * Set the channel associated with the throttle axis.
112   *
113   * @param channel The channel to set the axis to.
114   */
115  public void setThrottleChannel(int channel) {
116    m_hid.setThrottleChannel(channel);
117  }
118
119  /**
120   * Set the channel associated with the twist axis.
121   *
122   * @param channel The channel to set the axis to.
123   */
124  public void setTwistChannel(int channel) {
125    m_hid.setTwistChannel(channel);
126  }
127
128  /**
129   * Get the channel currently associated with the X axis.
130   *
131   * @return The channel for the axis.
132   */
133  public int getXChannel() {
134    return m_hid.getXChannel();
135  }
136
137  /**
138   * Get the channel currently associated with the Y axis.
139   *
140   * @return The channel for the axis.
141   */
142  public int getYChannel() {
143    return m_hid.getYChannel();
144  }
145
146  /**
147   * Get the channel currently associated with the Z axis.
148   *
149   * @return The channel for the axis.
150   */
151  public int getZChannel() {
152    return m_hid.getZChannel();
153  }
154
155  /**
156   * Get the channel currently associated with the twist axis.
157   *
158   * @return The channel for the axis.
159   */
160  public int getTwistChannel() {
161    return m_hid.getTwistChannel();
162  }
163
164  /**
165   * Get the channel currently associated with the throttle axis.
166   *
167   * @return The channel for the axis.
168   */
169  public int getThrottleChannel() {
170    return m_hid.getThrottleChannel();
171  }
172
173  /**
174   * Get the x position of the HID.
175   *
176   * @return the x position
177   */
178  public double getX() {
179    return m_hid.getX();
180  }
181
182  /**
183   * Get the y position of the HID.
184   *
185   * @return the y position
186   */
187  public double getY() {
188    return m_hid.getY();
189  }
190
191  /**
192   * Get the z position of the HID.
193   *
194   * @return the z position
195   */
196  public double getZ() {
197    return m_hid.getZ();
198  }
199
200  /**
201   * Get the twist value of the current joystick. This depends on the mapping of the joystick
202   * connected to the current port.
203   *
204   * @return The Twist value of the joystick.
205   */
206  public double getTwist() {
207    return m_hid.getTwist();
208  }
209
210  /**
211   * Get the throttle value of the current joystick. This depends on the mapping of the joystick
212   * connected to the current port.
213   *
214   * @return The Throttle value of the joystick.
215   */
216  public double getThrottle() {
217    return m_hid.getThrottle();
218  }
219
220  /**
221   * Get the magnitude of the direction vector formed by the joystick's current position relative to
222   * its origin.
223   *
224   * @return The magnitude of the direction vector
225   */
226  public double getMagnitude() {
227    return m_hid.getMagnitude();
228  }
229
230  /**
231   * Get the direction of the vector formed by the joystick and its origin in radians.
232   *
233   * @return The direction of the vector in radians
234   */
235  public double getDirectionRadians() {
236    return m_hid.getDirectionRadians();
237  }
238
239  /**
240   * Get the direction of the vector formed by the joystick and its origin in degrees.
241   *
242   * @return The direction of the vector in degrees
243   */
244  public double getDirectionDegrees() {
245    return m_hid.getDirectionDegrees();
246  }
247}