WPILibC++ 2023.4.3
BooleanEvent.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
8
9#include <functional>
10#include <memory>
11
12#include <units/time.h>
13#include <wpi/FunctionExtras.h>
14
15#include "EventLoop.h"
16
17namespace frc {
18
19/**
20 * This class provides an easy way to link actions to inputs. Each object
21 * represents a boolean condition to which callback actions can be bound using
22 * {@link #IfHigh(std::function<void()>)}.
23 *
24 * <p>These events can easily be composed using factories such as {@link
25 * #operator!},
26 * {@link #operator||}, {@link #operator&&} etc.
27 *
28 * <p>To get an event that activates only when this one changes, see {@link
29 * #Falling()} and {@link #Rising()}.
30 */
32 public:
33 /**
34 * Creates a new event with the given condition determining whether it is
35 * active.
36 *
37 * @param loop the loop that polls this event
38 * @param condition returns whether or not the event should be active
39 */
40 BooleanEvent(EventLoop* loop, std::function<bool()> condition);
41
42 /**
43 * Check whether this event is active or not.
44 *
45 * @return true if active.
46 */
47 bool GetAsBoolean() const;
48
49 /**
50 * Bind an action to this event.
51 *
52 * @param action the action to run if this event is active.
53 */
54 void IfHigh(std::function<void()> action);
55
56 operator std::function<bool()>(); // NOLINT
57
58 /**
59 * A method to "downcast" a BooleanEvent instance to a subclass (for example,
60 * to a command-based version of this class).
61 *
62 * @param ctor a method reference to the constructor of the subclass that
63 * accepts the loop as the first parameter and the condition/signal as the
64 * second.
65 * @return an instance of the subclass.
66 */
67 template <class T>
68 T CastTo(std::function<T(EventLoop*, std::function<bool()>)> ctor =
69 [](EventLoop* loop, std::function<bool()> condition) {
70 return T(loop, condition);
71 }) {
72 return ctor(m_loop, m_condition);
73 }
74
75 /**
76 * Creates a new event that is active when this event is inactive, i.e. that
77 * acts as the negation of this event.
78 *
79 * @return the negated event
80 */
82
83 /**
84 * Composes this event with another event, returning a new event that is
85 * active when both events are active.
86 *
87 * <p>The new event will use this event's polling loop.
88 *
89 * @param rhs the event to compose with
90 * @return the event that is active when both events are active
91 */
92 BooleanEvent operator&&(std::function<bool()> rhs);
93
94 /**
95 * Composes this event with another event, returning a new event that is
96 * active when either event is active.
97 *
98 * <p>The new event will use this event's polling loop.
99 *
100 * @param rhs the event to compose with
101 * @return the event that is active when either event is active
102 */
103 BooleanEvent operator||(std::function<bool()> rhs);
104
105 /**
106 * Get a new event that events only when this one newly changes to true.
107 *
108 * @return a new event representing when this one newly changes to true.
109 */
111
112 /**
113 * Get a new event that triggers only when this one newly changes to false.
114 *
115 * @return a new event representing when this one newly changes to false.
116 */
118
119 /**
120 * Creates a new debounced event from this event - it will become active when
121 * this event has been active for longer than the specified period.
122 *
123 * @param debounceTime The debounce period.
124 * @param type The debounce type.
125 * @return The debounced event.
126 */
127 BooleanEvent Debounce(units::second_t debounceTime,
129 frc::Debouncer::DebounceType::kRising);
130
131 private:
132 EventLoop* m_loop;
133 std::function<bool()> m_condition;
134};
135} // namespace frc
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
BooleanEvent Falling()
Get a new event that triggers only when this one newly changes to false.
bool GetAsBoolean() const
Check whether this event is active or not.
BooleanEvent operator!()
Creates a new event that is active when this event is inactive, i.e.
BooleanEvent operator&&(std::function< bool()> rhs)
Composes this event with another event, returning a new event that is active when both events are act...
BooleanEvent Debounce(units::second_t debounceTime, frc::Debouncer::DebounceType type=frc::Debouncer::DebounceType::kRising)
Creates a new debounced event from this event - it will become active when this event has been active...
void IfHigh(std::function< void()> action)
Bind an action to this event.
T CastTo(std::function< T(EventLoop *, std::function< bool()>)> ctor=[](EventLoop *loop, std::function< bool()> condition) { return T(loop, condition);})
A method to "downcast" a BooleanEvent instance to a subclass (for example, to a command-based version...
Definition: BooleanEvent.h:68
BooleanEvent Rising()
Get a new event that events only when this one newly changes to true.
BooleanEvent(EventLoop *loop, std::function< bool()> condition)
Creates a new event with the given condition determining whether it is active.
BooleanEvent operator||(std::function< bool()> rhs)
Composes this event with another event, returning a new event that is active when either event is act...
DebounceType
Definition: Debouncer.h:20
The loop polling BooleanEvent objects and executing the actions bound to them.
Definition: EventLoop.h:15
type
Definition: core.h:575
Definition: AprilTagFieldLayout.h:22