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