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 Xbox 360 or Xbox One controllers connected to the Driver Station. 012 * 013 * <p>This class handles Xbox 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 XboxController extends GenericHID { 018 /** Represents a digital button on an XboxController. */ 019 public enum Button { 020 kLeftBumper(5), 021 kRightBumper(6), 022 kLeftStick(9), 023 kRightStick(10), 024 kA(1), 025 kB(2), 026 kX(3), 027 kY(4), 028 kBack(7), 029 kStart(8); 030 031 @SuppressWarnings("MemberName") 032 public final int value; 033 034 Button(int value) { 035 this.value = value; 036 } 037 038 /** 039 * Get the human-friendly name of the button, matching the relevant methods. This is done by 040 * stripping the leading `k`, and if not a Bumper button append `Button`. 041 * 042 * <p>Primarily used for automated unit tests. 043 * 044 * @return the human-friendly name of the button. 045 */ 046 @Override 047 public String toString() { 048 var name = this.name().substring(1); // Remove leading `k` 049 if (name.endsWith("Bumper")) { 050 return name; 051 } 052 return name + "Button"; 053 } 054 } 055 056 /** Represents an axis on an XboxController. */ 057 public enum Axis { 058 kLeftX(0), 059 kRightX(4), 060 kLeftY(1), 061 kRightY(5), 062 kLeftTrigger(2), 063 kRightTrigger(3); 064 065 @SuppressWarnings("MemberName") 066 public final int value; 067 068 Axis(int value) { 069 this.value = value; 070 } 071 072 /** 073 * Get the human-friendly name of the axis, matching the relevant methods. This is done by 074 * stripping the leading `k`, and if a trigger axis append `Axis`. 075 * 076 * <p>Primarily used for automated unit tests. 077 * 078 * @return the human-friendly name of the axis. 079 */ 080 @Override 081 public String toString() { 082 var name = this.name().substring(1); // Remove leading `k` 083 if (name.endsWith("Trigger")) { 084 return name + "Axis"; 085 } 086 return name; 087 } 088 } 089 090 /** 091 * Construct an instance of a controller. 092 * 093 * @param port The port index on the Driver Station that the controller is plugged into. 094 */ 095 public XboxController(final int port) { 096 super(port); 097 098 HAL.report(tResourceType.kResourceType_XboxController, port + 1); 099 } 100 101 /** 102 * Get the X axis value of left side of the controller. 103 * 104 * @return The axis value. 105 */ 106 public double getLeftX() { 107 return getRawAxis(Axis.kLeftX.value); 108 } 109 110 /** 111 * Get the X axis value of right side of the controller. 112 * 113 * @return The axis value. 114 */ 115 public double getRightX() { 116 return getRawAxis(Axis.kRightX.value); 117 } 118 119 /** 120 * Get the Y axis value of left side of the controller. 121 * 122 * @return The axis value. 123 */ 124 public double getLeftY() { 125 return getRawAxis(Axis.kLeftY.value); 126 } 127 128 /** 129 * Get the Y axis value of right side of the controller. 130 * 131 * @return The axis value. 132 */ 133 public double getRightY() { 134 return getRawAxis(Axis.kRightY.value); 135 } 136 137 /** 138 * Get the left trigger (LT) axis value of the controller. Note that this axis is bound to the 139 * range of [0, 1] as opposed to the usual [-1, 1]. 140 * 141 * @return The axis value. 142 */ 143 public double getLeftTriggerAxis() { 144 return getRawAxis(Axis.kLeftTrigger.value); 145 } 146 147 /** 148 * Get the right trigger (RT) axis value of the controller. Note that this axis is bound to the 149 * range of [0, 1] as opposed to the usual [-1, 1]. 150 * 151 * @return The axis value. 152 */ 153 public double getRightTriggerAxis() { 154 return getRawAxis(Axis.kRightTrigger.value); 155 } 156 157 /** 158 * Read the value of the left bumper (LB) button on the controller. 159 * 160 * @return The state of the button. 161 */ 162 public boolean getLeftBumper() { 163 return getRawButton(Button.kLeftBumper.value); 164 } 165 166 /** 167 * Read the value of the right bumper (RB) button on the controller. 168 * 169 * @return The state of the button. 170 */ 171 public boolean getRightBumper() { 172 return getRawButton(Button.kRightBumper.value); 173 } 174 175 /** 176 * Whether the left bumper (LB) was pressed since the last check. 177 * 178 * @return Whether the button was pressed since the last check. 179 */ 180 public boolean getLeftBumperPressed() { 181 return getRawButtonPressed(Button.kLeftBumper.value); 182 } 183 184 /** 185 * Whether the right bumper (RB) was pressed since the last check. 186 * 187 * @return Whether the button was pressed since the last check. 188 */ 189 public boolean getRightBumperPressed() { 190 return getRawButtonPressed(Button.kRightBumper.value); 191 } 192 193 /** 194 * Whether the left bumper (LB) was released since the last check. 195 * 196 * @return Whether the button was released since the last check. 197 */ 198 public boolean getLeftBumperReleased() { 199 return getRawButtonReleased(Button.kLeftBumper.value); 200 } 201 202 /** 203 * Whether the right bumper (RB) was released since the last check. 204 * 205 * @return Whether the button was released since the last check. 206 */ 207 public boolean getRightBumperReleased() { 208 return getRawButtonReleased(Button.kRightBumper.value); 209 } 210 211 /** 212 * Read the value of the left stick button (LSB) on the controller. 213 * 214 * @return The state of the button. 215 */ 216 public boolean getLeftStickButton() { 217 return getRawButton(Button.kLeftStick.value); 218 } 219 220 /** 221 * Read the value of the right stick button (RSB) on the controller. 222 * 223 * @return The state of the button. 224 */ 225 public boolean getRightStickButton() { 226 return getRawButton(Button.kRightStick.value); 227 } 228 229 /** 230 * Whether the left stick button (LSB) was pressed since the last check. 231 * 232 * @return Whether the button was pressed since the last check. 233 */ 234 public boolean getLeftStickButtonPressed() { 235 return getRawButtonPressed(Button.kLeftStick.value); 236 } 237 238 /** 239 * Whether the right stick button (RSB) was pressed since the last check. 240 * 241 * @return Whether the button was pressed since the last check. 242 */ 243 public boolean getRightStickButtonPressed() { 244 return getRawButtonPressed(Button.kRightStick.value); 245 } 246 247 /** 248 * Whether the left stick button (LSB) was released since the last check. 249 * 250 * @return Whether the button was released since the last check. 251 */ 252 public boolean getLeftStickButtonReleased() { 253 return getRawButtonReleased(Button.kLeftStick.value); 254 } 255 256 /** 257 * Whether the right stick (RSB) button was released since the last check. 258 * 259 * @return Whether the button was released since the last check. 260 */ 261 public boolean getRightStickButtonReleased() { 262 return getRawButtonReleased(Button.kRightStick.value); 263 } 264 265 /** 266 * Read the value of the A button on the controller. 267 * 268 * @return The state of the button. 269 */ 270 public boolean getAButton() { 271 return getRawButton(Button.kA.value); 272 } 273 274 /** 275 * Whether the A button was pressed since the last check. 276 * 277 * @return Whether the button was pressed since the last check. 278 */ 279 public boolean getAButtonPressed() { 280 return getRawButtonPressed(Button.kA.value); 281 } 282 283 /** 284 * Whether the A button was released since the last check. 285 * 286 * @return Whether the button was released since the last check. 287 */ 288 public boolean getAButtonReleased() { 289 return getRawButtonReleased(Button.kA.value); 290 } 291 292 /** 293 * Read the value of the B button on the controller. 294 * 295 * @return The state of the button. 296 */ 297 public boolean getBButton() { 298 return getRawButton(Button.kB.value); 299 } 300 301 /** 302 * Whether the B button was pressed since the last check. 303 * 304 * @return Whether the button was pressed since the last check. 305 */ 306 public boolean getBButtonPressed() { 307 return getRawButtonPressed(Button.kB.value); 308 } 309 310 /** 311 * Whether the B button was released since the last check. 312 * 313 * @return Whether the button was released since the last check. 314 */ 315 public boolean getBButtonReleased() { 316 return getRawButtonReleased(Button.kB.value); 317 } 318 319 /** 320 * Read the value of the X button on the controller. 321 * 322 * @return The state of the button. 323 */ 324 public boolean getXButton() { 325 return getRawButton(Button.kX.value); 326 } 327 328 /** 329 * Whether the X button was pressed since the last check. 330 * 331 * @return Whether the button was pressed since the last check. 332 */ 333 public boolean getXButtonPressed() { 334 return getRawButtonPressed(Button.kX.value); 335 } 336 337 /** 338 * Whether the X button was released since the last check. 339 * 340 * @return Whether the button was released since the last check. 341 */ 342 public boolean getXButtonReleased() { 343 return getRawButtonReleased(Button.kX.value); 344 } 345 346 /** 347 * Read the value of the Y button on the controller. 348 * 349 * @return The state of the button. 350 */ 351 public boolean getYButton() { 352 return getRawButton(Button.kY.value); 353 } 354 355 /** 356 * Whether the Y button was pressed since the last check. 357 * 358 * @return Whether the button was pressed since the last check. 359 */ 360 public boolean getYButtonPressed() { 361 return getRawButtonPressed(Button.kY.value); 362 } 363 364 /** 365 * Whether the Y button was released since the last check. 366 * 367 * @return Whether the button was released since the last check. 368 */ 369 public boolean getYButtonReleased() { 370 return getRawButtonReleased(Button.kY.value); 371 } 372 373 /** 374 * Read the value of the back button on the controller. 375 * 376 * @return The state of the button. 377 */ 378 public boolean getBackButton() { 379 return getRawButton(Button.kBack.value); 380 } 381 382 /** 383 * Whether the back button was pressed since the last check. 384 * 385 * @return Whether the button was pressed since the last check. 386 */ 387 public boolean getBackButtonPressed() { 388 return getRawButtonPressed(Button.kBack.value); 389 } 390 391 /** 392 * Whether the back button was released since the last check. 393 * 394 * @return Whether the button was released since the last check. 395 */ 396 public boolean getBackButtonReleased() { 397 return getRawButtonReleased(Button.kBack.value); 398 } 399 400 /** 401 * Read the value of the start button on the controller. 402 * 403 * @return The state of the button. 404 */ 405 public boolean getStartButton() { 406 return getRawButton(Button.kStart.value); 407 } 408 409 /** 410 * Whether the start button was pressed since the last check. 411 * 412 * @return Whether the button was pressed since the last check. 413 */ 414 public boolean getStartButtonPressed() { 415 return getRawButtonPressed(Button.kStart.value); 416 } 417 418 /** 419 * Whether the start button was released since the last check. 420 * 421 * @return Whether the button was released since the last check. 422 */ 423 public boolean getStartButtonReleased() { 424 return getRawButtonReleased(Button.kStart.value); 425 } 426}