blob: 6e9b09b3e63862fd8bc0b636e85584eb5a1d8843 [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/DigitalOutput.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
25DigitalOutput::DigitalOutput(int channel) {
26 m_pwmGenerator = HAL_kInvalidHandle;
27 if (!SensorUtil::CheckDigitalChannel(channel)) {
28 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
29 "Digital Channel " + wpi::Twine(channel));
30 m_channel = std::numeric_limits<int>::max();
31 return;
32 }
33 m_channel = channel;
34
35 int32_t status = 0;
36 m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), false, &status);
37 if (status != 0) {
38 wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel);
39 m_channel = std::numeric_limits<int>::max();
40 m_handle = HAL_kInvalidHandle;
41 return;
42 }
43
44 HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel + 1);
45 SendableRegistry::GetInstance().AddLW(this, "DigitalOutput", channel);
46}
47
48DigitalOutput::~DigitalOutput() {
49 if (StatusIsFatal()) return;
50 // Disable the PWM in case it was running.
51 DisablePWM();
52
53 HAL_FreeDIOPort(m_handle);
54}
55
56void DigitalOutput::Set(bool value) {
57 if (StatusIsFatal()) return;
58
59 int32_t status = 0;
60 HAL_SetDIO(m_handle, value, &status);
61 wpi_setHALError(status);
62}
63
64bool DigitalOutput::Get() const {
65 if (StatusIsFatal()) return false;
66
67 int32_t status = 0;
68 bool val = HAL_GetDIO(m_handle, &status);
69 wpi_setHALError(status);
70 return val;
71}
72
73HAL_Handle DigitalOutput::GetPortHandleForRouting() const { return m_handle; }
74
75AnalogTriggerType DigitalOutput::GetAnalogTriggerTypeForRouting() const {
76 return (AnalogTriggerType)0;
77}
78
79bool DigitalOutput::IsAnalogTrigger() const { return false; }
80
81int DigitalOutput::GetChannel() const { return m_channel; }
82
83void DigitalOutput::Pulse(double length) {
84 if (StatusIsFatal()) return;
85
86 int32_t status = 0;
87 HAL_Pulse(m_handle, length, &status);
88 wpi_setHALError(status);
89}
90
91bool DigitalOutput::IsPulsing() const {
92 if (StatusIsFatal()) return false;
93
94 int32_t status = 0;
95 bool value = HAL_IsPulsing(m_handle, &status);
96 wpi_setHALError(status);
97 return value;
98}
99
100void DigitalOutput::SetPWMRate(double rate) {
101 if (StatusIsFatal()) return;
102
103 int32_t status = 0;
104 HAL_SetDigitalPWMRate(rate, &status);
105 wpi_setHALError(status);
106}
107
108void DigitalOutput::EnablePWM(double initialDutyCycle) {
109 if (m_pwmGenerator != HAL_kInvalidHandle) return;
110
111 int32_t status = 0;
112
113 if (StatusIsFatal()) return;
114 m_pwmGenerator = HAL_AllocateDigitalPWM(&status);
115 wpi_setHALError(status);
116
117 if (StatusIsFatal()) return;
118 HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status);
119 wpi_setHALError(status);
120
121 if (StatusIsFatal()) return;
122 HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status);
123 wpi_setHALError(status);
124}
125
126void DigitalOutput::DisablePWM() {
127 if (StatusIsFatal()) return;
128 if (m_pwmGenerator == HAL_kInvalidHandle) return;
129
130 int32_t status = 0;
131
132 // Disable the output by routing to a dead bit.
133 HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil::kDigitalChannels,
134 &status);
135 wpi_setHALError(status);
136
137 HAL_FreeDigitalPWM(m_pwmGenerator, &status);
138 wpi_setHALError(status);
139
140 m_pwmGenerator = HAL_kInvalidHandle;
141}
142
143void DigitalOutput::UpdateDutyCycle(double dutyCycle) {
144 if (StatusIsFatal()) return;
145
146 int32_t status = 0;
147 HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status);
148 wpi_setHALError(status);
149}
150
151void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) {
152 HAL_SetDIOSimDevice(m_handle, device);
153}
154
155void DigitalOutput::InitSendable(SendableBuilder& builder) {
156 builder.SetSmartDashboardType("Digital Output");
157 builder.AddBooleanProperty("Value", [=]() { return Get(); },
158 [=](bool value) { Set(value); });
159}