blob: 7210750ad5276adb293f53e716d81eefc8a50610 [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>
James Kuszmaul4b81d302019-12-14 20:53:14 -080014#include <hal/FRCUsageReporting.h>
15#include <hal/HALBase.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080016#include <hal/Ports.h>
17
18#include "frc/SensorUtil.h"
19#include "frc/WPIErrors.h"
20#include "frc/smartdashboard/SendableBuilder.h"
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080021#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080022
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) {
James Kuszmaul4b81d302019-12-14 20:53:14 -080037 wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080038 m_handle = HAL_kInvalidHandle;
39 m_channel = std::numeric_limits<int>::max();
40 return;
41 }
42
James Kuszmaul4b81d302019-12-14 20:53:14 -080043 HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1);
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);
James Kuszmaul4b81d302019-12-14 20:53:14 -080056 wpi_setHALError(status);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080057 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}