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;
006
007/**
008 * Interface for counting the number of ticks on a digital input channel. Encoders, Gear tooth
009 * sensors, and counters should all subclass this in order to build more advanced classes for
010 * control and driving.
011 *
012 * <p>All counters will immediately start counting - reset() them if you need them to be zeroed
013 * before use.
014 */
015public interface CounterBase {
016  /** The number of edges for the CounterBase to increment or decrement on. */
017  enum EncodingType {
018    /** Count only the rising edge. */
019    k1X(0),
020    /** Count both the rising and falling edge. */
021    k2X(1),
022    /** Count rising and falling on both channels. */
023    k4X(2);
024
025    /** EncodingType value. */
026    public final int value;
027
028    EncodingType(int value) {
029      this.value = value;
030    }
031  }
032
033  /**
034   * Get the count.
035   *
036   * @return the count
037   */
038  int get();
039
040  /** Reset the count to zero. */
041  void reset();
042
043  /**
044   * Get the time between the last two edges counted.
045   *
046   * @return the time between the last two ticks in seconds
047   */
048  double getPeriod();
049
050  /**
051   * Set the maximum time between edges to be considered stalled.
052   *
053   * @param maxPeriod the maximum period in seconds
054   */
055  void setMaxPeriod(double maxPeriod);
056
057  /**
058   * Determine if the counter is not moving.
059   *
060   * @return true if the counter has not changed for the max period
061   */
062  boolean getStopped();
063
064  /**
065   * Determine which direction the counter is going.
066   *
067   * @return true for one direction, false for the other
068   */
069  boolean getDirection();
070}