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.hal.simulation.PWMDataJNI;
009import edu.wpi.first.wpilibj.PWM;
010import edu.wpi.first.wpilibj.motorcontrol.PWMMotorController;
011
012/** Class to control a simulated PWM output. */
013public class PWMSim {
014  private final int m_index;
015
016  /**
017   * Constructs from a PWM object.
018   *
019   * @param pwm PWM to simulate
020   */
021  public PWMSim(PWM pwm) {
022    m_index = pwm.getChannel();
023  }
024
025  /**
026   * Constructs from a PWMMotorController object.
027   *
028   * @param motorctrl PWMMotorController to simulate
029   */
030  public PWMSim(PWMMotorController motorctrl) {
031    m_index = motorctrl.getChannel();
032  }
033
034  /**
035   * Constructs from a PWM channel number.
036   *
037   * @param channel Channel number
038   */
039  public PWMSim(int channel) {
040    m_index = channel;
041  }
042
043  /**
044   * Register a callback to be run when the PWM 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 = PWMDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
053    return new CallbackStore(m_index, uid, PWMDataJNI::cancelInitializedCallback);
054  }
055
056  /**
057   * Check whether the PWM has been initialized.
058   *
059   * @return true if initialized
060   */
061  public boolean getInitialized() {
062    return PWMDataJNI.getInitialized(m_index);
063  }
064
065  /**
066   * Define whether the PWM has been initialized.
067   *
068   * @param initialized whether this object is initialized
069   */
070  public void setInitialized(boolean initialized) {
071    PWMDataJNI.setInitialized(m_index, initialized);
072  }
073
074  /**
075   * Register a callback to be run when the PWM raw value changes.
076   *
077   * @param callback the callback
078   * @param initialNotify whether to run the callback 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 registerRawValueCallback(NotifyCallback callback, boolean initialNotify) {
083    int uid = PWMDataJNI.registerRawValueCallback(m_index, callback, initialNotify);
084    return new CallbackStore(m_index, uid, PWMDataJNI::cancelRawValueCallback);
085  }
086
087  /**
088   * Get the PWM raw value.
089   *
090   * @return the PWM raw value
091   */
092  public int getRawValue() {
093    return PWMDataJNI.getRawValue(m_index);
094  }
095
096  /**
097   * Set the PWM raw value.
098   *
099   * @param rawValue the PWM raw value
100   */
101  public void setRawValue(int rawValue) {
102    PWMDataJNI.setRawValue(m_index, rawValue);
103  }
104
105  /**
106   * Register a callback to be run when the PWM speed changes.
107   *
108   * @param callback the callback
109   * @param initialNotify whether to run the callback with the initial value
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 registerSpeedCallback(NotifyCallback callback, boolean initialNotify) {
114    int uid = PWMDataJNI.registerSpeedCallback(m_index, callback, initialNotify);
115    return new CallbackStore(m_index, uid, PWMDataJNI::cancelSpeedCallback);
116  }
117
118  /**
119   * Get the PWM speed.
120   *
121   * @return the PWM speed (-1.0 to 1.0)
122   */
123  public double getSpeed() {
124    return PWMDataJNI.getSpeed(m_index);
125  }
126
127  /**
128   * Set the PWM speed.
129   *
130   * @param speed the PWM speed (-1.0 to 1.0)
131   */
132  public void setSpeed(double speed) {
133    PWMDataJNI.setSpeed(m_index, speed);
134  }
135
136  /**
137   * Register a callback to be run when the PWM position changes.
138   *
139   * @param callback the callback
140   * @param initialNotify whether to run the callback with the initial value
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 registerPositionCallback(NotifyCallback callback, boolean initialNotify) {
145    int uid = PWMDataJNI.registerPositionCallback(m_index, callback, initialNotify);
146    return new CallbackStore(m_index, uid, PWMDataJNI::cancelPositionCallback);
147  }
148
149  /**
150   * Get the PWM position.
151   *
152   * @return the PWM position (0.0 to 1.0)
153   */
154  public double getPosition() {
155    return PWMDataJNI.getPosition(m_index);
156  }
157
158  /**
159   * Set the PWM position.
160   *
161   * @param position the PWM position (0.0 to 1.0)
162   */
163  public void setPosition(double position) {
164    PWMDataJNI.setPosition(m_index, position);
165  }
166
167  /**
168   * Register a callback to be run when the PWM period scale changes.
169   *
170   * @param callback the callback
171   * @param initialNotify whether to run the callback 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 registerPeriodScaleCallback(NotifyCallback callback, boolean initialNotify) {
176    int uid = PWMDataJNI.registerPeriodScaleCallback(m_index, callback, initialNotify);
177    return new CallbackStore(m_index, uid, PWMDataJNI::cancelPeriodScaleCallback);
178  }
179
180  /**
181   * Get the PWM period scale.
182   *
183   * @return the PWM period scale
184   */
185  public int getPeriodScale() {
186    return PWMDataJNI.getPeriodScale(m_index);
187  }
188
189  /**
190   * Set the PWM period scale.
191   *
192   * @param periodScale the PWM period scale
193   */
194  public void setPeriodScale(int periodScale) {
195    PWMDataJNI.setPeriodScale(m_index, periodScale);
196  }
197
198  /**
199   * Register a callback to be run when the PWM zero latch state changes.
200   *
201   * @param callback the callback
202   * @param initialNotify whether to run the callback with the initial state
203   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
204   *     this object so GC doesn't cancel the callback.
205   */
206  public CallbackStore registerZeroLatchCallback(NotifyCallback callback, boolean initialNotify) {
207    int uid = PWMDataJNI.registerZeroLatchCallback(m_index, callback, initialNotify);
208    return new CallbackStore(m_index, uid, PWMDataJNI::cancelZeroLatchCallback);
209  }
210
211  /**
212   * Check whether the PWM is zero latched.
213   *
214   * @return true if zero latched
215   */
216  public boolean getZeroLatch() {
217    return PWMDataJNI.getZeroLatch(m_index);
218  }
219
220  /**
221   * Define whether the PWM has been zero latched.
222   *
223   * @param zeroLatch true to indicate zero latched
224   */
225  public void setZeroLatch(boolean zeroLatch) {
226    PWMDataJNI.setZeroLatch(m_index, zeroLatch);
227  }
228
229  /** Reset all simulation data. */
230  public void resetData() {
231    PWMDataJNI.resetData(m_index);
232  }
233}