diff --git a/wpilibc/src/main/native/cpp/commands/InstantCommand.cpp b/wpilibc/src/main/native/cpp/commands/InstantCommand.cpp index cd07315998..88ebfde9cf 100644 --- a/wpilibc/src/main/native/cpp/commands/InstantCommand.cpp +++ b/wpilibc/src/main/native/cpp/commands/InstantCommand.cpp @@ -16,4 +16,30 @@ InstantCommand::InstantCommand(Subsystem& subsystem) : Command(subsystem) {} InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem) : Command(name, subsystem) {} +InstantCommand::InstantCommand(std::function func) : m_func(func) {} + +InstantCommand::InstantCommand(Subsystem& subsystem, std::function func) + : InstantCommand(subsystem) { + m_func = func; +} + +InstantCommand::InstantCommand(const wpi::Twine& name, + std::function func) + : InstantCommand(name) { + m_func = func; +} + +InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem, + std::function func) + : InstantCommand(name, subsystem) { + m_func = func; +} + +void InstantCommand::_Initialize() { + Command::_Initialize(); + if (m_func) { + m_func(); + } +} + bool InstantCommand::IsFinished() { return true; } diff --git a/wpilibc/src/main/native/include/frc/commands/InstantCommand.h b/wpilibc/src/main/native/include/frc/commands/InstantCommand.h index fc8f0581a9..e412e092bb 100644 --- a/wpilibc/src/main/native/include/frc/commands/InstantCommand.h +++ b/wpilibc/src/main/native/include/frc/commands/InstantCommand.h @@ -7,9 +7,12 @@ #pragma once +#include + #include #include "frc/commands/Command.h" +#include "frc/commands/Subsystem.h" namespace frc { @@ -42,10 +45,45 @@ class InstantCommand : public Command { */ InstantCommand(const wpi::Twine& name, Subsystem& subsystem); + /** + * Create a command that calls the given function when run. + * + * @param func The function to run when Initialize() is run. + */ + explicit InstantCommand(std::function func); + + /** + * Create a command that calls the given function when run. + * + * @param subsystem The subsystems that this command runs on. + * @param func The function to run when Initialize() is run. + */ + InstantCommand(Subsystem& subsystem, std::function func); + + /** + * Create a command that calls the given function when run. + * + * @param name The name of the command. + * @param func The function to run when Initialize() is run. + */ + InstantCommand(const wpi::Twine& name, std::function func); + + /** + * Create a command that calls the given function when run. + * + * @param name The name of the command. + * @param subsystem The subsystems that this command runs on. + * @param func The function to run when Initialize() is run. + */ + InstantCommand(const wpi::Twine& name, Subsystem& subsystem, + std::function func); + InstantCommand() = default; virtual ~InstantCommand() = default; protected: + std::function m_func = nullptr; + void _Initialize() override; bool IsFinished() override; }; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/command/InstantCommand.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/command/InstantCommand.java index 98bd9e2e70..1f3c5de7a5 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/command/InstantCommand.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/command/InstantCommand.java @@ -14,11 +14,14 @@ package edu.wpi.first.wpilibj.command; * {@link Command isFinished}. */ public class InstantCommand extends Command { + private Runnable m_func; + public InstantCommand() { } /** * Creates a new {@link InstantCommand InstantCommand} with the given name. + * * @param name the name for this command */ public InstantCommand(String name) { @@ -27,6 +30,7 @@ public class InstantCommand extends Command { /** * Creates a new {@link InstantCommand InstantCommand} with the given requirement. + * * @param subsystem the subsystem this command requires */ public InstantCommand(Subsystem subsystem) { @@ -35,6 +39,7 @@ public class InstantCommand extends Command { /** * Creates a new {@link InstantCommand InstantCommand} with the given name and requirement. + * * @param name the name for this command * @param subsystem the subsystem this command requires */ @@ -42,8 +47,64 @@ public class InstantCommand extends Command { super(name, subsystem); } + /** + * Creates a new {@link InstantCommand InstantCommand}. + * + * @param func the function to run when {@link Command#initialize() initialize()} is run + */ + public InstantCommand(Runnable func) { + m_func = func; + } + + /** + * Creates a new {@link InstantCommand InstantCommand}. + * + * @param name the name for this command + * @param func the function to run when {@link Command#initialize() initialize()} is run + */ + public InstantCommand(String name, Runnable func) { + super(name); + m_func = func; + } + + /** + * Creates a new {@link InstantCommand InstantCommand}. + * + * @param requirement the subsystem this command requires + * @param func the function to run when {@link Command#initialize() initialize()} is run + */ + public InstantCommand(Subsystem requirement, Runnable func) { + super(requirement); + m_func = func; + } + + /** + * Creates a new {@link InstantCommand InstantCommand}. + * + * @param name the name for this command + * @param requirement the subsystem this command requires + * @param func the function to run when {@link Command#initialize() initialize()} is run + */ + public InstantCommand(String name, Subsystem requirement, Runnable func) { + super(name, requirement); + m_func = func; + } + @Override protected boolean isFinished() { return true; } + + /** + * Trigger the stored function. + * + *

Called just before this Command runs the first time. + */ + @Override + protected void _initialize() { + super._initialize(); + if (m_func != null) { + m_func.run(); + } + } }