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