Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. 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 "AnalogTriggerOutput.h" |
| 9 | |
| 10 | #include "AnalogTrigger.h" |
| 11 | #include "HAL/HAL.h" |
| 12 | #include "WPIErrors.h" |
| 13 | |
| 14 | using namespace frc; |
| 15 | |
| 16 | /** |
| 17 | * Create an object that represents one of the four outputs from an analog |
| 18 | * trigger. |
| 19 | * |
| 20 | * Because this class derives from DigitalSource, it can be passed into routing |
| 21 | * functions for Counter, Encoder, etc. |
| 22 | * |
| 23 | * @param trigger A pointer to the trigger for which this is an output. |
| 24 | * @param outputType An enum that specifies the output on the trigger to |
| 25 | * represent. |
| 26 | */ |
| 27 | AnalogTriggerOutput::AnalogTriggerOutput(const AnalogTrigger& trigger, |
| 28 | AnalogTriggerType outputType) |
| 29 | : m_trigger(trigger), m_outputType(outputType) { |
| 30 | HAL_Report(HALUsageReporting::kResourceType_AnalogTriggerOutput, |
| 31 | trigger.GetIndex(), static_cast<uint8_t>(outputType)); |
| 32 | } |
| 33 | |
| 34 | AnalogTriggerOutput::~AnalogTriggerOutput() { |
| 35 | if (m_interrupt != HAL_kInvalidHandle) { |
| 36 | int32_t status = 0; |
| 37 | HAL_CleanInterrupts(m_interrupt, &status); |
| 38 | // ignore status, as an invalid handle just needs to be ignored. |
| 39 | m_interrupt = HAL_kInvalidHandle; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the state of the analog trigger output. |
| 45 | * |
| 46 | * @return The state of the analog trigger output. |
| 47 | */ |
| 48 | bool AnalogTriggerOutput::Get() const { |
| 49 | int32_t status = 0; |
| 50 | bool result = HAL_GetAnalogTriggerOutput( |
| 51 | m_trigger.m_trigger, static_cast<HAL_AnalogTriggerType>(m_outputType), |
| 52 | &status); |
| 53 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return The HAL Handle to the specified source. |
| 59 | */ |
| 60 | HAL_Handle AnalogTriggerOutput::GetPortHandleForRouting() const { |
| 61 | return m_trigger.m_trigger; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Is source an AnalogTrigger |
| 66 | */ |
| 67 | bool AnalogTriggerOutput::IsAnalogTrigger() const { return true; } |
| 68 | |
| 69 | /** |
| 70 | * @return The type of analog trigger output to be used. |
| 71 | */ |
| 72 | AnalogTriggerType AnalogTriggerOutput::GetAnalogTriggerTypeForRouting() const { |
| 73 | return m_outputType; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return The channel of the source. |
| 78 | */ |
| 79 | int AnalogTriggerOutput::GetChannel() const { return m_trigger.m_index; } |