WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
XboxController.h
Go to the documentation of this file.
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.
4
5// THIS FILE WAS AUTO-GENERATED BY ./wpilibc/generate_hids.py. DO NOT MODIFY
6
7#pragma once
8
11
12#include "frc/GenericHID.h"
13
14namespace frc {
15
16/**
17 * Handle input from Xbox controllers connected to the Driver Station.
18 *
19 * This class handles Xbox input that comes from the Driver Station. Each
20 * time a value is requested the most recent value is returned. There is a
21 * single class instance for each controller and the mapping of ports to
22 * hardware buttons depends on the code in the Driver Station.
23 *
24 * Only first party controllers from Microsoft are guaranteed to have the
25 * correct mapping, and only through the official NI DS. Sim is not guaranteed
26 * to have the same mapping, as well as any 3rd party controllers.
27 */
29 public wpi::Sendable,
30 public wpi::SendableHelper<XboxController> {
31 public:
32 /**
33 * Construct an instance of a controller.
34 *
35 * The controller index is the USB port on the Driver Station.
36 *
37 * @param port The port on the Driver Station that the controller is plugged
38 * into (0-5).
39 */
40 explicit XboxController(int port);
41
42 ~XboxController() override = default;
43
46
47 /**
48 * Get the X axis value of left side of the controller.
49 *
50 * @return the axis value.
51 */
52 double GetLeftX() const;
53
54 /**
55 * Get the X axis value of right side of the controller.
56 *
57 * @return the axis value.
58 */
59 double GetRightX() const;
60
61 /**
62 * Get the Y axis value of left side of the controller.
63 *
64 * @return the axis value.
65 */
66 double GetLeftY() const;
67
68 /**
69 * Get the Y axis value of right side of the controller.
70 *
71 * @return the axis value.
72 */
73 double GetRightY() const;
74
75 /**
76 * Get the left trigger axis value of the controller. Note that this axis
77 * is bound to the range of [0, 1] as opposed to the usual [-1, 1].
78 *
79 * @return the axis value.
80 */
81 double GetLeftTriggerAxis() const;
82
83 /**
84 * Constructs an event instance around the axis value of the left trigger.
85 * The returned trigger will be true when the axis value is greater than
86 * {@code threshold}.
87 * @param threshold the minimum axis value for the returned event to be true.
88 * This value should be in the range [0, 1] where 0 is the unpressed state of
89 * the axis.
90 * @param loop the event loop instance to attach the event to.
91 * @return an event instance that is true when the left trigger's axis
92 * exceeds the provided threshold, attached to the given event loop
93 */
94 BooleanEvent LeftTrigger(double threshold, EventLoop* loop) const;
95
96 /**
97 * Constructs an event instance around the axis value of the left trigger.
98 * The returned trigger will be true when the axis value is greater than 0.5.
99 * @param loop the event loop instance to attach the event to.
100 * @return an event instance that is true when the left trigger's axis
101 * exceeds 0.5, attached to the given event loop
102 */
104
105 /**
106 * Get the right trigger axis value of the controller. Note that this axis
107 * is bound to the range of [0, 1] as opposed to the usual [-1, 1].
108 *
109 * @return the axis value.
110 */
111 double GetRightTriggerAxis() const;
112
113 /**
114 * Constructs an event instance around the axis value of the right trigger.
115 * The returned trigger will be true when the axis value is greater than
116 * {@code threshold}.
117 * @param threshold the minimum axis value for the returned event to be true.
118 * This value should be in the range [0, 1] where 0 is the unpressed state of
119 * the axis.
120 * @param loop the event loop instance to attach the event to.
121 * @return an event instance that is true when the right trigger's axis
122 * exceeds the provided threshold, attached to the given event loop
123 */
124 BooleanEvent RightTrigger(double threshold, EventLoop* loop) const;
125
126 /**
127 * Constructs an event instance around the axis value of the right trigger.
128 * The returned trigger will be true when the axis value is greater than 0.5.
129 * @param loop the event loop instance to attach the event to.
130 * @return an event instance that is true when the right trigger's axis
131 * exceeds 0.5, attached to the given event loop
132 */
134
135 /**
136 * Read the value of the A button on the controller.
137 *
138 * @return The state of the button.
139 */
140 bool GetAButton() const;
141
142 /**
143 * Whether the A button was pressed since the last check.
144 *
145 * @return Whether the button was pressed since the last check.
146 */
148
149 /**
150 * Whether the A button was released since the last check.
151 *
152 * @return Whether the button was released since the last check.
153 */
155
156 /**
157 * Constructs an event instance around the A button's
158 * digital signal.
159 *
160 * @param loop the event loop instance to attach the event to.
161 * @return an event instance representing the A button's
162 * digital signal attached to the given loop.
163 */
165
166 /**
167 * Read the value of the B button on the controller.
168 *
169 * @return The state of the button.
170 */
171 bool GetBButton() const;
172
173 /**
174 * Whether the B button was pressed since the last check.
175 *
176 * @return Whether the button was pressed since the last check.
177 */
179
180 /**
181 * Whether the B button was released since the last check.
182 *
183 * @return Whether the button was released since the last check.
184 */
186
187 /**
188 * Constructs an event instance around the B button's
189 * digital signal.
190 *
191 * @param loop the event loop instance to attach the event to.
192 * @return an event instance representing the B button's
193 * digital signal attached to the given loop.
194 */
196
197 /**
198 * Read the value of the X button on the controller.
199 *
200 * @return The state of the button.
201 */
202 bool GetXButton() const;
203
204 /**
205 * Whether the X button was pressed since the last check.
206 *
207 * @return Whether the button was pressed since the last check.
208 */
210
211 /**
212 * Whether the X button was released since the last check.
213 *
214 * @return Whether the button was released since the last check.
215 */
217
218 /**
219 * Constructs an event instance around the X button's
220 * digital signal.
221 *
222 * @param loop the event loop instance to attach the event to.
223 * @return an event instance representing the X button's
224 * digital signal attached to the given loop.
225 */
227
228 /**
229 * Read the value of the Y button on the controller.
230 *
231 * @return The state of the button.
232 */
233 bool GetYButton() const;
234
235 /**
236 * Whether the Y button was pressed since the last check.
237 *
238 * @return Whether the button was pressed since the last check.
239 */
241
242 /**
243 * Whether the Y button was released since the last check.
244 *
245 * @return Whether the button was released since the last check.
246 */
248
249 /**
250 * Constructs an event instance around the Y button's
251 * digital signal.
252 *
253 * @param loop the event loop instance to attach the event to.
254 * @return an event instance representing the Y button's
255 * digital signal attached to the given loop.
256 */
258
259 /**
260 * Read the value of the left bumper button on the controller.
261 *
262 * @return The state of the button.
263 */
265
266 /**
267 * Whether the left bumper button was pressed since the last check.
268 *
269 * @return Whether the button was pressed since the last check.
270 */
272
273 /**
274 * Whether the left bumper button was released since the last check.
275 *
276 * @return Whether the button was released since the last check.
277 */
279
280 /**
281 * Constructs an event instance around the left bumper button's
282 * digital signal.
283 *
284 * @param loop the event loop instance to attach the event to.
285 * @return an event instance representing the left bumper button's
286 * digital signal attached to the given loop.
287 */
289
290 /**
291 * Read the value of the right bumper button on the controller.
292 *
293 * @return The state of the button.
294 */
296
297 /**
298 * Whether the right bumper button was pressed since the last check.
299 *
300 * @return Whether the button was pressed since the last check.
301 */
303
304 /**
305 * Whether the right bumper button was released since the last check.
306 *
307 * @return Whether the button was released since the last check.
308 */
310
311 /**
312 * Constructs an event instance around the right bumper button's
313 * digital signal.
314 *
315 * @param loop the event loop instance to attach the event to.
316 * @return an event instance representing the right bumper button's
317 * digital signal attached to the given loop.
318 */
320
321 /**
322 * Read the value of the back button on the controller.
323 *
324 * @return The state of the button.
325 */
326 bool GetBackButton() const;
327
328 /**
329 * Whether the back button was pressed since the last check.
330 *
331 * @return Whether the button was pressed since the last check.
332 */
334
335 /**
336 * Whether the back button was released since the last check.
337 *
338 * @return Whether the button was released since the last check.
339 */
341
342 /**
343 * Constructs an event instance around the back button's
344 * digital signal.
345 *
346 * @param loop the event loop instance to attach the event to.
347 * @return an event instance representing the back button's
348 * digital signal attached to the given loop.
349 */
351
352 /**
353 * Read the value of the start button on the controller.
354 *
355 * @return The state of the button.
356 */
357 bool GetStartButton() const;
358
359 /**
360 * Whether the start button was pressed since the last check.
361 *
362 * @return Whether the button was pressed since the last check.
363 */
365
366 /**
367 * Whether the start button was released since the last check.
368 *
369 * @return Whether the button was released since the last check.
370 */
372
373 /**
374 * Constructs an event instance around the start button's
375 * digital signal.
376 *
377 * @param loop the event loop instance to attach the event to.
378 * @return an event instance representing the start button's
379 * digital signal attached to the given loop.
380 */
382
383 /**
384 * Read the value of the left stick button on the controller.
385 *
386 * @return The state of the button.
387 */
388 bool GetLeftStickButton() const;
389
390 /**
391 * Whether the left stick button was pressed since the last check.
392 *
393 * @return Whether the button was pressed since the last check.
394 */
396
397 /**
398 * Whether the left stick button was released since the last check.
399 *
400 * @return Whether the button was released since the last check.
401 */
403
404 /**
405 * Constructs an event instance around the left stick button's
406 * digital signal.
407 *
408 * @param loop the event loop instance to attach the event to.
409 * @return an event instance representing the left stick button's
410 * digital signal attached to the given loop.
411 */
413
414 /**
415 * Read the value of the right stick button on the controller.
416 *
417 * @return The state of the button.
418 */
420
421 /**
422 * Whether the right stick button was pressed since the last check.
423 *
424 * @return Whether the button was pressed since the last check.
425 */
427
428 /**
429 * Whether the right stick button was released since the last check.
430 *
431 * @return Whether the button was released since the last check.
432 */
434
435 /**
436 * Constructs an event instance around the right stick button's
437 * digital signal.
438 *
439 * @param loop the event loop instance to attach the event to.
440 * @return an event instance representing the right stick button's
441 * digital signal attached to the given loop.
442 */
444
445 /**
446 * Read the value of the left bumper (LB) button on the controller.
447 *
448 * @return the state of the button
449 * @deprecated Use GetLeftBumperButton instead. This function is deprecated
450 * for removal to make function names consistent to allow the HID classes to
451 * be automatically generated.
452 */
453 [[deprecated("Use GetLeftBumperButton instead")]]
454 bool GetLeftBumper() const;
455
456 /**
457 * Read the value of the right bumper (RB) button on the controller.
458 *
459 * @return the state of the button
460 * @deprecated Use GetRightBumperButton instead. This function is deprecated
461 * for removal to make function names consistent to allow the HID classes to
462 * be automatically generated.
463 */
464 [[deprecated("Use GetRightBumperButton instead")]]
465 bool GetRightBumper() const;
466
467 /**
468 * Whether the left bumper (LB) was pressed since the last check.
469 *
470 * @return Whether the button was pressed since the last check
471 * @deprecated Use GetLeftBumperButtonPressed instead. This function is
472 * deprecated for removal to make function names consistent to allow the HID
473 * classes to be automatically generated.
474 */
475 [[deprecated("Use GetLeftBumperButtonPressed instead")]]
477
478 /**
479 * Whether the right bumper (RB) was pressed since the last check.
480 *
481 * @return Whether the button was pressed since the last check
482 * @deprecated Use GetRightBumperButtonPressed instead. This function is
483 * deprecated for removal to make function names consistent to allow the HID
484 * classes to be automatically generated.
485 */
486 [[deprecated("Use GetRightBumperButtonPressed instead")]]
488
489 /**
490 * Whether the left bumper (LB) was released since the last check.
491 *
492 * @return Whether the button was released since the last check.
493 * @deprecated Use GetLeftBumperButtonReleased instead. This function is
494 * deprecated for removal to make function names consistent to allow the HID
495 * classes to be automatically generated.
496 */
497 [[deprecated("Use GetLeftBumperButtonReleased instead")]]
499
500 /**
501 * Whether the right bumper (RB) was released since the last check.
502 *
503 * @return Whether the button was released since the last check.
504 * @deprecated Use GetRightBumperButtonReleased instead. This function is
505 * deprecated for removal to make function names consistent to allow the HID
506 * classes to be automatically generated.
507 */
508 [[deprecated("Use GetRightBumperButtonReleased instead")]]
510
511 /** Represents a digital button on an XboxController. */
512 struct Button {
513 /// A button.
514 static constexpr int kA = 1;
515 /// B button.
516 static constexpr int kB = 2;
517 /// X button.
518 static constexpr int kX = 3;
519 /// Y button.
520 static constexpr int kY = 4;
521 /// Left bumper button.
522 static constexpr int kLeftBumper = 5;
523 /// Right bumper button.
524 static constexpr int kRightBumper = 6;
525 /// Back button.
526 static constexpr int kBack = 7;
527 /// Start button.
528 static constexpr int kStart = 8;
529 /// Left stick button.
530 static constexpr int kLeftStick = 9;
531 /// Right stick button.
532 static constexpr int kRightStick = 10;
533 };
534
535 /** Represents an axis on an XboxController. */
536 struct Axis {
537 /// Left X axis.
538 static constexpr int kLeftX = 0;
539 /// Right X axis.
540 static constexpr int kRightX = 4;
541 /// Left Y axis.
542 static constexpr int kLeftY = 1;
543 /// Right Y axis.
544 static constexpr int kRightY = 5;
545 /// Left trigger.
546 static constexpr int kLeftTrigger = 2;
547 /// Right trigger.
548 static constexpr int kRightTrigger = 3;
549 };
550
551 void InitSendable(wpi::SendableBuilder& builder) override;
552};
553
554} // namespace frc
This class provides an easy way to link actions to active high logic signals.
Definition BooleanEvent.h:30
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.h:15
Handle input from standard HID devices connected to the Driver Station.
Definition GenericHID.h:24
Handle input from Xbox controllers connected to the Driver Station.
Definition XboxController.h:30
bool GetBButtonReleased()
Whether the B button was released since the last check.
BooleanEvent A(EventLoop *loop) const
Constructs an event instance around the A button's digital signal.
bool GetBackButtonPressed()
Whether the back button was pressed since the last check.
bool GetStartButtonPressed()
Whether the start button was pressed since the last check.
bool GetBButtonPressed()
Whether the B button was pressed since the last check.
double GetLeftX() const
Get the X axis value of left side of the controller.
bool GetAButton() const
Read the value of the A button on the controller.
bool GetLeftBumper() const
Read the value of the left bumper (LB) button on the controller.
bool GetLeftStickButton() const
Read the value of the left stick button on the controller.
BooleanEvent Back(EventLoop *loop) const
Constructs an event instance around the back button's digital signal.
BooleanEvent LeftStick(EventLoop *loop) const
Constructs an event instance around the left stick button's digital signal.
bool GetRightBumperReleased()
Whether the right bumper (RB) was released since the last check.
bool GetAButtonReleased()
Whether the A button was released since the last check.
BooleanEvent Start(EventLoop *loop) const
Constructs an event instance around the start button's digital signal.
bool GetLeftBumperButton() const
Read the value of the left bumper button on the controller.
bool GetBButton() const
Read the value of the B button on the controller.
XboxController(XboxController &&)=default
double GetLeftY() const
Get the Y axis value of left side of the controller.
bool GetXButtonPressed()
Whether the X button was pressed since the last check.
XboxController & operator=(XboxController &&)=default
BooleanEvent B(EventLoop *loop) const
Constructs an event instance around the B button's digital signal.
double GetLeftTriggerAxis() const
Get the left trigger axis value of the controller.
bool GetYButtonReleased()
Whether the Y button was released since the last check.
bool GetRightBumperButtonReleased()
Whether the right bumper button was released since the last check.
BooleanEvent RightBumper(EventLoop *loop) const
Constructs an event instance around the right bumper button's digital signal.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
BooleanEvent RightTrigger(EventLoop *loop) const
Constructs an event instance around the axis value of the right trigger.
bool GetLeftBumperPressed()
Whether the left bumper (LB) was pressed since the last check.
bool GetXButtonReleased()
Whether the X button was released since the last check.
bool GetRightBumper() const
Read the value of the right bumper (RB) button on the controller.
bool GetBackButtonReleased()
Whether the back button was released since the last check.
XboxController(int port)
Construct an instance of a controller.
bool GetLeftBumperReleased()
Whether the left bumper (LB) was released since the last check.
BooleanEvent RightTrigger(double threshold, EventLoop *loop) const
Constructs an event instance around the axis value of the right trigger.
bool GetRightBumperButtonPressed()
Whether the right bumper button was pressed since the last check.
bool GetLeftBumperButtonPressed()
Whether the left bumper button was pressed since the last check.
~XboxController() override=default
bool GetRightStickButtonPressed()
Whether the right stick button was pressed since the last check.
bool GetLeftBumperButtonReleased()
Whether the left bumper button was released since the last check.
bool GetYButton() const
Read the value of the Y button on the controller.
BooleanEvent LeftTrigger(EventLoop *loop) const
Constructs an event instance around the axis value of the left trigger.
double GetRightY() const
Get the Y axis value of right side of the controller.
bool GetAButtonPressed()
Whether the A button was pressed since the last check.
BooleanEvent X(EventLoop *loop) const
Constructs an event instance around the X button's digital signal.
BooleanEvent RightStick(EventLoop *loop) const
Constructs an event instance around the right stick button's digital signal.
bool GetLeftStickButtonPressed()
Whether the left stick button was pressed since the last check.
bool GetRightStickButtonReleased()
Whether the right stick button was released since the last check.
bool GetRightBumperPressed()
Whether the right bumper (RB) was pressed since the last check.
bool GetXButton() const
Read the value of the X button on the controller.
bool GetRightStickButton() const
Read the value of the right stick button on the controller.
bool GetStartButtonReleased()
Whether the start button was released since the last check.
bool GetLeftStickButtonReleased()
Whether the left stick button was released since the last check.
bool GetRightBumperButton() const
Read the value of the right bumper button on the controller.
double GetRightTriggerAxis() const
Get the right trigger axis value of the controller.
BooleanEvent LeftBumper(EventLoop *loop) const
Constructs an event instance around the left bumper button's digital signal.
bool GetBackButton() const
Read the value of the back button on the controller.
bool GetStartButton() const
Read the value of the start button on the controller.
BooleanEvent LeftTrigger(double threshold, EventLoop *loop) const
Constructs an event instance around the axis value of the left trigger.
bool GetYButtonPressed()
Whether the Y button was pressed since the last check.
BooleanEvent Y(EventLoop *loop) const
Constructs an event instance around the Y button's digital signal.
double GetRightX() const
Get the X axis value of right side of the controller.
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
Interface for Sendable objects.
Definition Sendable.h:16
Definition CAN.h:11
Represents an axis on an XboxController.
Definition XboxController.h:536
static constexpr int kLeftTrigger
Left trigger.
Definition XboxController.h:546
static constexpr int kLeftY
Left Y axis.
Definition XboxController.h:542
static constexpr int kLeftX
Left X axis.
Definition XboxController.h:538
static constexpr int kRightTrigger
Right trigger.
Definition XboxController.h:548
static constexpr int kRightY
Right Y axis.
Definition XboxController.h:544
static constexpr int kRightX
Right X axis.
Definition XboxController.h:540
Represents a digital button on an XboxController.
Definition XboxController.h:512
static constexpr int kA
A button.
Definition XboxController.h:514
static constexpr int kY
Y button.
Definition XboxController.h:520
static constexpr int kBack
Back button.
Definition XboxController.h:526
static constexpr int kB
B button.
Definition XboxController.h:516
static constexpr int kLeftBumper
Left bumper button.
Definition XboxController.h:522
static constexpr int kRightStick
Right stick button.
Definition XboxController.h:532
static constexpr int kRightBumper
Right bumper button.
Definition XboxController.h:524
static constexpr int kLeftStick
Left stick button.
Definition XboxController.h:530
static constexpr int kX
X button.
Definition XboxController.h:518
static constexpr int kStart
Start button.
Definition XboxController.h:528