mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +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); });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user