mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiutil] jni_util: Add JSpan and CriticalJSpan (#5554)
These replace JArrayRef et al and support statically sized arrays similar to std::span.
This commit is contained in:
@@ -69,12 +69,11 @@ Java_edu_wpi_first_hal_AddressableLEDJNI_setData
|
||||
(JNIEnv* env, jclass, jint handle, jbyteArray arr)
|
||||
{
|
||||
int32_t status = 0;
|
||||
JByteArrayRef jArrRef{env, arr};
|
||||
auto arrRef = jArrRef.array();
|
||||
JSpan<const jbyte> jArrRef{env, arr};
|
||||
HAL_WriteAddressableLEDData(
|
||||
static_cast<HAL_AddressableLEDHandle>(handle),
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(arrRef.data()),
|
||||
arrRef.size() / 4, &status);
|
||||
reinterpret_cast<const HAL_AddressableLEDData*>(jArrRef.data()),
|
||||
jArrRef.size() / 4, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
|
||||
@@ -71,11 +71,10 @@ Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacket
|
||||
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId)
|
||||
{
|
||||
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
||||
JByteArrayRef arr{env, data};
|
||||
auto arrRef = arr.array();
|
||||
JSpan<const jbyte> arr{env, data};
|
||||
int32_t status = 0;
|
||||
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arrRef.data()),
|
||||
arrRef.size(), apiId, &status);
|
||||
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arr.data()),
|
||||
arr.size(), apiId, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
@@ -90,12 +89,11 @@ Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketRepeating
|
||||
jint timeoutMs)
|
||||
{
|
||||
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
||||
JByteArrayRef arr{env, data};
|
||||
auto arrRef = arr.array();
|
||||
JSpan<const jbyte> arr{env, data};
|
||||
int32_t status = 0;
|
||||
HAL_WriteCANPacketRepeating(halHandle,
|
||||
reinterpret_cast<const uint8_t*>(arrRef.data()),
|
||||
arrRef.size(), apiId, timeoutMs, &status);
|
||||
reinterpret_cast<const uint8_t*>(arr.data()),
|
||||
arr.size(), apiId, timeoutMs, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
@@ -124,11 +122,10 @@ Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketNoThrow
|
||||
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId)
|
||||
{
|
||||
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
||||
JByteArrayRef arr{env, data};
|
||||
auto arrRef = arr.array();
|
||||
JSpan<const jbyte> arr{env, data};
|
||||
int32_t status = 0;
|
||||
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arrRef.data()),
|
||||
arrRef.size(), apiId, &status);
|
||||
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arr.data()),
|
||||
arr.size(), apiId, &status);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -143,12 +140,11 @@ Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketRepeatingNoThrow
|
||||
jint timeoutMs)
|
||||
{
|
||||
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
||||
JByteArrayRef arr{env, data};
|
||||
auto arrRef = arr.array();
|
||||
JSpan<const jbyte> arr{env, data};
|
||||
int32_t status = 0;
|
||||
HAL_WriteCANPacketRepeating(halHandle,
|
||||
reinterpret_cast<const uint8_t*>(arrRef.data()),
|
||||
arrRef.size(), apiId, timeoutMs, &status);
|
||||
reinterpret_cast<const uint8_t*>(arr.data()),
|
||||
arr.size(), apiId, timeoutMs, &status);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,11 @@ JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_can_CANJNI_FRCNetCommCANSessionMuxSendMessage
|
||||
(JNIEnv* env, jclass, jint messageID, jbyteArray data, jint periodMs)
|
||||
{
|
||||
JByteArrayRef dataArray{env, data};
|
||||
JSpan<const jbyte> dataArray{env, data};
|
||||
|
||||
const uint8_t* dataBuffer =
|
||||
reinterpret_cast<const uint8_t*>(dataArray.array().data());
|
||||
uint8_t dataSize = dataArray.array().size();
|
||||
reinterpret_cast<const uint8_t*>(dataArray.data());
|
||||
uint8_t dataSize = dataArray.size();
|
||||
|
||||
int32_t status = 0;
|
||||
HAL_CAN_SendMessage(messageID, dataBuffer, dataSize, periodMs, &status);
|
||||
|
||||
@@ -5,12 +5,15 @@
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "HALUtil.h"
|
||||
#include "edu_wpi_first_hal_DMAJNI.h"
|
||||
#include "hal/DMA.h"
|
||||
#include "hal/handles/HandlesInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
using namespace wpi::java;
|
||||
|
||||
namespace hal {
|
||||
bool GetEncoderBaseHandle(HAL_EncoderHandle handle,
|
||||
@@ -312,20 +315,17 @@ Java_edu_wpi_first_hal_DMAJNI_readDMA
|
||||
env->SetIntArrayRegion(buf, 0, dmaSample.captureSize,
|
||||
reinterpret_cast<jint*>(dmaSample.readBuffer));
|
||||
|
||||
int32_t* nativeArr =
|
||||
static_cast<int32_t*>(env->GetPrimitiveArrayCritical(store, nullptr));
|
||||
CriticalJSpan<jint> nativeArr{env, store};
|
||||
|
||||
std::copy_n(
|
||||
dmaSample.channelOffsets,
|
||||
sizeof(dmaSample.channelOffsets) / sizeof(dmaSample.channelOffsets[0]),
|
||||
nativeArr);
|
||||
nativeArr.data());
|
||||
nativeArr[22] = static_cast<int32_t>(dmaSample.captureSize);
|
||||
nativeArr[23] = static_cast<int32_t>(dmaSample.triggerChannels);
|
||||
nativeArr[24] = remaining;
|
||||
nativeArr[25] = readStatus;
|
||||
|
||||
env->ReleasePrimitiveArrayCritical(store, nativeArr, JNI_ABORT);
|
||||
|
||||
return dmaSample.timeStamp;
|
||||
}
|
||||
|
||||
|
||||
@@ -251,10 +251,10 @@ Java_edu_wpi_first_hal_DriverStationJNI_getAllJoystickData
|
||||
|
||||
HAL_GetAllJoystickData(axes, povs, buttons);
|
||||
|
||||
CriticalJFloatArrayRef jAxes(env, axesArray);
|
||||
CriticalJByteArrayRef jRawAxes(env, rawAxesArray);
|
||||
CriticalJShortArrayRef jPovs(env, povsArray);
|
||||
CriticalJLongArrayRef jButtons(env, buttonsAndMetadataArray);
|
||||
CriticalJSpan<jfloat> jAxes(env, axesArray);
|
||||
CriticalJSpan<jbyte> jRawAxes(env, rawAxesArray);
|
||||
CriticalJSpan<jshort> 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]));
|
||||
|
||||
@@ -79,7 +79,7 @@ Java_edu_wpi_first_hal_I2CJNI_i2CTransactionB
|
||||
jint returnValue =
|
||||
HAL_TransactionI2C(static_cast<HAL_I2CPort>(port), address,
|
||||
reinterpret_cast<const uint8_t*>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
JSpan<const jbyte>(env, dataToSend).data()),
|
||||
sendSize, recvBuf.data(), receiveSize);
|
||||
env->SetByteArrayRegion(dataReceived, 0, receiveSize,
|
||||
reinterpret_cast<const jbyte*>(recvBuf.data()));
|
||||
@@ -120,7 +120,7 @@ Java_edu_wpi_first_hal_I2CJNI_i2CWriteB
|
||||
jint returnValue =
|
||||
HAL_WriteI2C(static_cast<HAL_I2CPort>(port), address,
|
||||
reinterpret_cast<const uint8_t*>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
JSpan<const jbyte>(env, dataToSend).data()),
|
||||
sendSize);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ Java_edu_wpi_first_hal_SPIJNI_spiTransactionB
|
||||
jint retVal =
|
||||
HAL_TransactionSPI(static_cast<HAL_SPIPort>(port),
|
||||
reinterpret_cast<const uint8_t*>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
JSpan<const jbyte>(env, dataToSend).data()),
|
||||
recvBuf.data(), size);
|
||||
env->SetByteArrayRegion(dataReceived, 0, size,
|
||||
reinterpret_cast<const jbyte*>(recvBuf.data()));
|
||||
@@ -131,7 +131,7 @@ Java_edu_wpi_first_hal_SPIJNI_spiWriteB
|
||||
{
|
||||
jint retVal = HAL_WriteSPI(static_cast<HAL_SPIPort>(port),
|
||||
reinterpret_cast<const uint8_t*>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
JSpan<const jbyte>(env, dataToSend).data()),
|
||||
size);
|
||||
return retVal;
|
||||
}
|
||||
@@ -356,12 +356,11 @@ JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_SPIJNI_spiSetAutoTransmitData
|
||||
(JNIEnv* env, jclass, jint port, jbyteArray dataToSend, jint zeroSize)
|
||||
{
|
||||
JByteArrayRef jarr(env, dataToSend);
|
||||
JSpan<const jbyte> jarr(env, dataToSend);
|
||||
int32_t status = 0;
|
||||
HAL_SetSPIAutoTransmitData(
|
||||
static_cast<HAL_SPIPort>(port),
|
||||
reinterpret_cast<const uint8_t*>(jarr.array().data()),
|
||||
jarr.array().size(), zeroSize, &status);
|
||||
HAL_SetSPIAutoTransmitData(static_cast<HAL_SPIPort>(port),
|
||||
reinterpret_cast<const uint8_t*>(jarr.data()),
|
||||
jarr.size(), zeroSize, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
|
||||
@@ -261,11 +261,10 @@ Java_edu_wpi_first_hal_SerialPortJNI_serialWrite
|
||||
(JNIEnv* env, jclass, jint handle, jbyteArray dataToSend, jint size)
|
||||
{
|
||||
int32_t status = 0;
|
||||
jint retVal =
|
||||
HAL_WriteSerial(static_cast<HAL_SerialPortHandle>(handle),
|
||||
reinterpret_cast<const char*>(
|
||||
JByteArrayRef(env, dataToSend).array().data()),
|
||||
size, &status);
|
||||
jint retVal = HAL_WriteSerial(
|
||||
static_cast<HAL_SerialPortHandle>(handle),
|
||||
reinterpret_cast<const char*>(JSpan<const jbyte>(env, dataToSend).data()),
|
||||
size, &status);
|
||||
CheckStatus(env, status);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ Java_edu_wpi_first_hal_SimDeviceJNI_createSimValueEnumDouble
|
||||
}
|
||||
return HAL_CreateSimValueEnumDouble(
|
||||
device, JStringRef{env, name}.c_str(), direction, len, carr.data(),
|
||||
JDoubleArrayRef{env, optionValues}.array().data(), initialValue);
|
||||
JSpan<const jdouble>{env, optionValues}.data(), initialValue);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -269,7 +269,7 @@ JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_AddressableLEDDataJNI_setData
|
||||
(JNIEnv* env, jclass, jint index, jbyteArray arr)
|
||||
{
|
||||
JByteArrayRef jArrRef{env, arr};
|
||||
JSpan<const jbyte> jArrRef{env, arr};
|
||||
auto arrRef = jArrRef.array();
|
||||
HALSIM_SetAddressableLEDData(
|
||||
index, reinterpret_cast<const HAL_AddressableLEDData*>(arrRef.data()),
|
||||
|
||||
@@ -436,7 +436,7 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxes
|
||||
{
|
||||
HAL_JoystickAxes axes;
|
||||
{
|
||||
wpi::java::JFloatArrayRef jArrayRef(env, axesArray);
|
||||
JSpan<const jfloat> jArrayRef(env, axesArray);
|
||||
auto arrayRef = jArrayRef.array();
|
||||
auto arraySize = arrayRef.size();
|
||||
int maxCount =
|
||||
@@ -461,7 +461,7 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVs
|
||||
{
|
||||
HAL_JoystickPOVs povs;
|
||||
{
|
||||
wpi::java::JShortArrayRef jArrayRef(env, povsArray);
|
||||
JSpan<const jshort> jArrayRef(env, povsArray);
|
||||
auto arrayRef = jArrayRef.array();
|
||||
auto arraySize = arrayRef.size();
|
||||
int maxCount =
|
||||
|
||||
Reference in New Issue
Block a user