001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2008-2018 FIRST. 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.smartdashboard.SendableBuilder;
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  private static final double kGearToothThreshold = 55e-6;
021
022  /**
023   * Common code called by the constructors.
024   */
025  public void enableDirectionSensing(boolean directionSensitive) {
026    if (directionSensitive) {
027      setPulseLengthMode(kGearToothThreshold);
028    }
029  }
030
031  /**
032   * Construct a GearTooth sensor given a channel.
033   *
034   * <p>No direction sensing is assumed.
035   *
036   * @param channel The GPIO channel that the sensor is connected to.
037   */
038  public GearTooth(final int channel) {
039    this(channel, false);
040  }
041
042  /**
043   * Construct a GearTooth sensor given a channel.
044   *
045   * @param channel            The DIO channel that the sensor is connected to. 0-9 are on-board,
046   *                           10-25 are on the MXP port
047   * @param directionSensitive True to enable the pulse length decoding in hardware to specify count
048   *                           direction.
049   */
050  public GearTooth(final int channel, boolean directionSensitive) {
051    super(channel);
052    enableDirectionSensing(directionSensitive);
053    if (directionSensitive) {
054      HAL.report(tResourceType.kResourceType_GearTooth, channel, 0, "D");
055    } else {
056      HAL.report(tResourceType.kResourceType_GearTooth, channel, 0);
057    }
058    setName("GearTooth", channel);
059  }
060
061  /**
062   * Construct a GearTooth sensor given a digital input. This should be used when sharing digital
063   * inputs.
064   *
065   * @param source             An existing DigitalSource object (such as a DigitalInput)
066   * @param directionSensitive True to enable the pulse length decoding in hardware to specify count
067   *                           direction.
068   */
069  public GearTooth(DigitalSource source, boolean directionSensitive) {
070    super(source);
071    enableDirectionSensing(directionSensitive);
072    if (directionSensitive) {
073      HAL.report(tResourceType.kResourceType_GearTooth, source.getChannel(), 0, "D");
074    } else {
075      HAL.report(tResourceType.kResourceType_GearTooth, source.getChannel(), 0);
076    }
077    setName("GearTooth", source.getChannel());
078  }
079
080  /**
081   * Construct a GearTooth sensor given a digital input. This should be used when sharing digital
082   * inputs.
083   *
084   * <p>No direction sensing is assumed.
085   *
086   * @param source An object that fully describes the input that the sensor is connected to.
087   */
088  public GearTooth(DigitalSource source) {
089    this(source, false);
090  }
091
092  @Override
093  public void initSendable(SendableBuilder builder) {
094    super.initSendable(builder);
095    builder.setSmartDashboardType("Gear Tooth");
096  }
097}