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