001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2012. 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 java.nio.ByteBuffer;
011import java.nio.ByteOrder;
012
013import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
014import edu.wpi.first.wpilibj.communication.UsageReporting;
015import edu.wpi.first.wpilibj.hal.DIOJNI;
016import edu.wpi.first.wpilibj.hal.HALUtil;
017import edu.wpi.first.wpilibj.livewindow.LiveWindow;
018import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
019import edu.wpi.first.wpilibj.tables.ITable;
020
021/**
022 * Class to read a digital input. This class will read digital inputs and return
023 * the current value on the channel. Other devices such as encoders, gear tooth
024 * sensors, etc. that are implemented elsewhere will automatically allocate
025 * digital inputs and outputs as required. This class is only for devices like
026 * switches etc. that aren't implemented anywhere else.
027 */
028public class DigitalInput extends DigitalSource implements LiveWindowSendable {
029
030        /**
031         * Create an instance of a Digital Input class. Creates a digital input
032         * given a channel.
033         *
034         * @param channel
035         *            the DIO channel for the digital input 0-9 are on-board, 10-25 are on the MXP
036         */
037        public DigitalInput(int channel) {
038        initDigitalPort(channel, true);
039
040        LiveWindow.addSensor("DigitalInput", channel, this);
041        UsageReporting.report(tResourceType.kResourceType_DigitalInput, channel);
042        }
043
044        /**
045         * Get the value from a digital input channel. Retrieve the value of a
046         * single digital input channel from the FPGA.
047         *
048         * @return the status of the digital input
049         */
050        public boolean get() {
051                ByteBuffer status = ByteBuffer.allocateDirect(4);
052                // set the byte order
053                status.order(ByteOrder.LITTLE_ENDIAN);
054                boolean value = DIOJNI.getDIO(m_port, status.asIntBuffer()) != 0;
055                HALUtil.checkStatus(status.asIntBuffer());
056                return value;
057        }
058
059        /**
060         * Get the channel of the digital input
061         *
062         * @return The GPIO channel number that this object represents.
063         */
064        public int getChannel() {
065                return m_channel;
066        }
067
068        @Override
069        public boolean getAnalogTriggerForRouting() {
070                return false;
071        }
072
073        /*
074         * Live Window code, only does anything if live window is activated.
075         */
076        @Override
077        public String getSmartDashboardType() {
078                return "Digital Input";
079        }
080
081        private ITable m_table;
082
083        /**
084         * {@inheritDoc}
085         */
086        @Override
087        public void initTable(ITable subtable) {
088                m_table = subtable;
089                updateTable();
090        }
091
092        /**
093         * {@inheritDoc}
094         */
095        @Override
096        public void updateTable() {
097                if (m_table != null) {
098                        m_table.putBoolean("Value", get());
099                }
100        }
101
102        /**
103         * {@inheritDoc}
104         */
105        @Override
106        public ITable getTable() {
107                return m_table;
108        }
109
110        /**
111         * {@inheritDoc}
112         */
113        @Override
114        public void startLiveWindowMode() {
115        }
116
117        /**
118         * {@inheritDoc}
119         */
120        @Override
121        public void stopLiveWindowMode() {
122        }
123}