mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
`Trigger.getAsBoolean()` behavior has been changed from passing through the underlying boolean supplier to returning the latest cached signal as determined by the most recent call to `poll()`. This allows rising and falling edge triggers to have a consistent return value over an entire polling cycle, rather than only being high for the _first_ check in a cycle. Closes #8309
65 lines
1.7 KiB
Java
65 lines
1.7 KiB
Java
// 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.
|
|
|
|
package org.wpilib.event;
|
|
|
|
import java.util.Collection;
|
|
import java.util.ConcurrentModificationException;
|
|
import java.util.LinkedHashSet;
|
|
|
|
/**
|
|
* A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
|
|
*/
|
|
public final class EventLoop {
|
|
private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
|
|
private boolean m_running;
|
|
|
|
/** Default constructor. */
|
|
public EventLoop() {}
|
|
|
|
/**
|
|
* Bind a new action to run when the loop is polled.
|
|
*
|
|
* @param action the action to run.
|
|
*/
|
|
public void bind(Runnable action) {
|
|
if (m_running) {
|
|
throw new ConcurrentModificationException("Cannot bind EventLoop while it is running");
|
|
}
|
|
m_bindings.add(action);
|
|
}
|
|
|
|
/**
|
|
* Unbind an action from running when the loop is polled. Has no effect if the given action is not
|
|
* already bound.
|
|
*
|
|
* @param action the action to unbind.
|
|
*/
|
|
public void unbind(Runnable action) {
|
|
if (m_running) {
|
|
throw new ConcurrentModificationException("Cannot unbind EventLoop while it is running");
|
|
}
|
|
m_bindings.remove(action);
|
|
}
|
|
|
|
/** Poll all bindings. */
|
|
@SuppressWarnings("PMD.UnusedAssignment")
|
|
public void poll() {
|
|
try {
|
|
m_running = true;
|
|
m_bindings.forEach(Runnable::run);
|
|
} finally {
|
|
m_running = false;
|
|
}
|
|
}
|
|
|
|
/** Clear all bindings. */
|
|
public void clear() {
|
|
if (m_running) {
|
|
throw new ConcurrentModificationException("Cannot clear EventLoop while it is running");
|
|
}
|
|
m_bindings.clear();
|
|
}
|
|
}
|