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.simulation.NotifyCallback;
008import edu.wpi.first.hal.simulation.RoboRioDataJNI;
009import edu.wpi.first.wpilibj.RobotController;
010import edu.wpi.first.wpilibj.RobotController.RadioLEDState;
011
012/** A utility class to control a simulated RoboRIO. */
013public final class RoboRioSim {
014  private RoboRioSim() {
015    // Utility class
016  }
017
018  /**
019   * Register a callback to be run when the FPGA button state changes.
020   *
021   * @param callback the callback
022   * @param initialNotify whether to run the callback with the initial state
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 registerFPGAButtonCallback(
027      NotifyCallback callback, boolean initialNotify) {
028    int uid = RoboRioDataJNI.registerFPGAButtonCallback(callback, initialNotify);
029    return new CallbackStore(uid, RoboRioDataJNI::cancelFPGAButtonCallback);
030  }
031
032  /**
033   * Query the state of the FPGA button.
034   *
035   * @return the FPGA button state
036   */
037  public static boolean getFPGAButton() {
038    return RoboRioDataJNI.getFPGAButton();
039  }
040
041  /**
042   * Define the state of the FPGA button.
043   *
044   * @param fpgaButton the new state
045   */
046  public static void setFPGAButton(boolean fpgaButton) {
047    RoboRioDataJNI.setFPGAButton(fpgaButton);
048  }
049
050  /**
051   * Register a callback to be run whenever the Vin voltage changes.
052   *
053   * @param callback the callback
054   * @param initialNotify whether to call the callback with the initial state
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 registerVInVoltageCallback(
059      NotifyCallback callback, boolean initialNotify) {
060    int uid = RoboRioDataJNI.registerVInVoltageCallback(callback, initialNotify);
061    return new CallbackStore(uid, RoboRioDataJNI::cancelVInVoltageCallback);
062  }
063
064  /**
065   * Measure the Vin voltage.
066   *
067   * @return the Vin voltage
068   */
069  public static double getVInVoltage() {
070    return RoboRioDataJNI.getVInVoltage();
071  }
072
073  /**
074   * Define the Vin voltage.
075   *
076   * @param vInVoltage the new voltage
077   */
078  public static void setVInVoltage(double vInVoltage) {
079    RoboRioDataJNI.setVInVoltage(vInVoltage);
080  }
081
082  /**
083   * Register a callback to be run whenever the Vin current changes.
084   *
085   * @param callback the callback
086   * @param initialNotify whether the callback should be called with 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 registerVInCurrentCallback(
091      NotifyCallback callback, boolean initialNotify) {
092    int uid = RoboRioDataJNI.registerVInCurrentCallback(callback, initialNotify);
093    return new CallbackStore(uid, RoboRioDataJNI::cancelVInCurrentCallback);
094  }
095
096  /**
097   * Measure the Vin current.
098   *
099   * @return the Vin current
100   */
101  public static double getVInCurrent() {
102    return RoboRioDataJNI.getVInCurrent();
103  }
104
105  /**
106   * Define the Vin current.
107   *
108   * @param vInCurrent the new current
109   */
110  public static void setVInCurrent(double vInCurrent) {
111    RoboRioDataJNI.setVInCurrent(vInCurrent);
112  }
113
114  /**
115   * Register a callback to be run whenever the 6V rail voltage changes.
116   *
117   * @param callback the callback
118   * @param initialNotify whether the callback should be called with the initial value
119   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
120   *     this object so GC doesn't cancel the callback.
121   */
122  public static CallbackStore registerUserVoltage6VCallback(
123      NotifyCallback callback, boolean initialNotify) {
124    int uid = RoboRioDataJNI.registerUserVoltage6VCallback(callback, initialNotify);
125    return new CallbackStore(uid, RoboRioDataJNI::cancelUserVoltage6VCallback);
126  }
127
128  /**
129   * Measure the 6V rail voltage.
130   *
131   * @return the 6V rail voltage
132   */
133  public static double getUserVoltage6V() {
134    return RoboRioDataJNI.getUserVoltage6V();
135  }
136
137  /**
138   * Define the 6V rail voltage.
139   *
140   * @param userVoltage6V the new voltage
141   */
142  public static void setUserVoltage6V(double userVoltage6V) {
143    RoboRioDataJNI.setUserVoltage6V(userVoltage6V);
144  }
145
146  /**
147   * Register a callback to be run whenever the 6V rail current changes.
148   *
149   * @param callback the callback
150   * @param initialNotify whether the callback should be called with 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 registerUserCurrent6VCallback(
155      NotifyCallback callback, boolean initialNotify) {
156    int uid = RoboRioDataJNI.registerUserCurrent6VCallback(callback, initialNotify);
157    return new CallbackStore(uid, RoboRioDataJNI::cancelUserCurrent6VCallback);
158  }
159
160  /**
161   * Measure the 6V rail current.
162   *
163   * @return the 6V rail current
164   */
165  public static double getUserCurrent6V() {
166    return RoboRioDataJNI.getUserCurrent6V();
167  }
168
169  /**
170   * Define the 6V rail current.
171   *
172   * @param userCurrent6V the new current
173   */
174  public static void setUserCurrent6V(double userCurrent6V) {
175    RoboRioDataJNI.setUserCurrent6V(userCurrent6V);
176  }
177
178  /**
179   * Register a callback to be run whenever the 6V rail active state changes.
180   *
181   * @param callback the callback
182   * @param initialNotify whether the callback should be called with the initial state
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 registerUserActive6VCallback(
187      NotifyCallback callback, boolean initialNotify) {
188    int uid = RoboRioDataJNI.registerUserActive6VCallback(callback, initialNotify);
189    return new CallbackStore(uid, RoboRioDataJNI::cancelUserActive6VCallback);
190  }
191
192  /**
193   * Get the 6V rail active state.
194   *
195   * @return true if the 6V rail is active
196   */
197  public static boolean getUserActive6V() {
198    return RoboRioDataJNI.getUserActive6V();
199  }
200
201  /**
202   * Set the 6V rail active state.
203   *
204   * @param userActive6V true to make rail active
205   */
206  public static void setUserActive6V(boolean userActive6V) {
207    RoboRioDataJNI.setUserActive6V(userActive6V);
208  }
209
210  /**
211   * Register a callback to be run whenever the 5V rail voltage changes.
212   *
213   * @param callback the callback
214   * @param initialNotify whether the callback should be called with 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 registerUserVoltage5VCallback(
219      NotifyCallback callback, boolean initialNotify) {
220    int uid = RoboRioDataJNI.registerUserVoltage5VCallback(callback, initialNotify);
221    return new CallbackStore(uid, RoboRioDataJNI::cancelUserVoltage5VCallback);
222  }
223
224  /**
225   * Measure the 5V rail voltage.
226   *
227   * @return the 5V rail voltage
228   */
229  public static double getUserVoltage5V() {
230    return RoboRioDataJNI.getUserVoltage5V();
231  }
232
233  /**
234   * Define the 5V rail voltage.
235   *
236   * @param userVoltage5V the new voltage
237   */
238  public static void setUserVoltage5V(double userVoltage5V) {
239    RoboRioDataJNI.setUserVoltage5V(userVoltage5V);
240  }
241
242  /**
243   * Register a callback to be run whenever the 5V rail current changes.
244   *
245   * @param callback the callback
246   * @param initialNotify whether the callback should be called with the initial value
247   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
248   *     this object so GC doesn't cancel the callback.
249   */
250  public static CallbackStore registerUserCurrent5VCallback(
251      NotifyCallback callback, boolean initialNotify) {
252    int uid = RoboRioDataJNI.registerUserCurrent5VCallback(callback, initialNotify);
253    return new CallbackStore(uid, RoboRioDataJNI::cancelUserCurrent5VCallback);
254  }
255
256  /**
257   * Measure the 5V rail current.
258   *
259   * @return the 5V rail current
260   */
261  public static double getUserCurrent5V() {
262    return RoboRioDataJNI.getUserCurrent5V();
263  }
264
265  /**
266   * Define the 5V rail current.
267   *
268   * @param userCurrent5V the new current
269   */
270  public static void setUserCurrent5V(double userCurrent5V) {
271    RoboRioDataJNI.setUserCurrent5V(userCurrent5V);
272  }
273
274  /**
275   * Register a callback to be run whenever the 5V rail active state changes.
276   *
277   * @param callback the callback
278   * @param initialNotify whether the callback should be called with the initial state
279   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
280   *     this object so GC doesn't cancel the callback.
281   */
282  public static CallbackStore registerUserActive5VCallback(
283      NotifyCallback callback, boolean initialNotify) {
284    int uid = RoboRioDataJNI.registerUserActive5VCallback(callback, initialNotify);
285    return new CallbackStore(uid, RoboRioDataJNI::cancelUserActive5VCallback);
286  }
287
288  /**
289   * Get the 5V rail active state.
290   *
291   * @return true if the 5V rail is active
292   */
293  public static boolean getUserActive5V() {
294    return RoboRioDataJNI.getUserActive5V();
295  }
296
297  /**
298   * Set the 5V rail active state.
299   *
300   * @param userActive5V true to make rail active
301   */
302  public static void setUserActive5V(boolean userActive5V) {
303    RoboRioDataJNI.setUserActive5V(userActive5V);
304  }
305
306  /**
307   * Register a callback to be run whenever the 3.3V rail voltage changes.
308   *
309   * @param callback the callback
310   * @param initialNotify whether the callback should be called with the initial value
311   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
312   *     this object so GC doesn't cancel the callback.
313   */
314  public static CallbackStore registerUserVoltage3V3Callback(
315      NotifyCallback callback, boolean initialNotify) {
316    int uid = RoboRioDataJNI.registerUserVoltage3V3Callback(callback, initialNotify);
317    return new CallbackStore(uid, RoboRioDataJNI::cancelUserVoltage3V3Callback);
318  }
319
320  /**
321   * Measure the 3.3V rail voltage.
322   *
323   * @return the 3.3V rail voltage
324   */
325  public static double getUserVoltage3V3() {
326    return RoboRioDataJNI.getUserVoltage3V3();
327  }
328
329  /**
330   * Define the 3.3V rail voltage.
331   *
332   * @param userVoltage3V3 the new voltage
333   */
334  public static void setUserVoltage3V3(double userVoltage3V3) {
335    RoboRioDataJNI.setUserVoltage3V3(userVoltage3V3);
336  }
337
338  /**
339   * Register a callback to be run whenever the 3.3V rail current changes.
340   *
341   * @param callback the callback
342   * @param initialNotify whether the callback should be called with the initial value
343   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
344   *     this object so GC doesn't cancel the callback.
345   */
346  public static CallbackStore registerUserCurrent3V3Callback(
347      NotifyCallback callback, boolean initialNotify) {
348    int uid = RoboRioDataJNI.registerUserCurrent3V3Callback(callback, initialNotify);
349    return new CallbackStore(uid, RoboRioDataJNI::cancelUserCurrent3V3Callback);
350  }
351
352  /**
353   * Measure the 3.3V rail current.
354   *
355   * @return the 3.3V rail current
356   */
357  public static double getUserCurrent3V3() {
358    return RoboRioDataJNI.getUserCurrent3V3();
359  }
360
361  /**
362   * Define the 3.3V rail current.
363   *
364   * @param userCurrent3V3 the new current
365   */
366  public static void setUserCurrent3V3(double userCurrent3V3) {
367    RoboRioDataJNI.setUserCurrent3V3(userCurrent3V3);
368  }
369
370  /**
371   * Register a callback to be run whenever the 3.3V rail active state changes.
372   *
373   * @param callback the callback
374   * @param initialNotify whether the callback should be called with the initial state
375   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
376   *     this object so GC doesn't cancel the callback.
377   */
378  public static CallbackStore registerUserActive3V3Callback(
379      NotifyCallback callback, boolean initialNotify) {
380    int uid = RoboRioDataJNI.registerUserActive3V3Callback(callback, initialNotify);
381    return new CallbackStore(uid, RoboRioDataJNI::cancelUserActive3V3Callback);
382  }
383
384  /**
385   * Get the 3.3V rail active state.
386   *
387   * @return true if the 3.3V rail is active
388   */
389  public static boolean getUserActive3V3() {
390    return RoboRioDataJNI.getUserActive3V3();
391  }
392
393  /**
394   * Set the 3.3V rail active state.
395   *
396   * @param userActive3V3 true to make rail active
397   */
398  public static void setUserActive3V3(boolean userActive3V3) {
399    RoboRioDataJNI.setUserActive3V3(userActive3V3);
400  }
401
402  /**
403   * Register a callback to be run whenever the 6V rail number of faults changes.
404   *
405   * @param callback the callback
406   * @param initialNotify whether the callback should be called with the initial value
407   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
408   *     this object so GC doesn't cancel the callback.
409   */
410  public static CallbackStore registerUserFaults6VCallback(
411      NotifyCallback callback, boolean initialNotify) {
412    int uid = RoboRioDataJNI.registerUserFaults6VCallback(callback, initialNotify);
413    return new CallbackStore(uid, RoboRioDataJNI::cancelUserFaults6VCallback);
414  }
415
416  /**
417   * Get the 6V rail number of faults.
418   *
419   * @return number of faults
420   */
421  public static int getUserFaults6V() {
422    return RoboRioDataJNI.getUserFaults6V();
423  }
424
425  /**
426   * Set the 6V rail number of faults.
427   *
428   * @param userFaults6V number of faults
429   */
430  public static void setUserFaults6V(int userFaults6V) {
431    RoboRioDataJNI.setUserFaults6V(userFaults6V);
432  }
433
434  /**
435   * Register a callback to be run whenever the 5V rail number of faults changes.
436   *
437   * @param callback the callback
438   * @param initialNotify whether the callback should be called with the initial value
439   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
440   *     this object so GC doesn't cancel the callback.
441   */
442  public static CallbackStore registerUserFaults5VCallback(
443      NotifyCallback callback, boolean initialNotify) {
444    int uid = RoboRioDataJNI.registerUserFaults5VCallback(callback, initialNotify);
445    return new CallbackStore(uid, RoboRioDataJNI::cancelUserFaults5VCallback);
446  }
447
448  /**
449   * Get the 5V rail number of faults.
450   *
451   * @return number of faults
452   */
453  public static int getUserFaults5V() {
454    return RoboRioDataJNI.getUserFaults5V();
455  }
456
457  /**
458   * Set the 5V rail number of faults.
459   *
460   * @param userFaults5V number of faults
461   */
462  public static void setUserFaults5V(int userFaults5V) {
463    RoboRioDataJNI.setUserFaults5V(userFaults5V);
464  }
465
466  /**
467   * Register a callback to be run whenever the 3.3V rail number of faults changes.
468   *
469   * @param callback the callback
470   * @param initialNotify whether the callback should be called with the initial value
471   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
472   *     this object so GC doesn't cancel the callback.
473   */
474  public static CallbackStore registerUserFaults3V3Callback(
475      NotifyCallback callback, boolean initialNotify) {
476    int uid = RoboRioDataJNI.registerUserFaults3V3Callback(callback, initialNotify);
477    return new CallbackStore(uid, RoboRioDataJNI::cancelUserFaults3V3Callback);
478  }
479
480  /**
481   * Get the 3.3V rail number of faults.
482   *
483   * @return number of faults
484   */
485  public static int getUserFaults3V3() {
486    return RoboRioDataJNI.getUserFaults3V3();
487  }
488
489  /**
490   * Set the 3.3V rail number of faults.
491   *
492   * @param userFaults3V3 number of faults
493   */
494  public static void setUserFaults3V3(int userFaults3V3) {
495    RoboRioDataJNI.setUserFaults3V3(userFaults3V3);
496  }
497
498  /**
499   * Register a callback to be run whenever the Brownout voltage changes.
500   *
501   * @param callback the callback
502   * @param initialNotify whether to call the callback with the initial state
503   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
504   *     this object so GC doesn't cancel the callback.
505   */
506  public static CallbackStore registerBrownoutVoltageCallback(
507      NotifyCallback callback, boolean initialNotify) {
508    int uid = RoboRioDataJNI.registerBrownoutVoltageCallback(callback, initialNotify);
509    return new CallbackStore(uid, RoboRioDataJNI::cancelBrownoutVoltageCallback);
510  }
511
512  /**
513   * Measure the Brownout voltage.
514   *
515   * @return the Brownout voltage
516   */
517  public static double getBrownoutVoltage() {
518    return RoboRioDataJNI.getBrownoutVoltage();
519  }
520
521  /**
522   * Define the Brownout voltage.
523   *
524   * @param vInVoltage the new voltage
525   */
526  public static void setBrownoutVoltage(double vInVoltage) {
527    RoboRioDataJNI.setBrownoutVoltage(vInVoltage);
528  }
529
530  /**
531   * Register a callback to be run whenever the cpu temp changes.
532   *
533   * @param callback the callback
534   * @param initialNotify whether to call the callback with the initial state
535   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
536   *     this object so GC doesn't cancel the callback.
537   */
538  public static CallbackStore registerCPUTempCallback(
539      NotifyCallback callback, boolean initialNotify) {
540    int uid = RoboRioDataJNI.registerCPUTempCallback(callback, initialNotify);
541    return new CallbackStore(uid, RoboRioDataJNI::cancelCPUTempCallback);
542  }
543
544  /**
545   * Get the cpu temp.
546   *
547   * @return the cpu temp.
548   */
549  public static double getCPUTemp() {
550    return RoboRioDataJNI.getCPUTemp();
551  }
552
553  /**
554   * Set the cpu temp.
555   *
556   * @param cpuTemp the new cpu temp.
557   */
558  public static void setCPUTemp(double cpuTemp) {
559    RoboRioDataJNI.setCPUTemp(cpuTemp);
560  }
561
562  /**
563   * Register a callback to be run whenever the team number changes.
564   *
565   * @param callback the callback
566   * @param initialNotify whether to call the callback with the initial state
567   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
568   *     this object so GC doesn't cancel the callback.
569   */
570  public static CallbackStore registerTeamNumberCallback(
571      NotifyCallback callback, boolean initialNotify) {
572    int uid = RoboRioDataJNI.registerTeamNumberCallback(callback, initialNotify);
573    return new CallbackStore(uid, RoboRioDataJNI::cancelTeamNumberCallback);
574  }
575
576  /**
577   * Get the team number.
578   *
579   * @return the team number.
580   */
581  public static int getTeamNumber() {
582    return RoboRioDataJNI.getTeamNumber();
583  }
584
585  /**
586   * Set the team number.
587   *
588   * @param teamNumber the new team number.
589   */
590  public static void setTeamNumber(int teamNumber) {
591    RoboRioDataJNI.setTeamNumber(teamNumber);
592  }
593
594  /**
595   * Get the serial number.
596   *
597   * @return The serial number.
598   */
599  public static String getSerialNumber() {
600    return RoboRioDataJNI.getSerialNumber();
601  }
602
603  /**
604   * Set the serial number.
605   *
606   * @param serialNumber The serial number.
607   */
608  public static void setSerialNumber(String serialNumber) {
609    RoboRioDataJNI.setSerialNumber(serialNumber);
610  }
611
612  /**
613   * Get the comments string.
614   *
615   * @return The comments string.
616   */
617  public static String getComments() {
618    return RoboRioDataJNI.getComments();
619  }
620
621  /**
622   * Set the comments string.
623   *
624   * @param comments The comments string.
625   */
626  public static void setComments(String comments) {
627    RoboRioDataJNI.setComments(comments);
628  }
629
630  /**
631   * Register a callback to be run whenever the Radio led state changes.
632   *
633   * @param callback the callback
634   * @param initialNotify whether to call the callback with the initial state
635   * @return the {@link CallbackStore} object associated with this callback. Save a reference to
636   *     this object so GC doesn't cancel the callback.
637   */
638  public static CallbackStore registerRadioLEDStateCallback(
639      NotifyCallback callback, boolean initialNotify) {
640    int uid = RoboRioDataJNI.registerRadioLEDStateCallback(callback, initialNotify);
641    return new CallbackStore(uid, RoboRioDataJNI::cancelRadioLEDStateCallback);
642  }
643
644  /**
645   * Get the state of the radio led.
646   *
647   * @return The state of the radio led.
648   */
649  public static RadioLEDState getRadioLEDState() {
650    return RadioLEDState.fromValue(RoboRioDataJNI.getRadioLEDState());
651  }
652
653  /**
654   * Set the state of the radio led.
655   *
656   * @param state The state of the radio led.
657   */
658  public static void setRadioLEDState(RobotController.RadioLEDState state) {
659    RoboRioDataJNI.setRadioLEDState(state.value);
660  }
661
662  /** Reset all simulation data. */
663  public static void resetData() {
664    RoboRioDataJNI.resetData();
665  }
666}