WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 active high logic signals.
21 * Each object represents a digital signal to which callback actions can be
22 * bound using {@link #IfHigh(std::function<void()>)}.
23 *
24 * <p>BooleanEvents can easily be composed for advanced functionality using
25 * {@link #operator&&}, {@link #operator||}, and {@link #operator!}.
26 *
27 * <p>To get a new BooleanEvent that triggers when this one changes see {@link
28 * #Falling()} and {@link #Rising()}.
29 */
31 public:
32 /**
33 * Creates a new event that is active when the condition is true.
34 *
35 * @param loop the loop that polls this event
36 * @param signal the digital signal represented by this object.
37 */
38 BooleanEvent(EventLoop* loop, std::function<bool()> signal);
39
40 /**
41 * Returns the state of this signal (high or low) as of the last loop poll.
42 *
43 * @return true for the high state, false for the low state. If the event was
44 * never polled, it returns the state at event construction.
45 */
46 bool GetAsBoolean() const;
47
48 operator std::function<bool()>(); // NOLINT
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 /**
58 * A method to "downcast" a BooleanEvent instance to a subclass (for example,
59 * to a command-based version of this class).
60 *
61 * @param ctor a method reference to the constructor of the subclass that
62 * accepts the loop as the first parameter and the condition/signal as the
63 * second.
64 * @return an instance of the subclass.
65 */
66 template <class T>
67 T CastTo(std::function<T(EventLoop*, std::function<bool()>)> ctor =
68 [](EventLoop* loop, std::function<bool()> condition) {
69 return T(loop, condition);
70 }) {
71 return ctor(m_loop, [state = m_state] { return *state; });
72 }
73
74 /**
75 * Creates a new event that is active when this event is inactive.
76 *
77 * @return the negated event
78 */
80
81 /**
82 * Composes this event with another event, returning a new event that is
83 * active when both events are active.
84 *
85 * <p>The events must use the same event loop. If the events use different
86 * event loops, the composed signal won't update until both loops are polled.
87 *
88 * @param rhs the event to compose with
89 * @return the event that is active when both events are active
90 */
91 BooleanEvent operator&&(std::function<bool()> rhs);
92
93 /**
94 * Composes this event with another event, returning a new event that is
95 * active when either event is active.
96 *
97 * <p>The events must use the same event loop. If the events use different
98 * event loops, the composed signal won't update until both loops are polled.
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 * Creates a new event that triggers when this one changes from false to true.
107 *
108 * @return the new event.
109 */
111
112 /**
113 * Creates a new event that triggers when this one changes from true to false.
114 *
115 * @return the event.
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,
130
131 private:
132 EventLoop* m_loop;
133 std::function<bool()> m_signal;
134 std::shared_ptr<bool> m_state; // A programmer's worst nightmare.
135};
136} // 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 active high logic signals.
Definition BooleanEvent.h:30
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.
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 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:67
BooleanEvent Rising()
Creates a new event that triggers when this one changes from false to true.
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
@ kRising
Rising edge.
Definition Debouncer.h:25
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.h:15
Definition CAN.h:11