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 * Interrupt HAL JNI functions.
009 *
010 * @see "hal/Interrupts.h"
011 */
012public class InterruptJNI extends JNIWrapper {
013  public static final int HalInvalidHandle = 0;
014
015  /**
016   * Initializes an interrupt.
017   *
018   * @return the created interrupt handle
019   * @see "HAL_InitializeInterrupts"
020   */
021  public static native int initializeInterrupts();
022
023  /**
024   * Frees an interrupt.
025   *
026   * @param interruptHandle the interrupt handle
027   * @see "HAL_CleanInterrupts"
028   */
029  public static native void cleanInterrupts(int interruptHandle);
030
031  /**
032   * Waits for the defined interrupt to occur.
033   *
034   * @param interruptHandle the interrupt handle
035   * @param timeout timeout in seconds
036   * @param ignorePrevious if true, ignore interrupts that happened before waitForInterrupt was
037   *     called
038   * @return the mask of interrupts that fired
039   * @see "HAL_WaitForInterrupt"
040   */
041  public static native long waitForInterrupt(
042      int interruptHandle, double timeout, boolean ignorePrevious);
043
044  /**
045   * Waits for any interrupt covered by the mask to occur.
046   *
047   * @param interruptHandle the interrupt handle to use for the context
048   * @param mask the mask of interrupts to wait for
049   * @param timeout timeout in seconds
050   * @param ignorePrevious if true, ignore interrupts that happened before waitForInterrupt was
051   *     called
052   * @return the mask of interrupts that fired
053   * @see "HAL_WaitForMultipleInterrupts"
054   */
055  public static native long waitForMultipleInterrupts(
056      int interruptHandle, long mask, double timeout, boolean ignorePrevious);
057
058  /**
059   * Returns the timestamp for the rising interrupt that occurred most recently.
060   *
061   * <p>This is in the same time domain as getFPGATime(). It only contains the bottom 32 bits of the
062   * timestamp. If your robot has been running for over 1 hour, you will need to fill in the upper
063   * 32 bits yourself.
064   *
065   * @param interruptHandle the interrupt handle
066   * @return timestamp in microseconds since FPGA Initialization
067   */
068  public static native long readInterruptRisingTimestamp(int interruptHandle);
069
070  /**
071   * Returns the timestamp for the falling interrupt that occurred most recently.
072   *
073   * <p>This is in the same time domain as getFPGATime(). It only contains the bottom 32 bits of the
074   * timestamp. If your robot has been running for over 1 hour, you will need to fill in the upper
075   * 32 bits yourself.
076   *
077   * @param interruptHandle the interrupt handle
078   * @return timestamp in microseconds since FPGA Initialization
079   */
080  public static native long readInterruptFallingTimestamp(int interruptHandle);
081
082  /**
083   * Requests interrupts on a specific digital source.
084   *
085   * @param interruptHandle the interrupt handle
086   * @param digitalSourceHandle the digital source handle (either a HAL_AnalogTriggerHandle or a
087   *     HAL_DigitalHandle)
088   * @param analogTriggerType the trigger type if the source is an AnalogTrigger
089   * @see "HAL_RequestInterrupts"
090   */
091  public static native void requestInterrupts(
092      int interruptHandle, int digitalSourceHandle, int analogTriggerType);
093
094  /**
095   * Sets the edges to trigger the interrupt on.
096   *
097   * <p>Note that both edges triggered is a valid configuration.
098   *
099   * @param interruptHandle the interrupt handle
100   * @param risingEdge true for triggering on rising edge
101   * @param fallingEdge true for triggering on falling edge
102   * @see "HAL_SetInterruptUpSourceEdge"
103   */
104  public static native void setInterruptUpSourceEdge(
105      int interruptHandle, boolean risingEdge, boolean fallingEdge);
106
107  /**
108   * Releases a waiting interrupt.
109   *
110   * <p>This will release both rising and falling waiters.
111   *
112   * @param interruptHandle the interrupt handle to release
113   * @see "HAL_ReleaseWaitingInterrupt"
114   */
115  public static native void releaseWaitingInterrupt(int interruptHandle);
116}