001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj; 006 007import edu.wpi.first.hal.FRCNetComm.tResourceType; 008import edu.wpi.first.hal.HAL; 009 010/** 011 * Handle input from PS4 controllers connected to the Driver Station. 012 * 013 * <p>This class handles PS4 input that comes from the Driver Station. Each time a value is 014 * requested the most recent value is returned. There is a single class instance for each controller 015 * and the mapping of ports to hardware buttons depends on the code in the Driver Station. 016 */ 017public class PS4Controller extends GenericHID { 018 /** 019 * Construct an instance of a device. 020 * 021 * @param port The port index on the Driver Station that the device is plugged into. 022 */ 023 public PS4Controller(int port) { 024 super(port); 025 HAL.report(tResourceType.kResourceType_PS4Controller, port + 1); 026 } 027 028 /** Represents a digital button on a PS4Controller. */ 029 public enum Button { 030 kSquare(1), 031 kCross(2), 032 kCircle(3), 033 kTriangle(4), 034 kL1(5), 035 kR1(6), 036 kL2(7), 037 kR2(8), 038 kShare(9), 039 kOptions(10), 040 kL3(11), 041 kR3(12), 042 kPS(13), 043 kTouchpad(14); 044 045 public final int value; 046 047 Button(int index) { 048 this.value = index; 049 } 050 051 /** 052 * Get the human-friendly name of the button, matching the relevant methods. This is done by 053 * stripping the leading `k`, and if not the touchpad append `Button`. 054 * 055 * <p>Primarily used for automated unit tests. 056 * 057 * @return the human-friendly name of the button. 058 */ 059 @Override 060 public String toString() { 061 var name = this.name().substring(1); // Remove leading `k` 062 if (this == kTouchpad) { 063 return name; 064 } 065 return name + "Button"; 066 } 067 } 068 069 /** Represents an axis on a PS4Controller. */ 070 public enum Axis { 071 kLeftX(0), 072 kLeftY(1), 073 kRightX(2), 074 kRightY(5), 075 kL2(3), 076 kR2(4); 077 078 public final int value; 079 080 Axis(int index) { 081 value = index; 082 } 083 084 /** 085 * Get the human-friendly name of the axis, matching the relevant methods. This is done by 086 * stripping the leading `k`, and if one of L2/R2 append `Axis`. 087 * 088 * <p>Primarily used for automated unit tests. 089 * 090 * @return the human-friendly name of the axis. 091 */ 092 @Override 093 public String toString() { 094 var name = this.name().substring(1); // Remove leading `k` 095 if (name.endsWith("2")) { 096 return name + "Axis"; 097 } 098 return name; 099 } 100 } 101 102 /** 103 * Get the X axis value of left side of the controller. 104 * 105 * @return the axis value. 106 */ 107 public double getLeftX() { 108 return getRawAxis(Axis.kLeftX.value); 109 } 110 111 /** 112 * Get the X axis value of right side of the controller. 113 * 114 * @return the axis value. 115 */ 116 public double getRightX() { 117 return getRawAxis(Axis.kRightX.value); 118 } 119 120 /** 121 * Get the Y axis value of left side of the controller. 122 * 123 * @return the axis value. 124 */ 125 public double getLeftY() { 126 return getRawAxis(Axis.kLeftY.value); 127 } 128 129 /** 130 * Get the Y axis value of right side of the controller. 131 * 132 * @return the axis value. 133 */ 134 public double getRightY() { 135 return getRawAxis(Axis.kRightY.value); 136 } 137 138 /** 139 * Get the L2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as 140 * opposed to the usual [-1, 1]. 141 * 142 * @return the axis value. 143 */ 144 public double getL2Axis() { 145 return getRawAxis(Axis.kL2.value); 146 } 147 148 /** 149 * Get the R2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as 150 * opposed to the usual [-1, 1]. 151 * 152 * @return the axis value. 153 */ 154 public double getR2Axis() { 155 return getRawAxis(Axis.kR2.value); 156 } 157 158 /** 159 * Read the value of the left trigger button on the controller. 160 * 161 * @return The state of the button. 162 */ 163 public boolean getL2Button() { 164 return getRawButton(Button.kL2.value); 165 } 166 167 /** 168 * Read the value of the right trigger button on the controller. 169 * 170 * @return The state of the button. 171 */ 172 public boolean getR2Button() { 173 return getRawButton(Button.kR2.value); 174 } 175 176 /** 177 * Whether the L2 button was pressed since the last check. 178 * 179 * @return Whether the button was pressed since the last check. 180 */ 181 public boolean getL2ButtonPressed() { 182 return getRawButtonPressed(Button.kL2.value); 183 } 184 185 /** 186 * Whether the R2 button was pressed since the last check. 187 * 188 * @return Whether the button was pressed since the last check. 189 */ 190 public boolean getR2ButtonPressed() { 191 return getRawButtonPressed(Button.kR2.value); 192 } 193 194 /** 195 * Whether the L2 button was released since the last check. 196 * 197 * @return Whether the button was released since the last check. 198 */ 199 public boolean getL2ButtonReleased() { 200 return getRawButtonReleased(Button.kL2.value); 201 } 202 203 /** 204 * Whether the R2 button was released since the last check. 205 * 206 * @return Whether the button was released since the last check. 207 */ 208 public boolean getR2ButtonReleased() { 209 return getRawButtonReleased(Button.kR2.value); 210 } 211 212 /** 213 * Read the value of the L1 button on the controller. 214 * 215 * @return The state of the button. 216 */ 217 public boolean getL1Button() { 218 return getRawButton(Button.kL1.value); 219 } 220 221 /** 222 * Read the value of the R1 button on the controller. 223 * 224 * @return The state of the button. 225 */ 226 public boolean getR1Button() { 227 return getRawButton(Button.kR1.value); 228 } 229 230 /** 231 * Whether the L1 button was pressed since the last check. 232 * 233 * @return Whether the button was pressed since the last check. 234 */ 235 public boolean getL1ButtonPressed() { 236 return getRawButtonPressed(Button.kL1.value); 237 } 238 239 /** 240 * Whether the R1 button was pressed since the last check. 241 * 242 * @return Whether the button was pressed since the last check. 243 */ 244 public boolean getR1ButtonPressed() { 245 return getRawButtonPressed(Button.kR1.value); 246 } 247 248 /** 249 * Whether the L1 button was released since the last check. 250 * 251 * @return Whether the button was released since the last check. 252 */ 253 public boolean getL1ButtonReleased() { 254 return getRawButtonReleased(Button.kL1.value); 255 } 256 257 /** 258 * Whether the R1 button was released since the last check. 259 * 260 * @return Whether the button was released since the last check. 261 */ 262 public boolean getR1ButtonReleased() { 263 return getRawButtonReleased(Button.kR1.value); 264 } 265 266 /** 267 * Read the value of the L3 button (pressing the left analog stick) on the controller. 268 * 269 * @return The state of the button. 270 */ 271 public boolean getL3Button() { 272 return getRawButton(Button.kL3.value); 273 } 274 275 /** 276 * Read the value of the R3 button (pressing the right analog stick) on the controller. 277 * 278 * @return The state of the button. 279 */ 280 public boolean getR3Button() { 281 return getRawButton(Button.kR3.value); 282 } 283 284 /** 285 * Whether the L3 (left stick) button was pressed since the last check. 286 * 287 * @return Whether the button was pressed since the last check. 288 */ 289 public boolean getL3ButtonPressed() { 290 return getRawButtonPressed(Button.kL3.value); 291 } 292 293 /** 294 * Whether the R3 (right stick) button was pressed since the last check. 295 * 296 * @return Whether the button was pressed since the last check. 297 */ 298 public boolean getR3ButtonPressed() { 299 return getRawButtonPressed(Button.kR3.value); 300 } 301 302 /** 303 * Whether the L3 (left stick) button was released since the last check. 304 * 305 * @return Whether the button was released since the last check. 306 */ 307 public boolean getL3ButtonReleased() { 308 return getRawButtonReleased(Button.kL3.value); 309 } 310 311 /** 312 * Whether the R3 (right stick) button was released since the last check. 313 * 314 * @return Whether the button was released since the last check. 315 */ 316 public boolean getR3ButtonReleased() { 317 return getRawButtonReleased(Button.kR3.value); 318 } 319 320 /** 321 * Read the value of the Square button on the controller. 322 * 323 * @return The state of the button. 324 */ 325 public boolean getSquareButton() { 326 return getRawButton(Button.kSquare.value); 327 } 328 329 /** 330 * Whether the Square button was pressed since the last check. 331 * 332 * @return Whether the button was pressed since the last check. 333 */ 334 public boolean getSquareButtonPressed() { 335 return getRawButtonPressed(Button.kSquare.value); 336 } 337 338 /** 339 * Whether the Square button was released since the last check. 340 * 341 * @return Whether the button was released since the last check. 342 */ 343 public boolean getSquareButtonReleased() { 344 return getRawButtonReleased(Button.kSquare.value); 345 } 346 347 /** 348 * Read the value of the Cross button on the controller. 349 * 350 * @return The state of the button. 351 */ 352 public boolean getCrossButton() { 353 return getRawButton(Button.kCross.value); 354 } 355 356 /** 357 * Whether the Cross button was pressed since the last check. 358 * 359 * @return Whether the button was pressed since the last check. 360 */ 361 public boolean getCrossButtonPressed() { 362 return getRawButtonPressed(Button.kCross.value); 363 } 364 365 /** 366 * Whether the Cross button was released since the last check. 367 * 368 * @return Whether the button was released since the last check. 369 */ 370 public boolean getCrossButtonReleased() { 371 return getRawButtonReleased(Button.kCross.value); 372 } 373 374 /** 375 * Read the value of the Triangle button on the controller. 376 * 377 * @return The state of the button. 378 */ 379 public boolean getTriangleButton() { 380 return getRawButton(Button.kTriangle.value); 381 } 382 383 /** 384 * Whether the Triangle button was pressed since the last check. 385 * 386 * @return Whether the button was pressed since the last check. 387 */ 388 public boolean getTriangleButtonPressed() { 389 return getRawButtonPressed(Button.kTriangle.value); 390 } 391 392 /** 393 * Whether the Triangle button was released since the last check. 394 * 395 * @return Whether the button was released since the last check. 396 */ 397 public boolean getTriangleButtonReleased() { 398 return getRawButtonReleased(Button.kTriangle.value); 399 } 400 401 /** 402 * Read the value of the Circle button on the controller. 403 * 404 * @return The state of the button. 405 */ 406 public boolean getCircleButton() { 407 return getRawButton(Button.kCircle.value); 408 } 409 410 /** 411 * Whether the Circle button was pressed since the last check. 412 * 413 * @return Whether the button was pressed since the last check. 414 */ 415 public boolean getCircleButtonPressed() { 416 return getRawButtonPressed(Button.kCircle.value); 417 } 418 419 /** 420 * Whether the Circle button was released since the last check. 421 * 422 * @return Whether the button was released since the last check. 423 */ 424 public boolean getCircleButtonReleased() { 425 return getRawButtonReleased(Button.kCircle.value); 426 } 427 428 /** 429 * Read the value of the share button on the controller. 430 * 431 * @return The state of the button. 432 */ 433 public boolean getShareButton() { 434 return getRawButton(Button.kShare.value); 435 } 436 437 /** 438 * Whether the share button was pressed since the last check. 439 * 440 * @return Whether the button was pressed since the last check. 441 */ 442 public boolean getShareButtonPressed() { 443 return getRawButtonPressed(Button.kShare.value); 444 } 445 446 /** 447 * Whether the share button was released since the last check. 448 * 449 * @return Whether the button was released since the last check. 450 */ 451 public boolean getShareButtonReleased() { 452 return getRawButtonReleased(Button.kShare.value); 453 } 454 455 /** 456 * Read the value of the PS button on the controller. 457 * 458 * @return The state of the button. 459 */ 460 public boolean getPSButton() { 461 return getRawButton(Button.kPS.value); 462 } 463 464 /** 465 * Whether the PS button was pressed since the last check. 466 * 467 * @return Whether the button was pressed since the last check. 468 */ 469 public boolean getPSButtonPressed() { 470 return getRawButtonPressed(Button.kPS.value); 471 } 472 473 /** 474 * Whether the PS button was released since the last check. 475 * 476 * @return Whether the button was released since the last check. 477 */ 478 public boolean getPSButtonReleased() { 479 return getRawButtonReleased(Button.kPS.value); 480 } 481 482 /** 483 * Read the value of the options button on the controller. 484 * 485 * @return The state of the button. 486 */ 487 public boolean getOptionsButton() { 488 return getRawButton(Button.kOptions.value); 489 } 490 491 /** 492 * Whether the options button was pressed since the last check. 493 * 494 * @return Whether the button was pressed since the last check. 495 */ 496 public boolean getOptionsButtonPressed() { 497 return getRawButtonPressed(Button.kOptions.value); 498 } 499 500 /** 501 * Whether the options button was released since the last check. 502 * 503 * @return Whether the button was released since the last check. 504 */ 505 public boolean getOptionsButtonReleased() { 506 return getRawButtonReleased(Button.kOptions.value); 507 } 508 509 /** 510 * Read the value of the touchpad on the controller. 511 * 512 * @return The state of the touchpad. 513 */ 514 public boolean getTouchpad() { 515 return getRawButton(Button.kTouchpad.value); 516 } 517 518 /** 519 * Whether the touchpad was pressed since the last check. 520 * 521 * @return Whether the touchpad was pressed since the last check. 522 */ 523 public boolean getTouchpadPressed() { 524 return getRawButtonPressed(Button.kTouchpad.value); 525 } 526 527 /** 528 * Whether the touchpad was released since the last check. 529 * 530 * @return Whether the touchpad was released since the last check. 531 */ 532 public boolean getTouchpadReleased() { 533 return getRawButtonReleased(Button.kTouchpad.value); 534 } 535}