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.NotifyCallback;
008import edu.wpi.first.wpilibj.PneumaticsBase;
009import edu.wpi.first.wpilibj.PneumaticsModuleType;
010
011/** Common base class for pneumatics module simulation classes. */
012public abstract class PneumaticsBaseSim {
013  /** PneumaticsBase index. */
014  protected final int m_index;
015
016  /**
017   * Get a module sim for a specific type.
018   *
019   * @param module the module number / CAN ID.
020   * @param type the module type.
021   * @return the module object.
022   */
023  public static PneumaticsBaseSim getForType(int module, PneumaticsModuleType type) {
024    switch (type) {
025      case CTREPCM:
026        return new CTREPCMSim(module);
027      case REVPH:
028        return new REVPHSim(module);
029      default:
030        throw new IllegalArgumentException("Unknown module type");
031    }
032  }
033
034  /**
035   * Constructs a PneumaticsBaseSim with the given index.
036   *
037   * @param index The index.
038   */
039  protected PneumaticsBaseSim(int index) {
040    m_index = index;
041  }
042
043  /**
044   * Constructs a PneumaticsBaseSim for the given module.
045   *
046   * @param module The module.
047   */
048  protected PneumaticsBaseSim(PneumaticsBase module) {
049    this(module.getModuleNumber());
050  }
051
052  /**
053   * Check whether the PCM/PH has been initialized.
054   *
055   * @return true if initialized
056   */
057  public abstract boolean getInitialized();
058
059  /**
060   * Define whether the PCM/PH has been initialized.
061   *
062   * @param initialized true for initialized
063   */
064  public abstract void setInitialized(boolean initialized);
065
066  /**
067   * Register a callback to be run when the PCM/PH is initialized.
068   *
069   * @param callback the callback
070   * @param initialNotify whether to run the callback with the initial state
071   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
072   *     this object so GC doesn't cancel the callback.
073   */
074  public abstract CallbackStore registerInitializedCallback(
075      NotifyCallback callback, boolean initialNotify);
076
077  /**
078   * Check if the compressor is on.
079   *
080   * @return true if the compressor is active
081   */
082  public abstract boolean getCompressorOn();
083
084  /**
085   * Set whether the compressor is active.
086   *
087   * @param compressorOn the new value
088   */
089  public abstract void setCompressorOn(boolean compressorOn);
090
091  /**
092   * Register a callback to be run when the compressor activates.
093   *
094   * @param callback the callback
095   * @param initialNotify whether to run the callback with the initial state
096   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
097   *     this object so GC doesn't cancel the callback.
098   */
099  public abstract CallbackStore registerCompressorOnCallback(
100      NotifyCallback callback, boolean initialNotify);
101
102  /**
103   * Check the solenoid output on a specific channel.
104   *
105   * @param channel the channel to check
106   * @return the solenoid output
107   */
108  public abstract boolean getSolenoidOutput(int channel);
109
110  /**
111   * Change the solenoid output on a specific channel.
112   *
113   * @param channel the channel to check
114   * @param solenoidOutput the new solenoid output
115   */
116  public abstract void setSolenoidOutput(int channel, boolean solenoidOutput);
117
118  /**
119   * Register a callback to be run when the solenoid output on a channel changes.
120   *
121   * @param channel the channel to monitor
122   * @param callback the callback
123   * @param initialNotify should the callback be run with the initial value
124   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
125   *     this object so GC doesn't cancel the callback.
126   */
127  public abstract CallbackStore registerSolenoidOutputCallback(
128      int channel, NotifyCallback callback, boolean initialNotify);
129
130  /**
131   * Check the value of the pressure switch.
132   *
133   * @return the pressure switch value
134   */
135  public abstract boolean getPressureSwitch();
136
137  /**
138   * Set the value of the pressure switch.
139   *
140   * @param pressureSwitch the new value
141   */
142  public abstract void setPressureSwitch(boolean pressureSwitch);
143
144  /**
145   * Register a callback to be run whenever the pressure switch value changes.
146   *
147   * @param callback the callback
148   * @param initialNotify whether the callback should be called with the initial value
149   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
150   *     this object so GC doesn't cancel the callback.
151   */
152  public abstract CallbackStore registerPressureSwitchCallback(
153      NotifyCallback callback, boolean initialNotify);
154
155  /**
156   * Read the compressor current.
157   *
158   * @return the current of the compressor connected to this module
159   */
160  public abstract double getCompressorCurrent();
161
162  /**
163   * Set the compressor current.
164   *
165   * @param compressorCurrent the new compressor current
166   */
167  public abstract void setCompressorCurrent(double compressorCurrent);
168
169  /**
170   * Register a callback to be run whenever the compressor current changes.
171   *
172   * @param callback the callback
173   * @param initialNotify whether to call the callback with the initial state
174   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
175   *     this object so GC doesn't cancel the callback.
176   */
177  public abstract CallbackStore registerCompressorCurrentCallback(
178      NotifyCallback callback, boolean initialNotify);
179
180  /** Reset all simulation data for this object. */
181  public abstract void resetData();
182}