001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2017. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
011import edu.wpi.first.wpilibj.hal.HAL;
012import edu.wpi.first.wpilibj.livewindow.LiveWindow;
013
014/**
015 * Alias for counter class. Implement the gear tooth sensor supplied by FIRST. Currently there is no
016 * reverse sensing on the gear tooth sensor, but in future versions we might implement the necessary
017 * timing in the FPGA to sense direction.
018 */
019public class GearTooth extends Counter {
020
021  private static final double kGearToothThreshold = 55e-6;
022
023  /**
024   * Common code called by the constructors.
025   */
026  public void enableDirectionSensing(boolean directionSensitive) {
027    if (directionSensitive) {
028      setPulseLengthMode(kGearToothThreshold);
029    }
030  }
031
032  /**
033   * Construct a GearTooth sensor given a channel.
034   *
035   * <p>No direction sensing is assumed.
036   *
037   * @param channel The GPIO channel that the sensor is connected to.
038   */
039  public GearTooth(final int channel) {
040    this(channel, false);
041  }
042
043  /**
044   * Construct a GearTooth sensor given a channel.
045   *
046   * @param channel            The DIO channel that the sensor is connected to. 0-9 are on-board,
047   *                           10-25 are on the MXP port
048   * @param directionSensitive True to enable the pulse length decoding in hardware to specify count
049   *                           direction.
050   */
051  public GearTooth(final int channel, boolean directionSensitive) {
052    super(channel);
053    enableDirectionSensing(directionSensitive);
054    if (directionSensitive) {
055      HAL.report(tResourceType.kResourceType_GearTooth, channel, 0, "D");
056    } else {
057      HAL.report(tResourceType.kResourceType_GearTooth, channel, 0);
058    }
059    LiveWindow.addSensor("GearTooth", channel, this);
060  }
061
062  /**
063   * Construct a GearTooth sensor given a digital input. This should be used when sharing digital
064   * inputs.
065   *
066   * @param source             An existing DigitalSource object (such as a DigitalInput)
067   * @param directionSensitive True to enable the pulse length decoding in hardware to specify count
068   *                           direction.
069   */
070  public GearTooth(DigitalSource source, boolean directionSensitive) {
071    super(source);
072    enableDirectionSensing(directionSensitive);
073  }
074
075  /**
076   * Construct a GearTooth sensor given a digital input. This should be used when sharing digial
077   * inputs.
078   *
079   * <p>No direction sensing is assumed.
080   *
081   * @param source An object that fully descibes the input that the sensor is connected to.
082   */
083  public GearTooth(DigitalSource source) {
084    this(source, false);
085  }
086
087  /*
088   * Live Window code, only does anything if live window is activated.
089   */
090  public String getSmartDashboardType() {
091    return "Gear Tooth";
092  }
093}