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.wpilibj2.command.Command; 008import edu.wpi.first.wpilibj2.command.Subsystem; 009import java.util.function.BooleanSupplier; 010 011/** 012 * This class provides an easy way to link commands to OI inputs. 013 * 014 * <p>It is very easy to link a button to a command. For instance, you could link the trigger button 015 * of a joystick to a "score" command. 016 * 017 * <p>This class represents a subclass of Trigger that is specifically aimed at buttons on an 018 * operator interface as a common use case of the more generalized Trigger objects. This is a simple 019 * wrapper around Trigger with the method names renamed to fit the Button object use. 020 * 021 * @deprecated Replace with {@link Trigger}. 022 */ 023@Deprecated 024public class Button extends Trigger { 025 /** 026 * Default constructor; creates a button that is never pressed. 027 * 028 * @deprecated Replace with {@code new Button(() -> false) }. 029 */ 030 @Deprecated(since = "2023") 031 public Button() {} 032 033 /** 034 * Creates a new button with the given condition determining whether it is pressed. 035 * 036 * @param isPressed returns whether or not the trigger should be active 037 * @deprecated Replace with Trigger. 038 */ 039 @Deprecated 040 public Button(BooleanSupplier isPressed) { 041 super(isPressed); 042 } 043 044 /** 045 * Starts the given command whenever the button is newly pressed. 046 * 047 * @param command the command to start 048 * @return this button, so calls can be chained 049 * @deprecated Replace with {@link Trigger#onTrue(Command)} 050 */ 051 @Deprecated 052 public Button whenPressed(final Command command) { 053 whenActive(command); 054 return this; 055 } 056 057 /** 058 * Runs the given runnable whenever the button is newly pressed. 059 * 060 * @param toRun the runnable to run 061 * @param requirements the required subsystems 062 * @return this button, so calls can be chained 063 * @deprecated Replace with {@link #onTrue(Command)}, creating the InstantCommand manually 064 */ 065 @Deprecated 066 public Button whenPressed(final Runnable toRun, Subsystem... requirements) { 067 whenActive(toRun, requirements); 068 return this; 069 } 070 071 /** 072 * Constantly starts the given command while the button is held. 073 * 074 * <p>{@link Command#schedule()} will be called repeatedly while the button is held, and will be 075 * canceled when the button is released. 076 * 077 * @param command the command to start 078 * @return this button, so calls can be chained 079 * @deprecated Use {@link #whileTrue(Command)} with {@link 080 * edu.wpi.first.wpilibj2.command.RepeatCommand RepeatCommand}. 081 */ 082 @Deprecated 083 public Button whileHeld(final Command command) { 084 whileActiveContinuous(command); 085 return this; 086 } 087 088 /** 089 * Constantly runs the given runnable while the button is held. 090 * 091 * @param toRun the runnable to run 092 * @param requirements the required subsystems 093 * @return this button, so calls can be chained 094 * @deprecated Use {@link #whileTrue(Command)} and construct a RunCommand manually 095 */ 096 @Deprecated 097 public Button whileHeld(final Runnable toRun, Subsystem... requirements) { 098 whileActiveContinuous(toRun, requirements); 099 return this; 100 } 101 102 /** 103 * Starts the given command when the button is first pressed, and cancels it when it is released, 104 * but does not start it again if it ends or is otherwise interrupted. 105 * 106 * @param command the command to start 107 * @return this button, so calls can be chained 108 * @deprecated Replace with {@link Trigger#whileTrue(Command)} 109 */ 110 @Deprecated 111 public Button whenHeld(final Command command) { 112 whileActiveOnce(command); 113 return this; 114 } 115 116 /** 117 * Starts the command when the button is released. The command is set to be interruptible. 118 * 119 * @param command the command to start 120 * @return this button, so calls can be chained 121 * @deprecated Replace with {@link Trigger#onFalse(Command)} 122 */ 123 @Deprecated 124 public Button whenReleased(final Command command) { 125 whenInactive(command); 126 return this; 127 } 128 129 /** 130 * Runs the given runnable when the button is released. 131 * 132 * @param toRun the runnable to run 133 * @param requirements the required subsystems 134 * @return this button, so calls can be chained 135 * @deprecated Replace with {@link Trigger#onFalse(Command)}, creating the InstantCommand manually 136 */ 137 @Deprecated 138 public Button whenReleased(final Runnable toRun, Subsystem... requirements) { 139 whenInactive(toRun, requirements); 140 return this; 141 } 142 143 /** 144 * Toggles the command whenever the button is pressed (on then off then on). The command is set to 145 * be interruptible. 146 * 147 * @param command the command to start 148 * @return this button, so calls can be chained 149 * @deprecated Replace with {@link Trigger#toggleOnTrue(Command)} 150 */ 151 @Deprecated 152 public Button toggleWhenPressed(final Command command) { 153 toggleWhenActive(command); 154 return this; 155 } 156 157 /** 158 * Cancels the command when the button is pressed. 159 * 160 * @param command the command to start 161 * @return this button, so calls can be chained 162 * @deprecated Instead, pass this as an end condition to {@link Command#until(BooleanSupplier)}. 163 */ 164 @Deprecated 165 public Button cancelWhenPressed(final Command command) { 166 cancelWhenActive(command); 167 return this; 168 } 169}