001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ 003/* Open Source Software - may be modified and shared by FRC teams. The code */ 004/* must be accompanied by the FIRST BSD license file in the root directory of */ 005/* the project. */ 006/*----------------------------------------------------------------------------*/ 007 008package edu.wpi.first.wpilibj; 009 010import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType; 011import edu.wpi.first.wpilibj.hal.HAL; 012 013/** 014 * Handle input from Xbox 360 or Xbox One controllers connected to the Driver Station. 015 * 016 * <p>This class handles Xbox input that comes from the Driver Station. Each time a value is 017 * requested the most recent value is returned. There is a single class instance for each controller 018 * and the mapping of ports to hardware buttons depends on the code in the Driver Station. 019 */ 020public class XboxController extends GenericHID { 021 /** 022 * Represents a digital button on an XboxController. 023 */ 024 private enum Button { 025 kBumperLeft(5), 026 kBumperRight(6), 027 kStickLeft(9), 028 kStickRight(10), 029 kA(1), 030 kB(2), 031 kX(3), 032 kY(4), 033 kBack(7), 034 kStart(8); 035 036 @SuppressWarnings("MemberName") 037 private int value; 038 039 Button(int value) { 040 this.value = value; 041 } 042 } 043 044 /** 045 * Construct an instance of a joystick. The joystick index is the USB port on the drivers 046 * station. 047 * 048 * @param port The port on the Driver Station that the joystick is plugged into. 049 */ 050 public XboxController(final int port) { 051 super(port); 052 053 // HAL.report(tResourceType.kResourceType_XboxController, port); 054 HAL.report(tResourceType.kResourceType_Joystick, port); 055 } 056 057 /** 058 * Get the X axis value of the controller. 059 * 060 * @param hand Side of controller whose value should be returned. 061 * @return The X axis value of the controller. 062 */ 063 @Override 064 public double getX(Hand hand) { 065 if (hand.equals(Hand.kLeft)) { 066 return getRawAxis(0); 067 } else { 068 return getRawAxis(4); 069 } 070 } 071 072 /** 073 * Get the Y axis value of the controller. 074 * 075 * @param hand Side of controller whose value should be returned. 076 * @return The Y axis value of the controller. 077 */ 078 @Override 079 public double getY(Hand hand) { 080 if (hand.equals(Hand.kLeft)) { 081 return getRawAxis(1); 082 } else { 083 return getRawAxis(5); 084 } 085 } 086 087 /** 088 * Get the trigger axis value of the controller. 089 * 090 * @param hand Side of controller whose value should be returned. 091 * @return The trigger axis value of the controller. 092 */ 093 public double getTriggerAxis(Hand hand) { 094 if (hand.equals(Hand.kLeft)) { 095 return getRawAxis(2); 096 } else { 097 return getRawAxis(3); 098 } 099 } 100 101 /** 102 * Read the value of the bumper button on the controller. 103 * 104 * @param hand Side of controller whose value should be returned. 105 * @return The state of the button. 106 */ 107 public boolean getBumper(Hand hand) { 108 if (hand.equals(Hand.kLeft)) { 109 return getRawButton(Button.kBumperLeft.value); 110 } else { 111 return getRawButton(Button.kBumperRight.value); 112 } 113 } 114 115 /** 116 * Whether the bumper was pressed since the last check. 117 * 118 * @param hand Side of controller whose value should be returned. 119 * @return Whether the button was pressed since the last check. 120 */ 121 public boolean getBumperPressed(Hand hand) { 122 if (hand == Hand.kLeft) { 123 return getRawButtonPressed(Button.kBumperLeft.value); 124 } else { 125 return getRawButtonPressed(Button.kBumperRight.value); 126 } 127 } 128 129 /** 130 * Whether the bumper was released since the last check. 131 * 132 * @param hand Side of controller whose value should be returned. 133 * @return Whether the button was released since the last check. 134 */ 135 public boolean getBumperReleased(Hand hand) { 136 if (hand == Hand.kLeft) { 137 return getRawButtonReleased(Button.kBumperLeft.value); 138 } else { 139 return getRawButtonReleased(Button.kBumperRight.value); 140 } 141 } 142 143 /** 144 * Read the value of the stick button on the controller. 145 * 146 * @param hand Side of controller whose value should be returned. 147 * @return The state of the button. 148 */ 149 public boolean getStickButton(Hand hand) { 150 if (hand.equals(Hand.kLeft)) { 151 return getRawButton(Button.kStickLeft.value); 152 } else { 153 return getRawButton(Button.kStickRight.value); 154 } 155 } 156 157 /** 158 * Whether the stick button was pressed since the last check. 159 * 160 * @param hand Side of controller whose value should be returned. 161 * @return Whether the button was pressed since the last check. 162 */ 163 public boolean getStickButtonPressed(Hand hand) { 164 if (hand == Hand.kLeft) { 165 return getRawButtonPressed(Button.kStickLeft.value); 166 } else { 167 return getRawButtonPressed(Button.kStickRight.value); 168 } 169 } 170 171 /** 172 * Whether the stick button was released since the last check. 173 * 174 * @param hand Side of controller whose value should be returned. 175 * @return Whether the button was released since the last check. 176 */ 177 public boolean getStickButtonReleased(Hand hand) { 178 if (hand == Hand.kLeft) { 179 return getRawButtonReleased(Button.kStickLeft.value); 180 } else { 181 return getRawButtonReleased(Button.kStickRight.value); 182 } 183 } 184 185 /** 186 * Read the value of the A button on the controller. 187 * 188 * @return The state of the button. 189 */ 190 public boolean getAButton() { 191 return getRawButton(Button.kA.value); 192 } 193 194 /** 195 * Whether the A button was pressed since the last check. 196 * 197 * @return Whether the button was pressed since the last check. 198 */ 199 public boolean getAButtonPressed() { 200 return getRawButtonPressed(Button.kA.value); 201 } 202 203 /** 204 * Whether the A button was released since the last check. 205 * 206 * @return Whether the button was released since the last check. 207 */ 208 public boolean getAButtonReleased() { 209 return getRawButtonReleased(Button.kA.value); 210 } 211 212 /** 213 * Read the value of the B button on the controller. 214 * 215 * @return The state of the button. 216 */ 217 public boolean getBButton() { 218 return getRawButton(Button.kB.value); 219 } 220 221 /** 222 * Whether the B button was pressed since the last check. 223 * 224 * @return Whether the button was pressed since the last check. 225 */ 226 public boolean getBButtonPressed() { 227 return getRawButtonPressed(Button.kB.value); 228 } 229 230 /** 231 * Whether the B button was released since the last check. 232 * 233 * @return Whether the button was released since the last check. 234 */ 235 public boolean getBButtonReleased() { 236 return getRawButtonReleased(Button.kB.value); 237 } 238 239 /** 240 * Read the value of the X button on the controller. 241 * 242 * @return The state of the button. 243 */ 244 public boolean getXButton() { 245 return getRawButton(Button.kX.value); 246 } 247 248 /** 249 * Whether the X button was pressed since the last check. 250 * 251 * @return Whether the button was pressed since the last check. 252 */ 253 public boolean getXButtonPressed() { 254 return getRawButtonPressed(Button.kX.value); 255 } 256 257 /** 258 * Whether the X button was released since the last check. 259 * 260 * @return Whether the button was released since the last check. 261 */ 262 public boolean getXButtonReleased() { 263 return getRawButtonReleased(Button.kX.value); 264 } 265 266 /** 267 * Read the value of the Y button on the controller. 268 * 269 * @return The state of the button. 270 */ 271 public boolean getYButton() { 272 return getRawButton(Button.kY.value); 273 } 274 275 /** 276 * Whether the Y button was pressed since the last check. 277 * 278 * @return Whether the button was pressed since the last check. 279 */ 280 public boolean getYButtonPressed() { 281 return getRawButtonPressed(Button.kY.value); 282 } 283 284 /** 285 * Whether the Y button was released since the last check. 286 * 287 * @return Whether the button was released since the last check. 288 */ 289 public boolean getYButtonReleased() { 290 return getRawButtonReleased(Button.kY.value); 291 } 292 293 /** 294 * Read the value of the back button on the controller. 295 * 296 * @return The state of the button. 297 */ 298 public boolean getBackButton() { 299 return getRawButton(Button.kBack.value); 300 } 301 302 /** 303 * Whether the back button was pressed since the last check. 304 * 305 * @return Whether the button was pressed since the last check. 306 */ 307 public boolean getBackButtonPressed() { 308 return getRawButtonPressed(Button.kBack.value); 309 } 310 311 /** 312 * Whether the back button was released since the last check. 313 * 314 * @return Whether the button was released since the last check. 315 */ 316 public boolean getBackButtonReleased() { 317 return getRawButtonReleased(Button.kBack.value); 318 } 319 320 /** 321 * Read the value of the start button on the controller. 322 * 323 * @return The state of the button. 324 */ 325 public boolean getStartButton() { 326 return getRawButton(Button.kStart.value); 327 } 328 329 /** 330 * Whether the start button was pressed since the last check. 331 * 332 * @return Whether the button was pressed since the last check. 333 */ 334 public boolean getStartButtonPressed() { 335 return getRawButtonPressed(Button.kStart.value); 336 } 337 338 /** 339 * Whether the start button was released since the last check. 340 * 341 * @return Whether the button was released since the last check. 342 */ 343 public boolean getStartButtonReleased() { 344 return getRawButtonReleased(Button.kStart.value); 345 } 346}