WPILibC++ 2024.1.1-beta-4
PS4Controller.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#pragma once
6
7#include "frc/GenericHID.h"
8
9namespace frc {
10
11/**
12 * Handle input from PS4 controllers connected to the Driver Station.
13 *
14 * <p>This class handles PS4 input that comes from the Driver Station. Each time
15 * a value is requested the most recent value is returned. There is a single
16 * class instance for each controller and the mapping of ports to hardware
17 * buttons depends on the code in the Driver Station.
18 */
19class PS4Controller : public GenericHID {
20 public:
21 /**
22 * Construct an instance of an PS4 controller.
23 *
24 * The controller index is the USB port on the Driver Station.
25 *
26 * @param port The port on the Driver Station that the controller is plugged
27 * into (0-5).
28 */
29 explicit PS4Controller(int port);
30
31 ~PS4Controller() override = default;
32
35
36 /**
37 * Get the X axis value of left side of the controller.
38 *
39 * @return the axis value.
40 */
41 double GetLeftX() const;
42
43 /**
44 * Get the X axis value of right side of the controller.
45 *
46 * @return the axis value.
47 */
48 double GetRightX() const;
49
50 /**
51 * Get the Y axis value of left side of the controller.
52 *
53 * @return the axis value.
54 */
55 double GetLeftY() const;
56
57 /**
58 * Get the Y axis value of right side of the controller.
59 *
60 * @return the axis value.
61 */
62 double GetRightY() const;
63
64 /**
65 * Get the L2 axis value of the controller. Note that this axis is bound to
66 * the range of [0, 1] as opposed to the usual [-1, 1].
67 *
68 * @return the axis value.
69 */
70 double GetL2Axis() const;
71
72 /**
73 * Get the R2 axis value of the controller. Note that this axis is bound to
74 * the range of [0, 1] as opposed to the usual [-1, 1].
75 *
76 * @return the axis value.
77 */
78 double GetR2Axis() const;
79
80 /**
81 * Read the value of the Square button on the controller.
82 *
83 * @return The state of the button.
84 */
85 bool GetSquareButton() const;
86
87 /**
88 * Whether the Square button was pressed since the last check.
89 *
90 * @return Whether the button was pressed since the last check.
91 */
93
94 /**
95 * Whether the Square button was released since the last check.
96 *
97 * @return Whether the button was released since the last check.
98 */
100
101 /**
102 * Constructs an event instance around the square button's digital signal.
103 *
104 * @param loop the event loop instance to attach the event to.
105 * @return an event instance representing the square button's digital signal
106 * attached to the given loop.
107 */
109
110 /**
111 * Read the value of the Cross button on the controller.
112 *
113 * @return The state of the button.
114 */
115 bool GetCrossButton() const;
116
117 /**
118 * Whether the Cross button was pressed since the last check.
119 *
120 * @return Whether the button was pressed since the last check.
121 */
123
124 /**
125 * Whether the Cross button was released since the last check.
126 *
127 * @return Whether the button was released since the last check.
128 */
130
131 /**
132 * Constructs an event instance around the cross button's digital signal.
133 *
134 * @param loop the event loop instance to attach the event to.
135 * @return an event instance representing the cross button's digital signal
136 * attached to the given loop.
137 */
139
140 /**
141 * Read the value of the Circle button on the controller.
142 *
143 * @return The state of the button.
144 */
145 bool GetCircleButton() const;
146
147 /**
148 * Whether the Circle button was pressed since the last check.
149 *
150 * @return Whether the button was pressed since the last check.
151 */
153
154 /**
155 * Whether the Circle button was released since the last check.
156 *
157 * @return Whether the button was released since the last check.
158 */
160
161 /**
162 * Constructs an event instance around the circle button's digital signal.
163 *
164 * @param loop the event loop instance to attach the event to.
165 * @return an event instance representing the circle button's digital signal
166 * attached to the given loop.
167 */
169
170 /**
171 * Read the value of the Triangle button on the controller.
172 *
173 * @return The state of the button.
174 */
175 bool GetTriangleButton() const;
176
177 /**
178 * Whether the Triangle button was pressed since the last check.
179 *
180 * @return Whether the button was pressed since the last check.
181 */
183
184 /**
185 * Whether the Triangle button was released since the last check.
186 *
187 * @return Whether the button was released since the last check.
188 */
190
191 /**
192 * Constructs an event instance around the triangle button's digital signal.
193 *
194 * @param loop the event loop instance to attach the event to.
195 * @return an event instance representing the triangle button's digital signal
196 * attached to the given loop.
197 */
199
200 /**
201 * Read the value of the L1 button on the controller.
202 *
203 * @return The state of the button.
204 */
205 bool GetL1Button() const;
206
207 /**
208 * Whether the L1 button was pressed since the last check.
209 *
210 * @return Whether the button was pressed since the last check.
211 */
213
214 /**
215 * Whether the L1 button was released since the last check.
216 *
217 * @return Whether the button was released since the last check.
218 */
220
221 /**
222 * Constructs an event instance around the L1 button's digital signal.
223 *
224 * @param loop the event loop instance to attach the event to.
225 * @return an event instance representing the L1 button's digital signal
226 * attached to the given loop.
227 */
229
230 /**
231 * Read the value of the R1 button on the controller.
232 *
233 * @return The state of the button.
234 */
235 bool GetR1Button() const;
236
237 /**
238 * Whether the R1 button was pressed since the last check.
239 *
240 * @return Whether the button was pressed since the last check.
241 */
243
244 /**
245 * Whether the R1 button was released since the last check.
246 *
247 * @return Whether the button was released since the last check.
248 */
250
251 /**
252 * Constructs an event instance around the R1 button's digital signal.
253 *
254 * @param loop the event loop instance to attach the event to.
255 * @return an event instance representing the R1 button's digital signal
256 * attached to the given loop.
257 */
259
260 /**
261 * Read the value of the L2 button on the controller.
262 *
263 * @return The state of the button.
264 */
265 bool GetL2Button() const;
266
267 /**
268 * Whether the L2 button was pressed since the last check.
269 *
270 * @return Whether the button was pressed since the last check.
271 */
273
274 /**
275 * Whether the L2 button was released since the last check.
276 *
277 * @return Whether the button was released since the last check.
278 */
280
281 /**
282 * Constructs an event instance around the L2 button's digital signal.
283 *
284 * @param loop the event loop instance to attach the event to.
285 * @return an event instance representing the L2 button's digital signal
286 * attached to the given loop.
287 */
289
290 /**
291 * Read the value of the R2 button on the controller.
292 *
293 * @return The state of the button.
294 */
295 bool GetR2Button() const;
296
297 /**
298 * Whether the R2 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 R2 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 R2 button's digital signal.
313 *
314 * @param loop the event loop instance to attach the event to.
315 * @return an event instance representing the R2 button's digital signal
316 * attached to the given loop.
317 */
319
320 /**
321 * Read the value of the Share button on the controller.
322 *
323 * @return The state of the button.
324 */
325 bool GetShareButton() const;
326
327 /**
328 * Whether the Share button was pressed since the last check.
329 *
330 * @return Whether the button was pressed since the last check.
331 */
333
334 /**
335 * Whether the Share button was released since the last check.
336 *
337 * @return Whether the button was released since the last check.
338 */
340
341 /**
342 * Constructs an event instance around the share button's digital signal.
343 *
344 * @param loop the event loop instance to attach the event to.
345 * @return an event instance representing the share button's digital signal
346 * attached to the given loop.
347 */
349
350 /**
351 * Read the value of the Options button on the controller.
352 *
353 * @return The state of the button.
354 */
355 bool GetOptionsButton() const;
356
357 /**
358 * Whether the Options button was pressed since the last check.
359 *
360 * @return Whether the button was pressed since the last check.
361 */
363
364 /**
365 * Whether the Options button was released since the last check.
366 *
367 * @return Whether the button was released since the last check.
368 */
370
371 /**
372 * Constructs an event instance around the options button's digital signal.
373 *
374 * @param loop the event loop instance to attach the event to.
375 * @return an event instance representing the options button's digital signal
376 * attached to the given loop.
377 */
379
380 /**
381 * Read the value of the L3 button (pressing the left analog stick) on the
382 * controller.
383 *
384 * @return The state of the button.
385 */
386 bool GetL3Button() const;
387
388 /**
389 * Whether the L3 (left stick) button was pressed since the last check.
390 *
391 * @return Whether the button was pressed since the last check.
392 */
394
395 /**
396 * Whether the L3 (left stick) button was released since the last check.
397 *
398 * @return Whether the button was released since the last check.
399 */
401
402 /**
403 * Constructs an event instance around the L3 button's digital signal.
404 *
405 * @param loop the event loop instance to attach the event to.
406 * @return an event instance representing the L3 button's digital signal
407 * attached to the given loop.
408 */
410
411 /**
412 * Read the value of the R3 button (pressing the right analog stick) on the
413 * controller.
414 *
415 * @return The state of the button.
416 */
417 bool GetR3Button() const;
418
419 /**
420 * Whether the R3 (right stick) button was pressed since the last check.
421 *
422 * @return Whether the button was pressed since the last check.
423 */
425
426 /**
427 * Whether the R3 (right stick) button was released since the last check.
428 *
429 * @return Whether the button was released since the last check.
430 */
432
433 /**
434 * Constructs an event instance around the R3 button's digital signal.
435 *
436 * @param loop the event loop instance to attach the event to.
437 * @return an event instance representing the R3 button's digital signal
438 * attached to the given loop.
439 */
441
442 /**
443 * Read the value of the PS button on the controller.
444 *
445 * @return The state of the button.
446 */
447 bool GetPSButton() const;
448
449 /**
450 * Whether the PS button was pressed since the last check.
451 *
452 * @return Whether the button was pressed since the last check.
453 */
455
456 /**
457 * Whether the PS button was released since the last check.
458 *
459 * @return Whether the button was released since the last check.
460 */
462
463 /**
464 * Constructs an event instance around the PS button's digital signal.
465 *
466 * @param loop the event loop instance to attach the event to.
467 * @return an event instance representing the PS button's digital signal
468 * attached to the given loop.
469 */
471
472 /**
473 * Read the value of the touchpad button on the controller.
474 *
475 * @return The state of the button.
476 */
477 bool GetTouchpad() const;
478
479 /**
480 * Whether the touchpad was pressed since the last check.
481 *
482 * @return Whether the touchpad was pressed since the last check.
483 */
485
486 /**
487 * Whether the touchpad was released since the last check.
488 *
489 * @return Whether the touchpad was released since the last check.
490 */
492
493 /**
494 * Constructs an event instance around the touchpad's digital signal.
495 *
496 * @param loop the event loop instance to attach the event to.
497 * @return an event instance representing the touchpad's digital signal
498 * attached to the given loop.
499 */
501
502 struct Button {
503 static constexpr int kSquare = 1;
504 static constexpr int kCross = 2;
505 static constexpr int kCircle = 3;
506 static constexpr int kTriangle = 4;
507 static constexpr int kL1 = 5;
508 static constexpr int kR1 = 6;
509 static constexpr int kL2 = 7;
510 static constexpr int kR2 = 8;
511 static constexpr int kShare = 9;
512 static constexpr int kOptions = 10;
513 static constexpr int kL3 = 11;
514 static constexpr int kR3 = 12;
515 static constexpr int kPS = 13;
516 static constexpr int kTouchpad = 14;
517 };
518
519 struct Axis {
520 static constexpr int kLeftX = 0;
521 static constexpr int kLeftY = 1;
522 static constexpr int kRightX = 2;
523 static constexpr int kRightY = 5;
524 static constexpr int kL2 = 3;
525 static constexpr int kR2 = 4;
526 };
527};
528
529} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
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 PS4 controllers connected to the Driver Station.
Definition: PS4Controller.h:19
bool GetR1ButtonPressed()
Whether the R1 button was pressed since the last check.
bool GetR2Button() const
Read the value of the R2 button on the controller.
BooleanEvent Options(EventLoop *loop) const
Constructs an event instance around the options button's digital signal.
BooleanEvent Square(EventLoop *loop) const
Constructs an event instance around the square button's digital signal.
bool GetSquareButton() const
Read the value of the Square button on the controller.
bool GetL1Button() const
Read the value of the L1 button on the controller.
bool GetR2ButtonReleased()
Whether the R2 button was released since the last check.
BooleanEvent Circle(EventLoop *loop) const
Constructs an event instance around the circle button's digital signal.
bool GetTouchpad() const
Read the value of the touchpad button on the controller.
BooleanEvent Triangle(EventLoop *loop) const
Constructs an event instance around the triangle button's digital signal.
bool GetSquareButtonReleased()
Whether the Square button was released since the last check.
double GetRightY() const
Get the Y axis value of right side of the controller.
bool GetShareButtonReleased()
Whether the Share button was released since the last check.
BooleanEvent Cross(EventLoop *loop) const
Constructs an event instance around the cross button's digital signal.
bool GetR3ButtonPressed()
Whether the R3 (right stick) button was pressed since the last check.
bool GetL3ButtonReleased()
Whether the L3 (left stick) button was released since the last check.
bool GetL3Button() const
Read the value of the L3 button (pressing the left analog stick) on the controller.
bool GetPSButtonPressed()
Whether the PS button was pressed since the last check.
BooleanEvent R3(EventLoop *loop) const
Constructs an event instance around the R3 button's digital signal.
bool GetPSButton() const
Read the value of the PS button on the controller.
BooleanEvent R1(EventLoop *loop) const
Constructs an event instance around the R1 button's digital signal.
double GetRightX() const
Get the X axis value of right side of the controller.
bool GetL3ButtonPressed()
Whether the L3 (left stick) button was pressed since the last check.
bool GetR1ButtonReleased()
Whether the R1 button was released since the last check.
bool GetOptionsButtonPressed()
Whether the Options button was pressed since the last check.
BooleanEvent R2(EventLoop *loop) const
Constructs an event instance around the R2 button's digital signal.
bool GetL1ButtonReleased()
Whether the L1 button was released since the last check.
double GetLeftY() const
Get the Y axis value of left side of the controller.
bool GetCrossButtonPressed()
Whether the Cross button was pressed since the last check.
bool GetShareButton() const
Read the value of the Share button on the controller.
BooleanEvent Share(EventLoop *loop) const
Constructs an event instance around the share button's digital signal.
bool GetPSButtonReleased()
Whether the PS button was released since the last check.
bool GetCrossButton() const
Read the value of the Cross button on the controller.
bool GetCircleButton() const
Read the value of the Circle button on the controller.
bool GetCrossButtonReleased()
Whether the Cross button was released since the last check.
double GetLeftX() const
Get the X axis value of left side of the controller.
bool GetR1Button() const
Read the value of the R1 button on the controller.
BooleanEvent Touchpad(EventLoop *loop) const
Constructs an event instance around the touchpad's digital signal.
PS4Controller(int port)
Construct an instance of an PS4 controller.
bool GetTriangleButton() const
Read the value of the Triangle button on the controller.
BooleanEvent L1(EventLoop *loop) const
Constructs an event instance around the L1 button's digital signal.
BooleanEvent PS(EventLoop *loop) const
Constructs an event instance around the PS button's digital signal.
bool GetTriangleButtonReleased()
Whether the Triangle button was released since the last check.
bool GetL1ButtonPressed()
Whether the L1 button was pressed since the last check.
bool GetSquareButtonPressed()
Whether the Square button was pressed since the last check.
bool GetCircleButtonPressed()
Whether the Circle button was pressed since the last check.
bool GetTouchpadReleased()
Whether the touchpad was released since the last check.
bool GetTriangleButtonPressed()
Whether the Triangle button was pressed since the last check.
bool GetR3Button() const
Read the value of the R3 button (pressing the right analog stick) on the controller.
bool GetOptionsButton() const
Read the value of the Options button on the controller.
double GetR2Axis() const
Get the R2 axis value of the controller.
bool GetL2Button() const
Read the value of the L2 button on the controller.
bool GetL2ButtonReleased()
Whether the L2 button was released since the last check.
bool GetOptionsButtonReleased()
Whether the Options button was released since the last check.
bool GetR3ButtonReleased()
Whether the R3 (right stick) button was released since the last check.
bool GetL2ButtonPressed()
Whether the L2 button was pressed since the last check.
bool GetShareButtonPressed()
Whether the Share button was pressed since the last check.
bool GetR2ButtonPressed()
Whether the R2 button was pressed since the last check.
PS4Controller(PS4Controller &&)=default
PS4Controller & operator=(PS4Controller &&)=default
BooleanEvent L3(EventLoop *loop) const
Constructs an event instance around the L3 button's digital signal.
bool GetTouchpadPressed()
Whether the touchpad was pressed since the last check.
~PS4Controller() override=default
bool GetCircleButtonReleased()
Whether the Circle button was released since the last check.
double GetL2Axis() const
Get the L2 axis value of the controller.
BooleanEvent L2(EventLoop *loop) const
Constructs an event instance around the L2 button's digital signal.
Definition: AprilTagPoseEstimator.h:15
Definition: PS4Controller.h:519
static constexpr int kRightY
Definition: PS4Controller.h:523
static constexpr int kR2
Definition: PS4Controller.h:525
static constexpr int kLeftX
Definition: PS4Controller.h:520
static constexpr int kRightX
Definition: PS4Controller.h:522
static constexpr int kL2
Definition: PS4Controller.h:524
static constexpr int kLeftY
Definition: PS4Controller.h:521
Definition: PS4Controller.h:502
static constexpr int kTouchpad
Definition: PS4Controller.h:516
static constexpr int kL3
Definition: PS4Controller.h:513
static constexpr int kR1
Definition: PS4Controller.h:508
static constexpr int kTriangle
Definition: PS4Controller.h:506
static constexpr int kSquare
Definition: PS4Controller.h:503
static constexpr int kOptions
Definition: PS4Controller.h:512
static constexpr int kShare
Definition: PS4Controller.h:511
static constexpr int kR2
Definition: PS4Controller.h:510
static constexpr int kCircle
Definition: PS4Controller.h:505
static constexpr int kR3
Definition: PS4Controller.h:514
static constexpr int kL2
Definition: PS4Controller.h:509
static constexpr int kCross
Definition: PS4Controller.h:504
static constexpr int kPS
Definition: PS4Controller.h:515
static constexpr int kL1
Definition: PS4Controller.h:507