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