blob: f10419f40066e645ee770d0178e477fed081bfaa [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/XboxController.h"
6
7#include <hal/FRCUsageReporting.h>
8
James Kuszmaulcf324122023-01-14 14:07:17 -08009#include "frc/event/BooleanEvent.h"
10
Brian Silverman8fce7482020-01-05 13:18:21 -080011using namespace frc;
12
13XboxController::XboxController(int port) : GenericHID(port) {
14 HAL_Report(HALUsageReporting::kResourceType_XboxController, port + 1);
15}
16
Austin Schuh812d0d12021-11-04 20:16:48 -070017double XboxController::GetLeftX() const {
18 return GetRawAxis(Axis::kLeftX);
Brian Silverman8fce7482020-01-05 13:18:21 -080019}
20
Austin Schuh812d0d12021-11-04 20:16:48 -070021double XboxController::GetRightX() const {
22 return GetRawAxis(Axis::kRightX);
Brian Silverman8fce7482020-01-05 13:18:21 -080023}
24
Austin Schuh812d0d12021-11-04 20:16:48 -070025double XboxController::GetLeftY() const {
26 return GetRawAxis(Axis::kLeftY);
Brian Silverman8fce7482020-01-05 13:18:21 -080027}
28
Austin Schuh812d0d12021-11-04 20:16:48 -070029double XboxController::GetRightY() const {
30 return GetRawAxis(Axis::kRightY);
Brian Silverman8fce7482020-01-05 13:18:21 -080031}
32
Austin Schuh812d0d12021-11-04 20:16:48 -070033double XboxController::GetLeftTriggerAxis() const {
34 return GetRawAxis(Axis::kLeftTrigger);
Brian Silverman8fce7482020-01-05 13:18:21 -080035}
36
Austin Schuh812d0d12021-11-04 20:16:48 -070037double XboxController::GetRightTriggerAxis() const {
38 return GetRawAxis(Axis::kRightTrigger);
Brian Silverman8fce7482020-01-05 13:18:21 -080039}
40
Austin Schuh812d0d12021-11-04 20:16:48 -070041bool XboxController::GetLeftBumper() const {
42 return GetRawButton(Button::kLeftBumper);
Brian Silverman8fce7482020-01-05 13:18:21 -080043}
44
Austin Schuh812d0d12021-11-04 20:16:48 -070045bool XboxController::GetRightBumper() const {
46 return GetRawButton(Button::kRightBumper);
Brian Silverman8fce7482020-01-05 13:18:21 -080047}
48
Austin Schuh812d0d12021-11-04 20:16:48 -070049bool XboxController::GetLeftBumperPressed() {
50 return GetRawButtonPressed(Button::kLeftBumper);
51}
52
53bool XboxController::GetRightBumperPressed() {
54 return GetRawButtonPressed(Button::kRightBumper);
55}
56
57bool XboxController::GetLeftBumperReleased() {
58 return GetRawButtonReleased(Button::kLeftBumper);
59}
60
61bool XboxController::GetRightBumperReleased() {
62 return GetRawButtonReleased(Button::kRightBumper);
63}
64
James Kuszmaulcf324122023-01-14 14:07:17 -080065BooleanEvent XboxController::LeftBumper(EventLoop* loop) const {
66 return BooleanEvent(loop, [this]() { return this->GetLeftBumper(); });
67}
68
69BooleanEvent XboxController::RightBumper(EventLoop* loop) const {
70 return BooleanEvent(loop, [this]() { return this->GetRightBumper(); });
71}
72
Austin Schuh812d0d12021-11-04 20:16:48 -070073bool XboxController::GetLeftStickButton() const {
74 return GetRawButton(Button::kLeftStick);
75}
76
77bool XboxController::GetRightStickButton() const {
78 return GetRawButton(Button::kRightStick);
79}
80
81bool XboxController::GetLeftStickButtonPressed() {
82 return GetRawButtonPressed(Button::kLeftStick);
83}
84
85bool XboxController::GetRightStickButtonPressed() {
86 return GetRawButtonPressed(Button::kRightStick);
87}
88
89bool XboxController::GetLeftStickButtonReleased() {
90 return GetRawButtonReleased(Button::kLeftStick);
91}
92
93bool XboxController::GetRightStickButtonReleased() {
94 return GetRawButtonReleased(Button::kRightStick);
Brian Silverman8fce7482020-01-05 13:18:21 -080095}
96
James Kuszmaulcf324122023-01-14 14:07:17 -080097BooleanEvent XboxController::LeftStick(EventLoop* loop) const {
98 return BooleanEvent(loop, [this]() { return this->GetLeftStickButton(); });
99}
100
101BooleanEvent XboxController::RightStick(EventLoop* loop) const {
102 return BooleanEvent(loop, [this]() { return this->GetRightStickButton(); });
103}
104
Brian Silverman8fce7482020-01-05 13:18:21 -0800105bool XboxController::GetAButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700106 return GetRawButton(Button::kA);
Brian Silverman8fce7482020-01-05 13:18:21 -0800107}
108
109bool XboxController::GetAButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700110 return GetRawButtonPressed(Button::kA);
Brian Silverman8fce7482020-01-05 13:18:21 -0800111}
112
113bool XboxController::GetAButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700114 return GetRawButtonReleased(Button::kA);
Brian Silverman8fce7482020-01-05 13:18:21 -0800115}
116
James Kuszmaulcf324122023-01-14 14:07:17 -0800117BooleanEvent XboxController::A(EventLoop* loop) const {
118 return BooleanEvent(loop, [this]() { return this->GetAButton(); });
119}
120
Brian Silverman8fce7482020-01-05 13:18:21 -0800121bool XboxController::GetBButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700122 return GetRawButton(Button::kB);
Brian Silverman8fce7482020-01-05 13:18:21 -0800123}
124
125bool XboxController::GetBButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700126 return GetRawButtonPressed(Button::kB);
Brian Silverman8fce7482020-01-05 13:18:21 -0800127}
128
129bool XboxController::GetBButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700130 return GetRawButtonReleased(Button::kB);
Brian Silverman8fce7482020-01-05 13:18:21 -0800131}
132
James Kuszmaulcf324122023-01-14 14:07:17 -0800133BooleanEvent XboxController::B(EventLoop* loop) const {
134 return BooleanEvent(loop, [this]() { return this->GetBButton(); });
135}
136
Brian Silverman8fce7482020-01-05 13:18:21 -0800137bool XboxController::GetXButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700138 return GetRawButton(Button::kX);
Brian Silverman8fce7482020-01-05 13:18:21 -0800139}
140
141bool XboxController::GetXButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700142 return GetRawButtonPressed(Button::kX);
Brian Silverman8fce7482020-01-05 13:18:21 -0800143}
144
145bool XboxController::GetXButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700146 return GetRawButtonReleased(Button::kX);
Brian Silverman8fce7482020-01-05 13:18:21 -0800147}
148
James Kuszmaulcf324122023-01-14 14:07:17 -0800149BooleanEvent XboxController::X(EventLoop* loop) const {
150 return BooleanEvent(loop, [this]() { return this->GetXButton(); });
151}
152
Brian Silverman8fce7482020-01-05 13:18:21 -0800153bool XboxController::GetYButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700154 return GetRawButton(Button::kY);
Brian Silverman8fce7482020-01-05 13:18:21 -0800155}
156
157bool XboxController::GetYButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700158 return GetRawButtonPressed(Button::kY);
Brian Silverman8fce7482020-01-05 13:18:21 -0800159}
160
161bool XboxController::GetYButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700162 return GetRawButtonReleased(Button::kY);
Brian Silverman8fce7482020-01-05 13:18:21 -0800163}
164
James Kuszmaulcf324122023-01-14 14:07:17 -0800165BooleanEvent XboxController::Y(EventLoop* loop) const {
166 return BooleanEvent(loop, [this]() { return this->GetYButton(); });
167}
168
Brian Silverman8fce7482020-01-05 13:18:21 -0800169bool XboxController::GetBackButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700170 return GetRawButton(Button::kBack);
Brian Silverman8fce7482020-01-05 13:18:21 -0800171}
172
173bool XboxController::GetBackButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700174 return GetRawButtonPressed(Button::kBack);
Brian Silverman8fce7482020-01-05 13:18:21 -0800175}
176
177bool XboxController::GetBackButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700178 return GetRawButtonReleased(Button::kBack);
Brian Silverman8fce7482020-01-05 13:18:21 -0800179}
180
James Kuszmaulcf324122023-01-14 14:07:17 -0800181BooleanEvent XboxController::Back(EventLoop* loop) const {
182 return BooleanEvent(loop, [this]() { return this->GetBackButton(); });
183}
184
Brian Silverman8fce7482020-01-05 13:18:21 -0800185bool XboxController::GetStartButton() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700186 return GetRawButton(Button::kStart);
Brian Silverman8fce7482020-01-05 13:18:21 -0800187}
188
189bool XboxController::GetStartButtonPressed() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700190 return GetRawButtonPressed(Button::kStart);
Brian Silverman8fce7482020-01-05 13:18:21 -0800191}
192
193bool XboxController::GetStartButtonReleased() {
Austin Schuh812d0d12021-11-04 20:16:48 -0700194 return GetRawButtonReleased(Button::kStart);
Brian Silverman8fce7482020-01-05 13:18:21 -0800195}
James Kuszmaulcf324122023-01-14 14:07:17 -0800196
197BooleanEvent XboxController::Start(EventLoop* loop) const {
198 return BooleanEvent(loop, [this]() { return this->GetStartButton(); });
199}
200
201BooleanEvent XboxController::LeftTrigger(double threshold,
202 EventLoop* loop) const {
203 return BooleanEvent(loop, [this, threshold]() {
204 return this->GetLeftTriggerAxis() > threshold;
205 });
206}
207
208BooleanEvent XboxController::LeftTrigger(EventLoop* loop) const {
209 return this->LeftTrigger(0.5, loop);
210}
211
212BooleanEvent XboxController::RightTrigger(double threshold,
213 EventLoop* loop) const {
214 return BooleanEvent(loop, [this, threshold]() {
215 return this->GetRightTriggerAxis() > threshold;
216 });
217}
218
219BooleanEvent XboxController::RightTrigger(EventLoop* loop) const {
220 return this->RightTrigger(0.5, loop);
221}