Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -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> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | |
| 12 | #include <hal/DIO.h> |
| 13 | #include <hal/FRCUsageReporting.h> |
| 14 | #include <hal/HALBase.h> |
| 15 | #include <hal/Ports.h> |
| 16 | |
| 17 | #include "frc/SensorUtil.h" |
| 18 | #include "frc/WPIErrors.h" |
| 19 | #include "frc/smartdashboard/SendableBuilder.h" |
| 20 | #include "frc/smartdashboard/SendableRegistry.h" |
| 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_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel); |
| 37 | m_handle = HAL_kInvalidHandle; |
| 38 | m_channel = std::numeric_limits<int>::max(); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1); |
| 43 | SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel); |
| 44 | } |
| 45 | |
| 46 | DigitalInput::~DigitalInput() { |
| 47 | if (StatusIsFatal()) return; |
| 48 | HAL_FreeDIOPort(m_handle); |
| 49 | } |
| 50 | |
| 51 | bool DigitalInput::Get() const { |
| 52 | if (StatusIsFatal()) return false; |
| 53 | int32_t status = 0; |
| 54 | bool value = HAL_GetDIO(m_handle, &status); |
| 55 | wpi_setHALError(status); |
| 56 | return value; |
| 57 | } |
| 58 | |
| 59 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; } |
| 60 | |
| 61 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 62 | return (AnalogTriggerType)0; |
| 63 | } |
| 64 | |
| 65 | bool DigitalInput::IsAnalogTrigger() const { return false; } |
| 66 | |
| 67 | void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 68 | HAL_SetDIOSimDevice(m_handle, device); |
| 69 | } |
| 70 | |
| 71 | int DigitalInput::GetChannel() const { return m_channel; } |
| 72 | |
| 73 | void DigitalInput::InitSendable(SendableBuilder& builder) { |
| 74 | builder.SetSmartDashboardType("Digital Input"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 75 | builder.AddBooleanProperty( |
| 76 | "Value", [=]() { return Get(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 77 | } |