mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[hal,wpilib] Use new DS available API from mrccomm (#8302)
Instead of just having a max count for joystick values, there's an available mask of values. This is because in the future we're expecting there to be holes in the list of available buttons and axes. This updates everything to support that scenario. Also, Joystick buttons, axes, and POVs all now start at 0 instead of 1.
This commit is contained in:
@@ -133,142 +133,39 @@ Java_edu_wpi_first_hal_DriverStationJNI_nativeGetAllianceStation
|
||||
return static_cast<jint>(allianceStation);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getJoystickAxesRaw
|
||||
* Signature: (B[S)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxesRaw
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jshortArray axesRawArray)
|
||||
{
|
||||
HAL_JoystickAxes axes;
|
||||
HAL_GetJoystickAxes(joystickNum, &axes);
|
||||
|
||||
jsize javaSize = env->GetArrayLength(axesRawArray);
|
||||
if (axes.count > javaSize) {
|
||||
ThrowIllegalArgumentException(
|
||||
env,
|
||||
fmt::format("Native array size larger then passed in java array "
|
||||
"size\nNative Size: {} Java Size: {}",
|
||||
static_cast<int>(axes.count), static_cast<int>(javaSize)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
env->SetShortArrayRegion(axesRawArray, 0, axes.count, axes.raw);
|
||||
|
||||
return axes.count;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getJoystickAxes
|
||||
* Signature: (B[F)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxes
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jfloatArray axesArray)
|
||||
{
|
||||
HAL_JoystickAxes axes;
|
||||
HAL_GetJoystickAxes(joystickNum, &axes);
|
||||
|
||||
jsize javaSize = env->GetArrayLength(axesArray);
|
||||
if (axes.count > javaSize) {
|
||||
ThrowIllegalArgumentException(
|
||||
env,
|
||||
fmt::format("Native array size larger then passed in java array "
|
||||
"size\nNative Size: {} Java Size: {}",
|
||||
static_cast<int>(axes.count), static_cast<int>(javaSize)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
env->SetFloatArrayRegion(axesArray, 0, axes.count, axes.axes);
|
||||
|
||||
return axes.count;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getJoystickPOVs
|
||||
* Signature: (B[B)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickPOVs
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jbyteArray povsArray)
|
||||
{
|
||||
HAL_JoystickPOVs povs;
|
||||
HAL_GetJoystickPOVs(joystickNum, &povs);
|
||||
|
||||
jsize javaSize = env->GetArrayLength(povsArray);
|
||||
if (povs.count > javaSize) {
|
||||
ThrowIllegalArgumentException(
|
||||
env,
|
||||
fmt::format("Native array size larger then passed in java array "
|
||||
"size\nNative Size: {} Java Size: {}",
|
||||
static_cast<int>(povs.count), static_cast<int>(javaSize)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(povsArray, 0, povs.count,
|
||||
reinterpret_cast<const jbyte*>(povs.povs));
|
||||
|
||||
return povs.count;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getAllJoystickData
|
||||
* Signature: ([F[S[B[J)V
|
||||
* Signature: (I[F[S[B[J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getAllJoystickData
|
||||
(JNIEnv* env, jclass cls, jfloatArray axesArray, jshortArray rawAxesArray,
|
||||
jbyteArray povsArray, jlongArray buttonsAndMetadataArray)
|
||||
(JNIEnv* env, jclass cls, jint stick, jfloatArray axesArray,
|
||||
jshortArray rawAxesArray, jbyteArray povsArray,
|
||||
jlongArray buttonsAndMetadataArray)
|
||||
{
|
||||
HAL_JoystickAxes axes[HAL_kMaxJoysticks];
|
||||
HAL_JoystickPOVs povs[HAL_kMaxJoysticks];
|
||||
HAL_JoystickButtons buttons[HAL_kMaxJoysticks];
|
||||
HAL_JoystickAxes axes;
|
||||
HAL_JoystickPOVs povs;
|
||||
HAL_JoystickButtons buttons;
|
||||
|
||||
HAL_GetAllJoystickData(axes, povs, buttons);
|
||||
HAL_GetAllJoystickData(stick, &axes, &povs, &buttons);
|
||||
|
||||
CriticalJSpan<jfloat> jAxes(env, axesArray);
|
||||
CriticalJSpan<jshort> jRawAxes(env, rawAxesArray);
|
||||
CriticalJSpan<jbyte> jPovs(env, povsArray);
|
||||
CriticalJSpan<jlong> jButtons(env, buttonsAndMetadataArray);
|
||||
|
||||
static_assert(sizeof(jAxes[0]) == sizeof(axes[0].axes[0]));
|
||||
static_assert(sizeof(jRawAxes[0]) == sizeof(axes[0].raw[0]));
|
||||
static_assert(sizeof(jPovs[0]) == sizeof(povs[0].povs[0]));
|
||||
static_assert(sizeof(jAxes[0]) == sizeof(axes.axes[0]));
|
||||
static_assert(sizeof(jRawAxes[0]) == sizeof(axes.raw[0]));
|
||||
static_assert(sizeof(jPovs[0]) == sizeof(povs.povs[0]));
|
||||
|
||||
for (size_t i = 0; i < HAL_kMaxJoysticks; i++) {
|
||||
std::memcpy(&jAxes[i * HAL_kMaxJoystickAxes], axes[i].axes,
|
||||
sizeof(axes[i].axes));
|
||||
std::memcpy(&jRawAxes[i * HAL_kMaxJoystickAxes], axes[i].raw,
|
||||
sizeof(axes[i].raw));
|
||||
std::memcpy(&jPovs[i * HAL_kMaxJoystickPOVs], povs[i].povs,
|
||||
sizeof(povs[i].povs));
|
||||
jButtons[i * 4] = axes[i].count;
|
||||
jButtons[(i * 4) + 1] = povs[i].count;
|
||||
jButtons[(i * 4) + 2] = buttons[i].count;
|
||||
jButtons[(i * 4) + 3] = buttons[i].buttons;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getJoystickButtons
|
||||
* Signature: (BLjava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickButtons
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jobject count)
|
||||
{
|
||||
HAL_JoystickButtons joystickButtons;
|
||||
HAL_GetJoystickButtons(joystickNum, &joystickButtons);
|
||||
jbyte* countPtr =
|
||||
reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(count));
|
||||
*countPtr = joystickButtons.count;
|
||||
return joystickButtons.buttons;
|
||||
std::memcpy(&jAxes[0], axes.axes, sizeof(axes.axes));
|
||||
std::memcpy(&jRawAxes[0], axes.raw, sizeof(axes.raw));
|
||||
std::memcpy(&jPovs[0], povs.povs, sizeof(povs.povs));
|
||||
jButtons[0] = axes.available;
|
||||
jButtons[1] = povs.available;
|
||||
jButtons[2] = buttons.available;
|
||||
jButtons[3] = buttons.buttons;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -323,18 +220,6 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickName
|
||||
return str;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getJoystickAxisType
|
||||
* Signature: (BB)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxisType
|
||||
(JNIEnv*, jclass, jbyte joystickNum, jbyte axis)
|
||||
{
|
||||
return HAL_GetJoystickAxisType(joystickNum, axis);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_DriverStationJNI
|
||||
* Method: getMatchTime
|
||||
|
||||
@@ -427,11 +427,12 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setMatchTime
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickAxes
|
||||
* Signature: (B[F)V
|
||||
* Signature: (B[FS)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxes
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jfloatArray axesArray)
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jfloatArray axesArray,
|
||||
jshort availableAxes)
|
||||
{
|
||||
HAL_JoystickAxes axes;
|
||||
{
|
||||
@@ -440,7 +441,7 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxes
|
||||
auto arraySize = arrayRef.size();
|
||||
int maxCount =
|
||||
arraySize < HAL_kMaxJoystickAxes ? arraySize : HAL_kMaxJoystickAxes;
|
||||
axes.count = maxCount;
|
||||
axes.available = availableAxes;
|
||||
for (int i = 0; i < maxCount; i++) {
|
||||
axes.axes[i] = arrayRef[i];
|
||||
}
|
||||
@@ -452,11 +453,12 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxes
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickPOVs
|
||||
* Signature: (B[B)V
|
||||
* Signature: (B[BS)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVs
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jbyteArray povsArray)
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jbyteArray povsArray,
|
||||
jshort availablePovs)
|
||||
{
|
||||
HAL_JoystickPOVs povs;
|
||||
{
|
||||
@@ -465,7 +467,7 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVs
|
||||
auto arraySize = arrayRef.size();
|
||||
int maxCount =
|
||||
arraySize < HAL_kMaxJoystickPOVs ? arraySize : HAL_kMaxJoystickPOVs;
|
||||
povs.count = maxCount;
|
||||
povs.available = availablePovs;
|
||||
for (int i = 0; i < maxCount; i++) {
|
||||
povs.povs[i] = static_cast<HAL_JoystickPOV>(arrayRef[i]);
|
||||
}
|
||||
@@ -477,17 +479,15 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVs
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickButtons
|
||||
* Signature: (BII)V
|
||||
* Signature: (BJJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickButtons
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jint buttons, jint count)
|
||||
(JNIEnv* env, jclass, jbyte joystickNum, jlong buttons,
|
||||
jlong availableButtons)
|
||||
{
|
||||
if (count > 32) {
|
||||
count = 32;
|
||||
}
|
||||
HAL_JoystickButtons joystickButtons;
|
||||
joystickButtons.count = count;
|
||||
joystickButtons.available = availableButtons;
|
||||
joystickButtons.buttons = buttons;
|
||||
HALSIM_SetJoystickButtons(joystickNum, &joystickButtons);
|
||||
}
|
||||
@@ -654,49 +654,49 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOV
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickButtonsValue
|
||||
* Signature: (II)V
|
||||
* Signature: (IJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickButtonsValue
|
||||
(JNIEnv*, jclass, jint stick, jint buttons)
|
||||
(JNIEnv*, jclass, jint stick, jlong buttons)
|
||||
{
|
||||
HALSIM_SetJoystickButtonsValue(stick, buttons);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickAxisCount
|
||||
* Method: setJoystickAxesAvailable
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxisCount
|
||||
(JNIEnv*, jclass, jint stick, jint count)
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxesAvailable
|
||||
(JNIEnv*, jclass, jint stick, jint available)
|
||||
{
|
||||
HALSIM_SetJoystickAxisCount(stick, count);
|
||||
HALSIM_SetJoystickAxesAvailable(stick, available);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickPOVCount
|
||||
* Method: setJoystickPOVsAvailable
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVCount
|
||||
(JNIEnv*, jclass, jint stick, jint count)
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVsAvailable
|
||||
(JNIEnv*, jclass, jint stick, jint available)
|
||||
{
|
||||
HALSIM_SetJoystickPOVCount(stick, count);
|
||||
HALSIM_SetJoystickPOVsAvailable(stick, available);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickButtonCount
|
||||
* Signature: (II)V
|
||||
* Method: setJoystickButtonsAvailable
|
||||
* Signature: (IJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickButtonCount
|
||||
(JNIEnv*, jclass, jint stick, jint count)
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickButtonsAvailable
|
||||
(JNIEnv*, jclass, jint stick, jlong available)
|
||||
{
|
||||
HALSIM_SetJoystickButtonCount(stick, count);
|
||||
HALSIM_SetJoystickButtonsAvailable(stick, available);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -737,18 +737,6 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickName
|
||||
HALSIM_SetJoystickName(stick, &str);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setJoystickAxisType
|
||||
* Signature: (III)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxisType
|
||||
(JNIEnv*, jclass, jint stick, jint axis, jint type)
|
||||
{
|
||||
HALSIM_SetJoystickAxisType(stick, axis, type);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
|
||||
* Method: setGameSpecificMessage
|
||||
|
||||
Reference in New Issue
Block a user