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.wpilibj.simulation;
006
007import edu.wpi.first.hal.AllianceStationID;
008import edu.wpi.first.hal.simulation.DriverStationDataJNI;
009import edu.wpi.first.hal.simulation.NotifyCallback;
010import edu.wpi.first.wpilibj.DriverStation;
011
012/** Class to control a simulated driver station. */
013public final class DriverStationSim {
014  private DriverStationSim() {
015    throw new UnsupportedOperationException("This is a utility class!");
016  }
017
018  /**
019   * Register a callback on whether the DS is enabled.
020   *
021   * @param callback the callback that will be called whenever the enabled state is changed
022   * @param initialNotify if true, the callback will be run on the initial value
023   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
024   *     this object so GC doesn't cancel the callback.
025   */
026  public static CallbackStore registerEnabledCallback(
027      NotifyCallback callback, boolean initialNotify) {
028    int uid = DriverStationDataJNI.registerEnabledCallback(callback, initialNotify);
029    return new CallbackStore(uid, DriverStationDataJNI::cancelEnabledCallback);
030  }
031
032  /**
033   * Check if the DS is enabled.
034   *
035   * @return true if enabled
036   */
037  public static boolean getEnabled() {
038    return DriverStationDataJNI.getEnabled();
039  }
040
041  /**
042   * Change whether the DS is enabled.
043   *
044   * @param enabled the new value
045   */
046  public static void setEnabled(boolean enabled) {
047    DriverStationDataJNI.setEnabled(enabled);
048  }
049
050  /**
051   * Register a callback on whether the DS is in autonomous mode.
052   *
053   * @param callback the callback that will be called on autonomous mode entrance/exit
054   * @param initialNotify if true, the callback will be run on the initial value
055   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
056   *     this object so GC doesn't cancel the callback.
057   */
058  public static CallbackStore registerAutonomousCallback(
059      NotifyCallback callback, boolean initialNotify) {
060    int uid = DriverStationDataJNI.registerAutonomousCallback(callback, initialNotify);
061    return new CallbackStore(uid, DriverStationDataJNI::cancelAutonomousCallback);
062  }
063
064  /**
065   * Check if the DS is in autonomous.
066   *
067   * @return true if autonomous
068   */
069  public static boolean getAutonomous() {
070    return DriverStationDataJNI.getAutonomous();
071  }
072
073  /**
074   * Change whether the DS is in autonomous.
075   *
076   * @param autonomous the new value
077   */
078  public static void setAutonomous(boolean autonomous) {
079    DriverStationDataJNI.setAutonomous(autonomous);
080  }
081
082  /**
083   * Register a callback on whether the DS is in test mode.
084   *
085   * @param callback the callback that will be called whenever the test mode is entered or left
086   * @param initialNotify if true, the callback will be run on the initial value
087   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
088   *     this object so GC doesn't cancel the callback.
089   */
090  public static CallbackStore registerTestCallback(NotifyCallback callback, boolean initialNotify) {
091    int uid = DriverStationDataJNI.registerTestCallback(callback, initialNotify);
092    return new CallbackStore(uid, DriverStationDataJNI::cancelTestCallback);
093  }
094
095  /**
096   * Check if the DS is in test.
097   *
098   * @return true if test
099   */
100  public static boolean getTest() {
101    return DriverStationDataJNI.getTest();
102  }
103
104  /**
105   * Change whether the DS is in test.
106   *
107   * @param test the new value
108   */
109  public static void setTest(boolean test) {
110    DriverStationDataJNI.setTest(test);
111  }
112
113  /**
114   * Register a callback on the eStop state.
115   *
116   * @param callback the callback that will be called whenever the eStop state changes
117   * @param initialNotify if true, the callback will be run on the initial value
118   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
119   *     this object so GC doesn't cancel the callback.
120   */
121  public static CallbackStore registerEStopCallback(
122      NotifyCallback callback, boolean initialNotify) {
123    int uid = DriverStationDataJNI.registerEStopCallback(callback, initialNotify);
124    return new CallbackStore(uid, DriverStationDataJNI::cancelEStopCallback);
125  }
126
127  /**
128   * Check if eStop has been activated.
129   *
130   * @return true if eStopped
131   */
132  public static boolean getEStop() {
133    return DriverStationDataJNI.getEStop();
134  }
135
136  /**
137   * Set whether eStop is active.
138   *
139   * @param eStop true to activate
140   */
141  @SuppressWarnings("ParameterName")
142  public static void setEStop(boolean eStop) {
143    DriverStationDataJNI.setEStop(eStop);
144  }
145
146  /**
147   * Register a callback on whether the FMS is connected.
148   *
149   * @param callback the callback that will be called whenever the FMS connection changes
150   * @param initialNotify if true, the callback will be run on the initial value
151   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
152   *     this object so GC doesn't cancel the callback.
153   */
154  public static CallbackStore registerFmsAttachedCallback(
155      NotifyCallback callback, boolean initialNotify) {
156    int uid = DriverStationDataJNI.registerFmsAttachedCallback(callback, initialNotify);
157    return new CallbackStore(uid, DriverStationDataJNI::cancelFmsAttachedCallback);
158  }
159
160  /**
161   * Check if the FMS is connected.
162   *
163   * @return true if FMS is connected
164   */
165  public static boolean getFmsAttached() {
166    return DriverStationDataJNI.getFmsAttached();
167  }
168
169  /**
170   * Change whether the FMS is connected.
171   *
172   * @param fmsAttached the new value
173   */
174  public static void setFmsAttached(boolean fmsAttached) {
175    DriverStationDataJNI.setFmsAttached(fmsAttached);
176  }
177
178  /**
179   * Register a callback on whether the DS is connected.
180   *
181   * @param callback the callback that will be called whenever the DS connection changes
182   * @param initialNotify if true, the callback will be run on the initial value
183   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
184   *     this object so GC doesn't cancel the callback.
185   */
186  public static CallbackStore registerDsAttachedCallback(
187      NotifyCallback callback, boolean initialNotify) {
188    int uid = DriverStationDataJNI.registerDsAttachedCallback(callback, initialNotify);
189    return new CallbackStore(uid, DriverStationDataJNI::cancelDsAttachedCallback);
190  }
191
192  /**
193   * Check if the DS is attached.
194   *
195   * @return true if attached
196   */
197  public static boolean getDsAttached() {
198    return DriverStationDataJNI.getDsAttached();
199  }
200
201  /**
202   * Change whether the DS is attached.
203   *
204   * @param dsAttached the new value
205   */
206  public static void setDsAttached(boolean dsAttached) {
207    DriverStationDataJNI.setDsAttached(dsAttached);
208  }
209
210  /**
211   * Register a callback on the alliance station ID.
212   *
213   * @param callback the callback that will be called whenever the alliance station changes
214   * @param initialNotify if true, the callback will be run on the initial value
215   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
216   *     this object so GC doesn't cancel the callback.
217   */
218  public static CallbackStore registerAllianceStationIdCallback(
219      NotifyCallback callback, boolean initialNotify) {
220    int uid = DriverStationDataJNI.registerAllianceStationIdCallback(callback, initialNotify);
221    return new CallbackStore(uid, DriverStationDataJNI::cancelAllianceStationIdCallback);
222  }
223
224  /**
225   * Get the alliance station ID (color + number).
226   *
227   * @return the alliance station color and number
228   */
229  public static AllianceStationID getAllianceStationId() {
230    switch (DriverStationDataJNI.getAllianceStationId()) {
231      case 0:
232        return AllianceStationID.Red1;
233      case 1:
234        return AllianceStationID.Red2;
235      case 2:
236        return AllianceStationID.Red3;
237      case 3:
238        return AllianceStationID.Blue1;
239      case 4:
240        return AllianceStationID.Blue2;
241      case 5:
242        return AllianceStationID.Blue3;
243      default:
244        return null;
245    }
246  }
247
248  /**
249   * Change the alliance station.
250   *
251   * @param allianceStationId the new alliance station
252   */
253  public static void setAllianceStationId(AllianceStationID allianceStationId) {
254    int allianceStation;
255    switch (allianceStationId) {
256      case Red1:
257        allianceStation = 0;
258        break;
259      case Red2:
260        allianceStation = 1;
261        break;
262      case Red3:
263        allianceStation = 2;
264        break;
265      case Blue1:
266        allianceStation = 3;
267        break;
268      case Blue2:
269        allianceStation = 4;
270        break;
271      case Blue3:
272        allianceStation = 5;
273        break;
274      default:
275        return;
276    }
277    DriverStationDataJNI.setAllianceStationId(allianceStation);
278  }
279
280  /**
281   * Register a callback on match time.
282   *
283   * @param callback the callback that will be called whenever match time changes
284   * @param initialNotify if true, the callback will be run on the initial value
285   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
286   *     this object so GC doesn't cancel the callback.
287   */
288  public static CallbackStore registerMatchTimeCallback(
289      NotifyCallback callback, boolean initialNotify) {
290    int uid = DriverStationDataJNI.registerMatchTimeCallback(callback, initialNotify);
291    return new CallbackStore(uid, DriverStationDataJNI::cancelMatchTimeCallback);
292  }
293
294  /**
295   * Get the current value of the match timer.
296   *
297   * @return the current match time
298   */
299  public static double getMatchTime() {
300    return DriverStationDataJNI.getMatchTime();
301  }
302
303  /**
304   * Sets the match timer.
305   *
306   * @param matchTime the new match time
307   */
308  public static void setMatchTime(double matchTime) {
309    DriverStationDataJNI.setMatchTime(matchTime);
310  }
311
312  /** Updates DriverStation data so that new values are visible to the user program. */
313  public static void notifyNewData() {
314    DriverStationDataJNI.notifyNewData();
315    DriverStation.waitForData();
316  }
317
318  /**
319   * Sets suppression of DriverStation.reportError and reportWarning messages.
320   *
321   * @param shouldSend If false then messages will be suppressed.
322   */
323  public static void setSendError(boolean shouldSend) {
324    DriverStationDataJNI.setSendError(shouldSend);
325  }
326
327  /**
328   * Sets suppression of DriverStation.sendConsoleLine messages.
329   *
330   * @param shouldSend If false then messages will be suppressed.
331   */
332  public static void setSendConsoleLine(boolean shouldSend) {
333    DriverStationDataJNI.setSendConsoleLine(shouldSend);
334  }
335
336  /**
337   * Gets the joystick outputs.
338   *
339   * @param stick The joystick number
340   * @return The joystick outputs
341   */
342  public static long getJoystickOutputs(int stick) {
343    return DriverStationDataJNI.getJoystickOutputs(stick);
344  }
345
346  /**
347   * Gets the joystick rumble.
348   *
349   * @param stick The joystick number
350   * @param rumbleNum Rumble to get (0=left, 1=right)
351   * @return The joystick rumble value
352   */
353  public static int getJoystickRumble(int stick, int rumbleNum) {
354    return DriverStationDataJNI.getJoystickRumble(stick, rumbleNum);
355  }
356
357  /**
358   * Sets the state of one joystick button. Button indexes begin at 1.
359   *
360   * @param stick The joystick number
361   * @param button The button index, beginning at 1
362   * @param state The state of the joystick button
363   */
364  public static void setJoystickButton(int stick, int button, boolean state) {
365    DriverStationDataJNI.setJoystickButton(stick, button, state);
366  }
367
368  /**
369   * Gets the value of the axis on a joystick.
370   *
371   * @param stick The joystick number
372   * @param axis The analog axis number
373   * @param value The value of the axis on the joystick
374   */
375  public static void setJoystickAxis(int stick, int axis, double value) {
376    DriverStationDataJNI.setJoystickAxis(stick, axis, value);
377  }
378
379  /**
380   * Gets the state of a POV on a joystick.
381   *
382   * @param stick The joystick number
383   * @param pov The POV number
384   * @param value the angle of the POV in degrees, or -1 for not pressed
385   */
386  public static void setJoystickPOV(int stick, int pov, int value) {
387    DriverStationDataJNI.setJoystickPOV(stick, pov, value);
388  }
389
390  /**
391   * Sets the state of all the buttons on a joystick.
392   *
393   * @param stick The joystick number
394   * @param buttons The bitmap state of the buttons on the joystick
395   */
396  public static void setJoystickButtons(int stick, int buttons) {
397    DriverStationDataJNI.setJoystickButtonsValue(stick, buttons);
398  }
399
400  /**
401   * Sets the number of axes for a joystick.
402   *
403   * @param stick The joystick number
404   * @param count The number of axes on the indicated joystick
405   */
406  public static void setJoystickAxisCount(int stick, int count) {
407    DriverStationDataJNI.setJoystickAxisCount(stick, count);
408  }
409
410  /**
411   * Sets the number of POVs for a joystick.
412   *
413   * @param stick The joystick number
414   * @param count The number of POVs on the indicated joystick
415   */
416  public static void setJoystickPOVCount(int stick, int count) {
417    DriverStationDataJNI.setJoystickPOVCount(stick, count);
418  }
419
420  /**
421   * Sets the number of buttons for a joystick.
422   *
423   * @param stick The joystick number
424   * @param count The number of buttons on the indicated joystick
425   */
426  public static void setJoystickButtonCount(int stick, int count) {
427    DriverStationDataJNI.setJoystickButtonCount(stick, count);
428  }
429
430  /**
431   * Sets the value of isXbox for a joystick.
432   *
433   * @param stick The joystick number
434   * @param isXbox The value of isXbox
435   */
436  public static void setJoystickIsXbox(int stick, boolean isXbox) {
437    DriverStationDataJNI.setJoystickIsXbox(stick, isXbox);
438  }
439
440  /**
441   * Sets the value of type for a joystick.
442   *
443   * @param stick The joystick number
444   * @param type The value of type
445   */
446  public static void setJoystickType(int stick, int type) {
447    DriverStationDataJNI.setJoystickType(stick, type);
448  }
449
450  /**
451   * Sets the name of a joystick.
452   *
453   * @param stick The joystick number
454   * @param name The value of name
455   */
456  public static void setJoystickName(int stick, String name) {
457    DriverStationDataJNI.setJoystickName(stick, name);
458  }
459
460  /**
461   * Sets the types of Axes for a joystick.
462   *
463   * @param stick The joystick number
464   * @param axis The target axis
465   * @param type The type of axis
466   */
467  public static void setJoystickAxisType(int stick, int axis, int type) {
468    DriverStationDataJNI.setJoystickAxisType(stick, axis, type);
469  }
470
471  /**
472   * Sets the game specific message.
473   *
474   * @param message the game specific message
475   */
476  public static void setGameSpecificMessage(String message) {
477    DriverStationDataJNI.setGameSpecificMessage(message);
478  }
479
480  /**
481   * Sets the event name.
482   *
483   * @param name the event name
484   */
485  public static void setEventName(String name) {
486    DriverStationDataJNI.setEventName(name);
487  }
488
489  /**
490   * Sets the match type.
491   *
492   * @param type the match type
493   */
494  public static void setMatchType(DriverStation.MatchType type) {
495    int matchType;
496    switch (type) {
497      case Practice:
498        matchType = 1;
499        break;
500      case Qualification:
501        matchType = 2;
502        break;
503      case Elimination:
504        matchType = 3;
505        break;
506      case None:
507        matchType = 0;
508        break;
509      default:
510        return;
511    }
512    DriverStationDataJNI.setMatchType(matchType);
513  }
514
515  /**
516   * Sets the match number.
517   *
518   * @param matchNumber the match number
519   */
520  public static void setMatchNumber(int matchNumber) {
521    DriverStationDataJNI.setMatchNumber(matchNumber);
522  }
523
524  /**
525   * Sets the replay number.
526   *
527   * @param replayNumber the replay number
528   */
529  public static void setReplayNumber(int replayNumber) {
530    DriverStationDataJNI.setReplayNumber(replayNumber);
531  }
532
533  /** Reset all simulation data for the Driver Station. */
534  public static void resetData() {
535    DriverStationDataJNI.resetData();
536  }
537}