diff --git a/commands2/cmd.py b/commands2/cmd.py index 42b9043..0d7d894 100644 --- a/commands2/cmd.py +++ b/commands2/cmd.py @@ -111,6 +111,26 @@ def startRun( ) +def startRunEnd( + start: Callable[[], Any], + run: Callable[[], Any], + end: Callable[[], Any], + *requirements: Subsystem +) -> Command: + """ + Constructs a command that runs an action once, and then runs an action every iteration until interrupted, + and then runs a third action. + + :param start the action to run on start + :param run the action to run every iteration + :param end the action to run on interrupt + :returns: the command + """ + return FunctionalCommand( + start, run, lambda interrupted: end(), lambda: False, *requirements + ) + + def print_(message: str) -> Command: """ Constructs a command that prints a message and finishes. diff --git a/commands2/subsystem.py b/commands2/subsystem.py index d0b73d0..10b86a1 100644 --- a/commands2/subsystem.py +++ b/commands2/subsystem.py @@ -168,6 +168,27 @@ def startRun(self, start: Callable[[], None], run: Callable[[], None]) -> Comman return startRun(start, run, self) + def startRunEnd( + self, + start: Callable[[], None], + run: Callable[[], None], + end: Callable[[], None], + ) -> Command: + """ + Constructs a command that runs an action once, and then runs an action + every iteration until interrupted, and then runs a third action. + every iteration until interrupted, and then runs a third action. Requires + this subsystem. + + :param start the action to run on start + :param run the action to run every iteration + :param end the action to run on interrupt + :returns: the command + """ + from .cmd import startRunEnd + + return startRunEnd(start, run, end, self) + def idle(self) -> Command: """ Constructs a command that does nothing until interrupted. Requires this subsystem.