Skip to content

Commit

Permalink
add scheduled and done trigger factories to Commands java
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Sep 19, 2024
1 parent a453744 commit 20fe049
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
* <p>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.
*
* <p>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.
Expand Down

0 comments on commit 20fe049

Please sign in to comment.