Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/GenericHID.h" |
| 6 | |
| 7 | #include <hal/DriverStation.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
| 9 | #include "frc/DriverStation.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include "frc/Errors.h" |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 11 | #include "frc/event/BooleanEvent.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | using namespace frc; |
| 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | GenericHID::GenericHID(int port) { |
| 16 | if (port < 0 || port >= DriverStation::kJoystickPorts) { |
| 17 | throw FRC_MakeError(warn::BadJoystickIndex, "port {} out of range", port); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | } |
| 19 | m_port = port; |
| 20 | } |
| 21 | |
| 22 | bool GenericHID::GetRawButton(int button) const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 23 | return DriverStation::GetStickButton(m_port, button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | bool GenericHID::GetRawButtonPressed(int button) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | return DriverStation::GetStickButtonPressed(m_port, button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | bool GenericHID::GetRawButtonReleased(int button) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | return DriverStation::GetStickButtonReleased(m_port, button); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | } |
| 33 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 34 | BooleanEvent GenericHID::Button(int button, EventLoop* loop) const { |
| 35 | return BooleanEvent(loop, |
| 36 | [this, button]() { return this->GetRawButton(button); }); |
| 37 | } |
| 38 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | double GenericHID::GetRawAxis(int axis) const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 40 | return DriverStation::GetStickAxis(m_port, axis); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 43 | int GenericHID::GetPOV(int pov) const { |
| 44 | return DriverStation::GetStickPOV(m_port, pov); |
| 45 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 47 | BooleanEvent GenericHID::POV(int angle, EventLoop* loop) const { |
| 48 | return POV(0, angle, loop); |
| 49 | } |
| 50 | |
| 51 | BooleanEvent GenericHID::POV(int pov, int angle, EventLoop* loop) const { |
| 52 | return BooleanEvent( |
| 53 | loop, [this, pov, angle] { return this->GetPOV(pov) == angle; }); |
| 54 | } |
| 55 | |
| 56 | BooleanEvent GenericHID::POVUp(EventLoop* loop) const { |
| 57 | return POV(0, loop); |
| 58 | } |
| 59 | |
| 60 | BooleanEvent GenericHID::POVUpRight(EventLoop* loop) const { |
| 61 | return POV(45, loop); |
| 62 | } |
| 63 | |
| 64 | BooleanEvent GenericHID::POVRight(EventLoop* loop) const { |
| 65 | return POV(90, loop); |
| 66 | } |
| 67 | |
| 68 | BooleanEvent GenericHID::POVDownRight(EventLoop* loop) const { |
| 69 | return POV(135, loop); |
| 70 | } |
| 71 | |
| 72 | BooleanEvent GenericHID::POVDown(EventLoop* loop) const { |
| 73 | return POV(180, loop); |
| 74 | } |
| 75 | |
| 76 | BooleanEvent GenericHID::POVDownLeft(EventLoop* loop) const { |
| 77 | return POV(225, loop); |
| 78 | } |
| 79 | |
| 80 | BooleanEvent GenericHID::POVLeft(EventLoop* loop) const { |
| 81 | return POV(270, loop); |
| 82 | } |
| 83 | |
| 84 | BooleanEvent GenericHID::POVUpLeft(EventLoop* loop) const { |
| 85 | return POV(315, loop); |
| 86 | } |
| 87 | |
| 88 | BooleanEvent GenericHID::POVCenter(EventLoop* loop) const { |
| 89 | return POV(360, loop); |
| 90 | } |
| 91 | |
| 92 | BooleanEvent GenericHID::AxisLessThan(int axis, double threshold, |
| 93 | EventLoop* loop) const { |
| 94 | return BooleanEvent(loop, [this, axis, threshold]() { |
| 95 | return this->GetRawAxis(axis) < threshold; |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | BooleanEvent GenericHID::AxisGreaterThan(int axis, double threshold, |
| 100 | EventLoop* loop) const { |
| 101 | return BooleanEvent(loop, [this, axis, threshold]() { |
| 102 | return this->GetRawAxis(axis) > threshold; |
| 103 | }); |
| 104 | } |
| 105 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 106 | int GenericHID::GetAxisCount() const { |
| 107 | return DriverStation::GetStickAxisCount(m_port); |
| 108 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 109 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 110 | int GenericHID::GetPOVCount() const { |
| 111 | return DriverStation::GetStickPOVCount(m_port); |
| 112 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 113 | |
| 114 | int GenericHID::GetButtonCount() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 115 | return DriverStation::GetStickButtonCount(m_port); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 118 | bool GenericHID::IsConnected() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 119 | return DriverStation::IsJoystickConnected(m_port); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | GenericHID::HIDType GenericHID::GetType() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 123 | return static_cast<HIDType>(DriverStation::GetJoystickType(m_port)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::string GenericHID::GetName() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 127 | return DriverStation::GetJoystickName(m_port); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int GenericHID::GetAxisType(int axis) const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 131 | return DriverStation::GetJoystickAxisType(m_port, axis); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 134 | int GenericHID::GetPort() const { |
| 135 | return m_port; |
| 136 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | |
| 138 | void GenericHID::SetOutput(int outputNumber, bool value) { |
| 139 | m_outputs = |
| 140 | (m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1)); |
| 141 | |
| 142 | HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); |
| 143 | } |
| 144 | |
| 145 | void GenericHID::SetOutputs(int value) { |
| 146 | m_outputs = value; |
| 147 | HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); |
| 148 | } |
| 149 | |
| 150 | void GenericHID::SetRumble(RumbleType type, double value) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 151 | value = std::clamp(value, 0.0, 1.0); |
| 152 | double rumbleValue = value * 65535; |
| 153 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 154 | if (type == kLeftRumble) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 155 | m_leftRumble = rumbleValue; |
| 156 | } else if (type == kRightRumble) { |
| 157 | m_rightRumble = rumbleValue; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 158 | } else { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 159 | m_leftRumble = rumbleValue; |
| 160 | m_rightRumble = rumbleValue; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 161 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 162 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 163 | HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); |
| 164 | } |