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.PowerDistributionDataJNI;
009import edu.wpi.first.wpilibj.PowerDistribution;
010
011/** Class to control a simulated Power Distribution Panel (PDP). */
012public class PDPSim {
013  private final int m_index;
014
015  /** Constructs for the default PDP. */
016  public PDPSim() {
017    m_index = 0;
018  }
019
020  /**
021   * Constructs from a PDP module number (CAN ID).
022   *
023   * @param module module number
024   */
025  public PDPSim(int module) {
026    m_index = module;
027  }
028
029  /**
030   * Constructs from a PowerDistribution object.
031   *
032   * @param pdp PowerDistribution to simulate
033   */
034  public PDPSim(PowerDistribution pdp) {
035    m_index = pdp.getModule();
036  }
037
038  /**
039   * Register a callback to be run when the PDP is initialized.
040   *
041   * @param callback the callback
042   * @param initialNotify whether to run the callback with the initial state
043   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
044   *     this object so GC doesn't cancel the callback.
045   */
046  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
047    int uid =
048        PowerDistributionDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
049    return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelInitializedCallback);
050  }
051
052  /**
053   * Check whether the PDP has been initialized.
054   *
055   * @return true if initialized
056   */
057  public boolean getInitialized() {
058    return PowerDistributionDataJNI.getInitialized(m_index);
059  }
060
061  /**
062   * Define whether the PDP has been initialized.
063   *
064   * @param initialized whether this object is initialized
065   */
066  public void setInitialized(boolean initialized) {
067    PowerDistributionDataJNI.setInitialized(m_index, initialized);
068  }
069
070  /**
071   * Register a callback to be run whenever the PDP temperature changes.
072   *
073   * @param callback the callback
074   * @param initialNotify whether to call the callback with the initial state
075   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
076   *     this object so GC doesn't cancel the callback.
077   */
078  public CallbackStore registerTemperatureCallback(NotifyCallback callback, boolean initialNotify) {
079    int uid =
080        PowerDistributionDataJNI.registerTemperatureCallback(m_index, callback, initialNotify);
081    return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelTemperatureCallback);
082  }
083
084  /**
085   * Check the temperature of the PDP.
086   *
087   * @return the PDP temperature
088   */
089  public double getTemperature() {
090    return PowerDistributionDataJNI.getTemperature(m_index);
091  }
092
093  /**
094   * Define the PDP temperature.
095   *
096   * @param temperature the new PDP temperature
097   */
098  public void setTemperature(double temperature) {
099    PowerDistributionDataJNI.setTemperature(m_index, temperature);
100  }
101
102  /**
103   * Register a callback to be run whenever the PDP voltage changes.
104   *
105   * @param callback the callback
106   * @param initialNotify whether to call the callback with the initial state
107   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
108   *     this object so GC doesn't cancel the callback.
109   */
110  public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) {
111    int uid = PowerDistributionDataJNI.registerVoltageCallback(m_index, callback, initialNotify);
112    return new CallbackStore(m_index, uid, PowerDistributionDataJNI::cancelVoltageCallback);
113  }
114
115  /**
116   * Check the PDP voltage.
117   *
118   * @return the PDP voltage.
119   */
120  public double getVoltage() {
121    return PowerDistributionDataJNI.getVoltage(m_index);
122  }
123
124  /**
125   * Set the PDP voltage.
126   *
127   * @param voltage the new PDP voltage
128   */
129  public void setVoltage(double voltage) {
130    PowerDistributionDataJNI.setVoltage(m_index, voltage);
131  }
132
133  /**
134   * Register a callback to be run whenever the current of a specific channel changes.
135   *
136   * @param channel the channel
137   * @param callback the callback
138   * @param initialNotify whether to call the callback with the initial state
139   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
140   *     this object so GC doesn't cancel the callback.
141   */
142  public CallbackStore registerCurrentCallback(
143      int channel, NotifyCallback callback, boolean initialNotify) {
144    int uid =
145        PowerDistributionDataJNI.registerCurrentCallback(m_index, channel, callback, initialNotify);
146    return new CallbackStore(
147        m_index, channel, uid, PowerDistributionDataJNI::cancelCurrentCallback);
148  }
149
150  /**
151   * Read the current in one of the PDP channels.
152   *
153   * @param channel the channel to check
154   * @return the current in the given channel
155   */
156  public double getCurrent(int channel) {
157    return PowerDistributionDataJNI.getCurrent(m_index, channel);
158  }
159
160  /**
161   * Change the current in the given channel.
162   *
163   * @param channel the channel to edit
164   * @param current the new current for the channel
165   */
166  public void setCurrent(int channel, double current) {
167    PowerDistributionDataJNI.setCurrent(m_index, channel, current);
168  }
169
170  /** Reset all PDP simulation data. */
171  public void resetData() {
172    PowerDistributionDataJNI.resetData(m_index);
173  }
174}