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