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 007import java.nio.ByteBuffer; 008 009/** 010 * Driver Station JNI Functions. 011 * 012 * @see "hal/DriverStation.h" 013 */ 014public class DriverStationJNI extends JNIWrapper { 015 /** 016 * Sets the program starting flag in the DS. 017 * 018 * <p>This is what changes the DS to showing robot code ready. 019 * 020 * @see "HAL_ObserveUserProgramStarting" 021 */ 022 public static native void observeUserProgramStarting(); 023 024 /** 025 * Sets the disabled flag in the DS. 026 * 027 * <p>This is used for the DS to ensure the robot is properly responding to its state request. 028 * Ensure this gets called about every 50ms, or the robot will be disabled by the DS. 029 * 030 * @see "HAL_ObserveUserProgramDisabled" 031 */ 032 public static native void observeUserProgramDisabled(); 033 034 /** 035 * Sets the autonomous enabled flag in the DS. 036 * 037 * <p>This is used for the DS to ensure the robot is properly responding to its state request. 038 * Ensure this gets called about every 50ms, or the robot will be disabled by the DS. 039 * 040 * @see "HAL_ObserveUserProgramAutonomous" 041 */ 042 public static native void observeUserProgramAutonomous(); 043 044 /** 045 * Sets the teleoperated enabled flag in the DS. 046 * 047 * <p>This is used for the DS to ensure the robot is properly responding to its state request. 048 * Ensure this gets called about every 50ms, or the robot will be disabled by the DS. 049 * 050 * @see "HAL_ObserveUserProgramTeleop" 051 */ 052 public static native void observeUserProgramTeleop(); 053 054 /** 055 * Sets the test mode flag in the DS. 056 * 057 * <p>This is used for the DS to ensure the robot is properly responding to its state request. 058 * Ensure this gets called about every 50ms, or the robot will be disabled by the DS. 059 * 060 * @see "HAL_ObserveUserProgramTest" 061 */ 062 public static native void observeUserProgramTest(); 063 064 /** 065 * Gets the current control word of the driver station. 066 * 067 * <p>The control word contains the robot state. 068 * 069 * @return the control word 070 * @see "HAL_GetControlWord" 071 * @see getControlWord for a version easier to parse 072 */ 073 public static native int nativeGetControlWord(); 074 075 /** 076 * Gets the current control word of the driver station. 077 * 078 * <p>The control work contains the robot state. 079 * 080 * @param controlWord the ControlWord to update 081 * @see "HAL_GetControlWord" 082 */ 083 public static void getControlWord(ControlWord controlWord) { 084 int word = nativeGetControlWord(); 085 controlWord.update( 086 (word & 1) != 0, 087 ((word >> 1) & 1) != 0, 088 ((word >> 2) & 1) != 0, 089 ((word >> 3) & 1) != 0, 090 ((word >> 4) & 1) != 0, 091 ((word >> 5) & 1) != 0); 092 } 093 094 /** 095 * Gets the current alliance station ID. 096 * 097 * @return the alliance station ID int 098 * @see "HAL_GetAllianceStation" 099 */ 100 private static native int nativeGetAllianceStation(); 101 102 /** Unknown Alliance Station ID. */ 103 public static final int kUnknownAllianceStation = 0; 104 105 /** Red Alliance Station 1 ID. */ 106 public static final int kRed1AllianceStation = 1; 107 108 /** Red Alliance Station 2 ID. */ 109 public static final int kRed2AllianceStation = 2; 110 111 /** Red Alliance Station 3 ID. */ 112 public static final int kRed3AllianceStation = 3; 113 114 /** Blue Alliance Station 1 ID. */ 115 public static final int kBlue1AllianceStation = 4; 116 117 /** Blue Alliance Station 2 ID. */ 118 public static final int kBlue2AllianceStation = 5; 119 120 /** Blue Alliance Station 3 ID. */ 121 public static final int kBlue3AllianceStation = 6; 122 123 /** 124 * Gets the current alliance station ID. 125 * 126 * @return the alliance station ID as AllianceStationID 127 * @see "HAL_GetAllianceStation" 128 */ 129 public static AllianceStationID getAllianceStation() { 130 return switch (nativeGetAllianceStation()) { 131 case kUnknownAllianceStation -> AllianceStationID.Unknown; 132 case kRed1AllianceStation -> AllianceStationID.Red1; 133 case kRed2AllianceStation -> AllianceStationID.Red2; 134 case kRed3AllianceStation -> AllianceStationID.Red3; 135 case kBlue1AllianceStation -> AllianceStationID.Blue1; 136 case kBlue2AllianceStation -> AllianceStationID.Blue2; 137 case kBlue3AllianceStation -> AllianceStationID.Blue3; 138 default -> null; 139 }; 140 } 141 142 /** The maximum number of axes. */ 143 public static final int kMaxJoystickAxes = 12; 144 145 /** The maximum number of POVs. */ 146 public static final int kMaxJoystickPOVs = 8; 147 148 /** The maximum number of joysticks. */ 149 public static final int kMaxJoysticks = 6; 150 151 /** 152 * Gets the axes of a specific joystick. 153 * 154 * @param joystickNum the joystick number 155 * @param axesArray the axes values 156 * @return number of joystick axes, or 0 for error 157 * @see "HAL_GetJoystickAxes" 158 */ 159 public static native int getJoystickAxes(byte joystickNum, float[] axesArray); 160 161 /** 162 * Gets the axes of a specific joystick. 163 * 164 * @param joystickNum the joystick number 165 * @param rawAxesArray the raw int axes values (-32767-32768) 166 * @return number of joystick axes, or 0 for error 167 * @see "HAL_GetJoystickAxes" 168 */ 169 public static native int getJoystickAxesRaw(byte joystickNum, short[] rawAxesArray); 170 171 /** 172 * Gets the POVs of a specific joystick. 173 * 174 * @param joystickNum the joystick number 175 * @param povsArray the POV values 176 * @return number of POVs, or 0 for error 177 * @see "HAL_GetJoystickPOVs" 178 */ 179 public static native int getJoystickPOVs(byte joystickNum, byte[] povsArray); 180 181 /** 182 * Gets the buttons of a specific joystick. 183 * 184 * @param joystickNum the joystick number 185 * @param count the count of buttons 186 * @return The joystick button values 187 * @see "HAL_GetJoystickButtons" 188 */ 189 public static native int getJoystickButtons(byte joystickNum, ByteBuffer count); 190 191 /** 192 * Get all joystick data. 193 * 194 * @param axesArray all joystick axes 195 * @param rawAxesArray all joystick axes as int 196 * @param povsArray all povs 197 * @param buttonsAndMetadata array of long joystick axes count, long joystick povs count, long 198 * joystick buttons count, long joystick buttons values 199 * @see "HAL_GetAllJoystickData" 200 */ 201 public static native void getAllJoystickData( 202 float[] axesArray, short[] rawAxesArray, byte[] povsArray, long[] buttonsAndMetadata); 203 204 /** 205 * Set joystick outputs. 206 * 207 * @param joystickNum the joystick number 208 * @param outputs bitmask of outputs, 1 for on 0 for off 209 * @param leftRumble the left rumble value (0-FFFF) 210 * @param rightRumble the right rumble value (0-FFFF) 211 * @return the error code, or 0 for success 212 * @see "HAL_SetJoystickOutputs" 213 */ 214 public static native int setJoystickOutputs( 215 byte joystickNum, int outputs, int leftRumble, int rightRumble); 216 217 /** 218 * Gets whether a specific joystick is considered to be an Gamepad. 219 * 220 * @param joystickNum the joystick number 221 * @return 1 if gamepad, 0 otherwise 222 * @see "HAL_GetJoystickIsGamepad" 223 */ 224 public static native int getJoystickIsGamepad(byte joystickNum); 225 226 /** 227 * Gets the type of joystick connected. 228 * 229 * <p>This is device specific, and different depending on what system input type the joystick 230 * uses. 231 * 232 * @param joystickNum the joystick number 233 * @return the enumerated joystick type 234 * @see "HAL_GetJoystickType" 235 */ 236 public static native int getJoystickType(byte joystickNum); 237 238 /** 239 * Gets the name of a joystick. 240 * 241 * <p>The returned array must be freed with HAL_FreeJoystickName. 242 * 243 * @param joystickNum the joystick number 244 * @return the joystick name 245 * @see "HAL_GetJoystickName" 246 */ 247 public static native String getJoystickName(byte joystickNum); 248 249 /** 250 * Gets the type of a specific joystick axis. 251 * 252 * <p>This is device specific, and different depending on what system input type the joystick 253 * uses. 254 * 255 * @param joystickNum the joystick number 256 * @param axis the axis number 257 * @return the enumerated axis type 258 * @see "HAL_GetJoystickAxisType" 259 */ 260 public static native int getJoystickAxisType(byte joystickNum, byte axis); 261 262 /** 263 * Returns the approximate match time. 264 * 265 * <p>The FMS does not send an official match time to the robots, but does send an approximate 266 * match time. The value will count down the time remaining in the current period (auto or 267 * teleop). 268 * 269 * <p>Warning: This is not an official time (so it cannot be used to dispute ref calls or 270 * guarantee that a function will trigger before the match ends). 271 * 272 * <p>The Practice Match function of the DS approximates the behavior seen on the field. 273 * 274 * @return time remaining in current match period (auto or teleop) 275 * @see "HAL_GetMatchTime" 276 */ 277 public static native double getMatchTime(); 278 279 /** 280 * Gets info about a specific match. 281 * 282 * @param info the match info to populate 283 * @return the error code, or 0 for success 284 * @see "HAL_GetMatchInfo" 285 */ 286 public static native int getMatchInfo(MatchInfoData info); 287 288 /** 289 * Sends an error to the driver station. 290 * 291 * @param isError true for error, false for warning 292 * @param errorCode the error code 293 * @param isLVCode true for a LV error code, false for a standard error code 294 * @param details the details of the error 295 * @param location the file location of the error 296 * @param callStack the callstack of the error 297 * @param printMsg true to print the error message to stdout as well as to the DS 298 * @return the error code, or 0 for success 299 * @see "HAL_SendError" 300 */ 301 public static native int sendError( 302 boolean isError, 303 int errorCode, 304 boolean isLVCode, 305 String details, 306 String location, 307 String callStack, 308 boolean printMsg); 309 310 /** 311 * Sends a line to the driver station console. 312 * 313 * @param line the line to send 314 * @return the error code, or 0 for success 315 */ 316 public static native int sendConsoleLine(String line); 317 318 /** 319 * Refresh the DS control word. 320 * 321 * @return true if updated 322 * @see "HAL_RefreshDSData" 323 */ 324 public static native boolean refreshDSData(); 325 326 /** 327 * Adds an event handle to be signalled when new data arrives. 328 * 329 * @param handle the event handle to be signalled 330 */ 331 public static native void provideNewDataEventHandle(int handle); 332 333 /** 334 * Removes the event handle from being signalled when new data arrives. 335 * 336 * @param handle the event handle to remove 337 */ 338 public static native void removeNewDataEventHandle(int handle); 339 340 /** 341 * Gets if outputs are enabled by the control system. 342 * 343 * @return true if outputs are enabled 344 */ 345 public static native boolean getOutputsActive(); 346 347 /** Utility class. */ 348 private DriverStationJNI() {} 349}