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 16:39:38 -08:00
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
|
|
|
|
#include "HALUtil.h"
|
|
|
|
|
#include "edu_wpi_first_hal_AddressableLEDJNI.h"
|
|
|
|
|
#include "hal/AddressableLED.h"
|
|
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
using namespace hal;
|
2019-11-17 16:39:38 -08:00
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
2019-11-17 15:05:56 -08:00
|
|
|
static_assert(sizeof(jbyte) * 4 == sizeof(HAL_AddressableLEDData));
|
|
|
|
|
|
[hal, wpilib] AddressableLED: add support for other color orders (#7102)
Many LED strips use different color order (GRB in particular is common).
This makes the change at the HAL level. This solves 2 problems; first, no code needs to change in the high level drivers, which was challenging for C++, and second, simulation will behave properly as no conversion is needed. The HAL will accept an array of data objects in the same order no matter what the selected output order is, and will convert before sending it to the FPGA for output.
To accomplish this, NEON bulk load/interleave instructions are utilized. The low level implementation (load, store, and alignment functions) come from the Simd Library. The high level implementations are inspired by the image conversion functions in the simd library, but have diverged significantly.
Much of the implementation uses templates and inlined functions rather than runtime parameters; This is a trade off between the size of the generated code and the amount of function calls done at runtime. Currently, the entire conversion operation is inlined.
2025-02-07 15:36:41 -05:00
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_RGB ==
|
|
|
|
|
HAL_ALED_RGB);
|
|
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_RBG ==
|
|
|
|
|
HAL_ALED_RBG);
|
|
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_BGR ==
|
|
|
|
|
HAL_ALED_BGR);
|
|
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_BRG ==
|
|
|
|
|
HAL_ALED_BRG);
|
|
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_GBR ==
|
|
|
|
|
HAL_ALED_GBR);
|
|
|
|
|
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_GRB ==
|
|
|
|
|
HAL_ALED_GRB);
|
|
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
extern "C" {
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: initialize
|
|
|
|
|
* Signature: (I)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_initialize
|
|
|
|
|
(JNIEnv* env, jclass, jint handle)
|
|
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto ret = HAL_InitializeAddressableLED(
|
|
|
|
|
static_cast<HAL_DigitalHandle>(handle), &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: free
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_free
|
|
|
|
|
(JNIEnv* env, jclass, jint handle)
|
|
|
|
|
{
|
2024-09-07 13:58:15 -04:00
|
|
|
if (handle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_FreeAddressableLED(static_cast<HAL_AddressableLEDHandle>(handle));
|
|
|
|
|
}
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
[hal, wpilib] AddressableLED: add support for other color orders (#7102)
Many LED strips use different color order (GRB in particular is common).
This makes the change at the HAL level. This solves 2 problems; first, no code needs to change in the high level drivers, which was challenging for C++, and second, simulation will behave properly as no conversion is needed. The HAL will accept an array of data objects in the same order no matter what the selected output order is, and will convert before sending it to the FPGA for output.
To accomplish this, NEON bulk load/interleave instructions are utilized. The low level implementation (load, store, and alignment functions) come from the Simd Library. The high level implementations are inspired by the image conversion functions in the simd library, but have diverged significantly.
Much of the implementation uses templates and inlined functions rather than runtime parameters; This is a trade off between the size of the generated code and the amount of function calls done at runtime. Currently, the entire conversion operation is inlined.
2025-02-07 15:36:41 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: setColorOrder
|
|
|
|
|
* Signature: (II)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_setColorOrder
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jint colorOrder)
|
|
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDColorOrder(
|
|
|
|
|
static_cast<HAL_AddressableLEDHandle>(handle),
|
|
|
|
|
static_cast<HAL_AddressableLEDColorOrder>(colorOrder), &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: setLength
|
|
|
|
|
* Signature: (II)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_setLength
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jint length)
|
|
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDLength(static_cast<HAL_AddressableLEDHandle>(handle),
|
|
|
|
|
length, &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: setData
|
|
|
|
|
* Signature: (I[B)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_setData
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jbyteArray arr)
|
|
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
2023-08-24 00:02:56 -07:00
|
|
|
JSpan<const jbyte> jArrRef{env, arr};
|
2019-11-17 16:39:38 -08:00
|
|
|
HAL_WriteAddressableLEDData(
|
|
|
|
|
static_cast<HAL_AddressableLEDHandle>(handle),
|
2023-08-24 00:02:56 -07:00
|
|
|
reinterpret_cast<const HAL_AddressableLEDData*>(jArrRef.data()),
|
|
|
|
|
jArrRef.size() / 4, &status);
|
2019-11-17 16:39:38 -08:00
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_AddressableLEDJNI
|
|
|
|
|
* Method: setBitTiming
|
|
|
|
|
* Signature: (IIIII)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_AddressableLEDJNI_setBitTiming
|
2023-04-28 20:54:58 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jint highTime0, jint lowTime0,
|
|
|
|
|
jint highTime1, jint lowTime1)
|
2019-11-17 16:39:38 -08:00
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDBitTiming(static_cast<HAL_AddressableLEDHandle>(handle),
|
2023-04-28 20:54:58 -07:00
|
|
|
highTime0, lowTime0, highTime1, lowTime1,
|
2019-11-17 16:39:38 -08:00
|
|
|
&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);
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|