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.wpilibj2.command.button; 006 007import edu.wpi.first.wpilibj.XboxController; 008import edu.wpi.first.wpilibj.event.EventLoop; 009import edu.wpi.first.wpilibj2.command.CommandScheduler; 010 011/** 012 * A subclass of {@link XboxController} with {@link Trigger} factories for command-based. 013 * 014 * @see XboxController 015 */ 016@SuppressWarnings("MethodName") 017public class CommandXboxController extends CommandGenericHID { 018 private final XboxController m_hid; 019 020 /** 021 * Construct an instance of a controller. 022 * 023 * @param port The port index on the Driver Station that the controller is plugged into. 024 */ 025 public CommandXboxController(int port) { 026 super(port); 027 m_hid = new XboxController(port); 028 } 029 030 /** 031 * Get the underlying GenericHID object. 032 * 033 * @return the wrapped GenericHID object 034 */ 035 @Override 036 public XboxController getHID() { 037 return m_hid; 038 } 039 040 /** 041 * Constructs an event instance around the left bumper's digital signal. 042 * 043 * @return an event instance representing the left bumper's digital signal attached to the {@link 044 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 045 * @see #leftBumper(EventLoop) 046 */ 047 public Trigger leftBumper() { 048 return leftBumper(CommandScheduler.getInstance().getDefaultButtonLoop()); 049 } 050 051 /** 052 * Constructs an event instance around the left bumper's digital signal. 053 * 054 * @param loop the event loop instance to attach the event to. 055 * @return an event instance representing the right bumper's digital signal attached to the given 056 * loop. 057 */ 058 public Trigger leftBumper(EventLoop loop) { 059 return m_hid.leftBumper(loop).castTo(Trigger::new); 060 } 061 062 /** 063 * Constructs an event instance around the right bumper's digital signal. 064 * 065 * @return an event instance representing the right bumper's digital signal attached to the {@link 066 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 067 * @see #rightBumper(EventLoop) 068 */ 069 public Trigger rightBumper() { 070 return rightBumper(CommandScheduler.getInstance().getDefaultButtonLoop()); 071 } 072 073 /** 074 * Constructs an event instance around the right bumper's digital signal. 075 * 076 * @param loop the event loop instance to attach the event to. 077 * @return an event instance representing the left bumper's digital signal attached to the given 078 * loop. 079 */ 080 public Trigger rightBumper(EventLoop loop) { 081 return m_hid.rightBumper(loop).castTo(Trigger::new); 082 } 083 084 /** 085 * Constructs an event instance around the left stick button's digital signal. 086 * 087 * @return an event instance representing the left stick button's digital signal attached to the 088 * {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 089 * @see #leftStick(EventLoop) 090 */ 091 public Trigger leftStick() { 092 return leftStick(CommandScheduler.getInstance().getDefaultButtonLoop()); 093 } 094 095 /** 096 * Constructs an event instance around the left stick button's digital signal. 097 * 098 * @param loop the event loop instance to attach the event to. 099 * @return an event instance representing the left stick button's digital signal attached to the 100 * given loop. 101 */ 102 public Trigger leftStick(EventLoop loop) { 103 return m_hid.leftStick(loop).castTo(Trigger::new); 104 } 105 106 /** 107 * Constructs an event instance around the right stick button's digital signal. 108 * 109 * @return an event instance representing the right stick button's digital signal attached to the 110 * {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 111 * @see #rightStick(EventLoop) 112 */ 113 public Trigger rightStick() { 114 return rightStick(CommandScheduler.getInstance().getDefaultButtonLoop()); 115 } 116 117 /** 118 * Constructs an event instance around the right stick button's digital signal. 119 * 120 * @param loop the event loop instance to attach the event to. 121 * @return an event instance representing the right stick button's digital signal attached to the 122 * given loop. 123 */ 124 public Trigger rightStick(EventLoop loop) { 125 return m_hid.rightStick(loop).castTo(Trigger::new); 126 } 127 128 /** 129 * Constructs an event instance around the A button's digital signal. 130 * 131 * @return an event instance representing the A button's digital signal attached to the {@link 132 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 133 * @see #a(EventLoop) 134 */ 135 public Trigger a() { 136 return a(CommandScheduler.getInstance().getDefaultButtonLoop()); 137 } 138 139 /** 140 * Constructs an event instance around the A button's digital signal. 141 * 142 * @param loop the event loop instance to attach the event to. 143 * @return an event instance representing the A button's digital signal attached to the given 144 * loop. 145 */ 146 public Trigger a(EventLoop loop) { 147 return m_hid.a(loop).castTo(Trigger::new); 148 } 149 150 /** 151 * Constructs an event instance around the B button's digital signal. 152 * 153 * @return an event instance representing the B button's digital signal attached to the {@link 154 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 155 * @see #b(EventLoop) 156 */ 157 public Trigger b() { 158 return b(CommandScheduler.getInstance().getDefaultButtonLoop()); 159 } 160 161 /** 162 * Constructs an event instance around the B button's digital signal. 163 * 164 * @param loop the event loop instance to attach the event to. 165 * @return an event instance representing the B button's digital signal attached to the given 166 * loop. 167 */ 168 public Trigger b(EventLoop loop) { 169 return m_hid.b(loop).castTo(Trigger::new); 170 } 171 172 /** 173 * Constructs an event instance around the X button's digital signal. 174 * 175 * @return an event instance representing the X button's digital signal attached to the {@link 176 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 177 * @see #x(EventLoop) 178 */ 179 public Trigger x() { 180 return x(CommandScheduler.getInstance().getDefaultButtonLoop()); 181 } 182 183 /** 184 * Constructs an event instance around the X button's digital signal. 185 * 186 * @param loop the event loop instance to attach the event to. 187 * @return an event instance representing the X button's digital signal attached to the given 188 * loop. 189 */ 190 public Trigger x(EventLoop loop) { 191 return m_hid.x(loop).castTo(Trigger::new); 192 } 193 194 /** 195 * Constructs an event instance around the Y button's digital signal. 196 * 197 * @return an event instance representing the Y button's digital signal attached to the {@link 198 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 199 * @see #y(EventLoop) 200 */ 201 public Trigger y() { 202 return y(CommandScheduler.getInstance().getDefaultButtonLoop()); 203 } 204 205 /** 206 * Constructs an event instance around the Y button's digital signal. 207 * 208 * @param loop the event loop instance to attach the event to. 209 * @return an event instance representing the Y button's digital signal attached to the given 210 * loop. 211 */ 212 public Trigger y(EventLoop loop) { 213 return m_hid.y(loop).castTo(Trigger::new); 214 } 215 216 /** 217 * Constructs an event instance around the start button's digital signal. 218 * 219 * @return an event instance representing the start button's digital signal attached to the {@link 220 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 221 * @see #start(EventLoop) 222 */ 223 public Trigger start() { 224 return start(CommandScheduler.getInstance().getDefaultButtonLoop()); 225 } 226 227 /** 228 * Constructs an event instance around the start button's digital signal. 229 * 230 * @param loop the event loop instance to attach the event to. 231 * @return an event instance representing the start button's digital signal attached to the given 232 * loop. 233 */ 234 public Trigger start(EventLoop loop) { 235 return m_hid.start(loop).castTo(Trigger::new); 236 } 237 238 /** 239 * Constructs an event instance around the back button's digital signal. 240 * 241 * @return an event instance representing the back button's digital signal attached to the {@link 242 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 243 * @see #back(EventLoop) 244 */ 245 public Trigger back() { 246 return back(CommandScheduler.getInstance().getDefaultButtonLoop()); 247 } 248 249 /** 250 * Constructs an event instance around the back button's digital signal. 251 * 252 * @param loop the event loop instance to attach the event to. 253 * @return an event instance representing the back button's digital signal attached to the given 254 * loop. 255 */ 256 public Trigger back(EventLoop loop) { 257 return m_hid.back(loop).castTo(Trigger::new); 258 } 259 260 /** 261 * Get the X axis value of left side of the controller. 262 * 263 * @return The axis value. 264 */ 265 public double getLeftX() { 266 return m_hid.getLeftX(); 267 } 268 269 /** 270 * Get the X axis value of right side of the controller. 271 * 272 * @return The axis value. 273 */ 274 public double getRightX() { 275 return m_hid.getRightX(); 276 } 277 278 /** 279 * Get the Y axis value of left side of the controller. 280 * 281 * @return The axis value. 282 */ 283 public double getLeftY() { 284 return m_hid.getLeftY(); 285 } 286 287 /** 288 * Get the Y axis value of right side of the controller. 289 * 290 * @return The axis value. 291 */ 292 public double getRightY() { 293 return m_hid.getRightY(); 294 } 295 296 /** 297 * Get the left trigger (LT) axis value of the controller. Note that this axis is bound to the 298 * range of [0, 1] as opposed to the usual [-1, 1]. 299 * 300 * @return The axis value. 301 */ 302 public double getLeftTriggerAxis() { 303 return m_hid.getLeftTriggerAxis(); 304 } 305 306 /** 307 * Get the right trigger (RT) axis value of the controller. Note that this axis is bound to the 308 * range of [0, 1] as opposed to the usual [-1, 1]. 309 * 310 * @return The axis value. 311 */ 312 public double getRightTriggerAxis() { 313 return m_hid.getRightTriggerAxis(); 314 } 315}