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