mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[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:
@@ -7,6 +7,7 @@ package edu.wpi.first.wpilibj.event;
|
||||
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
|
||||
|
||||
import edu.wpi.first.math.filter.Debouncer;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
@@ -28,6 +29,9 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
/** Condition. */
|
||||
private final BooleanSupplier m_signal;
|
||||
|
||||
/** The state of the condition in the current loop poll. Nightmare to manage. */
|
||||
private final AtomicBoolean m_state = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Creates a new event with the given signal determining whether it is active.
|
||||
*
|
||||
@@ -37,16 +41,19 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
public BooleanEvent(EventLoop loop, BooleanSupplier signal) {
|
||||
m_loop = requireNonNullParam(loop, "loop", "BooleanEvent");
|
||||
m_signal = requireNonNullParam(signal, "signal", "BooleanEvent");
|
||||
m_state.set(m_signal.getAsBoolean());
|
||||
m_loop.bind(() -> m_state.set(m_signal.getAsBoolean()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the state of this signal (high or low).
|
||||
* Check the state of this signal (high or low) as of the last loop poll.
|
||||
*
|
||||
* @return true for the high state, false for the low state.
|
||||
* @return true for the high state, false for the low state. If the event was never polled, it
|
||||
* returns the state at event construction.
|
||||
*/
|
||||
@Override
|
||||
public final boolean getAsBoolean() {
|
||||
return m_signal.getAsBoolean();
|
||||
return m_state.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +64,7 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
public final void ifHigh(Runnable action) {
|
||||
m_loop.bind(
|
||||
() -> {
|
||||
if (m_signal.getAsBoolean()) {
|
||||
if (m_state.get()) {
|
||||
action.run();
|
||||
}
|
||||
});
|
||||
@@ -72,11 +79,11 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
return new BooleanEvent(
|
||||
m_loop,
|
||||
new BooleanSupplier() {
|
||||
private boolean m_previous = m_signal.getAsBoolean();
|
||||
private boolean m_previous = m_state.get();
|
||||
|
||||
@Override
|
||||
public boolean getAsBoolean() {
|
||||
boolean present = m_signal.getAsBoolean();
|
||||
boolean present = m_state.get();
|
||||
boolean ret = !m_previous && present;
|
||||
m_previous = present;
|
||||
return ret;
|
||||
@@ -93,11 +100,11 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
return new BooleanEvent(
|
||||
m_loop,
|
||||
new BooleanSupplier() {
|
||||
private boolean m_previous = m_signal.getAsBoolean();
|
||||
private boolean m_previous = m_state.get();
|
||||
|
||||
@Override
|
||||
public boolean getAsBoolean() {
|
||||
boolean present = m_signal.getAsBoolean();
|
||||
boolean present = m_state.get();
|
||||
boolean ret = m_previous && !present;
|
||||
m_previous = present;
|
||||
return ret;
|
||||
@@ -132,7 +139,7 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
|
||||
@Override
|
||||
public boolean getAsBoolean() {
|
||||
return m_debouncer.calculate(m_signal.getAsBoolean());
|
||||
return m_debouncer.calculate(m_state.get());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -144,35 +151,37 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
* @return the negated event
|
||||
*/
|
||||
public BooleanEvent negate() {
|
||||
return new BooleanEvent(m_loop, () -> !m_signal.getAsBoolean());
|
||||
return new BooleanEvent(m_loop, () -> !m_state.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Composes this event with another event, returning a new signal that is in the high state when
|
||||
* both signals are in the high state.
|
||||
*
|
||||
* <p>The new event will use this event's polling loop.
|
||||
* <p>The events must use the same event loop. If the events use different event loops, the
|
||||
* composed signal won't update until both loops are polled.
|
||||
*
|
||||
* @param other the event to compose with
|
||||
* @return the event that is active when both events are active
|
||||
*/
|
||||
public BooleanEvent and(BooleanSupplier other) {
|
||||
requireNonNullParam(other, "other", "and");
|
||||
return new BooleanEvent(m_loop, () -> m_signal.getAsBoolean() && other.getAsBoolean());
|
||||
return new BooleanEvent(m_loop, () -> m_state.get() && other.getAsBoolean());
|
||||
}
|
||||
|
||||
/**
|
||||
* Composes this event with another event, returning a new signal that is high when either signal
|
||||
* is high.
|
||||
*
|
||||
* <p>The new event will use this event's polling loop.
|
||||
* <p>The events must use the same event loop. If the events use different event loops, the
|
||||
* composed signal won't update until both loops are polled.
|
||||
*
|
||||
* @param other the event to compose with
|
||||
* @return a signal that is high when either signal is high.
|
||||
*/
|
||||
public BooleanEvent or(BooleanSupplier other) {
|
||||
requireNonNullParam(other, "other", "or");
|
||||
return new BooleanEvent(m_loop, () -> m_signal.getAsBoolean() || other.getAsBoolean());
|
||||
return new BooleanEvent(m_loop, () -> m_state.get() || other.getAsBoolean());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,6 +194,6 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
* @return an instance of the subclass.
|
||||
*/
|
||||
public <T extends BooleanSupplier> T castTo(BiFunction<EventLoop, BooleanSupplier, T> ctor) {
|
||||
return ctor.apply(m_loop, m_signal);
|
||||
return ctor.apply(m_loop, m_state::get);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user