WPILibC++  2020.3.2
Button.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <initializer_list>
12 #include <utility>
13 
14 #include <wpi/ArrayRef.h>
15 
16 #include "Trigger.h"
17 
18 namespace frc2 {
19 class Command;
26 class Button : public Trigger {
27  public:
33  explicit Button(std::function<bool()> isPressed);
34 
39  Button() = default;
40 
50  Button WhenPressed(Command* command, bool interruptible = true);
51 
62  template <class T, typename = std::enable_if_t<std::is_base_of_v<
63  Command, std::remove_reference_t<T>>>>
64  Button WhenPressed(T&& command, bool interruptible = true) {
65  WhenActive(std::forward<T>(command), interruptible);
66  return *this;
67  }
68 
75  Button WhenPressed(std::function<void()> toRun,
76  std::initializer_list<Subsystem*> requirements);
77 
84  Button WhenPressed(std::function<void()> toRun,
85  wpi::ArrayRef<Subsystem*> requirements = {});
86 
96  Button WhileHeld(Command* command, bool interruptible = true);
97 
108  template <class T, typename = std::enable_if_t<std::is_base_of_v<
109  Command, std::remove_reference_t<T>>>>
110  Button WhileHeld(T&& command, bool interruptible = true) {
111  WhileActiveContinous(std::forward<T>(command), interruptible);
112  return *this;
113  }
114 
121  Button WhileHeld(std::function<void()> toRun,
122  std::initializer_list<Subsystem*> requirements);
123 
130  Button WhileHeld(std::function<void()> toRun,
131  wpi::ArrayRef<Subsystem*> requirements = {});
132 
142  Button WhenHeld(Command* command, bool interruptible = true);
143 
154  template <class T, typename = std::enable_if_t<std::is_base_of_v<
155  Command, std::remove_reference_t<T>>>>
156  Button WhenHeld(T&& command, bool interruptible = true) {
157  WhileActiveOnce(std::forward<T>(command), interruptible);
158  return *this;
159  }
160 
170  Button WhenReleased(Command* command, bool interruptible = true);
171 
182  template <class T, typename = std::enable_if_t<std::is_base_of_v<
183  Command, std::remove_reference_t<T>>>>
184  Button WhenReleased(T&& command, bool interruptible = true) {
185  WhenInactive(std::forward<T>(command), interruptible);
186  return *this;
187  }
188 
195  Button WhenReleased(std::function<void()> toRun,
196  std::initializer_list<Subsystem*> requirements);
197 
204  Button WhenReleased(std::function<void()> toRun,
205  wpi::ArrayRef<Subsystem*> requirements = {});
206 
216  Button ToggleWhenPressed(Command* command, bool interruptible = true);
217 
228  template <class T, typename = std::enable_if_t<std::is_base_of_v<
229  Command, std::remove_reference_t<T>>>>
230  Button ToggleWhenPressed(T&& command, bool interruptible = true) {
231  ToggleWhenActive(std::forward<T>(command), interruptible);
232  return *this;
233  }
234 
243  Button CancelWhenPressed(Command* command);
244 };
245 } // namespace frc2
frc2::Button::WhenPressed
Button WhenPressed(Command *command, bool interruptible=true)
Binds a command to start when the button is pressed.
frc2::Trigger
A class used to bind command scheduling to events.
Definition: Trigger.h:30
frc2::Button
A class used to bind command scheduling to button presses.
Definition: Button.h:26
frc2::Button::WhenReleased
Button WhenReleased(Command *command, bool interruptible=true)
Binds a command to start when the button is released.
frc2::Trigger::WhileActiveOnce
Trigger WhileActiveOnce(Command *command, bool interruptible=true)
Binds a command to be started when the trigger becomes active, and cancelled when it becomes inactive...
frc2::Button::ToggleWhenPressed
Button ToggleWhenPressed(Command *command, bool interruptible=true)
Binds a command to start when the button is pressed, and be cancelled when it is pressed again.
frc2::Button::WhenReleased
Button WhenReleased(T &&command, bool interruptible=true)
Binds a command to start when the button is pressed.
Definition: Button.h:184
frc2::Button::WhenPressed
Button WhenPressed(T &&command, bool interruptible=true)
Binds a command to start when the button is pressed.
Definition: Button.h:64
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
frc2::Command
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
frc2::Button::ToggleWhenPressed
Button ToggleWhenPressed(T &&command, bool interruptible=true)
Binds a command to start when the button is pressed, and be cancelled when it is pessed again.
Definition: Button.h:230
frc2::Button::Button
Button()=default
Create a new button that is pressed active (default constructor) - activity can be further determined...
frc2::Trigger::WhenInactive
Trigger WhenInactive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes inactive.
frc2::Trigger::WhenActive
Trigger WhenActive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes active.
frc2::Button::WhenHeld
Button WhenHeld(Command *command, bool interruptible=true)
Binds a command to be started when the button is pressed, and cancelled when it is released.
frc2::Button::WhenHeld
Button WhenHeld(T &&command, bool interruptible=true)
Binds a command to be started when the button is pressed, and cancelled when it is released.
Definition: Button.h:156
frc2::Button::WhileHeld
Button WhileHeld(Command *command, bool interruptible=true)
Binds a command to be started repeatedly while the button is pressed, and cancelled when it is releas...
frc2::Trigger::ToggleWhenActive
Trigger ToggleWhenActive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes active, and be cancelled when it again becomes acti...
frc2::Button::CancelWhenPressed
Button CancelWhenPressed(Command *command)
Binds a command to be cancelled when the button is pressed.
frc2::Button::WhileHeld
Button WhileHeld(T &&command, bool interruptible=true)
Binds a command to be started repeatedly while the button is pressed, and cancelled when it is releas...
Definition: Button.h:110
frc2::Trigger::WhileActiveContinous
Trigger WhileActiveContinous(Command *command, bool interruptible=true)
Binds a command to be started repeatedly while the trigger is active, and cancelled when it becomes i...