blob: bd6318938d0266b894f4f4cd7dfc5b617ff6e11d [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul41fa78a2019-12-14 20:53:14 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Parker Schuhd3b7a8872018-02-19 16:42:27 -08003/* 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/AnalogTrigger.h"
9
10#include <memory>
James Kuszmaul41fa78a2019-12-14 20:53:14 -080011#include <utility>
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012
James Kuszmaul41fa78a2019-12-14 20:53:14 -080013#include <hal/FRCUsageReporting.h>
Austin Schuhf6b94632019-02-02 22:11:27 -080014#include <hal/HAL.h>
Parker Schuhd3b7a8872018-02-19 16:42:27 -080015
16#include "frc971/wpilib/ahal/AnalogInput.h"
James Kuszmaul41fa78a2019-12-14 20:53:14 -080017#include "frc971/wpilib/ahal/Base.h"
18#include "frc971/wpilib/ahal/DutyCycle.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080019#include "frc971/wpilib/ahal/WPIErrors.h"
20
21using namespace frc;
22
Parker Schuhd3b7a8872018-02-19 16:42:27 -080023AnalogTrigger::AnalogTrigger(int channel)
24 : AnalogTrigger(new AnalogInput(channel)) {
25 m_ownsAnalog = true;
26}
27
James Kuszmaul41fa78a2019-12-14 20:53:14 -080028AnalogTrigger::AnalogTrigger(AnalogInput* input) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080029 m_analogInput = input;
30 int32_t status = 0;
James Kuszmaul41fa78a2019-12-14 20:53:14 -080031 m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080032 if (status != 0) {
James Kuszmaul41fa78a2019-12-14 20:53:14 -080033 wpi_setHALError(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080034 m_trigger = HAL_kInvalidHandle;
James Kuszmauleb9f6fb2022-02-27 21:04:00 -080035 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080036 return;
37 }
James Kuszmaul41fa78a2019-12-14 20:53:14 -080038 int index = GetIndex();
Parker Schuhd3b7a8872018-02-19 16:42:27 -080039
James Kuszmaul41fa78a2019-12-14 20:53:14 -080040 HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
41}
42
43AnalogTrigger::AnalogTrigger(DutyCycle* input) {
44 m_dutyCycle = input;
45 int32_t status = 0;
46 m_trigger = HAL_InitializeAnalogTriggerDutyCycle(input->m_handle, &status);
47 if (status != 0) {
48 wpi_setHALError(status);
49 m_trigger = HAL_kInvalidHandle;
James Kuszmauleb9f6fb2022-02-27 21:04:00 -080050 HAL_CHECK_STATUS(status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -080051 return;
52 }
53 int index = GetIndex();
54
55 HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080056}
57
58AnalogTrigger::~AnalogTrigger() {
59 int32_t status = 0;
60 HAL_CleanAnalogTrigger(m_trigger, &status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -080061 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080062
James Kuszmaul41fa78a2019-12-14 20:53:14 -080063 if (m_ownsAnalog) {
Parker Schuhd3b7a8872018-02-19 16:42:27 -080064 delete m_analogInput;
65 }
66}
67
James Kuszmaul41fa78a2019-12-14 20:53:14 -080068AnalogTrigger::AnalogTrigger(AnalogTrigger &&rhs)
69 : m_trigger(std::move(rhs.m_trigger)) {
70 std::swap(m_analogInput, rhs.m_analogInput);
71 std::swap(m_dutyCycle, rhs.m_dutyCycle);
72 std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080073}
74
James Kuszmaul41fa78a2019-12-14 20:53:14 -080075AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) {
76 m_trigger = std::move(rhs.m_trigger);
77 std::swap(m_analogInput, rhs.m_analogInput);
78 std::swap(m_dutyCycle, rhs.m_dutyCycle);
79 std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
80
81 return *this;
82}
83
Parker Schuhd3b7a8872018-02-19 16:42:27 -080084void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {
85 if (StatusIsFatal()) return;
86 int32_t status = 0;
87 HAL_SetAnalogTriggerLimitsVoltage(m_trigger, lower, upper, &status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -080088 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -080089 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080090}
91
James Kuszmaul41fa78a2019-12-14 20:53:14 -080092void AnalogTrigger::SetLimitsDutyCycle(double lower, double upper) {
93 if (StatusIsFatal()) return;
94 int32_t status = 0;
95 HAL_SetAnalogTriggerLimitsDutyCycle(m_trigger, lower, upper, &status);
96 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -080097 HAL_CHECK_STATUS(status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -080098}
99
100void AnalogTrigger::SetLimitsRaw(int lower, int upper) {
101 if (StatusIsFatal()) return;
102 int32_t status = 0;
103 HAL_SetAnalogTriggerLimitsRaw(m_trigger, lower, upper, &status);
104 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800105 HAL_CHECK_STATUS(status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800106}
107
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800108void AnalogTrigger::SetAveraged(bool useAveragedValue) {
109 if (StatusIsFatal()) return;
110 int32_t status = 0;
111 HAL_SetAnalogTriggerAveraged(m_trigger, useAveragedValue, &status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800112 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800113 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800114}
115
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800116void AnalogTrigger::SetFiltered(bool useFilteredValue) {
117 if (StatusIsFatal()) return;
118 int32_t status = 0;
119 HAL_SetAnalogTriggerFiltered(m_trigger, useFilteredValue, &status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800120 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800121 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800122}
123
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800124int AnalogTrigger::GetIndex() const {
125 if (StatusIsFatal()) return -1;
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800126 int32_t status = 0;
127 auto ret = HAL_GetAnalogTriggerFPGAIndex(m_trigger, &status);
128 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800129 HAL_CHECK_STATUS(status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800130 return ret;
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800131}
132
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800133bool AnalogTrigger::GetInWindow() {
134 if (StatusIsFatal()) return false;
135 int32_t status = 0;
136 bool result = HAL_GetAnalogTriggerInWindow(m_trigger, &status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800137 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800138 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800139 return result;
140}
141
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800142bool AnalogTrigger::GetTriggerState() {
143 if (StatusIsFatal()) return false;
144 int32_t status = 0;
145 bool result = HAL_GetAnalogTriggerTriggerState(m_trigger, &status);
James Kuszmaul41fa78a2019-12-14 20:53:14 -0800146 wpi_setHALError(status);
James Kuszmauleb9f6fb2022-02-27 21:04:00 -0800147 HAL_CHECK_STATUS(status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800148 return result;
149}
150
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800151std::shared_ptr<AnalogTriggerOutput> AnalogTrigger::CreateOutput(
152 AnalogTriggerType type) const {
153 if (StatusIsFatal()) return nullptr;
154 return std::shared_ptr<AnalogTriggerOutput>(
155 new AnalogTriggerOutput(*this, type), NullDeleter<AnalogTriggerOutput>());
156}