From 3f1672e89f5c8e47eeeaa978c1c9700089b40d7b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 20 Jan 2021 20:42:39 -0800 Subject: [PATCH] [hal] Add SimDevice createInt() and createLong() (#3110) --- .../java/edu/wpi/first/hal/SimDevice.java | 36 ++++++++++ hal/src/main/native/include/hal/SimDevice.h | 71 +++++++++++++++++++ 2 files changed, 107 insertions(+) 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 9bc879f317..67c39fe351 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SimDevice.java +++ b/hal/src/main/java/edu/wpi/first/hal/SimDevice.java @@ -140,6 +140,42 @@ public class SimDevice implements AutoCloseable { return new SimValue(handle); } + /** + * Creates an int value on the simulated device. + * + *

Returns null if not in simulation. + * + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated double value object + */ + public SimInt createInt(String name, Direction direction, int initialValue) { + int handle = SimDeviceJNI.createSimValueInt(m_handle, name, direction.m_value, initialValue); + if (handle <= 0) { + return null; + } + return new SimInt(handle); + } + + /** + * Creates a long value on the simulated device. + * + *

Returns null if not in simulation. + * + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated double value object + */ + public SimLong createLong(String name, Direction direction, long initialValue) { + int handle = SimDeviceJNI.createSimValueLong(m_handle, name, direction.m_value, initialValue); + if (handle <= 0) { + return null; + } + return new SimLong(handle); + } + /** * Creates a double value on the simulated device. * diff --git a/hal/src/main/native/include/hal/SimDevice.h b/hal/src/main/native/include/hal/SimDevice.h index 339bb7df61..ae319da455 100644 --- a/hal/src/main/native/include/hal/SimDevice.h +++ b/hal/src/main/native/include/hal/SimDevice.h @@ -94,6 +94,46 @@ inline HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device, } // extern "C++" #endif +/** + * 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 + */ +inline HAL_SimValueHandle HAL_CreateSimValueInt(HAL_SimDeviceHandle device, + const char* name, + int32_t direction, + int32_t initialValue) { + struct HAL_Value v = HAL_MakeInt(initialValue); + return HAL_CreateSimValue(device, name, direction, &v); +} + +/** + * 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 + */ +inline HAL_SimValueHandle HAL_CreateSimValueLong(HAL_SimDeviceHandle device, + const char* name, + int32_t direction, + int64_t initialValue) { + struct HAL_Value v = HAL_MakeLong(initialValue); + return HAL_CreateSimValue(device, name, direction, &v); +} + /** * Creates a double value on a simulated device. * @@ -708,6 +748,37 @@ class SimDevice { return HAL_CreateSimValue(m_handle, name, direction, &initialValue); } + /** + * Creates an int value on the simulated device. + * + * If not in simulation, results in an "empty" object that evaluates to false + * in a boolean context. + * + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated double value object + */ + SimInt CreateInt(const char* name, int32_t direction, int32_t initialValue) { + return HAL_CreateSimValueInt(m_handle, name, direction, initialValue); + } + + /** + * Creates a long value on the simulated device. + * + * If not in simulation, results in an "empty" object that evaluates to false + * in a boolean context. + * + * @param name value name + * @param direction input/output/bidir (from perspective of user code) + * @param initialValue initial value + * @return simulated double value object + */ + SimLong CreateLong(const char* name, int32_t direction, + int64_t initialValue) { + return HAL_CreateSimValueLong(m_handle, name, direction, initialValue); + } + /** * Creates a double value on the simulated device. *