blob: d82f5705ab6bb3a619fb3bff98b94ec1194f7e46 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -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>
Brian Silverman8fce7482020-01-05 13:18:21 -080011
12#include <hal/DIO.h>
13#include <hal/FRCUsageReporting.h>
14#include <hal/HALBase.h>
15#include <hal/Ports.h>
16
17#include "frc/SensorUtil.h"
18#include "frc/WPIErrors.h"
19#include "frc/smartdashboard/SendableBuilder.h"
20#include "frc/smartdashboard/SendableRegistry.h"
21
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_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel);
37 m_handle = HAL_kInvalidHandle;
38 m_channel = std::numeric_limits<int>::max();
39 return;
40 }
41
42 HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1);
43 SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel);
44}
45
46DigitalInput::~DigitalInput() {
47 if (StatusIsFatal()) return;
48 HAL_FreeDIOPort(m_handle);
49}
50
51bool DigitalInput::Get() const {
52 if (StatusIsFatal()) return false;
53 int32_t status = 0;
54 bool value = HAL_GetDIO(m_handle, &status);
55 wpi_setHALError(status);
56 return value;
57}
58
59HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; }
60
61AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const {
62 return (AnalogTriggerType)0;
63}
64
65bool DigitalInput::IsAnalogTrigger() const { return false; }
66
67void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) {
68 HAL_SetDIOSimDevice(m_handle, device);
69}
70
71int DigitalInput::GetChannel() const { return m_channel; }
72
73void DigitalInput::InitSendable(SendableBuilder& builder) {
74 builder.SetSmartDashboardType("Digital Input");
Austin Schuh1e69f942020-11-14 15:06:14 -080075 builder.AddBooleanProperty(
76 "Value", [=]() { return Get(); }, nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -080077}