Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -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 "frc971/wpilib/ahal/DigitalInput.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | #include <sstream> |
| 12 | |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 13 | #include "hal/DIO.h" |
| 14 | #include "hal/HAL.h" |
| 15 | #include "hal/Ports.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/ahal/WPIErrors.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
| 20 | /** |
| 21 | * Create an instance of a Digital Input class. |
| 22 | * |
| 23 | * Creates a digital input given a channel. |
| 24 | * |
| 25 | * @param channel The DIO channel 0-9 are on-board, 10-25 are on the MXP port |
| 26 | */ |
| 27 | DigitalInput::DigitalInput(int channel) { |
| 28 | std::stringstream buf; |
| 29 | |
| 30 | if (!CheckDigitalChannel(channel)) { |
| 31 | buf << "Digital Channel " << channel; |
| 32 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 33 | m_channel = std::numeric_limits<int>::max(); |
| 34 | return; |
| 35 | } |
| 36 | m_channel = channel; |
| 37 | |
| 38 | int32_t status = 0; |
Austin Schuh | 9950f68 | 2021-11-06 15:27:58 -0700 | [diff] [blame^] | 39 | m_handle = |
| 40 | HAL_InitializeDIOPort(HAL_GetPort(channel), true, nullptr, &status); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 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 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Free resources associated with the Digital Input class. |
| 54 | */ |
| 55 | DigitalInput::~DigitalInput() { |
| 56 | if (StatusIsFatal()) return; |
| 57 | if (m_interrupt != HAL_kInvalidHandle) { |
Austin Schuh | 9950f68 | 2021-11-06 15:27:58 -0700 | [diff] [blame^] | 58 | HAL_CleanInterrupts(m_interrupt); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 59 | m_interrupt = HAL_kInvalidHandle; |
| 60 | } |
| 61 | |
| 62 | HAL_FreeDIOPort(m_handle); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the value from a digital input channel. |
| 67 | * |
| 68 | * Retrieve the value of a single digital input channel from the FPGA. |
| 69 | */ |
| 70 | bool DigitalInput::Get() const { |
| 71 | if (StatusIsFatal()) return false; |
| 72 | int32_t status = 0; |
| 73 | bool value = HAL_GetDIO(m_handle, &status); |
| 74 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 75 | return value; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return The GPIO channel number that this object represents. |
| 80 | */ |
| 81 | int DigitalInput::GetChannel() const { return m_channel; } |
| 82 | |
| 83 | /** |
| 84 | * @return The HAL Handle to the specified source. |
| 85 | */ |
| 86 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; } |
| 87 | |
| 88 | /** |
| 89 | * Is source an AnalogTrigger |
| 90 | */ |
| 91 | bool DigitalInput::IsAnalogTrigger() const { return false; } |
| 92 | |
| 93 | /** |
| 94 | * @return The type of analog trigger output to be used. 0 for Digitals |
| 95 | */ |
| 96 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 97 | return (AnalogTriggerType)0; |
| 98 | } |