Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/DigitalInput.h" |
| 6 | |
| 7 | #include <limits> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
| 9 | #include <hal/DIO.h> |
| 10 | #include <hal/FRCUsageReporting.h> |
| 11 | #include <hal/HALBase.h> |
| 12 | #include <hal/Ports.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include <wpi/StackTrace.h> |
| 14 | #include <wpi/sendable/SendableBuilder.h> |
| 15 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "frc/SensorUtil.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace frc; |
| 21 | |
| 22 | DigitalInput::DigitalInput(int channel) { |
| 23 | if (!SensorUtil::CheckDigitalChannel(channel)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 24 | throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | } |
| 26 | m_channel = channel; |
| 27 | |
| 28 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | std::string stackTrace = wpi::GetStackTrace(1); |
| 30 | m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, |
| 31 | stackTrace.c_str(), &status); |
| 32 | FRC_CheckErrorStatus(status, "Channel {}", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | |
| 34 | HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 35 | wpi::SendableRegistry::AddLW(this, "DigitalInput", channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DigitalInput::~DigitalInput() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | HAL_FreeDIOPort(m_handle); |
| 40 | } |
| 41 | |
| 42 | bool DigitalInput::Get() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | int32_t status = 0; |
| 44 | bool value = HAL_GetDIO(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 45 | FRC_CheckErrorStatus(status, "Channel {}", m_channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | return value; |
| 47 | } |
| 48 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 49 | HAL_Handle DigitalInput::GetPortHandleForRouting() const { |
| 50 | return m_handle; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 53 | AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const { |
| 54 | return static_cast<AnalogTriggerType>(0); |
| 55 | } |
| 56 | |
| 57 | bool DigitalInput::IsAnalogTrigger() const { |
| 58 | return false; |
| 59 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | |
| 61 | void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 62 | HAL_SetDIOSimDevice(m_handle, device); |
| 63 | } |
| 64 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | int DigitalInput::GetChannel() const { |
| 66 | return m_channel; |
| 67 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 69 | void DigitalInput::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | builder.SetSmartDashboardType("Digital Input"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 71 | builder.AddBooleanProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 72 | "Value", [=, this] { return Get(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | } |