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/** Class to control a simulated {@link edu.wpi.first.wpilibj.Solenoid}. */
012public class SolenoidSim {
013  private final PneumaticsBaseSim m_module;
014  private final int m_channel;
015
016  /**
017   * Constructs for a solenoid on the given pneumatics module.
018   *
019   * @param moduleSim the PCM the solenoid is connected to.
020   * @param channel the solenoid channel.
021   */
022  public SolenoidSim(PneumaticsBaseSim moduleSim, int channel) {
023    m_module = moduleSim;
024    m_channel = channel;
025  }
026
027  /**
028   * Constructs for a solenoid on a pneumatics module of the given type and ID.
029   *
030   * @param module the CAN ID of the pneumatics module the solenoid is connected to.
031   * @param moduleType the module type (PH or PCM)
032   * @param channel the solenoid channel.
033   */
034  public SolenoidSim(int module, PneumaticsModuleType moduleType, int channel) {
035    this(PneumaticsBaseSim.getForType(module, moduleType), channel);
036  }
037
038  /**
039   * Constructs for a solenoid on a pneumatics module of the given type and default ID.
040   *
041   * @param moduleType the module type (PH or PCM)
042   * @param channel the solenoid channel.
043   */
044  public SolenoidSim(PneumaticsModuleType moduleType, int channel) {
045    this(PneumaticsBase.getDefaultForType(moduleType), moduleType, channel);
046  }
047
048  /**
049   * Check the solenoid output.
050   *
051   * @return the solenoid output
052   */
053  public boolean getOutput() {
054    return m_module.getSolenoidOutput(m_channel);
055  }
056
057  /**
058   * Change the solenoid output.
059   *
060   * @param output the new solenoid output
061   */
062  public void setOutput(boolean output) {
063    m_module.setSolenoidOutput(m_channel, output);
064  }
065
066  /**
067   * Register a callback to be run when the output of this solenoid has changed.
068   *
069   * @param callback the callback
070   * @param initialNotify should the callback be run with the initial value
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 CallbackStore registerOutputCallback(NotifyCallback callback, boolean initialNotify) {
075    return m_module.registerSolenoidOutputCallback(m_channel, callback, initialNotify);
076  }
077
078  /**
079   * Get the wrapped {@link PneumaticsBaseSim} object.
080   *
081   * @return the wrapped {@link PneumaticsBaseSim} object.
082   */
083  public PneumaticsBaseSim getPCMSim() {
084    return m_module;
085  }
086}