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 * Counter HAL JNI functions.
009 *
010 * @see "hal/Counter.h"
011 */
012public class CounterJNI extends JNIWrapper {
013  /**
014   * Initializes a counter.
015   *
016   * @param channel the DIO channel
017   * @param risingEdge true to trigger on rising
018   * @return the created handle
019   * @see "HAL_InitializeCounter"
020   */
021  public static native int initializeCounter(int channel, boolean risingEdge);
022
023  /**
024   * Frees a counter.
025   *
026   * @param counterHandle the counter handle
027   * @see "HAL_FreeCounter"
028   */
029  public static native void freeCounter(int counterHandle);
030
031  /**
032   * Sets the up source to either detect rising edges or falling edges.
033   *
034   * <p>Note that both are allowed to be set true at the same time without issues.
035   *
036   * @param counterHandle the counter handle
037   * @param risingEdge true to trigger on rising
038   * @see "HAL_SetCounterUpSourceEdge"
039   */
040  public static native void setCounterEdgeConfiguration(int counterHandle, boolean risingEdge);
041
042  /**
043   * Resets the Counter to zero.
044   *
045   * <p>Sets the counter value to zero. This does not effect the running state of the counter, just
046   * sets the current value to zero.
047   *
048   * @param counterHandle the counter handle
049   * @see "HAL_ResetCounter"
050   */
051  public static native void resetCounter(int counterHandle);
052
053  /**
054   * Reads the current counter value.
055   *
056   * <p>Reads the value at this instant. It may still be running, so it reflects the current value.
057   * Next time it is read, it might have a different value.
058   *
059   * @param counterHandle the counter handle
060   * @return the current counter value
061   * @see "HAL_GetCounter"
062   */
063  public static native int getCounter(int counterHandle);
064
065  /**
066   * Gets the Period of the most recent count.
067   *
068   * <p>Returns the time interval of the most recent count. This can be used for velocity
069   * calculations to determine shaft speed.
070   *
071   * @param counterHandle the counter handle
072   * @return the period of the last two pulses in units of seconds
073   * @see "HAL_GetCounterPeriod"
074   */
075  public static native double getCounterPeriod(int counterHandle);
076
077  /**
078   * Sets the maximum period where the device is still considered "moving".
079   *
080   * <p>Sets the maximum period where the device is considered moving. This value is used to
081   * determine the "stopped" state of the counter using the HAL_GetCounterStopped method.
082   *
083   * @param counterHandle the counter handle
084   * @param maxPeriod the maximum period where the counted device is considered moving in seconds
085   * @see "HAL_SetCounterMaxPeriod"
086   */
087  public static native void setCounterMaxPeriod(int counterHandle, double maxPeriod);
088
089  /**
090   * Determines if the clock is stopped.
091   *
092   * <p>Determine if the clocked input is stopped based on the MaxPeriod value set using the
093   * SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the device (and counter) are
094   * assumed to be stopped and it returns true.
095   *
096   * @param counterHandle the counter handle
097   * @return true if the most recent counter period exceeds the MaxPeriod value set by SetMaxPeriod
098   * @see "HAL_GetCounterStopped"
099   */
100  public static native boolean getCounterStopped(int counterHandle);
101
102  /** Utility class. */
103  private CounterJNI() {}
104}