Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2008-2018 FIRST. 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 "frc/DigitalInput.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <hal/DIO.h> |
| 14 | #include <hal/HAL.h> |
| 15 | #include <hal/Ports.h> |
| 16 | |
| 17 | #include "frc/SensorUtil.h" |
| 18 | #include "frc/WPIErrors.h" |
| 19 | #include "frc/smartdashboard/SendableBuilder.h" |
| 20 | |
| 21 | using namespace frc; |
| 22 | |
| 23 | DigitalInput::DigitalInput(int channel) { |
| 24 | if (!SensorUtil::CheckDigitalChannel(channel)) { |
| 25 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, |
| 26 | "Digital Channel " + wpi::Twine(channel)); |
| 27 | m_channel = std::numeric_limits<int>::max(); |
| 28 | return; |
| 29 | } |
| 30 | m_channel = channel; |
| 31 | |
| 32 | int32_t status = 0; |
| 33 | m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status); |
| 34 | if (status != 0) { |
| 35 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalChannels(), |
| 36 | channel, HAL_GetErrorMessage(status)); |
| 37 | m_handle = HAL_kInvalidHandle; |
| 38 | m_channel = std::numeric_limits<int>::max(); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel); |
| 43 | SetName("DigitalInput", channel); |
| 44 | } |
| 45 | |
| 46 | DigitalInput::~DigitalInput() { |
| 47 | if (StatusIsFatal()) return; |
| 48 | if (m_interrupt != HAL_kInvalidHandle) { |
| 49 | int32_t status = 0; |
| 50 | HAL_CleanInterrupts(m_interrupt, &status); |
| 51 | // Ignore status, as an invalid handle just needs to be ignored. |
| 52 | m_interrupt = HAL_kInvalidHandle; |
| 53 | } |
| 54 | |
| 55 | HAL_FreeDIOPort(m_handle); |
| 56 | } |
| 57 | |
| 58 | DigitalInput::DigitalInput(DigitalInput&& rhs) |
| 59 | : DigitalSource(std::move(rhs)), m_channel(std::move(rhs.m_channel)) { |
| 60 | std::swap(m_handle, rhs.m_handle); |
| 61 | } |
| 62 | |
| 63 | DigitalInput& DigitalInput::operator=(DigitalInput&& rhs) { |
| 64 | DigitalSource::operator=(std::move(rhs)); |
| 65 | |
| 66 | m_channel = std::move(rhs.m_channel); |
| 67 | std::swap(m_handle, rhs.m_handle); |
| 68 | |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | bool DigitalInput::Get() const { |
| 73 | if (StatusIsFatal()) return false; |
| 74 | int32_t status = 0; |
| 75 | bool value = HAL_GetDIO(m_handle, &status); |
| 76 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 77 | return value; |
| 78 | } |
| 79 | |
| 80 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; } |
| 81 | |
| 82 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 83 | return (AnalogTriggerType)0; |
| 84 | } |
| 85 | |
| 86 | bool DigitalInput::IsAnalogTrigger() const { return false; } |
| 87 | |
| 88 | int DigitalInput::GetChannel() const { return m_channel; } |
| 89 | |
| 90 | void DigitalInput::InitSendable(SendableBuilder& builder) { |
| 91 | builder.SetSmartDashboardType("Digital Input"); |
| 92 | builder.AddBooleanProperty("Value", [=]() { return Get(); }, nullptr); |
| 93 | } |