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.hal.simulation.CTREPCMDataJNI;
008import edu.wpi.first.hal.simulation.NotifyCallback;
009import edu.wpi.first.wpilibj.PneumaticsBase;
010import edu.wpi.first.wpilibj.SensorUtil;
011
012/** Class to control a simulated Pneumatic Control Module (PCM). */
013@SuppressWarnings("AbbreviationAsWordInName")
014public class CTREPCMSim {
015  private final int m_index;
016
017  /** Constructs for the default PCM. */
018  public CTREPCMSim() {
019    m_index = SensorUtil.getDefaultCTREPCMModule();
020  }
021
022  /**
023   * Constructs from a PCM module number (CAN ID).
024   *
025   * @param module module number
026   */
027  public CTREPCMSim(int module) {
028    m_index = module;
029  }
030
031  /**
032   * Constructs from a Compressor object.
033   *
034   * @param module PCM module to simulate
035   */
036  public CTREPCMSim(PneumaticsBase module) {
037    m_index = module.getModuleNumber();
038  }
039
040  /**
041   * Register a callback to be run when the solenoid output on a channel changes.
042   *
043   * @param channel the channel to monitor
044   * @param callback the callback
045   * @param initialNotify should the callback be run with the initial value
046   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
047   *     this object so GC doesn't cancel the callback.
048   */
049  public CallbackStore registerSolenoidOutputCallback(
050      int channel, NotifyCallback callback, boolean initialNotify) {
051    int uid =
052        CTREPCMDataJNI.registerSolenoidOutputCallback(m_index, channel, callback, initialNotify);
053    return new CallbackStore(m_index, channel, uid, CTREPCMDataJNI::cancelSolenoidOutputCallback);
054  }
055
056  /**
057   * Check the solenoid output on a specific channel.
058   *
059   * @param channel the channel to check
060   * @return the solenoid output
061   */
062  public boolean getSolenoidOutput(int channel) {
063    return CTREPCMDataJNI.getSolenoidOutput(m_index, channel);
064  }
065
066  /**
067   * Change the solenoid output on a specific channel.
068   *
069   * @param channel the channel to check
070   * @param solenoidOutput the new solenoid output
071   */
072  public void setSolenoidOutput(int channel, boolean solenoidOutput) {
073    CTREPCMDataJNI.setSolenoidOutput(m_index, channel, solenoidOutput);
074  }
075
076  /**
077   * Register a callback to be run when the compressor is initialized.
078   *
079   * @param callback the callback
080   * @param initialNotify whether to run the callback with the initial state
081   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
082   *     this object so GC doesn't cancel the callback.
083   */
084  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
085    int uid = CTREPCMDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
086    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelInitializedCallback);
087  }
088
089  /**
090   * Check whether the compressor has been initialized.
091   *
092   * @return true if initialized
093   */
094  public boolean getInitialized() {
095    return CTREPCMDataJNI.getInitialized(m_index);
096  }
097
098  /**
099   * Define whether the compressor has been initialized.
100   *
101   * @param initialized whether the compressor is initialized
102   */
103  public void setInitialized(boolean initialized) {
104    CTREPCMDataJNI.setInitialized(m_index, initialized);
105  }
106
107  /**
108   * Register a callback to be run when the compressor activates.
109   *
110   * @param callback the callback
111   * @param initialNotify whether to run the callback with the initial state
112   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
113   *     this object so GC doesn't cancel the callback.
114   */
115  public CallbackStore registerCompressorOnCallback(
116      NotifyCallback callback, boolean initialNotify) {
117    int uid = CTREPCMDataJNI.registerCompressorOnCallback(m_index, callback, initialNotify);
118    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelCompressorOnCallback);
119  }
120
121  /**
122   * Check if the compressor is on.
123   *
124   * @return true if the compressor is active
125   */
126  public boolean getCompressorOn() {
127    return CTREPCMDataJNI.getCompressorOn(m_index);
128  }
129
130  /**
131   * Set whether the compressor is active.
132   *
133   * @param compressorOn the new value
134   */
135  public void setCompressorOn(boolean compressorOn) {
136    CTREPCMDataJNI.setCompressorOn(m_index, compressorOn);
137  }
138
139  /**
140   * Register a callback to be run whenever the closed loop state changes.
141   *
142   * @param callback the callback
143   * @param initialNotify whether the callback should be called with the initial value
144   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
145   *     this object so GC doesn't cancel the callback.
146   */
147  public CallbackStore registerClosedLoopEnabledCallback(
148      NotifyCallback callback, boolean initialNotify) {
149    int uid = CTREPCMDataJNI.registerClosedLoopEnabledCallback(m_index, callback, initialNotify);
150    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelClosedLoopEnabledCallback);
151  }
152
153  /**
154   * Check whether the closed loop compressor control is active.
155   *
156   * @return true if active
157   */
158  public boolean getClosedLoopEnabled() {
159    return CTREPCMDataJNI.getClosedLoopEnabled(m_index);
160  }
161
162  /**
163   * Turn on/off the closed loop control of the compressor.
164   *
165   * @param closedLoopEnabled whether the control loop is active
166   */
167  public void setClosedLoopEnabled(boolean closedLoopEnabled) {
168    CTREPCMDataJNI.setClosedLoopEnabled(m_index, closedLoopEnabled);
169  }
170
171  /**
172   * Register a callback to be run whenever the pressure switch value changes.
173   *
174   * @param callback the callback
175   * @param initialNotify whether the callback should be called with the initial value
176   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
177   *     this object so GC doesn't cancel the callback.
178   */
179  public CallbackStore registerPressureSwitchCallback(
180      NotifyCallback callback, boolean initialNotify) {
181    int uid = CTREPCMDataJNI.registerPressureSwitchCallback(m_index, callback, initialNotify);
182    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelPressureSwitchCallback);
183  }
184
185  /**
186   * Check the value of the pressure switch.
187   *
188   * @return the pressure switch value
189   */
190  public boolean getPressureSwitch() {
191    return CTREPCMDataJNI.getPressureSwitch(m_index);
192  }
193
194  /**
195   * Set the value of the pressure switch.
196   *
197   * @param pressureSwitch the new value
198   */
199  public void setPressureSwitch(boolean pressureSwitch) {
200    CTREPCMDataJNI.setPressureSwitch(m_index, pressureSwitch);
201  }
202
203  /**
204   * Register a callback to be run whenever the compressor current changes.
205   *
206   * @param callback the callback
207   * @param initialNotify whether to call the callback with the initial state
208   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
209   *     this object so GC doesn't cancel the callback.
210   */
211  public CallbackStore registerCompressorCurrentCallback(
212      NotifyCallback callback, boolean initialNotify) {
213    int uid = CTREPCMDataJNI.registerCompressorCurrentCallback(m_index, callback, initialNotify);
214    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelCompressorCurrentCallback);
215  }
216
217  /**
218   * Read the compressor current.
219   *
220   * @return the current of the compressor connected to this module
221   */
222  public double getCompressorCurrent() {
223    return CTREPCMDataJNI.getCompressorCurrent(m_index);
224  }
225
226  /**
227   * Set the compressor current.
228   *
229   * @param compressorCurrent the new compressor current
230   */
231  public void setCompressorCurrent(double compressorCurrent) {
232    CTREPCMDataJNI.setCompressorCurrent(m_index, compressorCurrent);
233  }
234
235  /** Reset all simulation data for this object. */
236  public void resetData() {
237    CTREPCMDataJNI.resetData(m_index);
238  }
239}