mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpilib] Add EventLoop (#4104)
This is a generic expansion of the command-based Trigger framework.
This commit is contained in:
64
wpilibc/src/main/native/cpp/event/BooleanEvent.cpp
Normal file
64
wpilibc/src/main/native/cpp/event/BooleanEvent.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/event/BooleanEvent.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
BooleanEvent::BooleanEvent(EventLoop* loop, std::function<bool()> condition)
|
||||
: m_loop(loop), m_condition(std::move(condition)) {}
|
||||
|
||||
BooleanEvent::operator std::function<bool()>() {
|
||||
return m_condition;
|
||||
}
|
||||
|
||||
bool BooleanEvent::GetAsBoolean() const {
|
||||
return m_condition();
|
||||
}
|
||||
|
||||
void BooleanEvent::IfHigh(wpi::unique_function<void()> action) {
|
||||
m_loop->Bind(m_condition, std::move(action));
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator!() {
|
||||
return BooleanEvent(this->m_loop, [lhs = m_condition] { return !lhs(); });
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator&&(std::function<bool()> rhs) {
|
||||
return BooleanEvent(this->m_loop,
|
||||
[lhs = m_condition, rhs] { return lhs() && rhs(); });
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::operator||(std::function<bool()> rhs) {
|
||||
return BooleanEvent(this->m_loop,
|
||||
[lhs = m_condition, rhs] { return lhs() || 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;
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
BooleanEvent BooleanEvent::Debounce(units::second_t debounceTime,
|
||||
frc::Debouncer::DebounceType type) {
|
||||
return BooleanEvent(
|
||||
this->m_loop,
|
||||
[debouncer = frc::Debouncer(debounceTime, type),
|
||||
lhs = m_condition]() mutable { return debouncer.Calculate(lhs()); });
|
||||
}
|
||||
30
wpilibc/src/main/native/cpp/event/EventLoop.cpp
Normal file
30
wpilibc/src/main/native/cpp/event/EventLoop.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/event/EventLoop.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
EventLoop::EventLoop() {}
|
||||
|
||||
void EventLoop::Binding::Poll() {
|
||||
if (condition()) {
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
void EventLoop::Bind(std::function<bool()> condition,
|
||||
wpi::unique_function<void()> action) {
|
||||
m_bindings.emplace_back(Binding{condition, std::move(action)});
|
||||
}
|
||||
|
||||
void EventLoop::Poll() {
|
||||
for (Binding& binding : m_bindings) {
|
||||
binding.Poll();
|
||||
}
|
||||
}
|
||||
|
||||
void EventLoop::Clear() {
|
||||
m_bindings.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user