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.PneumaticsControlModule;
010import edu.wpi.first.wpilibj.SensorUtil;
011
012/** Class to control a simulated Pneumatic Control Module (PCM). */
013public class CTREPCMSim extends PneumaticsBaseSim {
014  /** Constructs for the default PCM. */
015  public CTREPCMSim() {
016    super(SensorUtil.getDefaultCTREPCMModule());
017  }
018
019  /**
020   * Constructs from a PCM module number (CAN ID).
021   *
022   * @param module module number
023   */
024  public CTREPCMSim(int module) {
025    super(module);
026  }
027
028  /**
029   * Constructs from a PneumaticsControlModule object.
030   *
031   * @param module PCM module to simulate
032   */
033  public CTREPCMSim(PneumaticsControlModule module) {
034    super(module);
035  }
036
037  /**
038   * Check whether the closed loop compressor control is active.
039   *
040   * @return true if active
041   */
042  public boolean getClosedLoopEnabled() {
043    return CTREPCMDataJNI.getClosedLoopEnabled(m_index);
044  }
045
046  /**
047   * Turn on/off the closed loop control of the compressor.
048   *
049   * @param closedLoopEnabled whether the control loop is active
050   */
051  public void setClosedLoopEnabled(boolean closedLoopEnabled) {
052    CTREPCMDataJNI.setClosedLoopEnabled(m_index, closedLoopEnabled);
053  }
054
055  /**
056   * Register a callback to be run whenever the closed loop state changes.
057   *
058   * @param callback the callback
059   * @param initialNotify whether the callback should be called with the initial value
060   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
061   *     this object so GC doesn't cancel the callback.
062   */
063  public CallbackStore registerClosedLoopEnabledCallback(
064      NotifyCallback callback, boolean initialNotify) {
065    int uid = CTREPCMDataJNI.registerClosedLoopEnabledCallback(m_index, callback, initialNotify);
066    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelClosedLoopEnabledCallback);
067  }
068
069  @Override
070  public boolean getInitialized() {
071    return CTREPCMDataJNI.getInitialized(m_index);
072  }
073
074  @Override
075  public void setInitialized(boolean initialized) {
076    CTREPCMDataJNI.setInitialized(m_index, initialized);
077  }
078
079  @Override
080  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
081    int uid = CTREPCMDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
082    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelInitializedCallback);
083  }
084
085  @Override
086  public boolean getCompressorOn() {
087    return CTREPCMDataJNI.getCompressorOn(m_index);
088  }
089
090  @Override
091  public void setCompressorOn(boolean compressorOn) {
092    CTREPCMDataJNI.setCompressorOn(m_index, compressorOn);
093  }
094
095  @Override
096  public CallbackStore registerCompressorOnCallback(
097      NotifyCallback callback, boolean initialNotify) {
098    int uid = CTREPCMDataJNI.registerCompressorOnCallback(m_index, callback, initialNotify);
099    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelCompressorOnCallback);
100  }
101
102  @Override
103  public boolean getSolenoidOutput(int channel) {
104    return CTREPCMDataJNI.getSolenoidOutput(m_index, channel);
105  }
106
107  @Override
108  public void setSolenoidOutput(int channel, boolean solenoidOutput) {
109    CTREPCMDataJNI.setSolenoidOutput(m_index, channel, solenoidOutput);
110  }
111
112  @Override
113  public CallbackStore registerSolenoidOutputCallback(
114      int channel, NotifyCallback callback, boolean initialNotify) {
115    int uid =
116        CTREPCMDataJNI.registerSolenoidOutputCallback(m_index, channel, callback, initialNotify);
117    return new CallbackStore(m_index, channel, uid, CTREPCMDataJNI::cancelSolenoidOutputCallback);
118  }
119
120  @Override
121  public boolean getPressureSwitch() {
122    return CTREPCMDataJNI.getPressureSwitch(m_index);
123  }
124
125  @Override
126  public void setPressureSwitch(boolean pressureSwitch) {
127    CTREPCMDataJNI.setPressureSwitch(m_index, pressureSwitch);
128  }
129
130  @Override
131  public CallbackStore registerPressureSwitchCallback(
132      NotifyCallback callback, boolean initialNotify) {
133    int uid = CTREPCMDataJNI.registerPressureSwitchCallback(m_index, callback, initialNotify);
134    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelPressureSwitchCallback);
135  }
136
137  @Override
138  public double getCompressorCurrent() {
139    return CTREPCMDataJNI.getCompressorCurrent(m_index);
140  }
141
142  @Override
143  public void setCompressorCurrent(double compressorCurrent) {
144    CTREPCMDataJNI.setCompressorCurrent(m_index, compressorCurrent);
145  }
146
147  @Override
148  public CallbackStore registerCompressorCurrentCallback(
149      NotifyCallback callback, boolean initialNotify) {
150    int uid = CTREPCMDataJNI.registerCompressorCurrentCallback(m_index, callback, initialNotify);
151    return new CallbackStore(m_index, uid, CTREPCMDataJNI::cancelCompressorCurrentCallback);
152  }
153
154  @Override
155  public void resetData() {
156    CTREPCMDataJNI.resetData(m_index);
157  }
158}