[wpilib] Add EventLoop (#4104)

This is a generic expansion of the command-based Trigger framework.
This commit is contained in:
Starlight220
2022-06-09 08:16:51 +03:00
committed by GitHub
parent 16a4888c52
commit 45b7fc445b
27 changed files with 1265 additions and 428 deletions

View File

@@ -38,9 +38,10 @@ class CommandScheduler::Impl {
// commands. Also used as a list of currently-registered subsystems.
wpi::DenseMap<Subsystem*, std::unique_ptr<Command>> subsystems;
frc::EventLoop defaultButtonLoop;
// The set of currently-registered buttons that will be polled every
// iteration.
wpi::SmallVector<wpi::unique_function<void()>, 4> buttons;
frc::EventLoop* activeButtonLoop{&defaultButtonLoop};
bool disabled{false};
@@ -95,12 +96,20 @@ void CommandScheduler::SetPeriod(units::second_t period) {
m_watchdog.SetTimeout(period);
}
void CommandScheduler::AddButton(wpi::unique_function<void()> button) {
m_impl->buttons.emplace_back(std::move(button));
frc::EventLoop* CommandScheduler::GetActiveButtonLoop() const {
return m_impl->activeButtonLoop;
}
void CommandScheduler::SetActiveButtonLoop(frc::EventLoop* loop) {
m_impl->activeButtonLoop = loop;
}
frc::EventLoop* CommandScheduler::GetDefaultButtonLoop() const {
return &(m_impl->defaultButtonLoop);
}
void CommandScheduler::ClearButtons() {
m_impl->buttons.clear();
m_impl->activeButtonLoop->Clear();
}
void CommandScheduler::Schedule(bool interruptible, Command* command) {
@@ -200,10 +209,11 @@ void CommandScheduler::Run() {
m_watchdog.AddEpoch("Subsystem Periodic()");
}
// Cache the active instance to avoid concurrency problems if SetActiveLoop()
// is called from inside the button bindings.
frc::EventLoop* loopCache = m_impl->activeButtonLoop;
// Poll buttons for new commands to add.
for (auto&& button : m_impl->buttons) {
button();
}
loopCache->Poll();
m_watchdog.AddEpoch("buttons.Run()");
m_impl->inRunLoop = true;