[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:
Gold856
2023-10-05 00:22:57 -04:00
committed by GitHub
parent 6576d9b474
commit 58141d6eb5
5 changed files with 890 additions and 58 deletions

View File

@@ -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); });
}