mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal] Add handle constructor and name getters for sim devices (#4925)
This commit is contained in:
@@ -106,6 +106,15 @@ public class SimDevice implements AutoCloseable {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the simulated device.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return SimDeviceJNI.getSimDeviceName(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a value on the simulated device.
|
||||
*
|
||||
|
||||
@@ -33,6 +33,14 @@ public class SimDeviceJNI extends JNIWrapper {
|
||||
*/
|
||||
public static native void freeSimDevice(int handle);
|
||||
|
||||
/**
|
||||
* Get the name of a simulated device.
|
||||
*
|
||||
* @param handle simulated device handle
|
||||
* @return name of the simulated device
|
||||
*/
|
||||
public static native String getSimDeviceName(int handle);
|
||||
|
||||
private static native int createSimValueNative(
|
||||
int device, String name, int direction, int type, long value1, double value2);
|
||||
|
||||
|
||||
@@ -12,6 +12,10 @@ HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) {
|
||||
|
||||
void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) {}
|
||||
|
||||
const char* HAL_GetSimDeviceName(HAL_SimDeviceHandle handle) {
|
||||
return "";
|
||||
}
|
||||
|
||||
HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device,
|
||||
const char* name, int32_t direction,
|
||||
const struct HAL_Value* initialValue) {
|
||||
|
||||
@@ -63,6 +63,18 @@ Java_edu_wpi_first_hal_SimDeviceJNI_freeSimDevice
|
||||
HAL_FreeSimDevice(handle);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_SimDeviceJNI
|
||||
* Method: getSimDeviceName
|
||||
* Signature: (I)Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_edu_wpi_first_hal_SimDeviceJNI_getSimDeviceName
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
return MakeJString(env, HAL_GetSimDeviceName(handle));
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_SimDeviceJNI
|
||||
* Method: createSimValueNative
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#ifdef __cplusplus
|
||||
#include <initializer_list>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
#include "hal/Types.h"
|
||||
@@ -66,6 +67,14 @@ HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name);
|
||||
*/
|
||||
void HAL_FreeSimDevice(HAL_SimDeviceHandle handle);
|
||||
|
||||
/**
|
||||
* Get the name of a simulated device
|
||||
*
|
||||
* @param handle simulated device handle
|
||||
* @return name of the simulated device
|
||||
*/
|
||||
const char* HAL_GetSimDeviceName(HAL_SimDeviceHandle handle);
|
||||
|
||||
/**
|
||||
* Creates a value on a simulated device.
|
||||
*
|
||||
@@ -731,6 +740,15 @@ class SimDevice {
|
||||
*/
|
||||
operator HAL_SimDeviceHandle() const { return m_handle; } // NOLINT
|
||||
|
||||
/**
|
||||
* Get the name of the simulated device.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
std::string GetName() const {
|
||||
return std::string(HAL_GetSimDeviceName(m_handle));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a value on the simulated device.
|
||||
*
|
||||
|
||||
@@ -26,6 +26,10 @@ void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) {
|
||||
SimSimDeviceData->FreeDevice(handle);
|
||||
}
|
||||
|
||||
const char* HAL_GetSimDeviceName(HAL_SimDeviceHandle handle) {
|
||||
return SimSimDeviceData->GetDeviceName(handle);
|
||||
}
|
||||
|
||||
HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device,
|
||||
const char* name, int32_t direction,
|
||||
const struct HAL_Value* initialValue) {
|
||||
|
||||
@@ -27,6 +27,12 @@ SimDeviceSim::SimDeviceSim(const char* name, int index, int channel) {
|
||||
fmt::format("{}[{},{}]", name, index, channel).c_str());
|
||||
}
|
||||
|
||||
SimDeviceSim::SimDeviceSim(HAL_SimDeviceHandle handle) : m_handle(handle) {}
|
||||
|
||||
std::string SimDeviceSim::GetName() const {
|
||||
return std::string(HALSIM_GetSimDeviceName(m_handle));
|
||||
}
|
||||
|
||||
hal::SimValue SimDeviceSim::GetValue(const char* name) const {
|
||||
return HALSIM_GetSimValueHandle(m_handle, name);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,20 @@ class SimDeviceSim {
|
||||
*/
|
||||
SimDeviceSim(const char* name, int index, int channel);
|
||||
|
||||
/**
|
||||
* Constructs a SimDeviceSim.
|
||||
*
|
||||
* @param handle the low level handle for the corresponding SimDevice.
|
||||
*/
|
||||
explicit SimDeviceSim(HAL_SimDeviceHandle handle);
|
||||
|
||||
/**
|
||||
* Get the name of this object.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
std::string GetName() const;
|
||||
|
||||
/**
|
||||
* Get the property object with the given name.
|
||||
*
|
||||
|
||||
@@ -20,6 +20,8 @@ TEST(SimDeviceSimTest, Basic) {
|
||||
EXPECT_FALSE(simBool.Get());
|
||||
simBool.Set(true);
|
||||
EXPECT_TRUE(devBool.Get());
|
||||
|
||||
EXPECT_EQ(sim.GetName(), "test");
|
||||
}
|
||||
|
||||
TEST(SimDeviceSimTest, EnumerateDevices) {
|
||||
|
||||
@@ -24,7 +24,7 @@ public class SimDeviceSim {
|
||||
* @param name name of the SimDevice
|
||||
*/
|
||||
public SimDeviceSim(String name) {
|
||||
m_handle = SimDeviceDataJNI.getSimDeviceHandle(name);
|
||||
this(SimDeviceDataJNI.getSimDeviceHandle(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,6 +48,24 @@ public class SimDeviceSim {
|
||||
this(name + "[" + index + "," + channel + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a SimDeviceSim.
|
||||
*
|
||||
* @param handle the low level handle for the corresponding SimDevice
|
||||
*/
|
||||
public SimDeviceSim(int handle) {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of this object.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
public String getName() {
|
||||
return SimDeviceDataJNI.getSimDeviceName(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the property object with the given name.
|
||||
*
|
||||
|
||||
@@ -27,6 +27,8 @@ class SimDeviceSimTest {
|
||||
assertFalse(simBool.get());
|
||||
simBool.set(true);
|
||||
assertTrue(devBool.get());
|
||||
|
||||
assertEquals(dev.getName(), "test");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user