James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 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/DutyCycle.h" |
| 9 | |
| 10 | #include <hal/DutyCycle.h> |
| 11 | #include <hal/FRCUsageReporting.h> |
| 12 | |
| 13 | #include "frc/DigitalSource.h" |
| 14 | #include "frc/WPIErrors.h" |
| 15 | #include "frc/smartdashboard/SendableBuilder.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | DutyCycle::DutyCycle(DigitalSource* source) |
| 20 | : m_source{source, NullDeleter<DigitalSource>()} { |
| 21 | if (m_source == nullptr) { |
| 22 | wpi_setWPIError(NullParameter); |
| 23 | } else { |
| 24 | InitDutyCycle(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | DutyCycle::DutyCycle(DigitalSource& source) |
| 29 | : m_source{&source, NullDeleter<DigitalSource>()} { |
| 30 | InitDutyCycle(); |
| 31 | } |
| 32 | |
| 33 | DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source) |
| 34 | : m_source{std::move(source)} { |
| 35 | if (m_source == nullptr) { |
| 36 | wpi_setWPIError(NullParameter); |
| 37 | } else { |
| 38 | InitDutyCycle(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | DutyCycle::~DutyCycle() { HAL_FreeDutyCycle(m_handle); } |
| 43 | |
| 44 | void DutyCycle::InitDutyCycle() { |
| 45 | int32_t status = 0; |
| 46 | m_handle = |
| 47 | HAL_InitializeDutyCycle(m_source->GetPortHandleForRouting(), |
| 48 | static_cast<HAL_AnalogTriggerType>( |
| 49 | m_source->GetAnalogTriggerTypeForRouting()), |
| 50 | &status); |
| 51 | wpi_setHALError(status); |
| 52 | int index = GetFPGAIndex(); |
| 53 | HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1); |
| 54 | SendableRegistry::GetInstance().AddLW(this, "Duty Cycle", index); |
| 55 | } |
| 56 | |
| 57 | int DutyCycle::GetFPGAIndex() const { |
| 58 | int32_t status = 0; |
| 59 | auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status); |
| 60 | wpi_setHALError(status); |
| 61 | return retVal; |
| 62 | } |
| 63 | |
| 64 | int DutyCycle::GetFrequency() const { |
| 65 | int32_t status = 0; |
| 66 | auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status); |
| 67 | wpi_setHALError(status); |
| 68 | return retVal; |
| 69 | } |
| 70 | |
| 71 | double DutyCycle::GetOutput() const { |
| 72 | int32_t status = 0; |
| 73 | auto retVal = HAL_GetDutyCycleOutput(m_handle, &status); |
| 74 | wpi_setHALError(status); |
| 75 | return retVal; |
| 76 | } |
| 77 | |
| 78 | unsigned int DutyCycle::GetOutputRaw() const { |
| 79 | int32_t status = 0; |
| 80 | auto retVal = HAL_GetDutyCycleOutputRaw(m_handle, &status); |
| 81 | wpi_setHALError(status); |
| 82 | return retVal; |
| 83 | } |
| 84 | |
| 85 | unsigned int DutyCycle::GetOutputScaleFactor() const { |
| 86 | int32_t status = 0; |
| 87 | auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status); |
| 88 | wpi_setHALError(status); |
| 89 | return retVal; |
| 90 | } |
| 91 | |
| 92 | int DutyCycle::GetSourceChannel() const { return m_source->GetChannel(); } |
| 93 | |
| 94 | void DutyCycle::InitSendable(SendableBuilder& builder) { |
| 95 | builder.SetSmartDashboardType("Duty Cycle"); |
| 96 | builder.AddDoubleProperty("Frequency", |
| 97 | [this] { return this->GetFrequency(); }, nullptr); |
| 98 | builder.AddDoubleProperty("Output", [this] { return this->GetOutput(); }, |
| 99 | nullptr); |
| 100 | } |