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.DIOJNI;
011import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
012import edu.wpi.first.wpilibj.hal.HAL;
013import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
014
015/**
016 * Class to read a digital input. This class will read digital inputs and return the current value
017 * on the channel. Other devices such as encoders, gear tooth sensors, etc. that are implemented
018 * elsewhere will automatically allocate digital inputs and outputs as required. This class is only
019 * for devices like switches etc. that aren't implemented anywhere else.
020 */
021public class DigitalInput extends DigitalSource implements Sendable {
022  private int m_channel = 0;
023  private int m_handle = 0;
024
025  /**
026   * Create an instance of a Digital Input class. Creates a digital input given a channel.
027   *
028   * @param channel the DIO channel for the digital input 0-9 are on-board, 10-25 are on the MXP
029   */
030  public DigitalInput(int channel) {
031    checkDigitalChannel(channel);
032    m_channel = channel;
033
034    m_handle = DIOJNI.initializeDIOPort(DIOJNI.getPort((byte) channel), true);
035
036    HAL.report(tResourceType.kResourceType_DigitalInput, channel);
037    setName("DigitalInput", channel);
038  }
039
040  /**
041   * Frees the resources for this output.
042   */
043  public void free() {
044    super.free();
045    if (m_interrupt != 0) {
046      cancelInterrupts();
047    }
048
049    DIOJNI.freeDIOPort(m_handle);
050  }
051
052  /**
053   * Get the value from a digital input channel. Retrieve the value of a single digital input
054   * channel from the FPGA.
055   *
056   * @return the status of the digital input
057   */
058  public boolean get() {
059    return DIOJNI.getDIO(m_handle);
060  }
061
062  /**
063   * Get the channel of the digital input.
064   *
065   * @return The GPIO channel number that this object represents.
066   */
067  @Override
068  public int getChannel() {
069    return m_channel;
070  }
071
072  /**
073   * Get the analog trigger type.
074   *
075   * @return false
076   */
077  @Override
078  public int getAnalogTriggerTypeForRouting() {
079    return 0;
080  }
081
082  /**
083   * Is this an analog trigger.
084   *
085   * @return true if this is an analog trigger
086   */
087  @Override
088  public boolean isAnalogTrigger() {
089    return false;
090  }
091
092  /**
093   * Get the HAL Port Handle.
094   *
095   * @return The HAL Handle to the specified source.
096   */
097  @Override
098  public int getPortHandleForRouting() {
099    return m_handle;
100  }
101
102  @Override
103  public void initSendable(SendableBuilder builder) {
104    builder.setSmartDashboardType("Digital Input");
105    builder.addBooleanProperty("Value", this::get, null);
106  }
107}