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.
039   */
040  public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) {
041    int uid = AnalogOutDataJNI.registerVoltageCallback(m_index, callback, initialNotify);
042    return new CallbackStore(m_index, uid, AnalogOutDataJNI::cancelVoltageCallback);
043  }
044
045  /**
046   * Read the analog output voltage.
047   *
048   * @return the voltage on this analog output
049   */
050  public double getVoltage() {
051    return AnalogOutDataJNI.getVoltage(m_index);
052  }
053
054  /**
055   * Set the analog output voltage.
056   *
057   * @param voltage the new voltage on this analog output
058   */
059  public void setVoltage(double voltage) {
060    AnalogOutDataJNI.setVoltage(m_index, voltage);
061  }
062
063  /**
064   * Register a callback to be run when this analog output is initialized.
065   *
066   * @param callback the callback
067   * @param initialNotify whether to run the callback with the initial state
068   * @return the {@link CallbackStore} object associated with this callback.
069   */
070  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
071    int uid = AnalogOutDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
072    return new CallbackStore(m_index, uid, AnalogOutDataJNI::cancelInitializedCallback);
073  }
074
075  /**
076   * Check whether this analog output has been initialized.
077   *
078   * @return true if initialized
079   */
080  public boolean getInitialized() {
081    return AnalogOutDataJNI.getInitialized(m_index);
082  }
083
084  /**
085   * Define whether this analog output has been initialized.
086   *
087   * @param initialized whether this object is initialized
088   */
089  public void setInitialized(boolean initialized) {
090    AnalogOutDataJNI.setInitialized(m_index, initialized);
091  }
092
093  /** Reset all simulation data on this object. */
094  public void resetData() {
095    AnalogOutDataJNI.resetData(m_index);
096  }
097}