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
007/**
008 * Hardware Abstraction Layer (HAL) Utilities JNI Functions.
009 *
010 * @see "hal/HALBase.h"
011 */
012public final class HALUtil extends JNIWrapper {
013  public static final int NULL_PARAMETER = -1005;
014  public static final int SAMPLE_RATE_TOO_HIGH = 1001;
015  public static final int VOLTAGE_OUT_OF_RANGE = 1002;
016  public static final int LOOP_TIMING_ERROR = 1004;
017  public static final int INCOMPATIBLE_STATE = 1015;
018  public static final int ANALOG_TRIGGER_PULSE_OUTPUT_ERROR = -1011;
019  public static final int NO_AVAILABLE_RESOURCES = -104;
020  public static final int PARAMETER_OUT_OF_RANGE = -1028;
021
022  public static final int RUNTIME_ROBORIO = 0;
023  public static final int RUNTIME_ROBORIO2 = 1;
024  public static final int RUNTIME_SIMULATION = 2;
025
026  /**
027   * Returns the FPGA Version number.
028   *
029   * <p>For now, expect this to be competition year.
030   *
031   * @return FPGA Version number.
032   * @see "HAL_GetFPGAVersion"
033   */
034  public static native short getFPGAVersion();
035
036  /**
037   * Returns the FPGA Revision number.
038   *
039   * <p>The format of the revision is 3 numbers. The 12 most significant bits are the Major
040   * Revision. the next 8 bits are the Minor Revision. The 12 least significant bits are the Build
041   * Number.
042   *
043   * @return FPGA Revision number.
044   * @see "HAL_GetFPGARevision"
045   */
046  public static native int getFPGARevision();
047
048  /**
049   * Returns the roboRIO serial number.
050   *
051   * @return The roboRIO serial number.
052   * @see "HAL_GetSerialNumber"
053   */
054  public static native String getSerialNumber();
055
056  /**
057   * Returns the comments from the roboRIO web interface.
058   *
059   * @return The comments string.
060   * @see "HAL_GetComments"
061   */
062  public static native String getComments();
063
064  /**
065   * Returns the team number configured for the robot controller.
066   *
067   * @return team number, or 0 if not found.
068   * @see "HAL_GetTeamNumber"
069   */
070  public static native int getTeamNumber();
071
072  /**
073   * Reads the microsecond-resolution timer on the FPGA.
074   *
075   * @return The current time in microseconds according to the FPGA (since FPGA reset).
076   */
077  public static native long getFPGATime();
078
079  /**
080   * Returns the runtime type of the HAL.
081   *
082   * @return HAL Runtime Type
083   * @see RUNTIME_ROBORIO
084   * @see RUNTIME_ROBORIO2
085   * @see RUNTIME_SIMULATION
086   * @see "HAL_GetRuntimeType"
087   */
088  public static native int getHALRuntimeType();
089
090  /**
091   * Gets the state of the "USER" button on the roboRIO.
092   *
093   * <p>Warning: the User Button is used to stop user programs from automatically loading if it is
094   * held for more then 5 seconds. Because of this, it's not recommended to be used by teams for any
095   * other purpose.
096   *
097   * @return true if the button is currently pressed down
098   * @see "HAL_GetFPGAButton"
099   */
100  public static native boolean getFPGAButton();
101
102  /**
103   * Gets the error message for a specific status code.
104   *
105   * @param code the status code
106   * @return the error message for the code. This does not need to be freed.
107   * @see "HAL_GetErrorMessage"
108   */
109  public static native String getHALErrorMessage(int code);
110
111  /**
112   * Get the last HAL error code.
113   *
114   * @return error code
115   */
116  public static native int getHALErrno();
117
118  /**
119   * Returns the textual description of the system error code.
120   *
121   * @param errno errno to get description of
122   * @return description of errno
123   * @see "std:strerror"
124   */
125  public static native String getHALstrerror(int errno);
126
127  /**
128   * Gets the error message for the last HAL error.
129   *
130   * @return the error message for the code.
131   */
132  public static String getHALstrerror() {
133    return getHALstrerror(getHALErrno());
134  }
135
136  private HALUtil() {}
137}