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