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.util.ArrayList;
008import java.util.List;
009
010/**
011 * JNI Wrapper for HAL<br>
012 * .
013 */
014public final class HAL extends JNIWrapper {
015  public static native boolean initialize(int timeout, int mode);
016
017  public static native void shutdown();
018
019  public static native boolean hasMain();
020
021  public static native void runMain();
022
023  public static native void exitMain();
024
025  private static native void simPeriodicBeforeNative();
026
027  private static final List<Runnable> s_simPeriodicBefore = new ArrayList<>();
028
029  public static class SimPeriodicBeforeCallback implements AutoCloseable {
030    private SimPeriodicBeforeCallback(Runnable r) {
031      m_run = r;
032    }
033
034    @Override
035    public void close() {
036      synchronized (s_simPeriodicBefore) {
037        s_simPeriodicBefore.remove(m_run);
038      }
039    }
040
041    private final Runnable m_run;
042  }
043
044  /**
045   * Registers a callback to be run by IterativeRobotBase prior to the user's simulationPeriodic
046   * code.
047   *
048   * @param r runnable
049   * @return Callback object (must be retained for callback to stay active).
050   */
051  public static SimPeriodicBeforeCallback registerSimPeriodicBeforeCallback(Runnable r) {
052    synchronized (s_simPeriodicBefore) {
053      s_simPeriodicBefore.add(r);
054    }
055    return new SimPeriodicBeforeCallback(r);
056  }
057
058  /**
059   * Runs SimPeriodicBefore callbacks. IterativeRobotBase calls this prior to the user's
060   * simulationPeriodic code.
061   */
062  public static void simPeriodicBefore() {
063    simPeriodicBeforeNative();
064    synchronized (s_simPeriodicBefore) {
065      for (Runnable r : s_simPeriodicBefore) {
066        r.run();
067      }
068    }
069  }
070
071  private static native void simPeriodicAfterNative();
072
073  private static final List<Runnable> s_simPeriodicAfter = new ArrayList<>();
074
075  public static class SimPeriodicAfterCallback implements AutoCloseable {
076    private SimPeriodicAfterCallback(Runnable r) {
077      m_run = r;
078    }
079
080    @Override
081    public void close() {
082      synchronized (s_simPeriodicAfter) {
083        s_simPeriodicAfter.remove(m_run);
084      }
085    }
086
087    private final Runnable m_run;
088  }
089
090  /**
091   * Registers a callback to be run by IterativeRobotBase after the user's simulationPeriodic code.
092   *
093   * @param r runnable
094   * @return Callback object (must be retained for callback to stay active).
095   */
096  public static SimPeriodicAfterCallback registerSimPeriodicAfterCallback(Runnable r) {
097    synchronized (s_simPeriodicAfter) {
098      s_simPeriodicAfter.add(r);
099    }
100    return new SimPeriodicAfterCallback(r);
101  }
102
103  /**
104   * Runs SimPeriodicAfter callbacks. IterativeRobotBase calls this after the user's
105   * simulationPeriodic code.
106   */
107  public static void simPeriodicAfter() {
108    simPeriodicAfterNative();
109    synchronized (s_simPeriodicAfter) {
110      for (Runnable r : s_simPeriodicAfter) {
111        r.run();
112      }
113    }
114  }
115
116  public static native boolean getBrownedOut();
117
118  public static native boolean getSystemActive();
119
120  public static native int getPortWithModule(byte module, byte channel);
121
122  public static native int getPort(byte channel);
123
124  public static void report(int resource, int instanceNumber) {
125    report(resource, instanceNumber, 0, "");
126  }
127
128  public static void report(int resource, int instanceNumber, int context) {
129    report(resource, instanceNumber, context, "");
130  }
131
132  public static int report(int resource, int instanceNumber, int context, String feature) {
133    return DriverStationJNI.report(resource, instanceNumber, context, feature);
134  }
135
136  private HAL() {}
137}