Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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" |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 20 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 21 | |
| 22 | using namespace frc; |
| 23 | |
| 24 | DigitalInput::DigitalInput(int channel) { |
| 25 | if (!SensorUtil::CheckDigitalChannel(channel)) { |
| 26 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, |
| 27 | "Digital Channel " + wpi::Twine(channel)); |
| 28 | m_channel = std::numeric_limits<int>::max(); |
| 29 | return; |
| 30 | } |
| 31 | m_channel = channel; |
| 32 | |
| 33 | int32_t status = 0; |
| 34 | m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status); |
| 35 | if (status != 0) { |
| 36 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalChannels(), |
| 37 | channel, HAL_GetErrorMessage(status)); |
| 38 | m_handle = HAL_kInvalidHandle; |
| 39 | m_channel = std::numeric_limits<int>::max(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 44 | SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | DigitalInput::~DigitalInput() { |
| 48 | if (StatusIsFatal()) return; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 49 | HAL_FreeDIOPort(m_handle); |
| 50 | } |
| 51 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 52 | bool DigitalInput::Get() const { |
| 53 | if (StatusIsFatal()) return false; |
| 54 | int32_t status = 0; |
| 55 | bool value = HAL_GetDIO(m_handle, &status); |
| 56 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 57 | return value; |
| 58 | } |
| 59 | |
| 60 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; } |
| 61 | |
| 62 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 63 | return (AnalogTriggerType)0; |
| 64 | } |
| 65 | |
| 66 | bool DigitalInput::IsAnalogTrigger() const { return false; } |
| 67 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 68 | void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 69 | HAL_SetDIOSimDevice(m_handle, device); |
| 70 | } |
| 71 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 72 | int DigitalInput::GetChannel() const { return m_channel; } |
| 73 | |
| 74 | void DigitalInput::InitSendable(SendableBuilder& builder) { |
| 75 | builder.SetSmartDashboardType("Digital Input"); |
| 76 | builder.AddBooleanProperty("Value", [=]() { return Get(); }, nullptr); |
| 77 | } |