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) 2014-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/AnalogOutput.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <hal/HAL.h> |
| 14 | #include <hal/Ports.h> |
| 15 | |
| 16 | #include "frc/SensorUtil.h" |
| 17 | #include "frc/WPIErrors.h" |
| 18 | #include "frc/smartdashboard/SendableBuilder.h" |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 19 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 20 | |
| 21 | using namespace frc; |
| 22 | |
| 23 | AnalogOutput::AnalogOutput(int channel) { |
| 24 | if (!SensorUtil::CheckAnalogOutputChannel(channel)) { |
| 25 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, |
| 26 | "analog output " + wpi::Twine(channel)); |
| 27 | m_channel = std::numeric_limits<int>::max(); |
| 28 | m_port = HAL_kInvalidHandle; |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | m_channel = channel; |
| 33 | |
| 34 | HAL_PortHandle port = HAL_GetPort(m_channel); |
| 35 | int32_t status = 0; |
| 36 | m_port = HAL_InitializeAnalogOutputPort(port, &status); |
| 37 | if (status != 0) { |
| 38 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumAnalogOutputs(), channel, |
| 39 | HAL_GetErrorMessage(status)); |
| 40 | m_channel = std::numeric_limits<int>::max(); |
| 41 | m_port = HAL_kInvalidHandle; |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 46 | SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); } |
| 50 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 51 | void AnalogOutput::SetVoltage(double voltage) { |
| 52 | int32_t status = 0; |
| 53 | HAL_SetAnalogOutput(m_port, voltage, &status); |
| 54 | |
| 55 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 56 | } |
| 57 | |
| 58 | double AnalogOutput::GetVoltage() const { |
| 59 | int32_t status = 0; |
| 60 | double voltage = HAL_GetAnalogOutput(m_port, &status); |
| 61 | |
| 62 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 63 | |
| 64 | return voltage; |
| 65 | } |
| 66 | |
| 67 | int AnalogOutput::GetChannel() { return m_channel; } |
| 68 | |
| 69 | void AnalogOutput::InitSendable(SendableBuilder& builder) { |
| 70 | builder.SetSmartDashboardType("Analog Output"); |
| 71 | builder.AddDoubleProperty("Value", [=]() { return GetVoltage(); }, |
| 72 | [=](double value) { SetVoltage(value); }); |
| 73 | } |