WPILibC++ 2023.4.3
Button.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <initializer_list>
9#include <span>
10#include <utility>
11
12#include <wpi/deprecated.h>
13
14#include "Trigger.h"
16
17namespace frc2 {
18class Command;
19/**
20 * A class used to bind command scheduling to button presses. Can be composed
21 * with other buttons with the operators in Trigger.
22 *
23 * This class is provided by the NewCommands VendorDep
24 *
25 * @see Trigger
26 */
27class Button : public Trigger {
28 public:
29 /**
30 * Create a new button that is pressed when the given condition is true.
31 *
32 * @param isPressed Whether the button is pressed.
33 * @deprecated Replace with Trigger
34 */
35 WPI_DEPRECATED("Replace with Trigger")
36 explicit Button(std::function<bool()> isPressed);
37
38 /**
39 * Create a new button that is pressed active (default constructor) - activity
40 * can be further determined by subclass code.
41 * @deprecated Replace with Trigger
42 */
43 WPI_DEPRECATED("Replace with Trigger")
44 Button() = default;
45
46 /**
47 * Binds a command to start when the button is pressed. Takes a
48 * raw pointer, and so is non-owning; users are responsible for the lifespan
49 * of the command.
50 *
51 * @param command The command to bind.
52 * @return The trigger, for chained calls.
53 * @deprecated Replace with Trigger::OnTrue()
54 */
55 WPI_DEPRECATED("Replace with Trigger#OnTrue()")
56 Button WhenPressed(Command* command);
57
58 /**
59 * Binds a command to start when the button is pressed. Transfers
60 * command ownership to the button scheduler, so the user does not have to
61 * worry about lifespan - rvalue refs will be *moved*, lvalue refs will be
62 * *copied.*
63 *
64 * @param command The command to bind.
65 * @return The trigger, for chained calls.
66 * @deprecated Replace with Trigger::OnTrue()
67 */
68 template <class T, typename = std::enable_if_t<std::is_base_of_v<
69 Command, std::remove_reference_t<T>>>>
70 WPI_DEPRECATED("Replace with Trigger#OnTrue()")
71 Button WhenPressed(T&& command) {
72 WhenActive(std::forward<T>(command));
73 return *this;
74 }
75
76 /**
77 * Binds a runnable to execute when the button is pressed.
78 *
79 * @param toRun the runnable to execute.
80 * @param requirements the required subsystems.
81 * @deprecated Replace with Trigger::OnTrue(cmd::RunOnce())
82 */
83 WPI_DEPRECATED("Replace with Trigger#OnTrue(cmd::RunOnce())")
84 Button WhenPressed(std::function<void()> toRun,
85 std::initializer_list<Subsystem*> requirements);
86
87 /**
88 * Binds a runnable to execute when the button is pressed.
89 *
90 * @param toRun the runnable to execute.
91 * @param requirements the required subsystems.
92 * @deprecated Replace with Trigger::OnTrue(cmd::RunOnce())
93 */
94 WPI_DEPRECATED("Replace with Trigger#OnTrue(cmd::RunOnce())")
95 Button WhenPressed(std::function<void()> toRun,
96 std::span<Subsystem* const> requirements = {});
97
98 /**
99 * Binds a command to be started repeatedly while the button is pressed, and
100 * canceled when it is released. Takes a raw pointer, and so is non-owning;
101 * users are responsible for the lifespan of the command.
102 *
103 * @param command The command to bind.
104 * @return The button, for chained calls.
105 * @deprecated Replace with Trigger::WhileTrue(command.Repeatedly())
106 */
107 WPI_DEPRECATED("Replace with Trigger#WhileTrue(command.Repeatedly())")
108 Button WhileHeld(Command* command);
109
110 /**
111 * Binds a command to be started repeatedly while the button is pressed, and
112 * canceled when it is released. Transfers command ownership to the button
113 * scheduler, so the user does not have to worry about lifespan - rvalue refs
114 * will be *moved*, lvalue refs will be *copied.*
115 *
116 * @param command The command to bind.
117 * @return The button, for chained calls.
118 * @deprecated Replace with Trigger::WhileTrue(command.Repeatedly())
119 */
120 template <class T, typename = std::enable_if_t<std::is_base_of_v<
121 Command, std::remove_reference_t<T>>>>
122 WPI_DEPRECATED("Replace with Trigger#WhileTrue(command.Repeatedly())")
123 Button WhileHeld(T&& command) {
124 WhileActiveContinous(std::forward<T>(command));
125 return *this;
126 }
127
128 /**
129 * Binds a runnable to execute repeatedly while the button is pressed.
130 *
131 * @param toRun the runnable to execute.
132 * @param requirements the required subsystems.
133 * @deprecated Replace with Trigger::WhileTrue(cmd::Run())
134 */
135 WPI_DEPRECATED("Replace with Trigger#WhileTrue(cmd::Run())")
136 Button WhileHeld(std::function<void()> toRun,
137 std::initializer_list<Subsystem*> requirements);
138
139 /**
140 * Binds a runnable to execute repeatedly while the button is pressed.
141 *
142 * @param toRun the runnable to execute.
143 * @param requirements the required subsystems.
144 * @deprecated Replace with Trigger::WhileTrue(cmd::Run())
145 */
146 WPI_DEPRECATED("Replace with Trigger#WhileTrue(cmd::Run())")
147 Button WhileHeld(std::function<void()> toRun,
148 std::span<Subsystem* const> requirements = {});
149
150 /**
151 * Binds a command to be started when the button is pressed, and canceled
152 * when it is released. Takes a raw pointer, and so is non-owning; users are
153 * responsible for the lifespan of the command.
154 *
155 * @param command The command to bind.
156 * @return The button, for chained calls.
157 * @deprecated Replace with Trigger::WhileTrue()
158 */
159 WPI_DEPRECATED("Replace with Trigger#WhileTrue()")
160 Button WhenHeld(Command* command);
161
162 /**
163 * Binds a command to be started when the button is pressed, and canceled
164 * when it is released. Transfers command ownership to the button scheduler,
165 * so the user does not have to worry about lifespan - rvalue refs will be
166 * *moved*, lvalue refs will be *copied.*
167 *
168 * @param command The command to bind.
169 * @return The button, for chained calls.
170 * @deprecated Replace with Trigger::WhileTrue()
171 */
172 template <class T, typename = std::enable_if_t<std::is_base_of_v<
173 Command, std::remove_reference_t<T>>>>
174 WPI_DEPRECATED("Replace with Trigger#WhileTrue()")
175 Button WhenHeld(T&& command) {
176 WhileActiveOnce(std::forward<T>(command));
177 return *this;
178 }
179
180 /**
181 * Binds a command to start when the button is released. Takes a
182 * raw pointer, and so is non-owning; users are responsible for the lifespan
183 * of the command.
184 *
185 * @param command The command to bind.
186 * @return The button, for chained calls.
187 * @deprecated Replace with Trigger::OnFalse()
188 */
189 WPI_DEPRECATED("Replace with Trigger#OnFalse()")
190 Button WhenReleased(Command* command);
191
192 /**
193 * Binds a command to start when the button is pressed. Transfers
194 * command ownership to the button scheduler, so the user does not have to
195 * worry about lifespan - rvalue refs will be *moved*, lvalue refs will be
196 * *copied.*
197 *
198 * @param command The command to bind.
199 * @return The button, for chained calls.
200 * @deprecated Replace with Trigger::OnFalse()
201 */
202 template <class T, typename = std::enable_if_t<std::is_base_of_v<
203 Command, std::remove_reference_t<T>>>>
204 WPI_DEPRECATED("Replace with Trigger#OnFalse()")
205 Button WhenReleased(T&& command) {
206 WhenInactive(std::forward<T>(command));
207 return *this;
208 }
209
210 /**
211 * Binds a runnable to execute when the button is released.
212 *
213 * @param toRun the runnable to execute.
214 * @param requirements the required subsystems.
215 * @deprecated Replace with Trigger::OnFalse(cmd::RunOnce())
216 */
217 WPI_DEPRECATED("Replace with Trigger#OnFalse(cmd::RunOnce())")
218 Button WhenReleased(std::function<void()> toRun,
219 std::initializer_list<Subsystem*> requirements);
220
221 /**
222 * Binds a runnable to execute when the button is released.
223 *
224 * @param toRun the runnable to execute.
225 * @param requirements the required subsystems.
226 * @deprecated Replace with Trigger::OnFalse(cmd::RunOnce())
227 */
228 WPI_DEPRECATED("Replace with Trigger#OnFalse(cmd::RunOnce())")
229 Button WhenReleased(std::function<void()> toRun,
230 std::span<Subsystem* const> requirements = {});
231
232 /**
233 * Binds a command to start when the button is pressed, and be canceled when
234 * it is pressed again. Takes a raw pointer, and so is non-owning; users are
235 * responsible for the lifespan of the command.
236 *
237 * @param command The command to bind.
238 * @return The button, for chained calls.
239 * @deprecated Replace with Trigger::ToggleOnTrue()
240 */
241 WPI_DEPRECATED("Replace with Trigger#ToggleOnTrue()")
242 Button ToggleWhenPressed(Command* command);
243
244 /**
245 * Binds a command to start when the button is pressed, and be canceled when
246 * it is pressed again. Transfers command ownership to the button scheduler,
247 * so the user does not have to worry about lifespan - rvalue refs will be
248 * *moved*, lvalue refs will be *copied.*
249 *
250 * @param command The command to bind.
251 * @return The button, for chained calls.
252 * @deprecated Replace with Trigger::ToggleOnTrue()
253 */
254 template <class T, typename = std::enable_if_t<std::is_base_of_v<
255 Command, std::remove_reference_t<T>>>>
256 WPI_DEPRECATED("Replace with Trigger#ToggleOnTrue()")
257 Button ToggleWhenPressed(T&& command) {
258 ToggleWhenActive(std::forward<T>(command));
259 return *this;
260 }
261
262 /**
263 * Binds a command to be canceled when the button is pressed. Takes a
264 * raw pointer, and so is non-owning; users are responsible for the lifespan
265 * and scheduling of the command.
266 *
267 * @param command The command to bind.
268 * @return The button, for chained calls.
269 * @deprecated Pass this as a command end condition with Until() instead.
270 */
271 WPI_DEPRECATED("Pass this as a command end condition with Until() instead.")
272 Button CancelWhenPressed(Command* command);
273};
274} // namespace frc2
A class used to bind command scheduling to button presses.
Definition: Button.h:27
This class provides an easy way to link commands to conditions.
Definition: Trigger.h:35
Definition: InstantCommand.h:14
Definition: StdDeque.h:50