diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java index a53d792d0e4..3c98de06ba9 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java @@ -12,6 +12,9 @@ import edu.wpi.first.util.sendable.Sendable; import edu.wpi.first.util.sendable.SendableBuilder; import edu.wpi.first.util.sendable.SendableRegistry; +import edu.wpi.first.wpilibj.event.EventLoop; +import edu.wpi.first.wpilibj2.command.button.Trigger; + import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -497,6 +500,65 @@ public WrapperCommand finallyDo(Runnable end) { return finallyDo(interrupted -> end.run()); } + /** + * Returns a {@link Trigger} that follows if this command is scheduled. + * + * @param eventLoop the event loop to use + * @return the trigger + */ + public Trigger scheduled(EventLoop eventLoop) { + return new Trigger(eventLoop, this::isScheduled); + } + + /** + * Returns a {@link Trigger} that follows if this command is scheduled. + * + * @return the trigger + */ + public Trigger scheduled() { + return new Trigger(this::isScheduled); + } + + /** + * Returns a {@link Trigger} that follows if this command is done. + * + *

This trigger emits 1 rising edge and 1 falling edge when the command + * finishes or is interupted/canceled. + * + * @param eventLoop the event loop to use + * @return the trigger + */ + public Trigger done(EventLoop eventLoop) { + BooleanSupplier isDone = + new BooleanSupplier() { + boolean wasJustScheduled = false; + + @Override + public boolean getAsBoolean() { + if (isScheduled()) { + wasJustScheduled = true; + } else if (wasJustScheduled) { + wasJustScheduled = false; + return true; + } + return false; + } + }; + return new Trigger(eventLoop, isDone); + } + + /** + * Returns a {@link Trigger} that follows if this command is done. + * + *

This trigger emits 1 rising edge and 1 falling edge when the command + * finishes or is interupted/canceled. + * + * @return the trigger + */ + public Trigger done() { + return done(CommandScheduler.getInstance().getActiveButtonLoop()); + } + /** * Decorates this command with a lambda to call on interrupt, following the command's inherent * {@link #end(boolean)} method.