Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 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> |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 14 | #include <hal/FRCUsageReporting.h> |
| 15 | #include <hal/HALBase.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 16 | #include <hal/Ports.h> |
| 17 | |
| 18 | #include "frc/SensorUtil.h" |
| 19 | #include "frc/WPIErrors.h" |
| 20 | #include "frc/smartdashboard/SendableBuilder.h" |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 21 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 22 | |
| 23 | using namespace frc; |
| 24 | |
| 25 | DigitalOutput::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) { |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 38 | wpi_setHALErrorWithRange(status, 0, HAL_GetNumDigitalChannels(), channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 39 | m_channel = std::numeric_limits<int>::max(); |
| 40 | m_handle = HAL_kInvalidHandle; |
| 41 | return; |
| 42 | } |
| 43 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 44 | HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel + 1); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 45 | SendableRegistry::GetInstance().AddLW(this, "DigitalOutput", channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DigitalOutput::~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 Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 56 | void DigitalOutput::Set(bool value) { |
| 57 | if (StatusIsFatal()) return; |
| 58 | |
| 59 | int32_t status = 0; |
| 60 | HAL_SetDIO(m_handle, value, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 61 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool DigitalOutput::Get() const { |
| 65 | if (StatusIsFatal()) return false; |
| 66 | |
| 67 | int32_t status = 0; |
| 68 | bool val = HAL_GetDIO(m_handle, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 69 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 70 | return val; |
| 71 | } |
| 72 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 73 | HAL_Handle DigitalOutput::GetPortHandleForRouting() const { return m_handle; } |
| 74 | |
| 75 | AnalogTriggerType DigitalOutput::GetAnalogTriggerTypeForRouting() const { |
| 76 | return (AnalogTriggerType)0; |
| 77 | } |
| 78 | |
| 79 | bool DigitalOutput::IsAnalogTrigger() const { return false; } |
| 80 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 81 | int DigitalOutput::GetChannel() const { return m_channel; } |
| 82 | |
| 83 | void DigitalOutput::Pulse(double length) { |
| 84 | if (StatusIsFatal()) return; |
| 85 | |
| 86 | int32_t status = 0; |
| 87 | HAL_Pulse(m_handle, length, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 88 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool DigitalOutput::IsPulsing() const { |
| 92 | if (StatusIsFatal()) return false; |
| 93 | |
| 94 | int32_t status = 0; |
| 95 | bool value = HAL_IsPulsing(m_handle, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 96 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 97 | return value; |
| 98 | } |
| 99 | |
| 100 | void DigitalOutput::SetPWMRate(double rate) { |
| 101 | if (StatusIsFatal()) return; |
| 102 | |
| 103 | int32_t status = 0; |
| 104 | HAL_SetDigitalPWMRate(rate, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 105 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void 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); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 115 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 116 | |
| 117 | if (StatusIsFatal()) return; |
| 118 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 119 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 120 | |
| 121 | if (StatusIsFatal()) return; |
| 122 | HAL_SetDigitalPWMOutputChannel(m_pwmGenerator, m_channel, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 123 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void 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); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 135 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 136 | |
| 137 | HAL_FreeDigitalPWM(m_pwmGenerator, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 138 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 139 | |
| 140 | m_pwmGenerator = HAL_kInvalidHandle; |
| 141 | } |
| 142 | |
| 143 | void DigitalOutput::UpdateDutyCycle(double dutyCycle) { |
| 144 | if (StatusIsFatal()) return; |
| 145 | |
| 146 | int32_t status = 0; |
| 147 | HAL_SetDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle, &status); |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 148 | wpi_setHALError(status); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 149 | } |
| 150 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 151 | void DigitalOutput::SetSimDevice(HAL_SimDeviceHandle device) { |
| 152 | HAL_SetDIOSimDevice(m_handle, device); |
| 153 | } |
| 154 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 155 | void DigitalOutput::InitSendable(SendableBuilder& builder) { |
| 156 | builder.SetSmartDashboardType("Digital Output"); |
| 157 | builder.AddBooleanProperty("Value", [=]() { return Get(); }, |
| 158 | [=](bool value) { Set(value); }); |
| 159 | } |