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.AddressableLEDDataJNI;
008import edu.wpi.first.hal.simulation.ConstBufferCallback;
009import edu.wpi.first.hal.simulation.NotifyCallback;
010import edu.wpi.first.wpilibj.AddressableLED;
011
012/** Class to control a simulated addressable LED. */
013public class AddressableLEDSim {
014  private final int m_channel;
015
016  /**
017   * Constructs an addressable LED for a specific channel.
018   *
019   * @param channel output channel
020   */
021  public AddressableLEDSim(int channel) {
022    m_channel = channel;
023  }
024
025  /**
026   * Constructs from an AddressableLED object.
027   *
028   * @param addressableLED AddressableLED to simulate
029   */
030  public AddressableLEDSim(AddressableLED addressableLED) {
031    m_channel = addressableLED.getChannel();
032  }
033
034  /**
035   * Register a callback on the Initialized property.
036   *
037   * @param callback the callback that will be called whenever the Initialized property is changed
038   * @param initialNotify if true, the callback will be run on the initial value
039   * @return the {@link CallbackStore} object associated with this callback.
040   */
041  public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
042    int uid = AddressableLEDDataJNI.registerInitializedCallback(m_channel, callback, initialNotify);
043    return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelInitializedCallback);
044  }
045
046  /**
047   * Check if initialized.
048   *
049   * @return true if initialized
050   */
051  public boolean getInitialized() {
052    return AddressableLEDDataJNI.getInitialized(m_channel);
053  }
054
055  /**
056   * Change the Initialized value of the LED strip.
057   *
058   * @param initialized the new value
059   */
060  public void setInitialized(boolean initialized) {
061    AddressableLEDDataJNI.setInitialized(m_channel, initialized);
062  }
063
064  /**
065   * Register a callback on the start.
066   *
067   * @param callback the callback that will be called whenever the start is changed
068   * @param initialNotify if true, the callback will be run on the initial value
069   * @return the {@link CallbackStore} object associated with this callback.
070   */
071  public CallbackStore registerStartCallback(NotifyCallback callback, boolean initialNotify) {
072    int uid = AddressableLEDDataJNI.registerStartCallback(m_channel, callback, initialNotify);
073    return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelStartCallback);
074  }
075
076  /**
077   * Get the start.
078   *
079   * @return the start
080   */
081  public int getStart() {
082    return AddressableLEDDataJNI.getStart(m_channel);
083  }
084
085  /**
086   * Change the start.
087   *
088   * @param start the new start
089   */
090  public void setStart(int start) {
091    AddressableLEDDataJNI.setStart(m_channel, start);
092  }
093
094  /**
095   * Register a callback on the length.
096   *
097   * @param callback the callback that will be called whenever the length is changed
098   * @param initialNotify if true, the callback will be run on the initial value
099   * @return the {@link CallbackStore} object associated with this callback.
100   */
101  public CallbackStore registerLengthCallback(NotifyCallback callback, boolean initialNotify) {
102    int uid = AddressableLEDDataJNI.registerLengthCallback(m_channel, callback, initialNotify);
103    return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelLengthCallback);
104  }
105
106  /**
107   * Get the length of the LED strip.
108   *
109   * @return the length
110   */
111  public int getLength() {
112    return AddressableLEDDataJNI.getLength(m_channel);
113  }
114
115  /**
116   * Change the length of the LED strip.
117   *
118   * @param length the new value
119   */
120  public void setLength(int length) {
121    AddressableLEDDataJNI.setLength(m_channel, length);
122  }
123
124  /**
125   * Register a callback on the LED data.
126   *
127   * @param callback the callback that will be called whenever the LED data is changed
128   * @return the {@link CallbackStore} object associated with this callback.
129   */
130  public static CallbackStore registerDataCallback(ConstBufferCallback callback) {
131    int uid = AddressableLEDDataJNI.registerDataCallback(callback);
132    return new CallbackStore(uid, AddressableLEDDataJNI::cancelDataCallback);
133  }
134
135  /**
136   * Get the LED data.
137   *
138   * @return the LED data
139   */
140  public byte[] getData() {
141    return getGlobalData(getStart(), getLength());
142  }
143
144  /**
145   * Change the LED data.
146   *
147   * @param data the new data
148   */
149  public void setData(byte[] data) {
150    setGlobalData(getStart(), data);
151  }
152
153  /**
154   * Get the global LED data.
155   *
156   * @param start start, in LEDs
157   * @param length length, in LEDs
158   * @return the LED data
159   */
160  public static byte[] getGlobalData(int start, int length) {
161    return AddressableLEDDataJNI.getData(start, length);
162  }
163
164  /**
165   * Change the global LED data.
166   *
167   * @param start start, in LEDs
168   * @param data the new data
169   */
170  public static void setGlobalData(int start, byte[] data) {
171    AddressableLEDDataJNI.setData(start, data);
172  }
173
174  /** Reset all simulation data for this LED object. */
175  public void resetData() {
176    AddressableLEDDataJNI.resetData(m_channel);
177  }
178}