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.wpilibj.simulation;
006
007import edu.wpi.first.wpilibj.Joystick;
008
009/** Class to control a simulated joystick. */
010public class JoystickSim extends GenericHIDSim {
011  private Joystick m_joystick;
012
013  /**
014   * Constructs from a Joystick object.
015   *
016   * @param joystick joystick to simulate
017   */
018  @SuppressWarnings("this-escape")
019  public JoystickSim(Joystick joystick) {
020    super(joystick);
021    m_joystick = joystick;
022    // default to a reasonable joystick configuration
023    setAxisCount(5);
024    setButtonCount(12);
025    setPOVCount(1);
026  }
027
028  /**
029   * Constructs from a joystick port number.
030   *
031   * @param port port number
032   */
033  @SuppressWarnings("this-escape")
034  public JoystickSim(int port) {
035    super(port);
036    // default to a reasonable joystick configuration
037    setAxisCount(5);
038    setButtonCount(12);
039    setPOVCount(1);
040  }
041
042  /**
043   * Set the X value of the joystick.
044   *
045   * @param value the new X value
046   */
047  public void setX(double value) {
048    setRawAxis(m_joystick != null ? m_joystick.getXChannel() : Joystick.kDefaultXChannel, value);
049  }
050
051  /**
052   * Set the Y value of the joystick.
053   *
054   * @param value the new Y value
055   */
056  public void setY(double value) {
057    setRawAxis(m_joystick != null ? m_joystick.getYChannel() : Joystick.kDefaultYChannel, value);
058  }
059
060  /**
061   * Set the Z value of the joystick.
062   *
063   * @param value the new Z value
064   */
065  public void setZ(double value) {
066    setRawAxis(m_joystick != null ? m_joystick.getZChannel() : Joystick.kDefaultZChannel, value);
067  }
068
069  /**
070   * Set the twist value of the joystick.
071   *
072   * @param value the new twist value
073   */
074  public void setTwist(double value) {
075    setRawAxis(
076        m_joystick != null ? m_joystick.getTwistChannel() : Joystick.kDefaultTwistChannel, value);
077  }
078
079  /**
080   * Set the throttle value of the joystick.
081   *
082   * @param value the new throttle value
083   */
084  public void setThrottle(double value) {
085    setRawAxis(
086        m_joystick != null ? m_joystick.getThrottleChannel() : Joystick.kDefaultThrottleChannel,
087        value);
088  }
089
090  /**
091   * Set the trigger value of the joystick.
092   *
093   * @param state the new value
094   */
095  public void setTrigger(boolean state) {
096    setRawButton(1, state);
097  }
098
099  /**
100   * Set the top state of the joystick.
101   *
102   * @param state the new state
103   */
104  public void setTop(boolean state) {
105    setRawButton(2, state);
106  }
107}