mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Split the two command implementations into separate libraries (#2012)
This will allow us at the user code side to determine to include old commands, new commands or both.
This commit is contained in:
committed by
Peter Johnson
parent
2ad15cae19
commit
509819d83f
@@ -0,0 +1,73 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/Trigger.h"
|
||||
#include "frc/commands/Command.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* This class provides an easy way to link commands to OI inputs.
|
||||
*
|
||||
* It is very easy to link a button to a command. For instance, you could link
|
||||
* the trigger button of a joystick to a "score" command.
|
||||
*
|
||||
* This class represents a subclass of Trigger that is specifically aimed at
|
||||
* buttons on an operator interface as a common use case of the more generalized
|
||||
* Trigger objects. This is a simple wrapper around Trigger with the method
|
||||
* names renamed to fit the Button object use.
|
||||
*/
|
||||
class Button : public Trigger {
|
||||
public:
|
||||
Button() = default;
|
||||
Button(Button&&) = default;
|
||||
Button& operator=(Button&&) = default;
|
||||
|
||||
/**
|
||||
* Specifies the command to run when a button is first pressed.
|
||||
*
|
||||
* @param command The pointer to the command to run
|
||||
*/
|
||||
virtual void WhenPressed(Command* command);
|
||||
|
||||
/**
|
||||
* Specifies the command to be scheduled while the button is pressed.
|
||||
*
|
||||
* The command will be scheduled repeatedly while the button is pressed and
|
||||
* will be canceled when the button is released.
|
||||
*
|
||||
* @param command The pointer to the command to run
|
||||
*/
|
||||
virtual void WhileHeld(Command* command);
|
||||
|
||||
/**
|
||||
* Specifies the command to run when the button is released.
|
||||
*
|
||||
* The command will be scheduled a single time.
|
||||
*
|
||||
* @param command The pointer to the command to run
|
||||
*/
|
||||
virtual void WhenReleased(Command* command);
|
||||
|
||||
/**
|
||||
* Cancels the specificed command when the button is pressed.
|
||||
*
|
||||
* @param command The command to be canceled
|
||||
*/
|
||||
virtual void CancelWhenPressed(Command* command);
|
||||
|
||||
/**
|
||||
* Toggle the specified command when the button is pressed.
|
||||
*
|
||||
* @param command The command to be toggled
|
||||
*/
|
||||
virtual void ToggleWhenPressed(Command* command);
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,32 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class ButtonScheduler {
|
||||
public:
|
||||
ButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~ButtonScheduler() = default;
|
||||
|
||||
ButtonScheduler(ButtonScheduler&&) = default;
|
||||
ButtonScheduler& operator=(ButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute() = 0;
|
||||
void Start();
|
||||
|
||||
protected:
|
||||
bool m_pressedLast;
|
||||
Trigger* m_button;
|
||||
Command* m_command;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/ButtonScheduler.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class CancelButtonScheduler : public ButtonScheduler {
|
||||
public:
|
||||
CancelButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~CancelButtonScheduler() = default;
|
||||
|
||||
CancelButtonScheduler(CancelButtonScheduler&&) = default;
|
||||
CancelButtonScheduler& operator=(CancelButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/ButtonScheduler.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class HeldButtonScheduler : public ButtonScheduler {
|
||||
public:
|
||||
HeldButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~HeldButtonScheduler() = default;
|
||||
|
||||
HeldButtonScheduler(HeldButtonScheduler&&) = default;
|
||||
HeldButtonScheduler& operator=(HeldButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/Button.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class InternalButton : public Button {
|
||||
public:
|
||||
InternalButton() = default;
|
||||
explicit InternalButton(bool inverted);
|
||||
virtual ~InternalButton() = default;
|
||||
|
||||
InternalButton(InternalButton&&) = default;
|
||||
InternalButton& operator=(InternalButton&&) = default;
|
||||
|
||||
void SetInverted(bool inverted);
|
||||
void SetPressed(bool pressed);
|
||||
|
||||
virtual bool Get();
|
||||
|
||||
private:
|
||||
bool m_pressed = false;
|
||||
bool m_inverted = false;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,30 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/GenericHID.h"
|
||||
#include "frc/buttons/Button.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class JoystickButton : public Button {
|
||||
public:
|
||||
JoystickButton(GenericHID* joystick, int buttonNumber);
|
||||
virtual ~JoystickButton() = default;
|
||||
|
||||
JoystickButton(JoystickButton&&) = default;
|
||||
JoystickButton& operator=(JoystickButton&&) = default;
|
||||
|
||||
virtual bool Get();
|
||||
|
||||
private:
|
||||
GenericHID* m_joystick;
|
||||
int m_buttonNumber;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <networktables/NetworkTableEntry.h>
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "frc/buttons/Button.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class NetworkButton : public Button {
|
||||
public:
|
||||
NetworkButton(const wpi::Twine& tableName, const wpi::Twine& field);
|
||||
NetworkButton(std::shared_ptr<nt::NetworkTable> table,
|
||||
const wpi::Twine& field);
|
||||
virtual ~NetworkButton() = default;
|
||||
|
||||
NetworkButton(NetworkButton&&) = default;
|
||||
NetworkButton& operator=(NetworkButton&&) = default;
|
||||
|
||||
virtual bool Get();
|
||||
|
||||
private:
|
||||
nt::NetworkTableEntry m_entry;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/GenericHID.h"
|
||||
#include "frc/buttons/Button.h"
|
||||
|
||||
namespace frc {
|
||||
class POVButton : public Button {
|
||||
public:
|
||||
/**
|
||||
* Creates a POV button for triggering commands.
|
||||
*
|
||||
* @param joystick The GenericHID object that has the POV
|
||||
* @param angle The desired angle in degrees (e.g. 90, 270)
|
||||
* @param povNumber The POV number (@see GenericHID#GetPOV)
|
||||
*/
|
||||
POVButton(GenericHID& joystick, int angle, int povNumber = 0);
|
||||
virtual ~POVButton() = default;
|
||||
|
||||
POVButton(POVButton&&) = default;
|
||||
POVButton& operator=(POVButton&&) = default;
|
||||
|
||||
bool Get() override;
|
||||
|
||||
private:
|
||||
GenericHID* m_joystick;
|
||||
int m_angle;
|
||||
int m_povNumber;
|
||||
};
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/ButtonScheduler.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class PressedButtonScheduler : public ButtonScheduler {
|
||||
public:
|
||||
PressedButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~PressedButtonScheduler() = default;
|
||||
|
||||
PressedButtonScheduler(PressedButtonScheduler&&) = default;
|
||||
PressedButtonScheduler& operator=(PressedButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/ButtonScheduler.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class ReleasedButtonScheduler : public ButtonScheduler {
|
||||
public:
|
||||
ReleasedButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~ReleasedButtonScheduler() = default;
|
||||
|
||||
ReleasedButtonScheduler(ReleasedButtonScheduler&&) = default;
|
||||
ReleasedButtonScheduler& operator=(ReleasedButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,28 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/buttons/ButtonScheduler.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Trigger;
|
||||
class Command;
|
||||
|
||||
class ToggleButtonScheduler : public ButtonScheduler {
|
||||
public:
|
||||
ToggleButtonScheduler(bool last, Trigger* button, Command* orders);
|
||||
virtual ~ToggleButtonScheduler() = default;
|
||||
|
||||
ToggleButtonScheduler(ToggleButtonScheduler&&) = default;
|
||||
ToggleButtonScheduler& operator=(ToggleButtonScheduler&&) = default;
|
||||
|
||||
virtual void Execute();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,56 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
#include "frc/smartdashboard/SendableHelper.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
class Command;
|
||||
|
||||
/**
|
||||
* This class provides an easy way to link commands to inputs.
|
||||
*
|
||||
* It is very easy to link a polled input to a command. For instance, you could
|
||||
* link the trigger button of a joystick to a "score" command or an encoder
|
||||
* reaching a particular value.
|
||||
*
|
||||
* It is encouraged that teams write a subclass of Trigger if they want to have
|
||||
* something unusual (for instance, if they want to react to the user holding
|
||||
* a button while the robot is reading a certain sensor input). For this, they
|
||||
* only have to write the {@link Trigger#Get()} method to get the full
|
||||
* functionality of the Trigger class.
|
||||
*/
|
||||
class Trigger : public Sendable, public SendableHelper<Trigger> {
|
||||
public:
|
||||
Trigger() = default;
|
||||
~Trigger() override = default;
|
||||
|
||||
Trigger(const Trigger& rhs);
|
||||
Trigger& operator=(const Trigger& rhs);
|
||||
Trigger(Trigger&& rhs);
|
||||
Trigger& operator=(Trigger&& rhs);
|
||||
|
||||
bool Grab();
|
||||
virtual bool Get() = 0;
|
||||
void WhenActive(Command* command);
|
||||
void WhileActive(Command* command);
|
||||
void WhenInactive(Command* command);
|
||||
void CancelWhenActive(Command* command);
|
||||
void ToggleWhenActive(Command* command);
|
||||
|
||||
void InitSendable(SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::atomic_bool m_sendablePressed{false};
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user