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 static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
008
009import edu.wpi.first.wpilibj.GenericHID;
010
011/** A {@link Button} that gets its state from a {@link GenericHID}. */
012public class JoystickButton extends Button {
013  private final GenericHID m_joystick;
014  private final int m_buttonNumber;
015
016  /**
017   * Creates a joystick button for triggering commands.
018   *
019   * @param joystick The GenericHID object that has the button (e.g. Joystick, KinectStick, etc)
020   * @param buttonNumber The button number (see {@link GenericHID#getRawButton(int) }
021   */
022  public JoystickButton(GenericHID joystick, int buttonNumber) {
023    requireNonNullParam(joystick, "joystick", "JoystickButton");
024
025    m_joystick = joystick;
026    m_buttonNumber = buttonNumber;
027  }
028
029  /**
030   * Gets the value of the joystick button.
031   *
032   * @return The value of the joystick button
033   */
034  @Override
035  public boolean get() {
036    return m_joystick.getRawButton(m_buttonNumber);
037  }
038}