blob: 2068b5dec0a56ee5d1fe805a1aa56d4b1b56e0cd [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/DigitalOutput.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
24DigitalOutput::DigitalOutput(int channel) {
25 m_pwmGenerator = HAL_kInvalidHandle;
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), false, &status);
36 if (status != 0) {
37 wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalChannels(),
38 channel, HAL_GetErrorMessage(status));
39 m_channel = std::numeric_limits<int>::max();
40 m_handle = HAL_kInvalidHandle;
41 return;
42 }
43
44 HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel);
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080045 SendableRegistry::GetInstance().AddLW(this, "DigitalOutput", channel);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080046}
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
Brian Silverman41cdd3e2019-01-19 19:48:58 -080056void DigitalOutput::Set(bool value) {
57 if (StatusIsFatal()) return;
58
59 int32_t status = 0;
60 HAL_SetDIO(m_handle, value, &status);
61 wpi_setErrorWithContext(status, HAL_GetErrorMessage(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_setErrorWithContext(status, HAL_GetErrorMessage(status));
70 return val;
71}
72
73int DigitalOutput::GetChannel() const { return m_channel; }
74
75void DigitalOutput::Pulse(double length) {
76 if (StatusIsFatal()) return;
77
78 int32_t status = 0;
79 HAL_Pulse(m_handle, length, &status);
80 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
81}
82
83bool DigitalOutput::IsPulsing() const {
84 if (StatusIsFatal()) return false;
85
86 int32_t status = 0;
87 bool value = HAL_IsPulsing(m_handle, &status);
88 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
89 return value;
90}
91
92void DigitalOutput::SetPWMRate(double rate) {
93 if (StatusIsFatal()) return;
94
95 int32_t status = 0;
96 HAL_SetDigitalPWMRate(rate, &status);
97 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
98}
99
100void DigitalOutput::EnablePWM(double initialDutyCycle) {
101 if (m_pwmGenerator != HAL_kInvalidHandle) return;
102
103 int32_t status = 0;
104
105 if (StatusIsFatal()) return;
106 m_pwmGenerator = HAL_AllocateDigitalPWM(&status);
107 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
108
109 if (StatusIsFatal()) return;
110 HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status);
111 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
112
113 if (StatusIsFatal()) return;
114 HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status);
115 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
116}
117
118void DigitalOutput::DisablePWM() {
119 if (StatusIsFatal()) return;
120 if (m_pwmGenerator == HAL_kInvalidHandle) return;
121
122 int32_t status = 0;
123
124 // Disable the output by routing to a dead bit.
125 HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil::kDigitalChannels,
126 &status);
127 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
128
129 HAL_FreeDigitalPWM(m_pwmGenerator, &status);
130 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
131
132 m_pwmGenerator = HAL_kInvalidHandle;
133}
134
135void DigitalOutput::UpdateDutyCycle(double dutyCycle) {
136 if (StatusIsFatal()) return;
137
138 int32_t status = 0;
139 HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status);
140 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
141}
142
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800143void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) {
144 HAL_SetDIOSimDevice(m_handle, device);
145}
146
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800147void DigitalOutput::InitSendable(SendableBuilder& builder) {
148 builder.SetSmartDashboardType("Digital Output");
149 builder.AddBooleanProperty("Value", [=]() { return Get(); },
150 [=](bool value) { Set(value); });
151}