mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
[wpilib] Make BooleanEvent more consistent (#5436)
Co-authored-by: Tyler Veness <calcmogul@gmail.com> Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com>
This commit is contained in:
@@ -7,56 +7,61 @@
|
||||
using namespace frc;
|
||||
|
||||
BooleanEvent::BooleanEvent(EventLoop* loop, std::function<bool()> condition)
|
||||
: m_loop(loop), m_condition(std::move(condition)) {}
|
||||
: m_loop(loop), m_condition(std::move(condition)) {
|
||||
m_state = std::make_shared<bool>(m_condition());
|
||||
m_loop->Bind(
|
||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
[condition = m_condition, state = m_state] { *state = condition(); });
|
||||
}
|
||||
|
||||
BooleanEvent::operator std::function<bool()>() {
|
||||
return m_condition;
|
||||
return [state = m_state] { return *state; };
|
||||
}
|
||||
|
||||
bool BooleanEvent::GetAsBoolean() const {
|
||||
return m_condition();
|
||||
return *m_state;
|
||||
}
|
||||
|
||||
void BooleanEvent::IfHigh(std::function<void()> action) {
|
||||
m_loop->Bind([condition = m_condition, action = std::move(action)] {
|
||||
if (condition()) {
|
||||
m_loop->Bind([state = m_state, action = std::move(action)] {
|
||||
if (*state) {
|
||||
action();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator!() {
|
||||
return BooleanEvent(this->m_loop, [lhs = m_condition] { return !lhs(); });
|
||||
return BooleanEvent(this->m_loop, [state = m_state] { return !*state; });
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator&&(std::function<bool()> rhs) {
|
||||
return BooleanEvent(this->m_loop,
|
||||
[lhs = m_condition, rhs] { return lhs() && rhs(); });
|
||||
[state = m_state, rhs] { return *state && rhs(); });
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator||(std::function<bool()> rhs) {
|
||||
return BooleanEvent(this->m_loop,
|
||||
[lhs = m_condition, rhs] { return lhs() || rhs(); });
|
||||
[state = m_state, rhs] { return *state || rhs(); });
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::Rising() {
|
||||
return BooleanEvent(
|
||||
this->m_loop, [lhs = m_condition, m_previous = m_condition()]() mutable {
|
||||
bool present = lhs();
|
||||
bool past = m_previous;
|
||||
m_previous = present;
|
||||
return !past && present;
|
||||
});
|
||||
return BooleanEvent(this->m_loop,
|
||||
[state = m_state, m_previous = *m_state]() mutable {
|
||||
bool present = *state;
|
||||
bool past = m_previous;
|
||||
m_previous = present;
|
||||
return !past && present;
|
||||
});
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::Falling() {
|
||||
return BooleanEvent(
|
||||
this->m_loop, [lhs = m_condition, m_previous = m_condition()]() mutable {
|
||||
bool present = lhs();
|
||||
bool past = m_previous;
|
||||
m_previous = present;
|
||||
return past && !present;
|
||||
});
|
||||
return BooleanEvent(this->m_loop,
|
||||
[state = m_state, m_previous = *m_state]() mutable {
|
||||
bool present = *state;
|
||||
bool past = m_previous;
|
||||
m_previous = present;
|
||||
return past && !present;
|
||||
});
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::Debounce(units::second_t debounceTime,
|
||||
@@ -64,5 +69,5 @@ BooleanEvent BooleanEvent::Debounce(units::second_t debounceTime,
|
||||
return BooleanEvent(
|
||||
this->m_loop,
|
||||
[debouncer = frc::Debouncer(debounceTime, type),
|
||||
lhs = m_condition]() mutable { return debouncer.Calculate(lhs()); });
|
||||
state = m_state]() mutable { return debouncer.Calculate(*state); });
|
||||
}
|
||||
|
||||
@@ -40,9 +40,10 @@ class BooleanEvent {
|
||||
BooleanEvent(EventLoop* loop, std::function<bool()> condition);
|
||||
|
||||
/**
|
||||
* Check whether this event is active or not.
|
||||
* Check whether this event is active or not as of the last loop poll.
|
||||
*
|
||||
* @return true if active.
|
||||
* @return true if active, false if not active. If the event was never polled,
|
||||
* it returns the state at event construction.
|
||||
*/
|
||||
bool GetAsBoolean() const;
|
||||
|
||||
@@ -69,7 +70,7 @@ class BooleanEvent {
|
||||
[](EventLoop* loop, std::function<bool()> condition) {
|
||||
return T(loop, condition);
|
||||
}) {
|
||||
return ctor(m_loop, m_condition);
|
||||
return ctor(m_loop, [state = m_state] { return *state; });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +85,8 @@ class BooleanEvent {
|
||||
* Composes this event with another event, returning a new event that is
|
||||
* active when both events are active.
|
||||
*
|
||||
* <p>The new event will use this event's polling loop.
|
||||
* <p>The events must use the same event loop. If the events use different
|
||||
* event loops, the composed signal won't update until both loops are polled.
|
||||
*
|
||||
* @param rhs the event to compose with
|
||||
* @return the event that is active when both events are active
|
||||
@@ -95,7 +97,8 @@ class BooleanEvent {
|
||||
* Composes this event with another event, returning a new event that is
|
||||
* active when either event is active.
|
||||
*
|
||||
* <p>The new event will use this event's polling loop.
|
||||
* <p>The events must use the same event loop. If the events use different
|
||||
* event loops, the composed signal won't update until both loops are polled.
|
||||
*
|
||||
* @param rhs the event to compose with
|
||||
* @return the event that is active when either event is active
|
||||
@@ -131,5 +134,6 @@ class BooleanEvent {
|
||||
private:
|
||||
EventLoop* m_loop;
|
||||
std::function<bool()> m_condition;
|
||||
std::shared_ptr<bool> m_state; // A programmer's worst nightmare.
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user