Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "DigitalInput.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | #include <sstream> |
| 12 | |
| 13 | #include "HAL/DIO.h" |
| 14 | #include "HAL/HAL.h" |
| 15 | #include "HAL/Ports.h" |
| 16 | #include "LiveWindow/LiveWindow.h" |
| 17 | #include "WPIErrors.h" |
| 18 | |
| 19 | using namespace frc; |
| 20 | |
| 21 | /** |
| 22 | * Create an instance of a Digital Input class. |
| 23 | * |
| 24 | * Creates a digital input given a channel. |
| 25 | * |
| 26 | * @param channel The DIO channel 0-9 are on-board, 10-25 are on the MXP port |
| 27 | */ |
| 28 | DigitalInput::DigitalInput(int channel) { |
| 29 | std::stringstream buf; |
| 30 | |
| 31 | if (!CheckDigitalChannel(channel)) { |
| 32 | buf << "Digital Channel " << channel; |
| 33 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 34 | m_channel = std::numeric_limits<int>::max(); |
| 35 | return; |
| 36 | } |
| 37 | m_channel = channel; |
| 38 | |
| 39 | int32_t status = 0; |
| 40 | m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status); |
| 41 | if (status != 0) { |
| 42 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalChannels(), |
| 43 | channel, HAL_GetErrorMessage(status)); |
| 44 | m_handle = HAL_kInvalidHandle; |
| 45 | m_channel = std::numeric_limits<int>::max(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | LiveWindow::GetInstance()->AddSensor("DigitalInput", channel, this); |
| 50 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Free resources associated with the Digital Input class. |
| 55 | */ |
| 56 | DigitalInput::~DigitalInput() { |
| 57 | if (StatusIsFatal()) return; |
| 58 | if (m_interrupt != HAL_kInvalidHandle) { |
| 59 | int32_t status = 0; |
| 60 | HAL_CleanInterrupts(m_interrupt, &status); |
| 61 | // ignore status, as an invalid handle just needs to be ignored. |
| 62 | m_interrupt = HAL_kInvalidHandle; |
| 63 | } |
| 64 | |
| 65 | HAL_FreeDIOPort(m_handle); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the value from a digital input channel. |
| 70 | * |
| 71 | * Retrieve the value of a single digital input channel from the FPGA. |
| 72 | */ |
| 73 | bool DigitalInput::Get() const { |
| 74 | if (StatusIsFatal()) return false; |
| 75 | int32_t status = 0; |
| 76 | bool value = HAL_GetDIO(m_handle, &status); |
| 77 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return The GPIO channel number that this object represents. |
| 83 | */ |
| 84 | int DigitalInput::GetChannel() const { return m_channel; } |
| 85 | |
| 86 | /** |
| 87 | * @return The HAL Handle to the specified source. |
| 88 | */ |
| 89 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; } |
| 90 | |
| 91 | /** |
| 92 | * Is source an AnalogTrigger |
| 93 | */ |
| 94 | bool DigitalInput::IsAnalogTrigger() const { return false; } |
| 95 | |
| 96 | /** |
| 97 | * @return The type of analog trigger output to be used. 0 for Digitals |
| 98 | */ |
| 99 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 100 | return (AnalogTriggerType)0; |
| 101 | } |
| 102 | |
| 103 | void DigitalInput::UpdateTable() { |
| 104 | if (m_table != nullptr) { |
| 105 | m_table->PutBoolean("Value", Get()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void DigitalInput::StartLiveWindowMode() {} |
| 110 | |
| 111 | void DigitalInput::StopLiveWindowMode() {} |
| 112 | |
| 113 | std::string DigitalInput::GetSmartDashboardType() const { |
| 114 | return "DigitalInput"; |
| 115 | } |
| 116 | |
| 117 | void DigitalInput::InitTable(std::shared_ptr<ITable> subTable) { |
| 118 | m_table = subTable; |
| 119 | UpdateTable(); |
| 120 | } |
| 121 | |
| 122 | std::shared_ptr<ITable> DigitalInput::GetTable() const { return m_table; } |