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