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/XboxController.h" |
| 6 | |
| 7 | #include <hal/FRCUsageReporting.h> |
| 8 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 9 | #include "frc/event/BooleanEvent.h" |
| 10 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | using namespace frc; |
| 12 | |
| 13 | XboxController::XboxController(int port) : GenericHID(port) { |
| 14 | HAL_Report(HALUsageReporting::kResourceType_XboxController, port + 1); |
| 15 | } |
| 16 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | double XboxController::GetLeftX() const { |
| 18 | return GetRawAxis(Axis::kLeftX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 21 | double XboxController::GetRightX() const { |
| 22 | return GetRawAxis(Axis::kRightX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | } |
| 24 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | double XboxController::GetLeftY() const { |
| 26 | return GetRawAxis(Axis::kLeftY); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 29 | double XboxController::GetRightY() const { |
| 30 | return GetRawAxis(Axis::kRightY); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | double XboxController::GetLeftTriggerAxis() const { |
| 34 | return GetRawAxis(Axis::kLeftTrigger); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | double XboxController::GetRightTriggerAxis() const { |
| 38 | return GetRawAxis(Axis::kRightTrigger); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 41 | bool XboxController::GetLeftBumper() const { |
| 42 | return GetRawButton(Button::kLeftBumper); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 45 | bool XboxController::GetRightBumper() const { |
| 46 | return GetRawButton(Button::kRightBumper); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 49 | bool XboxController::GetLeftBumperPressed() { |
| 50 | return GetRawButtonPressed(Button::kLeftBumper); |
| 51 | } |
| 52 | |
| 53 | bool XboxController::GetRightBumperPressed() { |
| 54 | return GetRawButtonPressed(Button::kRightBumper); |
| 55 | } |
| 56 | |
| 57 | bool XboxController::GetLeftBumperReleased() { |
| 58 | return GetRawButtonReleased(Button::kLeftBumper); |
| 59 | } |
| 60 | |
| 61 | bool XboxController::GetRightBumperReleased() { |
| 62 | return GetRawButtonReleased(Button::kRightBumper); |
| 63 | } |
| 64 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 65 | BooleanEvent XboxController::LeftBumper(EventLoop* loop) const { |
| 66 | return BooleanEvent(loop, [this]() { return this->GetLeftBumper(); }); |
| 67 | } |
| 68 | |
| 69 | BooleanEvent XboxController::RightBumper(EventLoop* loop) const { |
| 70 | return BooleanEvent(loop, [this]() { return this->GetRightBumper(); }); |
| 71 | } |
| 72 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 73 | bool XboxController::GetLeftStickButton() const { |
| 74 | return GetRawButton(Button::kLeftStick); |
| 75 | } |
| 76 | |
| 77 | bool XboxController::GetRightStickButton() const { |
| 78 | return GetRawButton(Button::kRightStick); |
| 79 | } |
| 80 | |
| 81 | bool XboxController::GetLeftStickButtonPressed() { |
| 82 | return GetRawButtonPressed(Button::kLeftStick); |
| 83 | } |
| 84 | |
| 85 | bool XboxController::GetRightStickButtonPressed() { |
| 86 | return GetRawButtonPressed(Button::kRightStick); |
| 87 | } |
| 88 | |
| 89 | bool XboxController::GetLeftStickButtonReleased() { |
| 90 | return GetRawButtonReleased(Button::kLeftStick); |
| 91 | } |
| 92 | |
| 93 | bool XboxController::GetRightStickButtonReleased() { |
| 94 | return GetRawButtonReleased(Button::kRightStick); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | } |
| 96 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 97 | BooleanEvent XboxController::LeftStick(EventLoop* loop) const { |
| 98 | return BooleanEvent(loop, [this]() { return this->GetLeftStickButton(); }); |
| 99 | } |
| 100 | |
| 101 | BooleanEvent XboxController::RightStick(EventLoop* loop) const { |
| 102 | return BooleanEvent(loop, [this]() { return this->GetRightStickButton(); }); |
| 103 | } |
| 104 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 105 | bool XboxController::GetAButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 106 | return GetRawButton(Button::kA); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool XboxController::GetAButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 110 | return GetRawButtonPressed(Button::kA); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool XboxController::GetAButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 114 | return GetRawButtonReleased(Button::kA); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 115 | } |
| 116 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 117 | BooleanEvent XboxController::A(EventLoop* loop) const { |
| 118 | return BooleanEvent(loop, [this]() { return this->GetAButton(); }); |
| 119 | } |
| 120 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 121 | bool XboxController::GetBButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 122 | return GetRawButton(Button::kB); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool XboxController::GetBButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 126 | return GetRawButtonPressed(Button::kB); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool XboxController::GetBButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 130 | return GetRawButtonReleased(Button::kB); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 133 | BooleanEvent XboxController::B(EventLoop* loop) const { |
| 134 | return BooleanEvent(loop, [this]() { return this->GetBButton(); }); |
| 135 | } |
| 136 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | bool XboxController::GetXButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 138 | return GetRawButton(Button::kX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool XboxController::GetXButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 142 | return GetRawButtonPressed(Button::kX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool XboxController::GetXButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 146 | return GetRawButtonReleased(Button::kX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 147 | } |
| 148 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 149 | BooleanEvent XboxController::X(EventLoop* loop) const { |
| 150 | return BooleanEvent(loop, [this]() { return this->GetXButton(); }); |
| 151 | } |
| 152 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 153 | bool XboxController::GetYButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 154 | return GetRawButton(Button::kY); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool XboxController::GetYButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 158 | return GetRawButtonPressed(Button::kY); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool XboxController::GetYButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 162 | return GetRawButtonReleased(Button::kY); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 163 | } |
| 164 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 165 | BooleanEvent XboxController::Y(EventLoop* loop) const { |
| 166 | return BooleanEvent(loop, [this]() { return this->GetYButton(); }); |
| 167 | } |
| 168 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 169 | bool XboxController::GetBackButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 170 | return GetRawButton(Button::kBack); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool XboxController::GetBackButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 174 | return GetRawButtonPressed(Button::kBack); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool XboxController::GetBackButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 178 | return GetRawButtonReleased(Button::kBack); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 179 | } |
| 180 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 181 | BooleanEvent XboxController::Back(EventLoop* loop) const { |
| 182 | return BooleanEvent(loop, [this]() { return this->GetBackButton(); }); |
| 183 | } |
| 184 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 185 | bool XboxController::GetStartButton() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 186 | return GetRawButton(Button::kStart); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | bool XboxController::GetStartButtonPressed() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 190 | return GetRawButtonPressed(Button::kStart); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool XboxController::GetStartButtonReleased() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 194 | return GetRawButtonReleased(Button::kStart); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 195 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 196 | |
| 197 | BooleanEvent XboxController::Start(EventLoop* loop) const { |
| 198 | return BooleanEvent(loop, [this]() { return this->GetStartButton(); }); |
| 199 | } |
| 200 | |
| 201 | BooleanEvent XboxController::LeftTrigger(double threshold, |
| 202 | EventLoop* loop) const { |
| 203 | return BooleanEvent(loop, [this, threshold]() { |
| 204 | return this->GetLeftTriggerAxis() > threshold; |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | BooleanEvent XboxController::LeftTrigger(EventLoop* loop) const { |
| 209 | return this->LeftTrigger(0.5, loop); |
| 210 | } |
| 211 | |
| 212 | BooleanEvent XboxController::RightTrigger(double threshold, |
| 213 | EventLoop* loop) const { |
| 214 | return BooleanEvent(loop, [this, threshold]() { |
| 215 | return this->GetRightTriggerAxis() > threshold; |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | BooleanEvent XboxController::RightTrigger(EventLoop* loop) const { |
| 220 | return this->RightTrigger(0.5, loop); |
| 221 | } |