WPILibC++ 2024.3.2
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 as of the last loop poll.
44 *
45 * @return true if active, false if not active. If the event was never polled,
46 * it returns the state at event construction.
47 */
48 bool GetAsBoolean() const;
49
50 /**
51 * Bind an action to this event.
52 *
53 * @param action the action to run if this event is active.
54 */
55 void IfHigh(std::function<void()> action);
56
57 operator std::function<bool()>(); // NOLINT
58
59 /**
60 * A method to "downcast" a BooleanEvent instance to a subclass (for example,
61 * to a command-based version of this class).
62 *
63 * @param ctor a method reference to the constructor of the subclass that
64 * accepts the loop as the first parameter and the condition/signal as the
65 * second.
66 * @return an instance of the subclass.
67 */
68 template <class T>
69 T CastTo(std::function<T(EventLoop*, std::function<bool()>)> ctor =
70 [](EventLoop* loop, std::function<bool()> condition) {
71 return T(loop, condition);
72 }) {
73 return ctor(m_loop, [state = m_state] { return *state; });
74 }
75
76 /**
77 * Creates a new event that is active when this event is inactive, i.e. that
78 * acts as the negation of this event.
79 *
80 * @return the negated event
81 */
83
84 /**
85 * Composes this event with another event, returning a new event that is
86 * active when both events are active.
87 *
88 * <p>The events must use the same event loop. If the events use different
89 * event loops, the composed signal won't update until both loops are polled.
90 *
91 * @param rhs the event to compose with
92 * @return the event that is active when both events are active
93 */
94 BooleanEvent operator&&(std::function<bool()> rhs);
95
96 /**
97 * Composes this event with another event, returning a new event that is
98 * active when either event is active.
99 *
100 * <p>The events must use the same event loop. If the events use different
101 * event loops, the composed signal won't update until both loops are polled.
102 *
103 * @param rhs the event to compose with
104 * @return the event that is active when either event is active
105 */
106 BooleanEvent operator||(std::function<bool()> rhs);
107
108 /**
109 * Get a new event that events only when this one newly changes to true.
110 *
111 * @return a new event representing when this one newly changes to true.
112 */
114
115 /**
116 * Get a new event that triggers only when this one newly changes to false.
117 *
118 * @return a new event representing when this one newly changes to false.
119 */
121
122 /**
123 * Creates a new debounced event from this event - it will become active when
124 * this event has been active for longer than the specified period.
125 *
126 * @param debounceTime The debounce period.
127 * @param type The debounce type.
128 * @return The debounced event.
129 */
130 BooleanEvent Debounce(units::second_t debounceTime,
132 frc::Debouncer::DebounceType::kRising);
133
134 private:
135 EventLoop* m_loop;
136 std::function<bool()> m_condition;
137 std::shared_ptr<bool> m_state; // A programmer's worst nightmare.
138};
139} // 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 as of the last loop poll.
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:69
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
Type of debouncing to perform.
Definition: Debouncer.h:23
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition: EventLoop.h:15
state
Definition: core.h:2271
type
Definition: core.h:556
Definition: AprilTagPoseEstimator.h:15