blob: 02757415de3ef06d000c35e66bbe2520b8e97268 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/DigitalInput.h"
6
7#include <limits>
Brian Silverman8fce7482020-01-05 13:18:21 -08008
9#include <hal/DIO.h>
10#include <hal/FRCUsageReporting.h>
11#include <hal/HALBase.h>
12#include <hal/Ports.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070013#include <wpi/StackTrace.h>
14#include <wpi/sendable/SendableBuilder.h>
15#include <wpi/sendable/SendableRegistry.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080016
Austin Schuh812d0d12021-11-04 20:16:48 -070017#include "frc/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080018#include "frc/SensorUtil.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080019
20using namespace frc;
21
22DigitalInput::DigitalInput(int channel) {
23 if (!SensorUtil::CheckDigitalChannel(channel)) {
Austin Schuh812d0d12021-11-04 20:16:48 -070024 throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080025 }
26 m_channel = channel;
27
28 int32_t status = 0;
Austin Schuh812d0d12021-11-04 20:16:48 -070029 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 Silverman8fce7482020-01-05 13:18:21 -080033
34 HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1);
Austin Schuh812d0d12021-11-04 20:16:48 -070035 wpi::SendableRegistry::AddLW(this, "DigitalInput", channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080036}
37
38DigitalInput::~DigitalInput() {
Brian Silverman8fce7482020-01-05 13:18:21 -080039 HAL_FreeDIOPort(m_handle);
40}
41
42bool DigitalInput::Get() const {
Brian Silverman8fce7482020-01-05 13:18:21 -080043 int32_t status = 0;
44 bool value = HAL_GetDIO(m_handle, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070045 FRC_CheckErrorStatus(status, "Channel {}", m_channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080046 return value;
47}
48
Austin Schuh812d0d12021-11-04 20:16:48 -070049HAL_Handle DigitalInput::GetPortHandleForRouting() const {
50 return m_handle;
Brian Silverman8fce7482020-01-05 13:18:21 -080051}
52
Austin Schuh812d0d12021-11-04 20:16:48 -070053AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const {
54 return static_cast<AnalogTriggerType>(0);
55}
56
57bool DigitalInput::IsAnalogTrigger() const {
58 return false;
59}
Brian Silverman8fce7482020-01-05 13:18:21 -080060
61void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) {
62 HAL_SetDIOSimDevice(m_handle, device);
63}
64
Austin Schuh812d0d12021-11-04 20:16:48 -070065int DigitalInput::GetChannel() const {
66 return m_channel;
67}
Brian Silverman8fce7482020-01-05 13:18:21 -080068
Austin Schuh812d0d12021-11-04 20:16:48 -070069void DigitalInput::InitSendable(wpi::SendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -080070 builder.SetSmartDashboardType("Digital Input");
Austin Schuh1e69f942020-11-14 15:06:14 -080071 builder.AddBooleanProperty(
James Kuszmaulcf324122023-01-14 14:07:17 -080072 "Value", [=, this] { return Get(); }, nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -080073}