From 3a4f77bba37b06b5f8b561ff655ef537d467a9ad Mon Sep 17 00:00:00 2001 From: Tim Winters Date: Wed, 1 May 2024 10:00:12 -0400 Subject: [PATCH] Add startRun command factory --- commands2/cmd.py | 16 ++++++++++++++++ commands2/subsystem.py | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/commands2/cmd.py b/commands2/cmd.py index 7e9f535a..42b9043e 100644 --- a/commands2/cmd.py +++ b/commands2/cmd.py @@ -95,6 +95,22 @@ def runEnd( ) +def startRun( + start: Callable[[], Any], run: Callable[[], Any], *requirements: Subsystem +) -> Command: + """ + Constructs a command that runs an action once and another action every iteration until interrupted. + + :param start: the action to run on start + :param run: the action to run every iteration + :param requirements: subsystems the action requires + :returns: the command + """ + return FunctionalCommand( + start, run, lambda interrupt: None, 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 3cb2792b..1abdf23b 100644 --- a/commands2/subsystem.py +++ b/commands2/subsystem.py @@ -156,6 +156,18 @@ def runEnd(self, run: Callable[[], None], end: Callable[[], None]) -> Command: return runEnd(run, end, self) + def startRun(self, start: Callable[[], None], run: Callable[[], None]) -> Command: + """ + Constructs a command that runs an action once and another action every iteration until interrupted. Requires this subsystem. + + :param start: the action to run on start + :param run: the action to run every iteration + :returns: the command + """ + from .cmd import startRun + + return startRun(start, run, self) + # # From SubsystemBase #