001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) 2008-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.buttons; 009 010import edu.wpi.first.wpilibj.command.Command; 011 012/** 013 * This class provides an easy way to link commands to OI inputs. 014 * 015 * <p>It is very easy to link a button to a command. For instance, you could link the trigger button 016 * of a joystick to a "score" command. 017 * 018 * <p>This class represents a subclass of Trigger that is specifically aimed at buttons on an 019 * operator interface as a common use case of the more generalized Trigger objects. This is a simple 020 * wrapper around Trigger with the method names renamed to fit the Button object use. 021 */ 022public abstract class Button extends Trigger { 023 /** 024 * Starts the given command whenever the button is newly pressed. 025 * 026 * @param command the command to start 027 */ 028 public void whenPressed(final Command command) { 029 whenActive(command); 030 } 031 032 /** 033 * Constantly starts the given command while the button is held. 034 * 035 * {@link Command#start()} will be called repeatedly while the button is held, and will be 036 * canceled when the button is released. 037 * 038 * @param command the command to start 039 */ 040 public void whileHeld(final Command command) { 041 whileActive(command); 042 } 043 044 /** 045 * Starts the command when the button is released. 046 * 047 * @param command the command to start 048 */ 049 public void whenReleased(final Command command) { 050 whenInactive(command); 051 } 052 053 /** 054 * Toggles the command whenever the button is pressed (on then off then on). 055 * 056 * @param command the command to start 057 */ 058 public void toggleWhenPressed(final Command command) { 059 toggleWhenActive(command); 060 } 061 062 /** 063 * Cancel the command when the button is pressed. 064 * 065 * @param command the command to start 066 */ 067 public void cancelWhenPressed(final Command command) { 068 cancelWhenActive(command); 069 } 070}