WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
BooleanEvent.hpp
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 <memory>
9
10#include "EventLoop.hpp"
12#include "wpi/units/time.hpp"
14
15namespace wpi {
16
17/**
18 * This class provides an easy way to link actions to active high logic signals.
19 * Each object represents a digital signal to which callback actions can be
20 * bound using {@link #IfHigh(std::function<void()>)}.
21 *
22 * <p>BooleanEvents can easily be composed for advanced functionality using
23 * {@link #operator&&}, {@link #operator||}, and {@link #operator!}.
24 *
25 * <p>To get a new BooleanEvent that triggers when this one changes see {@link
26 * #Falling()} and {@link #Rising()}.
27 */
29 public:
30 /**
31 * Creates a new event that is active when the condition is true.
32 *
33 * @param loop the loop that polls this event
34 * @param signal the digital signal represented by this object.
35 */
36 BooleanEvent(EventLoop* loop, std::function<bool()> signal);
37
38 /**
39 * Returns the state of this signal (high or low) as of the last loop poll.
40 *
41 * @return true for the high state, false for the low state. If the event was
42 * never polled, it returns the state at event construction.
43 */
44 bool GetAsBoolean() const;
45
46 operator std::function<bool()>(); // NOLINT
47
48 /**
49 * Bind an action to this event.
50 *
51 * @param action the action to run if this event is active.
52 */
53 void IfHigh(std::function<void()> action);
54
55 /**
56 * A method to "downcast" a BooleanEvent instance to a subclass (for example,
57 * to a command-based version of this class).
58 *
59 * @param ctor a method reference to the constructor of the subclass that
60 * accepts the loop as the first parameter and the condition/signal as the
61 * second.
62 * @return an instance of the subclass.
63 */
64 template <class T>
65 T CastTo(std::function<T(EventLoop*, std::function<bool()>)> ctor =
66 [](EventLoop* loop, std::function<bool()> condition) {
67 return T(loop, condition);
68 }) {
69 return ctor(m_loop, [state = m_state] { return *state; });
70 }
71
72 /**
73 * Creates a new event that is active when this event is inactive.
74 *
75 * @return the negated event
76 */
78
79 /**
80 * Composes this event with another event, returning a new event that is
81 * active when both events are active.
82 *
83 * <p>The events must use the same event loop. If the events use different
84 * event loops, the composed signal won't update until both loops are polled.
85 *
86 * @param rhs the event to compose with
87 * @return the event that is active when both events are active
88 */
89 BooleanEvent operator&&(std::function<bool()> rhs);
90
91 /**
92 * Composes this event with another event, returning a new event that is
93 * active when either event is active.
94 *
95 * <p>The events must use the same event loop. If the events use different
96 * event loops, the composed signal won't update until both loops are polled.
97 *
98 * @param rhs the event to compose with
99 * @return the event that is active when either event is active
100 */
101 BooleanEvent operator||(std::function<bool()> rhs);
102
103 /**
104 * Creates a new event that triggers when this one changes from false to true.
105 *
106 * @return the new event.
107 */
109
110 /**
111 * Creates a new event that triggers when this one changes from true to false.
112 *
113 * @return the event.
114 */
116
117 /**
118 * Creates a new debounced event from this event - it will become active when
119 * this event has been active for longer than the specified period.
120 *
121 * @param debounceTime The debounce period.
122 * @param type The debounce type.
123 * @return The debounced event.
124 */
125 BooleanEvent Debounce(wpi::units::second_t debounceTime,
128
129 private:
130 /// Poller loop.
131 EventLoop* m_loop;
132
133 std::function<bool()> m_signal;
134
135 /// The state of the condition in the current loop poll.
136 std::shared_ptr<bool> m_state;
137};
138} // namespace wpi
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
BooleanEvent Rising()
Creates a new event that triggers when this one changes from false to true.
BooleanEvent(EventLoop *loop, std::function< bool()> signal)
Creates a new event that is active when the condition is true.
BooleanEvent Falling()
Creates a new event that triggers when this one changes from true to false.
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.hpp:65
BooleanEvent Debounce(wpi::units::second_t debounceTime, wpi::math::Debouncer::DebounceType type=wpi::math::Debouncer::DebounceType::kRising)
Creates a new debounced event from this event - it will become active when this event has been active...
BooleanEvent operator&&(std::function< bool()> rhs)
Composes this event with another event, returning a new event that is active when both events are act...
bool GetAsBoolean() const
Returns the state of this signal (high or low) as of the last loop poll.
BooleanEvent operator!()
Creates a new event that is active when this event is inactive.
BooleanEvent operator||(std::function< bool()> rhs)
Composes this event with another event, returning a new event that is active when either event is act...
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.hpp:15
DebounceType
Type of debouncing to perform.
Definition Debouncer.hpp:21
@ kRising
Rising edge.
Definition Debouncer.hpp:23
Definition CvSource.hpp:15