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