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.AnalogInDataJNI;
008import edu.wpi.first.hal.simulation.NotifyCallback;
009import edu.wpi.first.wpilibj.AnalogInput;
010
011/** Class to control a simulated analog input. */
012public class AnalogInputSim {
013  private final int m_index;
014
015  /**
016   * Constructs from an AnalogInput object.
017   *
018   * @param analogInput AnalogInput to simulate
019   */
020  public AnalogInputSim(AnalogInput analogInput) {
021    m_index = analogInput.getChannel();
022  }
023
024  /**
025   * Constructs from an analog input channel number.
026   *
027   * @param channel Channel number
028   */
029  public AnalogInputSim(int channel) {
030    m_index = channel;
031  }
032
033  /**
034   * Register a callback on whether the analog input is initialized.
035   *
036   * @param callback the callback that will be called whenever the analog input is initialized
037   * @param initialNotify if true, the callback will be run on the initial value
038   * @return the {@link CallbackStore} object associated with this callback.
039   */
040  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
041    int uid = AnalogInDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
042    return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelInitializedCallback);
043  }
044
045  /**
046   * Check if this analog input has been initialized.
047   *
048   * @return true if initialized
049   */
050  public boolean getInitialized() {
051    return AnalogInDataJNI.getInitialized(m_index);
052  }
053
054  /**
055   * Change whether this analog input has been initialized.
056   *
057   * @param initialized the new value
058   */
059  public void setInitialized(boolean initialized) {
060    AnalogInDataJNI.setInitialized(m_index, initialized);
061  }
062
063  /**
064   * Register a callback on the number of average bits.
065   *
066   * @param callback the callback that will be called whenever the number of average bits is changed
067   * @param initialNotify if true, the callback will be run on the initial value
068   * @return the {@link CallbackStore} object associated with this callback.
069   */
070  public CallbackStore registerAverageBitsCallback(NotifyCallback callback, boolean initialNotify) {
071    int uid = AnalogInDataJNI.registerAverageBitsCallback(m_index, callback, initialNotify);
072    return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAverageBitsCallback);
073  }
074
075  /**
076   * Get the number of average bits.
077   *
078   * @return the number of average bits
079   */
080  public int getAverageBits() {
081    return AnalogInDataJNI.getAverageBits(m_index);
082  }
083
084  /**
085   * Change the number of average bits.
086   *
087   * @param averageBits the new value
088   */
089  public void setAverageBits(int averageBits) {
090    AnalogInDataJNI.setAverageBits(m_index, averageBits);
091  }
092
093  /**
094   * Register a callback on the amount of oversampling bits.
095   *
096   * @param callback the callback that will be called whenever the oversampling bits are changed.
097   * @param initialNotify if true, the callback will be run on the initial value
098   * @return the {@link CallbackStore} object associated with this callback.
099   */
100  public CallbackStore registerOversampleBitsCallback(
101      NotifyCallback callback, boolean initialNotify) {
102    int uid = AnalogInDataJNI.registerOversampleBitsCallback(m_index, callback, initialNotify);
103    return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelOversampleBitsCallback);
104  }
105
106  /**
107   * Get the amount of oversampling bits.
108   *
109   * @return the amount of oversampling bits
110   */
111  public int getOversampleBits() {
112    return AnalogInDataJNI.getOversampleBits(m_index);
113  }
114
115  /**
116   * Change the amount of oversampling bits.
117   *
118   * @param oversampleBits the new value
119   */
120  public void setOversampleBits(int oversampleBits) {
121    AnalogInDataJNI.setOversampleBits(m_index, oversampleBits);
122  }
123
124  /**
125   * Register a callback on the voltage.
126   *
127   * @param callback the callback that will be called whenever the voltage is changed.
128   * @param initialNotify if true, the callback will be run on the initial value
129   * @return the {@link CallbackStore} object associated with this callback.
130   */
131  public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) {
132    int uid = AnalogInDataJNI.registerVoltageCallback(m_index, callback, initialNotify);
133    return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelVoltageCallback);
134  }
135
136  /**
137   * Get the voltage.
138   *
139   * @return the voltage
140   */
141  public double getVoltage() {
142    return AnalogInDataJNI.getVoltage(m_index);
143  }
144
145  /**
146   * Change the voltage.
147   *
148   * @param voltage the new value
149   */
150  public void setVoltage(double voltage) {
151    AnalogInDataJNI.setVoltage(m_index, voltage);
152  }
153
154  /** Reset all simulation data for this object. */
155  public void resetData() {
156    AnalogInDataJNI.resetData(m_index);
157  }
158}