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 org.wpilib.simulation;
006
007import java.util.EnumSet;
008import org.wpilib.driverstation.GenericHID;
009import org.wpilib.driverstation.POVDirection;
010
011/** Class to control a simulated generic joystick. */
012public class GenericHIDSim {
013  /** GenericHID port. */
014  protected final int m_port;
015
016  /**
017   * Constructs from a GenericHID object.
018   *
019   * @param joystick joystick to simulate
020   */
021  public GenericHIDSim(GenericHID joystick) {
022    m_port = joystick.getPort();
023  }
024
025  /**
026   * Constructs from a joystick port number.
027   *
028   * @param port port number
029   */
030  public GenericHIDSim(int port) {
031    m_port = port;
032  }
033
034  /** Updates joystick data so that new values are visible to the user program. */
035  public void notifyNewData() {
036    DriverStationSim.notifyNewData();
037  }
038
039  /**
040   * Set the value of a given button.
041   *
042   * @param button the button to set
043   * @param value the new value
044   */
045  public void setRawButton(int button, boolean value) {
046    DriverStationSim.setJoystickButton(m_port, button, value);
047  }
048
049  /**
050   * Set the value of a given axis.
051   *
052   * @param axis the axis to set
053   * @param value the new value
054   */
055  public void setRawAxis(int axis, double value) {
056    DriverStationSim.setJoystickAxis(m_port, axis, value);
057  }
058
059  /**
060   * Set the value of a given POV.
061   *
062   * @param pov the POV to set
063   * @param value the new value
064   */
065  public void setPOV(int pov, POVDirection value) {
066    DriverStationSim.setJoystickPOV(m_port, pov, value);
067  }
068
069  /**
070   * Set the value of the default POV (port 0).
071   *
072   * @param value the new value
073   */
074  public void setPOV(POVDirection value) {
075    setPOV(0, value);
076  }
077
078  /**
079   * Set the maximum axis index for this device.
080   *
081   * @param maximumIndex the new maximum axis index
082   */
083  public void setAxesMaximumIndex(int maximumIndex) {
084    DriverStationSim.setJoystickAxesMaximumIndex(m_port, maximumIndex);
085  }
086
087  /**
088   * Set the axis count of this device.
089   *
090   * @param count the new axis count
091   */
092  public void setAxesAvailable(int count) {
093    DriverStationSim.setJoystickAxesAvailable(m_port, count);
094  }
095
096  /**
097   * Set the maximum POV index for this device.
098   *
099   * @param maximumIndex the new maximum POV index
100   */
101  public void setPOVsMaximumIndex(int maximumIndex) {
102    DriverStationSim.setJoystickPOVsMaximumIndex(m_port, maximumIndex);
103  }
104
105  /**
106   * Set the POV count of this device.
107   *
108   * @param count the new POV count
109   */
110  public void setPOVsAvailable(int count) {
111    DriverStationSim.setJoystickPOVsAvailable(m_port, count);
112  }
113
114  /**
115   * Set the maximum button index for this device.
116   *
117   * @param maximumIndex the new maximum button index
118   */
119  public void setButtonsMaximumIndex(int maximumIndex) {
120    DriverStationSim.setJoystickButtonsMaximumIndex(m_port, maximumIndex);
121  }
122
123  /**
124   * Set the button count of this device.
125   *
126   * @param count the new button count
127   */
128  public void setButtonsAvailable(long count) {
129    DriverStationSim.setJoystickButtonsAvailable(m_port, count);
130  }
131
132  /**
133   * Set the type of this device.
134   *
135   * @param type the new device type
136   */
137  public void setGamepadType(GenericHID.HIDType type) {
138    DriverStationSim.setJoystickGamepadType(m_port, type.value);
139  }
140
141  /**
142   * Set the supported outputs of this device.
143   *
144   * @param supportedOutputs the new supported outputs
145   */
146  public void setSupportedOutputs(EnumSet<GenericHID.SupportedOutput> supportedOutputs) {
147    int supportedOutputsInt = 0;
148    for (GenericHID.SupportedOutput output : supportedOutputs) {
149      supportedOutputsInt |= output.getValue();
150    }
151    DriverStationSim.setJoystickSupportedOutputs(m_port, supportedOutputsInt);
152  }
153
154  /**
155   * Set the name of this device.
156   *
157   * @param name the new device name
158   */
159  public void setName(String name) {
160    DriverStationSim.setJoystickName(m_port, name);
161  }
162
163  /**
164   * Get the led color set.
165   *
166   * @return the led color set
167   */
168  public int getLeds() {
169    return DriverStationSim.getJoystickLeds(m_port);
170  }
171
172  /**
173   * Get the joystick rumble.
174   *
175   * @param type the rumble to read
176   * @return the rumble value
177   */
178  public double getRumble(GenericHID.RumbleType type) {
179    int intType =
180        switch (type) {
181          case LEFT_RUMBLE -> 0;
182          case RIGHT_RUMBLE -> 1;
183          case LEFT_TRIGGER_RUMBLE -> 2;
184          case RIGHT_TRIGGER_RUMBLE -> 3;
185        };
186    int value = DriverStationSim.getJoystickRumble(m_port, intType);
187    return value / 65535.0;
188  }
189}