2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
package edu.wpi.first.wpilibj.simulation;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
import edu.wpi.first.hal.simulation.AddressableLEDDataJNI;
|
|
|
|
|
import edu.wpi.first.hal.simulation.ConstBufferCallback;
|
|
|
|
|
import edu.wpi.first.hal.simulation.NotifyCallback;
|
2020-07-04 10:10:43 -07:00
|
|
|
import edu.wpi.first.wpilibj.AddressableLED;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Class to control a simulated addressable LED. */
|
2019-11-17 15:05:56 -08:00
|
|
|
public class AddressableLEDSim {
|
2025-07-21 21:52:10 -07:00
|
|
|
private final int m_channel;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs an addressable LED for a specific channel.
|
|
|
|
|
*
|
|
|
|
|
* @param channel output channel
|
|
|
|
|
*/
|
|
|
|
|
public AddressableLEDSim(int channel) {
|
|
|
|
|
m_channel = channel;
|
2020-07-04 10:10:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs from an AddressableLED object.
|
|
|
|
|
*
|
|
|
|
|
* @param addressableLED AddressableLED to simulate
|
|
|
|
|
*/
|
|
|
|
|
public AddressableLEDSim(AddressableLED addressableLED) {
|
2025-07-21 21:52:10 -07:00
|
|
|
m_channel = addressableLED.getChannel();
|
2020-07-04 10:10:43 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the Initialized property.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the Initialized property is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
|
2025-07-21 21:52:10 -07:00
|
|
|
int uid = AddressableLEDDataJNI.registerInitializedCallback(m_channel, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelInitializedCallback);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Check if initialized.
|
|
|
|
|
*
|
|
|
|
|
* @return true if initialized
|
|
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public boolean getInitialized() {
|
2025-07-21 21:52:10 -07:00
|
|
|
return AddressableLEDDataJNI.getInitialized(m_channel);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the Initialized value of the LED strip.
|
|
|
|
|
*
|
|
|
|
|
* @param initialized the new value
|
|
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public void setInitialized(boolean initialized) {
|
2025-07-21 21:52:10 -07:00
|
|
|
AddressableLEDDataJNI.setInitialized(m_channel, initialized);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Register a callback on the start.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param callback the callback that will be called whenever the start is changed
|
2021-01-11 21:55:45 -08:00
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public CallbackStore registerStartCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = AddressableLEDDataJNI.registerStartCallback(m_channel, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelStartCallback);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Get the start.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @return the start
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public int getStart() {
|
|
|
|
|
return AddressableLEDDataJNI.getStart(m_channel);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Change the start.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param start the new start
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public void setStart(int start) {
|
|
|
|
|
AddressableLEDDataJNI.setStart(m_channel, start);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the length.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the length is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public CallbackStore registerLengthCallback(NotifyCallback callback, boolean initialNotify) {
|
2025-07-21 21:52:10 -07:00
|
|
|
int uid = AddressableLEDDataJNI.registerLengthCallback(m_channel, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_channel, uid, AddressableLEDDataJNI::cancelLengthCallback);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the length of the LED strip.
|
|
|
|
|
*
|
|
|
|
|
* @return the length
|
|
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public int getLength() {
|
2025-07-21 21:52:10 -07:00
|
|
|
return AddressableLEDDataJNI.getLength(m_channel);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the length of the LED strip.
|
|
|
|
|
*
|
|
|
|
|
* @param length the new value
|
|
|
|
|
*/
|
2019-11-17 15:05:56 -08:00
|
|
|
public void setLength(int length) {
|
2025-07-21 21:52:10 -07:00
|
|
|
AddressableLEDDataJNI.setLength(m_channel, length);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Register a callback on the LED data.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param callback the callback that will be called whenever the LED data is changed
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public static CallbackStore registerDataCallback(ConstBufferCallback callback) {
|
|
|
|
|
int uid = AddressableLEDDataJNI.registerDataCallback(callback);
|
|
|
|
|
return new CallbackStore(uid, AddressableLEDDataJNI::cancelDataCallback);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Get the LED data.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @return the LED data
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public byte[] getData() {
|
|
|
|
|
return getGlobalData(getStart(), getLength());
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Change the LED data.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param data the new data
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public void setData(byte[] data) {
|
|
|
|
|
setGlobalData(getStart(), data);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Get the global LED data.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param start start, in LEDs
|
|
|
|
|
* @param length length, in LEDs
|
2021-01-11 21:55:45 -08:00
|
|
|
* @return the LED data
|
|
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public static byte[] getGlobalData(int start, int length) {
|
|
|
|
|
return AddressableLEDDataJNI.getData(start, length);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-07-21 21:52:10 -07:00
|
|
|
* Change the global LED data.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-07-21 21:52:10 -07:00
|
|
|
* @param start start, in LEDs
|
2021-01-11 21:55:45 -08:00
|
|
|
* @param data the new data
|
|
|
|
|
*/
|
2025-07-21 21:52:10 -07:00
|
|
|
public static void setGlobalData(int start, byte[] data) {
|
|
|
|
|
AddressableLEDDataJNI.setData(start, data);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/** Reset all simulation data for this LED object. */
|
2019-11-17 15:05:56 -08:00
|
|
|
public void resetData() {
|
2025-07-21 21:52:10 -07:00
|
|
|
AddressableLEDDataJNI.resetData(m_channel);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
}
|