mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[hal, wpilib] Update Addressable LED support (#8100)
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
using namespace hal;
|
||||
using namespace wpi::java;
|
||||
|
||||
static_assert(sizeof(jbyte) * 4 == sizeof(HAL_AddressableLEDData));
|
||||
static_assert(sizeof(jbyte) * 3 == sizeof(HAL_AddressableLEDData));
|
||||
|
||||
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_RGB ==
|
||||
HAL_ALED_RGB);
|
||||
@@ -36,12 +36,12 @@ extern "C" {
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_initialize
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
(JNIEnv* env, jclass, jint channel)
|
||||
{
|
||||
int32_t status = 0;
|
||||
auto ret = HAL_InitializeAddressableLED(
|
||||
static_cast<HAL_DigitalHandle>(handle), &status);
|
||||
CheckStatus(env, status);
|
||||
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
|
||||
auto ret = HAL_InitializeAddressableLED(channel, stack.c_str(), &status);
|
||||
CheckStatusForceThrow(env, status);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -61,17 +61,16 @@ Java_edu_wpi_first_hal_AddressableLEDJNI_free
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: setColorOrder
|
||||
* Method: setStart
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setColorOrder
|
||||
(JNIEnv* env, jclass, jint handle, jint colorOrder)
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setStart
|
||||
(JNIEnv* env, jclass, jint handle, jint start)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetAddressableLEDColorOrder(
|
||||
static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
static_cast<HAL_AddressableLEDColorOrder>(colorOrder), &status);
|
||||
HAL_SetAddressableLEDStart(static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
start, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
@@ -93,80 +92,79 @@ Java_edu_wpi_first_hal_AddressableLEDJNI_setLength
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: setData
|
||||
* Signature: (I[B)V
|
||||
* Signature: (II[BII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setData
|
||||
(JNIEnv* env, jclass, jint handle, jbyteArray arr)
|
||||
(JNIEnv* env, jclass, jint outStart, jint colorOrder, jbyteArray data,
|
||||
jint start, jint len)
|
||||
{
|
||||
if (!data) {
|
||||
ThrowNullPointerException(env, "data is null");
|
||||
return;
|
||||
}
|
||||
if (start < 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "start must be >= 0");
|
||||
return;
|
||||
}
|
||||
if (len < 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "len must be >= 0");
|
||||
return;
|
||||
}
|
||||
JSpan<const jbyte> cdata{env, data};
|
||||
if (static_cast<unsigned int>(start + len) > cdata.size()) {
|
||||
ThrowIndexOutOfBoundsException(
|
||||
env, "start + len must be smaller than array length");
|
||||
return;
|
||||
}
|
||||
if ((len % 3) != 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "len must be a multiple of 3");
|
||||
return;
|
||||
}
|
||||
auto rawdata = cdata.uarray().subspan(start, len);
|
||||
int32_t status = 0;
|
||||
JSpan<const jbyte> jArrRef{env, arr};
|
||||
HAL_WriteAddressableLEDData(
|
||||
static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(jArrRef.data()),
|
||||
jArrRef.size() / 4, &status);
|
||||
CheckStatus(env, status);
|
||||
HAL_SetAddressableLEDData(
|
||||
outStart, rawdata.size() / 3,
|
||||
static_cast<HAL_AddressableLEDColorOrder>(colorOrder),
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(rawdata.data()), &status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: setBitTiming
|
||||
* Signature: (IIIII)V
|
||||
* Method: setDataFromBuffer
|
||||
* Signature: (IILjava/lang/Object;II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setBitTiming
|
||||
(JNIEnv* env, jclass, jint handle, jint highTime0, jint lowTime0,
|
||||
jint highTime1, jint lowTime1)
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setDataFromBuffer
|
||||
(JNIEnv* env, jclass, jint outStart, jint colorOrder, jobject data,
|
||||
jint start, jint len)
|
||||
{
|
||||
if (!data) {
|
||||
ThrowNullPointerException(env, "data is null");
|
||||
return;
|
||||
}
|
||||
if (start < 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "start must be >= 0");
|
||||
return;
|
||||
}
|
||||
if (len < 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "len must be >= 0");
|
||||
return;
|
||||
}
|
||||
JSpan<const jbyte> cdata{env, data, static_cast<size_t>(start + len)};
|
||||
if (!cdata) {
|
||||
ThrowIllegalArgumentException(env, "data must be a native ByteBuffer");
|
||||
return;
|
||||
}
|
||||
if ((len % 3) != 0) {
|
||||
ThrowIndexOutOfBoundsException(env, "len must be a multiple of 3");
|
||||
return;
|
||||
}
|
||||
auto rawdata = cdata.uarray().subspan(start, len);
|
||||
int32_t status = 0;
|
||||
HAL_SetAddressableLEDBitTiming(static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
highTime0, lowTime0, highTime1, lowTime1,
|
||||
&status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: setSyncTime
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_setSyncTime
|
||||
(JNIEnv* env, jclass, jint handle, jint syncTime)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetAddressableLEDSyncTime(static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
syncTime, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: start
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_start
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_StartAddressableLEDOutput(static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
&status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
||||
* Method: stop
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_AddressableLEDJNI_stop
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_StopAddressableLEDOutput(static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
&status);
|
||||
CheckStatus(env, status);
|
||||
HAL_SetAddressableLEDData(
|
||||
outStart, rawdata.size() / 3,
|
||||
static_cast<HAL_AddressableLEDColorOrder>(colorOrder),
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(rawdata.data()), &status);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -40,6 +40,7 @@ static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_SYSTEMCORE ==
|
||||
|
||||
static JavaVM* jvm = nullptr;
|
||||
static JException illegalArgExCls;
|
||||
static JException indexOobExCls;
|
||||
static JException boundaryExCls;
|
||||
static JException allocationExCls;
|
||||
static JException halHandleExCls;
|
||||
@@ -68,6 +69,7 @@ static const JClassInit classes[] = {
|
||||
|
||||
static const JExceptionInit exceptions[] = {
|
||||
{"java/lang/IllegalArgumentException", &illegalArgExCls},
|
||||
{"java/lang/IndexOutOfBoundsException", &indexOobExCls},
|
||||
{"edu/wpi/first/hal/util/BoundaryException", &boundaryExCls},
|
||||
{"edu/wpi/first/hal/util/AllocationException", &allocationExCls},
|
||||
{"edu/wpi/first/hal/util/HalHandleException", &halHandleExCls},
|
||||
@@ -159,6 +161,10 @@ void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg) {
|
||||
illegalArgExCls.Throw(env, msg);
|
||||
}
|
||||
|
||||
void ThrowIndexOutOfBoundsException(JNIEnv* env, std::string_view msg) {
|
||||
indexOobExCls.Throw(env, msg);
|
||||
}
|
||||
|
||||
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
|
||||
double upper) {
|
||||
static jmethodID getMessage = nullptr;
|
||||
|
||||
@@ -46,6 +46,7 @@ void ThrowNullPointerException(JNIEnv* env, std::string_view msg);
|
||||
void ThrowCANStreamOverflowException(JNIEnv* env, jobjectArray messages,
|
||||
jint length);
|
||||
void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg);
|
||||
void ThrowIndexOutOfBoundsException(JNIEnv* env, std::string_view msg);
|
||||
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
|
||||
double upper);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "edu_wpi_first_hal_simulation_AddressableLEDDataJNI.h"
|
||||
#include "hal/simulation/AddressableLEDData.h"
|
||||
|
||||
static_assert(sizeof(jbyte) * 4 == sizeof(HAL_AddressableLEDData));
|
||||
static_assert(sizeof(jbyte) * 3 == sizeof(HAL_AddressableLEDData));
|
||||
|
||||
using namespace hal;
|
||||
using namespace wpi::java;
|
||||
@@ -71,53 +71,52 @@ Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setInitialized
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: registerOutputPortCallback
|
||||
* Method: registerStartCallback
|
||||
* Signature: (ILjava/lang/Object;Z)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_registerOutputPortCallback
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_registerStartCallback
|
||||
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
|
||||
{
|
||||
return sim::AllocateCallback(
|
||||
env, index, callback, initialNotify,
|
||||
&HALSIM_RegisterAddressableLEDOutputPortCallback);
|
||||
return sim::AllocateCallback(env, index, callback, initialNotify,
|
||||
&HALSIM_RegisterAddressableLEDStartCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: cancelOutputPortCallback
|
||||
* Method: cancelStartCallback
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_cancelOutputPortCallback
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_cancelStartCallback
|
||||
(JNIEnv* env, jclass, jint index, jint handle)
|
||||
{
|
||||
return sim::FreeCallback(env, handle, index,
|
||||
&HALSIM_CancelAddressableLEDOutputPortCallback);
|
||||
&HALSIM_CancelAddressableLEDStartCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: getOutputPort
|
||||
* Method: getStart
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_getOutputPort
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_getStart
|
||||
(JNIEnv*, jclass, jint index)
|
||||
{
|
||||
return HALSIM_GetAddressableLEDOutputPort(index);
|
||||
return HALSIM_GetAddressableLEDStart(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: setOutputPort
|
||||
* Method: setStart
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setOutputPort
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setStart
|
||||
(JNIEnv*, jclass, jint index, jint value)
|
||||
{
|
||||
HALSIM_SetAddressableLEDOutputPort(index, value);
|
||||
HALSIM_SetAddressableLEDStart(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -170,96 +169,50 @@ Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setLength
|
||||
HALSIM_SetAddressableLEDLength(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: registerRunningCallback
|
||||
* Signature: (ILjava/lang/Object;Z)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_registerRunningCallback
|
||||
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
|
||||
{
|
||||
return sim::AllocateCallback(env, index, callback, initialNotify,
|
||||
&HALSIM_RegisterAddressableLEDRunningCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: cancelRunningCallback
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_cancelRunningCallback
|
||||
(JNIEnv* env, jclass, jint index, jint handle)
|
||||
{
|
||||
return sim::FreeCallback(env, handle, index,
|
||||
&HALSIM_CancelAddressableLEDRunningCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: getRunning
|
||||
* Signature: (I)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_getRunning
|
||||
(JNIEnv*, jclass, jint index)
|
||||
{
|
||||
return HALSIM_GetAddressableLEDRunning(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: setRunning
|
||||
* Signature: (IZ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setRunning
|
||||
(JNIEnv*, jclass, jint index, jboolean value)
|
||||
{
|
||||
HALSIM_SetAddressableLEDRunning(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: registerDataCallback
|
||||
* Signature: (ILjava/lang/Object;)I
|
||||
* Signature: (Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_registerDataCallback
|
||||
(JNIEnv* env, jclass, jint index, jobject callback)
|
||||
(JNIEnv* env, jclass, jobject callback)
|
||||
{
|
||||
return sim::AllocateConstBufferCallback(
|
||||
env, index, callback, &HALSIM_RegisterAddressableLEDDataCallback);
|
||||
env, -1, callback,
|
||||
[](int32_t, HAL_ConstBufferCallback callback, void* param) {
|
||||
return HALSIM_RegisterAddressableLEDDataCallback(callback, param);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: cancelDataCallback
|
||||
* Signature: (II)V
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_cancelDataCallback
|
||||
(JNIEnv* env, jclass, jint index, jint handle)
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
sim::FreeConstBufferCallback(env, handle, index,
|
||||
&HALSIM_CancelAddressableLEDDataCallback);
|
||||
sim::FreeConstBufferCallback(env, handle, -1, [](int32_t, int32_t uid) {
|
||||
HALSIM_CancelAddressableLEDDataCallback(uid);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: getData
|
||||
* Signature: (I)[B
|
||||
* Signature: (II)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_getData
|
||||
(JNIEnv* env, jclass, jint index)
|
||||
(JNIEnv* env, jclass, jint start, jint length)
|
||||
{
|
||||
auto data =
|
||||
std::make_unique<HAL_AddressableLEDData[]>(HAL_kAddressableLEDMaxLength);
|
||||
int32_t length = HALSIM_GetAddressableLEDData(index, data.get());
|
||||
length = HALSIM_GetAddressableLEDData(start, length, data.get());
|
||||
return MakeJByteArray(
|
||||
env, std::span(reinterpret_cast<jbyte*>(data.get()), length * 4));
|
||||
env, std::span(reinterpret_cast<jbyte*>(data.get()), length * 3));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -269,13 +222,13 @@ Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_getData
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setData
|
||||
(JNIEnv* env, jclass, jint index, jbyteArray arr)
|
||||
(JNIEnv* env, jclass, jint start, jbyteArray arr)
|
||||
{
|
||||
JSpan<const jbyte> jArrRef{env, arr};
|
||||
auto arrRef = jArrRef.array();
|
||||
HALSIM_SetAddressableLEDData(
|
||||
index, reinterpret_cast<const HAL_AddressableLEDData*>(arrRef.data()),
|
||||
arrRef.size() / 4);
|
||||
start, arrRef.size() / 3,
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(arrRef.data()));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -290,16 +243,4 @@ Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_resetData
|
||||
HALSIM_ResetAddressableLEDData(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_AddressableLEDDataJNI
|
||||
* Method: findForChannel
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_findForChannel
|
||||
(JNIEnv*, jclass, jint channel)
|
||||
{
|
||||
return HALSIM_FindAddressableLEDForChannel(channel);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
Reference in New Issue
Block a user