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 kRed1AllianceStation = 0;
151  public static final int kRed2AllianceStation = 1;
152  public static final int kRed3AllianceStation = 2;
153  public static final int kBlue1AllianceStation = 3;
154  public static final int kBlue2AllianceStation = 4;
155  public static final int kBlue3AllianceStation = 5;
156
157  /**
158   * Gets the current alliance station ID.
159   *
160   * @return the alliance station ID as AllianceStationID
161   * @see "HAL_GetAllianceStation"
162   */
163  public static AllianceStationID getAllianceStation() {
164    switch (nativeGetAllianceStation()) {
165      case kRed1AllianceStation:
166        return AllianceStationID.Red1;
167      case kRed2AllianceStation:
168        return AllianceStationID.Red2;
169      case kRed3AllianceStation:
170        return AllianceStationID.Red3;
171      case kBlue1AllianceStation:
172        return AllianceStationID.Blue1;
173      case kBlue2AllianceStation:
174        return AllianceStationID.Blue2;
175      case kBlue3AllianceStation:
176        return AllianceStationID.Blue3;
177      default:
178        return null;
179    }
180  }
181
182  public static final int kMaxJoystickAxes = 12;
183  public static final int kMaxJoystickPOVs = 12;
184  public static final int kMaxJoysticks = 6;
185
186  /**
187   * Gets the axes of a specific joystick.
188   *
189   * @param joystickNum the joystick number
190   * @param axesArray the axes values
191   * @return number of joystick axes, or 0 for error
192   * @see "HAL_GetJoystickAxes"
193   */
194  public static native int getJoystickAxes(byte joystickNum, float[] axesArray);
195
196  /**
197   * Gets the axes of a specific joystick.
198   *
199   * @param joystickNum the joystick number
200   * @param rawAxesArray the raw int axes values (0-255)
201   * @return number of joystick axes, or 0 for error
202   * @see "HAL_GetJoystickAxes"
203   */
204  public static native int getJoystickAxesRaw(byte joystickNum, int[] rawAxesArray);
205
206  /**
207   * Gets the POVs of a specific joystick.
208   *
209   * @param joystickNum the joystick number
210   * @param povsArray the POV values
211   * @return number of POVs, or 0 for error
212   * @see "HAL_GetJoystickPOVs"
213   */
214  public static native int getJoystickPOVs(byte joystickNum, short[] povsArray);
215
216  /**
217   * Gets the buttons of a specific joystick.
218   *
219   * @param joystickNum the joystick number
220   * @param count the count of buttons
221   * @return The joystick button values
222   * @see "HAL_GetJoystickButtons"
223   */
224  public static native int getJoystickButtons(byte joystickNum, ByteBuffer count);
225
226  /**
227   * Get all joystick data.
228   *
229   * @param axesArray all joystick axes
230   * @param rawAxesArray all joystick axes as int
231   * @param povsArray all povs
232   * @param buttonsAndMetadata array of long joystick axes count, long joystick povs count, long
233   *     jostick buttons count, long joystick buttons values
234   * @see "HAL_GetAllJoystickData"
235   */
236  public static native void getAllJoystickData(
237      float[] axesArray, byte[] rawAxesArray, short[] povsArray, long[] buttonsAndMetadata);
238
239  /**
240   * Set joystick outputs.
241   *
242   * @param joystickNum the joystick number
243   * @param outputs bitmask of outputs, 1 for on 0 for off
244   * @param leftRumble the left rumble value (0-FFFF)
245   * @param rightRumble the right rumble value (0-FFFF)
246   * @return the error code, or 0 for success
247   * @see "HAL_SetJoystickOutputs"
248   */
249  public static native int setJoystickOutputs(
250      byte joystickNum, int outputs, short leftRumble, short rightRumble);
251
252  /**
253   * Gets whether a specific joystick is considered to be an XBox controller.
254   *
255   * @param joystickNum the joystick number
256   * @return 1 if xbox, 0 otherwise
257   * @see "HAL_GetJoystickIsXbox"
258   */
259  public static native int getJoystickIsXbox(byte joystickNum);
260
261  /**
262   * Gets the type of joystick connected.
263   *
264   * <p>This is device specific, and different depending on what system input type the joystick
265   * uses.
266   *
267   * @param joystickNum the joystick number
268   * @return the enumerated joystick type
269   * @see "HAL_GetJoystickType"
270   */
271  public static native int getJoystickType(byte joystickNum);
272
273  /**
274   * Gets the name of a joystick.
275   *
276   * <p>The returned array must be freed with HAL_FreeJoystickName.
277   *
278   * @param joystickNum the joystick number
279   * @return the joystick name
280   * @see "HAL_GetJoystickName"
281   */
282  public static native String getJoystickName(byte joystickNum);
283
284  /**
285   * Gets the type of a specific joystick axis.
286   *
287   * <p>This is device specific, and different depending on what system input type the joystick
288   * uses.
289   *
290   * @param joystickNum the joystick number
291   * @param axis the axis number
292   * @return the enumerated axis type
293   * @see "HAL_GetJoystickAxisType"
294   */
295  public static native int getJoystickAxisType(byte joystickNum, byte axis);
296
297  /**
298   * Returns the approximate match time.
299   *
300   * <p>The FMS does not send an official match time to the robots, but does send an approximate
301   * match time. The value will count down the time remaining in the current period (auto or
302   * teleop).
303   *
304   * <p>Warning: This is not an official time (so it cannot be used to dispute ref calls or
305   * guarantee that a function will trigger before the match ends).
306   *
307   * <p>The Practice Match function of the DS approximates the behavior seen on the field.
308   *
309   * @return time remaining in current match period (auto or teleop)
310   * @see "HAL_GetMatchTime"
311   */
312  public static native double getMatchTime();
313
314  /**
315   * Gets info about a specific match.
316   *
317   * @param info the match info to populate
318   * @return the error code, or 0 for success
319   * @see "HAL_GetMatchInfo"
320   */
321  public static native int getMatchInfo(MatchInfoData info);
322
323  /**
324   * Sends an error to the driver station.
325   *
326   * @param isError true for error, false for warning
327   * @param errorCode the error code
328   * @param isLVCode true for a LV error code, false for a standard error code
329   * @param details the details of the error
330   * @param location the file location of the error
331   * @param callStack the callstack of the error
332   * @param printMsg true to print the error message to stdout as well as to the DS
333   * @return the error code, or 0 for success
334   * @see "HAL_SendError"
335   */
336  public static native int sendError(
337      boolean isError,
338      int errorCode,
339      boolean isLVCode,
340      String details,
341      String location,
342      String callStack,
343      boolean printMsg);
344
345  /**
346   * Sends a line to the driver station console.
347   *
348   * @param line the line to send
349   * @return the error code, or 0 for success
350   */
351  public static native int sendConsoleLine(String line);
352
353  /**
354   * Refresh the DS control word.
355   *
356   * @return true if updated
357   * @see "HAL_RefreshDSData"
358   */
359  public static native boolean refreshDSData();
360
361  public static native void provideNewDataEventHandle(int handle);
362
363  public static native void removeNewDataEventHandle(int handle);
364
365  /**
366   * Gets if outputs are enabled by the control system.
367   *
368   * @return true if outputs are enabled
369   */
370  public static native boolean getOutputsActive();
371
372  private DriverStationJNI() {}
373}