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 b753de3b21..5dd7dfa344 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/SimDeviceJNI.java @@ -81,6 +81,36 @@ public class SimDeviceJNI extends JNIWrapper { initialValue.getNativeDouble()); } + /** + * Creates an int value on a simulated device. + * + *
Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. + * + * @param device simulated device handle + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated value handle + */ + public static int createSimValueInt(int device, String name, int direction, int initialValue) { + return createSimValueNative(device, name, direction, HALValue.kInt, initialValue, 0.0); + } + + /** + * Creates a long value on a simulated device. + * + *
Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. + * + * @param device simulated device handle + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated value handle + */ + public static int createSimValueLong(int device, String name, int direction, long initialValue) { + return createSimValueNative(device, name, direction, HALValue.kLong, initialValue, 0.0); + } + /** * Creates a double value on a simulated device. * @@ -221,6 +251,22 @@ public class SimDeviceJNI extends JNIWrapper { */ public static native HALValue getSimValue(int handle); + /** + * Gets a simulated value (int). + * + * @param handle simulated value handle + * @return The current value + */ + public static native int getSimValueInt(int handle); + + /** + * Gets a simulated value (long). + * + * @param handle simulated value handle + * @return The current value + */ + public static native long getSimValueLong(int handle); + /** * Gets a simulated value (double). * @@ -257,6 +303,26 @@ public class SimDeviceJNI extends JNIWrapper { setSimValueNative(handle, value.getType(), value.getNativeLong(), value.getNativeDouble()); } + /** + * Sets a simulated value (int). + * + * @param handle simulated value handle + * @param value the value to set + */ + public static void setSimValueInt(int handle, int value) { + setSimValueNative(handle, HALValue.kInt, value, 0.0); + } + + /** + * Sets a simulated value (long). + * + * @param handle simulated value handle + * @param value the value to set + */ + public static void setSimValueLong(int handle, long value) { + setSimValueNative(handle, HALValue.kLong, value, 0.0); + } + /** * Sets a simulated value (double). * diff --git a/hal/src/main/java/edu/wpi/first/hal/SimInt.java b/hal/src/main/java/edu/wpi/first/hal/SimInt.java new file mode 100644 index 0000000000..445667b9ab --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/SimInt.java @@ -0,0 +1,35 @@ +// 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. + +package edu.wpi.first.hal; + +/** A wrapper around a simulator int value handle. */ +public class SimInt extends SimValue { + /** + * Wraps a simulated value handle as returned by SimDeviceJNI.createSimValueInt(). + * + * @param handle simulated value handle + */ + public SimInt(int handle) { + super(handle); + } + + /** + * Gets the simulated value. + * + * @return The current value + */ + public int get() { + return SimDeviceJNI.getSimValueInt(m_handle); + } + + /** + * Sets the simulated value. + * + * @param value the value to set + */ + public void set(int value) { + SimDeviceJNI.setSimValueInt(m_handle, value); + } +} diff --git a/hal/src/main/java/edu/wpi/first/hal/SimLong.java b/hal/src/main/java/edu/wpi/first/hal/SimLong.java new file mode 100644 index 0000000000..830e4f34c8 --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/SimLong.java @@ -0,0 +1,35 @@ +// 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. + +package edu.wpi.first.hal; + +/** A wrapper around a simulator long value handle. */ +public class SimLong extends SimValue { + /** + * Wraps a simulated value handle as returned by SimDeviceJNI.createSimValueLong(). + * + * @param handle simulated value handle + */ + public SimLong(int handle) { + super(handle); + } + + /** + * Gets the simulated value. + * + * @return The current value + */ + public long get() { + return SimDeviceJNI.getSimValueLong(m_handle); + } + + /** + * Sets the simulated value. + * + * @param value the value to set + */ + public void set(long value) { + SimDeviceJNI.setSimValueLong(m_handle, value); + } +} diff --git a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp index 68f6e24f6f..fed774b811 100644 --- a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp +++ b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp @@ -153,6 +153,30 @@ Java_edu_wpi_first_hal_SimDeviceJNI_getSimValue return hal::CreateHALValue(env, HAL_GetSimValue(handle)); } +/* + * Class: edu_wpi_first_hal_SimDeviceJNI + * Method: getSimValueInt + * Signature: (I)I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_SimDeviceJNI_getSimValueInt + (JNIEnv*, jclass, jint handle) +{ + return HAL_GetSimValueInt(handle); +} + +/* + * Class: edu_wpi_first_hal_SimDeviceJNI + * Method: getSimValueLong + * Signature: (I)J + */ +JNIEXPORT jlong JNICALL +Java_edu_wpi_first_hal_SimDeviceJNI_getSimValueLong + (JNIEnv*, jclass, jint handle) +{ + return HAL_GetSimValueLong(handle); +} + /* * Class: edu_wpi_first_hal_SimDeviceJNI * Method: getSimValueDouble diff --git a/hal/src/main/native/include/hal/SimDevice.h b/hal/src/main/native/include/hal/SimDevice.h index b2e92f9182..4a79d3a3f9 100644 --- a/hal/src/main/native/include/hal/SimDevice.h +++ b/hal/src/main/native/include/hal/SimDevice.h @@ -196,6 +196,30 @@ inline HAL_Value HAL_GetSimValue(HAL_SimValueHandle handle) { } // extern "C++" #endif +/** + * Gets a simulated value (int). + * + * @param handle simulated value handle + * @return The current value + */ +inline int32_t HAL_GetSimValueInt(HAL_SimValueHandle handle) { + struct HAL_Value v; + HAL_GetSimValue(handle, &v); + return v.type == HAL_INT ? v.data.v_int : 0; +} + +/** + * Gets a simulated value (long). + * + * @param handle simulated value handle + * @return The current value + */ +inline int64_t HAL_GetSimValueLong(HAL_SimValueHandle handle) { + struct HAL_Value v; + HAL_GetSimValue(handle, &v); + return v.type == HAL_LONG ? v.data.v_long : 0; +} + /** * Gets a simulated value (double). * @@ -248,6 +272,28 @@ inline void HAL_SetSimValue(HAL_SimValueHandle handle, const HAL_Value& value) { } // extern "C++" #endif +/** + * Sets a simulated value (int). + * + * @param handle simulated value handle + * @param value the value to set + */ +inline void HAL_SetSimValueInt(HAL_SimValueHandle handle, int value) { + struct HAL_Value v = HAL_MakeInt(value); + HAL_SetSimValue(handle, &v); +} + +/** + * Sets a simulated value (long). + * + * @param handle simulated value handle + * @param value the value to set + */ +inline void HAL_SetSimValueLong(HAL_SimValueHandle handle, int64_t value) { + struct HAL_Value v = HAL_MakeLong(value); + HAL_SetSimValue(handle, &v); +} + /** * Sets a simulated value (double). * @@ -342,6 +388,74 @@ class SimValue { HAL_SimValueHandle m_handle = HAL_kInvalidHandle; }; +/** + * C++ wrapper around a HAL simulator int value handle. + */ +class SimInt : public SimValue { + public: + /** + * Default constructor that results in an "empty" object that is false in + * a boolean context. + */ + SimInt() = default; + + /** + * Wraps a simulated value handle as returned by HAL_CreateSimValueInt(). + * + * @param handle simulated value handle + */ + /*implicit*/ SimInt(HAL_SimValueHandle val) // NOLINT + : SimValue(val) {} + + /** + * Gets the simulated value. + * + * @return The current value + */ + int32_t Get() const { return HAL_GetSimValueInt(m_handle); } + + /** + * Sets the simulated value. + * + * @param value the value to set + */ + void Set(int32_t value) { HAL_SetSimValueInt(m_handle, value); } +}; + +/** + * C++ wrapper around a HAL simulator long value handle. + */ +class SimLong : public SimValue { + public: + /** + * Default constructor that results in an "empty" object that is false in + * a boolean context. + */ + SimLong() = default; + + /** + * Wraps a simulated value handle as returned by HAL_CreateSimValueLong(). + * + * @param handle simulated value handle + */ + /*implicit*/ SimLong(HAL_SimValueHandle val) // NOLINT + : SimValue(val) {} + + /** + * Gets the simulated value. + * + * @return The current value + */ + int64_t Get() const { return HAL_GetSimValueLong(m_handle); } + + /** + * Sets the simulated value. + * + * @param value the value to set + */ + void Set(int64_t value) { HAL_SetSimValueLong(m_handle, value); } +}; + /** * C++ wrapper around a HAL simulator double value handle. */