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