blob: 7210750ad5276adb293f53e716d81eefc8a50610 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
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>
11#include <utility>
12
13#include <hal/DIO.h>
14#include <hal/FRCUsageReporting.h>
15#include <hal/HALBase.h>
16#include <hal/Ports.h>
17
18#include "frc/SensorUtil.h"
19#include "frc/WPIErrors.h"
20#include "frc/smartdashboard/SendableBuilder.h"
21#include "frc/smartdashboard/SendableRegistry.h"
22
23using namespace frc;
24
25DigitalInput::DigitalInput(int channel) {
26 if (!SensorUtil::CheckDigitalChannel(channel)) {
27 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
28 "Digital Channel " + wpi::Twine(channel));
29 m_channel = std::numeric_limits<int>::max();
30 return;
31 }
32 m_channel = channel;
33
34 int32_t status = 0;
35 m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status);
36 if (status != 0) {
37 wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel);
38 m_handle = HAL_kInvalidHandle;
39 m_channel = std::numeric_limits<int>::max();
40 return;
41 }
42
43 HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1);
44 SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel);
45}
46
47DigitalInput::~DigitalInput() {
48 if (StatusIsFatal()) return;
49 HAL_FreeDIOPort(m_handle);
50}
51
52bool DigitalInput::Get() const {
53 if (StatusIsFatal()) return false;
54 int32_t status = 0;
55 bool value = HAL_GetDIO(m_handle, &status);
56 wpi_setHALError(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
68void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) {
69 HAL_SetDIOSimDevice(m_handle, device);
70}
71
72int 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}