diff --git a/hal/src/main/java/edu/wpi/first/hal/SimDevice.java b/hal/src/main/java/edu/wpi/first/hal/SimDevice.java index db3a587e18..85d438fb34 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SimDevice.java +++ b/hal/src/main/java/edu/wpi/first/hal/SimDevice.java @@ -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. * diff --git a/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java b/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java index 8723cd30ed..37ad79dd50 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java @@ -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); diff --git a/hal/src/main/native/athena/SimDevice.cpp b/hal/src/main/native/athena/SimDevice.cpp index 3a15f46056..03b35f7b46 100644 --- a/hal/src/main/native/athena/SimDevice.cpp +++ b/hal/src/main/native/athena/SimDevice.cpp @@ -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) { diff --git a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp index f681e71009..494b732c02 100644 --- a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp +++ b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp @@ -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 diff --git a/hal/src/main/native/include/hal/SimDevice.h b/hal/src/main/native/include/hal/SimDevice.h index f90cb9bb5b..d51b37416f 100644 --- a/hal/src/main/native/include/hal/SimDevice.h +++ b/hal/src/main/native/include/hal/SimDevice.h @@ -9,6 +9,7 @@ #ifdef __cplusplus #include #include +#include #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. * diff --git a/hal/src/main/native/sim/SimDevice.cpp b/hal/src/main/native/sim/SimDevice.cpp index b6c637cedc..48b755e6aa 100644 --- a/hal/src/main/native/sim/SimDevice.cpp +++ b/hal/src/main/native/sim/SimDevice.cpp @@ -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) { diff --git a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp index 34fd1e3640..70e43e72dd 100644 --- a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp @@ -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); } diff --git a/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h b/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h index 0a4e4d8352..6e16116e1d 100644 --- a/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h +++ b/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h @@ -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. * diff --git a/wpilibc/src/test/native/cpp/simulation/SimDeviceSimTest.cpp b/wpilibc/src/test/native/cpp/simulation/SimDeviceSimTest.cpp index 5ef267510d..7e015e48c1 100644 --- a/wpilibc/src/test/native/cpp/simulation/SimDeviceSimTest.cpp +++ b/wpilibc/src/test/native/cpp/simulation/SimDeviceSimTest.cpp @@ -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) { diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java index bca1f6f3b6..a64e2f33af 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java @@ -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. * diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/SimDeviceSimTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/SimDeviceSimTest.java index 1daea9627f..62a24d1d75 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/SimDeviceSimTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/SimDeviceSimTest.java @@ -27,6 +27,8 @@ class SimDeviceSimTest { assertFalse(simBool.get()); simBool.set(true); assertTrue(devBool.get()); + + assertEquals(dev.getName(), "test"); } }