blob: d0fa41b9c0ea297008c090e244b2eb62717e81b5 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080020#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080021
22using namespace frc;
23
24DigitalInput::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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080044 SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080045}
46
47DigitalInput::~DigitalInput() {
48 if (StatusIsFatal()) return;
Brian Silverman41cdd3e2019-01-19 19:48:58 -080049 HAL_FreeDIOPort(m_handle);
50}
51
Brian Silverman41cdd3e2019-01-19 19:48:58 -080052bool 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
60HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; }
61
62AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const {
63 return (AnalogTriggerType)0;
64}
65
66bool DigitalInput::IsAnalogTrigger() const { return false; }
67
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080068void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) {
69 HAL_SetDIOSimDevice(m_handle, device);
70}
71
Brian Silverman41cdd3e2019-01-19 19:48:58 -080072int DigitalInput::GetChannel() const { return m_channel; }
73
74void DigitalInput::InitSendable(SendableBuilder& builder) {
75 builder.SetSmartDashboardType("Digital Input");
76 builder.AddBooleanProperty("Value", [=]() { return Get(); }, nullptr);
77}