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.AnalogOutDataJNI;
008import edu.wpi.first.hal.simulation.NotifyCallback;
009import edu.wpi.first.wpilibj.AnalogOutput;
010
011/** Class to control a simulated analog output. */
012public class AnalogOutputSim {
013  private final int m_index;
014
015  /**
016   * Constructs from an AnalogOutput object.
017   *
018   * @param analogOutput AnalogOutput to simulate
019   */
020  public AnalogOutputSim(AnalogOutput analogOutput) {
021    m_index = analogOutput.getChannel();
022  }
023
024  /**
025   * Constructs from an analog output channel number.
026   *
027   * @param channel Channel number
028   */
029  public AnalogOutputSim(int channel) {
030    m_index = channel;
031  }
032
033  /**
034   * Register a callback to be run whenever the voltage changes.
035   *
036   * @param callback the callback
037   * @param initialNotify whether to call the callback with the initial state
038   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
039   *     this object so GC doesn't cancel the callback.
040   */
041  public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) {
042    int uid = AnalogOutDataJNI.registerVoltageCallback(m_index, callback, initialNotify);
043    return new CallbackStore(m_index, uid, AnalogOutDataJNI::cancelVoltageCallback);
044  }
045
046  /**
047   * Read the analog output voltage.
048   *
049   * @return the voltage on this analog output
050   */
051  public double getVoltage() {
052    return AnalogOutDataJNI.getVoltage(m_index);
053  }
054
055  /**
056   * Set the analog output voltage.
057   *
058   * @param voltage the new voltage on this analog output
059   */
060  public void setVoltage(double voltage) {
061    AnalogOutDataJNI.setVoltage(m_index, voltage);
062  }
063
064  /**
065   * Register a callback to be run when this analog output is initialized.
066   *
067   * @param callback the callback
068   * @param initialNotify whether to run the callback with the initial state
069   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
070   *     this object so GC doesn't cancel the callback.
071   */
072  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
073    int uid = AnalogOutDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
074    return new CallbackStore(m_index, uid, AnalogOutDataJNI::cancelInitializedCallback);
075  }
076
077  /**
078   * Check whether this analog output has been initialized.
079   *
080   * @return true if initialized
081   */
082  public boolean getInitialized() {
083    return AnalogOutDataJNI.getInitialized(m_index);
084  }
085
086  /**
087   * Define whether this analog output has been initialized.
088   *
089   * @param initialized whether this object is initialized
090   */
091  public void setInitialized(boolean initialized) {
092    AnalogOutDataJNI.setInitialized(m_index, initialized);
093  }
094
095  /** Reset all simulation data on this object. */
096  public void resetData() {
097    AnalogOutDataJNI.resetData(m_index);
098  }
099}