mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
[commands] Revert to original Trigger implementation (#4673)
Trigger was refactored to use BooleanEvent when it was introduced in #4104. This reverts to the original implementation until edge-based BooleanEvents can be fixed.
This commit is contained in:
@@ -54,7 +54,12 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
* @param action the action to run if this event is active.
|
||||
*/
|
||||
public final void ifHigh(Runnable action) {
|
||||
m_loop.bind(m_signal, action);
|
||||
m_loop.bind(
|
||||
() -> {
|
||||
if (m_signal.getAsBoolean()) {
|
||||
action.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,45 +6,27 @@ package edu.wpi.first.wpilibj.event;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/** The loop polling {@link BooleanEvent} objects and executing the actions bound to them. */
|
||||
public final class EventLoop {
|
||||
private final Collection<Binding> m_bindings = new LinkedHashSet<>();
|
||||
private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
|
||||
|
||||
/**
|
||||
* Bind a new action to run whenever the condition is true.
|
||||
*
|
||||
* @param condition the condition to listen to.
|
||||
* @param action the action to run.
|
||||
*/
|
||||
public void bind(BooleanSupplier condition, Runnable action) {
|
||||
m_bindings.add(new Binding(condition, action));
|
||||
public void bind(Runnable action) {
|
||||
m_bindings.add(action);
|
||||
}
|
||||
|
||||
/** Poll all bindings. */
|
||||
public void poll() {
|
||||
m_bindings.forEach(Binding::poll);
|
||||
m_bindings.forEach(Runnable::run);
|
||||
}
|
||||
|
||||
/** Clear all bindings. */
|
||||
public void clear() {
|
||||
m_bindings.clear();
|
||||
}
|
||||
|
||||
private static class Binding {
|
||||
private final BooleanSupplier m_condition;
|
||||
private final Runnable m_action;
|
||||
|
||||
private Binding(BooleanSupplier condition, Runnable action) {
|
||||
this.m_condition = condition;
|
||||
this.m_action = action;
|
||||
}
|
||||
|
||||
void poll() {
|
||||
if (m_condition.getAsBoolean()) {
|
||||
m_action.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ class EventLoopTest {
|
||||
var counterTrue = new AtomicInteger(0);
|
||||
var counterFalse = new AtomicInteger(0);
|
||||
var loop = new EventLoop();
|
||||
loop.bind(() -> true, counterTrue::incrementAndGet);
|
||||
loop.bind(() -> false, counterFalse::incrementAndGet);
|
||||
new BooleanEvent(loop, () -> true).ifHigh(counterTrue::incrementAndGet);
|
||||
new BooleanEvent(loop, () -> false).ifHigh(counterFalse::incrementAndGet);
|
||||
|
||||
assertEquals(0, counterTrue.get());
|
||||
assertEquals(0, counterFalse.get());
|
||||
@@ -40,7 +40,7 @@ class EventLoopTest {
|
||||
var loop = new EventLoop();
|
||||
|
||||
// first ensure binding works
|
||||
loop.bind(condition::get, counter::incrementAndGet);
|
||||
new BooleanEvent(loop, condition::get).ifHigh(counter::incrementAndGet);
|
||||
|
||||
condition.set(false);
|
||||
loop.poll();
|
||||
|
||||
Reference in New Issue
Block a user