Skip to content

Commit eb2023e

Browse files
committed
Add Subsystem factories
1 parent 39188ac commit eb2023e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import edu.wpi.first.util.sendable.Sendable;
88
import edu.wpi.first.util.sendable.SendableBuilder;
99
import edu.wpi.first.util.sendable.SendableRegistry;
10+
import java.util.Set;
11+
import java.util.function.Supplier;
1012

1113
/**
1214
* A robot subsystem. Subsystems are the basic unit of robot organization in the Command-based
@@ -179,6 +181,18 @@ public Command runEnd(Runnable run, Runnable end) {
179181
return Commands.runEnd(run, end, this);
180182
}
181183

184+
/**
185+
* Constructs a {@link DeferredCommand} with the provided supplier. This subsystem is added as a
186+
* requirement.
187+
*
188+
* @param supplier the command supplier.
189+
* @return the command.
190+
* @see DeferredCommand
191+
*/
192+
public Command defer(Supplier<Command> supplier) {
193+
return Commands.defer(supplier, Set.of(this));
194+
}
195+
182196
/**
183197
* Associates a {@link Sendable} with this Subsystem. Also update the child's name.
184198
*

wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ CommandPtr Subsystem::RunEnd(std::function<void()> run,
8181
return cmd::RunEnd(std::move(run), std::move(end), {this});
8282
}
8383

84+
CommandPtr Subsystem::Defer(wpi::unique_function<Command*()> supplier) {
85+
return cmd::Defer(std::move(supplier), {this});
86+
}
87+
88+
CommandPtr Subsystem::Defer(wpi::unique_function<CommandPtr()> supplier) {
89+
return cmd::Defer(std::move(supplier), {this});
90+
}
91+
8492
void Subsystem::InitSendable(wpi::SendableBuilder& builder) {
8593
builder.SetSmartDashboardType("Subsystem");
8694
builder.AddBooleanProperty(

wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ class Subsystem : public wpi::Sendable, public wpi::SendableHelper<Subsystem> {
186186
[[nodiscard]]
187187
CommandPtr RunEnd(std::function<void()> run, std::function<void()> end);
188188

189+
/**
190+
* Constructs a DeferredCommand with the provided supplier. This subsystem is
191+
* added as a requirement.
192+
*
193+
* @param supplier the command supplier.
194+
* @return the command.
195+
*/
196+
CommandPtr Defer(wpi::unique_function<Command*()> supplier);
197+
198+
/**
199+
* Constructs a DeferredCommand with the provided supplier. This subsystem is
200+
* added as a requirement.
201+
*
202+
* @param supplier the command supplier.
203+
* @return the command.
204+
*/
205+
CommandPtr Defer(wpi::unique_function<CommandPtr()> supplier);
206+
189207
void InitSendable(wpi::SendableBuilder& builder) override;
190208

191209
protected:

0 commit comments

Comments
 (0)