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.DIODataJNI;
008import edu.wpi.first.hal.simulation.NotifyCallback;
009import edu.wpi.first.wpilibj.DigitalInput;
010import edu.wpi.first.wpilibj.DigitalOutput;
011
012/** Class to control a simulated digital input or output. */
013public class DIOSim {
014  private final int m_index;
015
016  /**
017   * Constructs from a DigitalInput object.
018   *
019   * @param input DigitalInput to simulate
020   */
021  public DIOSim(DigitalInput input) {
022    m_index = input.getChannel();
023  }
024
025  /**
026   * Constructs from a DigitalOutput object.
027   *
028   * @param output DigitalOutput to simulate
029   */
030  public DIOSim(DigitalOutput output) {
031    m_index = output.getChannel();
032  }
033
034  /**
035   * Constructs from a digital I/O channel number.
036   *
037   * @param channel Channel number
038   */
039  public DIOSim(int channel) {
040    m_index = channel;
041  }
042
043  /**
044   * Register a callback to be run when this DIO is initialized.
045   *
046   * @param callback the callback
047   * @param initialNotify whether to run the callback with the initial state
048   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
049   *     this object so GC doesn't cancel the callback.
050   */
051  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
052    int uid = DIODataJNI.registerInitializedCallback(m_index, callback, initialNotify);
053    return new CallbackStore(m_index, uid, DIODataJNI::cancelInitializedCallback);
054  }
055
056  /**
057   * Check whether this DIO has been initialized.
058   *
059   * @return true if initialized
060   */
061  public boolean getInitialized() {
062    return DIODataJNI.getInitialized(m_index);
063  }
064
065  /**
066   * Define whether this DIO has been initialized.
067   *
068   * @param initialized whether this object is initialized
069   */
070  public void setInitialized(boolean initialized) {
071    DIODataJNI.setInitialized(m_index, initialized);
072  }
073
074  /**
075   * Register a callback to be run whenever the DIO value changes.
076   *
077   * @param callback the callback
078   * @param initialNotify whether the callback should be called with the initial value
079   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
080   *     this object so GC doesn't cancel the callback.
081   */
082  public CallbackStore registerValueCallback(NotifyCallback callback, boolean initialNotify) {
083    int uid = DIODataJNI.registerValueCallback(m_index, callback, initialNotify);
084    return new CallbackStore(m_index, uid, DIODataJNI::cancelValueCallback);
085  }
086
087  /**
088   * Read the value of the DIO port.
089   *
090   * @return the DIO value
091   */
092  public boolean getValue() {
093    return DIODataJNI.getValue(m_index);
094  }
095
096  /**
097   * Change the DIO value.
098   *
099   * @param value the new value
100   */
101  public void setValue(boolean value) {
102    DIODataJNI.setValue(m_index, value);
103  }
104
105  /**
106   * Register a callback to be run whenever the pulse length changes.
107   *
108   * @param callback the callback
109   * @param initialNotify whether to call the callback with the initial state
110   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
111   *     this object so GC doesn't cancel the callback.
112   */
113  public CallbackStore registerPulseLengthCallback(NotifyCallback callback, boolean initialNotify) {
114    int uid = DIODataJNI.registerPulseLengthCallback(m_index, callback, initialNotify);
115    return new CallbackStore(m_index, uid, DIODataJNI::cancelPulseLengthCallback);
116  }
117
118  /**
119   * Read the pulse length.
120   *
121   * @return the pulse length of this DIO port
122   */
123  public double getPulseLength() {
124    return DIODataJNI.getPulseLength(m_index);
125  }
126
127  /**
128   * Change the pulse length of this DIO port.
129   *
130   * @param pulseLength the new pulse length
131   */
132  public void setPulseLength(double pulseLength) {
133    DIODataJNI.setPulseLength(m_index, pulseLength);
134  }
135
136  /**
137   * Register a callback to be run whenever this DIO changes to be an input.
138   *
139   * @param callback the callback
140   * @param initialNotify whether the callback should be called with the initial state
141   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
142   *     this object so GC doesn't cancel the callback.
143   */
144  public CallbackStore registerIsInputCallback(NotifyCallback callback, boolean initialNotify) {
145    int uid = DIODataJNI.registerIsInputCallback(m_index, callback, initialNotify);
146    return new CallbackStore(m_index, uid, DIODataJNI::cancelIsInputCallback);
147  }
148
149  /**
150   * Check whether this DIO port is currently an Input.
151   *
152   * @return true if Input
153   */
154  public boolean getIsInput() {
155    return DIODataJNI.getIsInput(m_index);
156  }
157
158  /**
159   * Define whether this DIO port is an Input.
160   *
161   * @param isInput whether this DIO should be an Input
162   */
163  public void setIsInput(boolean isInput) {
164    DIODataJNI.setIsInput(m_index, isInput);
165  }
166
167  /**
168   * Register a callback to be run whenever the filter index changes.
169   *
170   * @param callback the callback
171   * @param initialNotify whether the callback should be called with the initial value
172   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
173   *     this object so GC doesn't cancel the callback.
174   */
175  public CallbackStore registerFilterIndexCallback(NotifyCallback callback, boolean initialNotify) {
176    int uid = DIODataJNI.registerFilterIndexCallback(m_index, callback, initialNotify);
177    return new CallbackStore(m_index, uid, DIODataJNI::cancelFilterIndexCallback);
178  }
179
180  /**
181   * Read the filter index.
182   *
183   * @return the filter index of this DIO port
184   */
185  public int getFilterIndex() {
186    return DIODataJNI.getFilterIndex(m_index);
187  }
188
189  /**
190   * Change the filter index of this DIO port.
191   *
192   * @param filterIndex the new filter index
193   */
194  public void setFilterIndex(int filterIndex) {
195    DIODataJNI.setFilterIndex(m_index, filterIndex);
196  }
197
198  /** Reset all simulation data of this object. */
199  public void resetData() {
200    DIODataJNI.resetData(m_index);
201  }
202}