001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.hal; 006 007/** 008 * SimDevice JNI Functions. 009 * 010 * @see "hal/SimDevice.h" 011 */ 012public class SimDeviceJNI extends JNIWrapper { 013 /** Input to user code from the simulator. */ 014 public static final int kInput = 0; 015 016 /** Output from user code to the simulator. */ 017 public static final int kOutput = 1; 018 019 /** Bidirectional between user code and simulator. */ 020 public static final int kBidir = 2; 021 022 /** 023 * Creates a simulated device. 024 * 025 * <p>The device name must be unique. 0 is returned if the device name already exists. If multiple 026 * instances of the same device are desired, recommend appending the instance/unique identifier in 027 * brackets to the base name, e.g. "device[1]". 028 * 029 * <p>Using a device name of the form "Type:Name" will create a WebSockets node with a type value 030 * of "Type" and a device value of "Name" 031 * 032 * <p>0 is returned if not in simulation. 033 * 034 * @param name device name 035 * @return simulated device handle 036 */ 037 public static native int createSimDevice(String name); 038 039 /** 040 * Frees a simulated device. 041 * 042 * <p>This also allows the same device name to be used again. This also frees all the simulated 043 * values created on the device. 044 * 045 * @param handle simulated device handle 046 */ 047 public static native void freeSimDevice(int handle); 048 049 /** 050 * Get the name of a simulated device. 051 * 052 * @param handle simulated device handle 053 * @return name of the simulated device 054 */ 055 public static native String getSimDeviceName(int handle); 056 057 private static native int createSimValueNative( 058 int device, String name, int direction, int type, long value1, double value2); 059 060 /** 061 * Creates a value on a simulated device. 062 * 063 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 064 * 065 * @param device simulated device handle 066 * @param name value name 067 * @param direction input/output/bidir (from perspective of user code) 068 * @param initialValue initial value 069 * @return simulated value handle 070 */ 071 public static int createSimValue(int device, String name, int direction, HALValue initialValue) { 072 return createSimValueNative( 073 device, 074 name, 075 direction, 076 initialValue.getType(), 077 initialValue.getNativeLong(), 078 initialValue.getNativeDouble()); 079 } 080 081 /** 082 * Creates an int value on a simulated device. 083 * 084 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 085 * 086 * @param device simulated device handle 087 * @param name value name 088 * @param direction input/output/bidir (from perspective of user code) 089 * @param initialValue initial value 090 * @return simulated value handle 091 */ 092 public static int createSimValueInt(int device, String name, int direction, int initialValue) { 093 return createSimValueNative(device, name, direction, HALValue.kInt, initialValue, 0.0); 094 } 095 096 /** 097 * Creates a long value on a simulated device. 098 * 099 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 100 * 101 * @param device simulated device handle 102 * @param name value name 103 * @param direction input/output/bidir (from perspective of user code) 104 * @param initialValue initial value 105 * @return simulated value handle 106 */ 107 public static int createSimValueLong(int device, String name, int direction, long initialValue) { 108 return createSimValueNative(device, name, direction, HALValue.kLong, initialValue, 0.0); 109 } 110 111 /** 112 * Creates a double value on a simulated device. 113 * 114 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 115 * 116 * @param device simulated device handle 117 * @param name value name 118 * @param direction input/output/bidir (from perspective of user code) 119 * @param initialValue initial value 120 * @return simulated value handle 121 */ 122 public static int createSimValueDouble( 123 int device, String name, int direction, double initialValue) { 124 return createSimValueNative(device, name, direction, HALValue.kDouble, 0, initialValue); 125 } 126 127 /** 128 * Creates an enumerated value on a simulated device. 129 * 130 * <p>Enumerated values are always in the range 0 to numOptions-1. 131 * 132 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 133 * 134 * @param device simulated device handle 135 * @param name value name 136 * @param direction input/output/bidir (from perspective of user code) 137 * @param options array of option descriptions 138 * @param initialValue initial value (selection) 139 * @return simulated value handle 140 */ 141 public static native int createSimValueEnum( 142 int device, String name, int direction, String[] options, int initialValue); 143 144 /** 145 * Creates an enumerated value on a simulated device with double values. 146 * 147 * <p>Enumerated values are always in the range 0 to numOptions-1. 148 * 149 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 150 * 151 * @param device simulated device handle 152 * @param name value name 153 * @param direction input/output/bidir (from perspective of user code) 154 * @param options array of option descriptions 155 * @param optionValues array of option values (must be the same size as options) 156 * @param initialValue initial value (selection) 157 * @return simulated value handle 158 */ 159 public static native int createSimValueEnumDouble( 160 int device, 161 String name, 162 int direction, 163 String[] options, 164 double[] optionValues, 165 int initialValue); 166 167 /** 168 * Creates a boolean value on a simulated device. 169 * 170 * <p>Returns 0 if not in simulation; this can be used to avoid calls to Set/Get functions. 171 * 172 * @param device simulated device handle 173 * @param name value name 174 * @param direction input/output/bidir (from perspective of user code) 175 * @param initialValue initial value 176 * @return simulated value handle 177 */ 178 public static int createSimValueBoolean( 179 int device, String name, int direction, boolean initialValue) { 180 return createSimValueNative( 181 device, name, direction, HALValue.kBoolean, initialValue ? 1 : 0, 0.0); 182 } 183 184 /** 185 * Gets a simulated value. 186 * 187 * @param handle simulated value handle 188 * @return The current value 189 */ 190 public static native HALValue getSimValue(int handle); 191 192 /** 193 * Gets a simulated value (int). 194 * 195 * @param handle simulated value handle 196 * @return The current value 197 */ 198 public static native int getSimValueInt(int handle); 199 200 /** 201 * Gets a simulated value (long). 202 * 203 * @param handle simulated value handle 204 * @return The current value 205 */ 206 public static native long getSimValueLong(int handle); 207 208 /** 209 * Gets a simulated value (double). 210 * 211 * @param handle simulated value handle 212 * @return The current value 213 */ 214 public static native double getSimValueDouble(int handle); 215 216 /** 217 * Gets a simulated value (enum). 218 * 219 * @param handle simulated value handle 220 * @return The current value 221 */ 222 public static native int getSimValueEnum(int handle); 223 224 /** 225 * Gets a simulated value (boolean). 226 * 227 * @param handle simulated value handle 228 * @return The current value 229 */ 230 public static native boolean getSimValueBoolean(int handle); 231 232 private static native void setSimValueNative(int handle, int type, long value1, double value2); 233 234 /** 235 * Sets a simulated value. 236 * 237 * @param handle simulated value handle 238 * @param value the value to set 239 */ 240 public static void setSimValue(int handle, HALValue value) { 241 setSimValueNative(handle, value.getType(), value.getNativeLong(), value.getNativeDouble()); 242 } 243 244 /** 245 * Sets a simulated value (int). 246 * 247 * @param handle simulated value handle 248 * @param value the value to set 249 */ 250 public static void setSimValueInt(int handle, int value) { 251 setSimValueNative(handle, HALValue.kInt, value, 0.0); 252 } 253 254 /** 255 * Sets a simulated value (long). 256 * 257 * @param handle simulated value handle 258 * @param value the value to set 259 */ 260 public static void setSimValueLong(int handle, long value) { 261 setSimValueNative(handle, HALValue.kLong, value, 0.0); 262 } 263 264 /** 265 * Sets a simulated value (double). 266 * 267 * @param handle simulated value handle 268 * @param value the value to set 269 */ 270 public static void setSimValueDouble(int handle, double value) { 271 setSimValueNative(handle, HALValue.kDouble, 0, value); 272 } 273 274 /** 275 * Sets a simulated value (enum). 276 * 277 * @param handle simulated value handle 278 * @param value the value to set 279 */ 280 public static void setSimValueEnum(int handle, int value) { 281 setSimValueNative(handle, HALValue.kEnum, value, 0.0); 282 } 283 284 /** 285 * Sets a simulated value (boolean). 286 * 287 * @param handle simulated value handle 288 * @param value the value to set 289 */ 290 public static void setSimValueBoolean(int handle, boolean value) { 291 setSimValueNative(handle, HALValue.kBoolean, value ? 1 : 0, 0.0); 292 } 293 294 /** 295 * Resets a simulated double or integral value to 0. Has no effect on other value types. Use this 296 * instead of Set(0) for resetting incremental sensor values like encoder counts or gyro 297 * accumulated angle to ensure correct behavior in a distributed system (e.g. WebSockets). 298 * 299 * @param handle simulated value handle 300 */ 301 public static native void resetSimValue(int handle); 302 303 /** Utility class. */ 304 private SimDeviceJNI() {} 305}