diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggers.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggers.java new file mode 100644 index 00000000000..9bef66abfa3 --- /dev/null +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggers.java @@ -0,0 +1,52 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.wpilibj2.command.button; + +import edu.wpi.first.wpilibj.DriverStation; + +/** + * A class containing static {@link Trigger} factories for running callbacks when the robot mode + * changes. + */ +public final class RobotModeTriggers { + // Utility class + private RobotModeTriggers() {} + + /** + * Returns a trigger that is true when the robot is enabled in autonomous mode. + * + * @return A trigger that is true when the robot is enabled in autonomous mode. + */ + public static Trigger autonomous() { + return new Trigger(DriverStation::isAutonomousEnabled); + } + + /** + * Returns a trigger that is true when the robot is enabled in teleop mode. + * + * @return A trigger that is true when the robot is enabled in teleop mode. + */ + public static Trigger teleop() { + return new Trigger(DriverStation::isTeleopEnabled); + } + + /** + * Returns a trigger that is true when the robot is disabled. + * + * @return A trigger that is true when the robot is disabled. + */ + public static Trigger disabled() { + return new Trigger(DriverStation::isDisabled); + } + + /** + * Returns a trigger that is true when the robot is enabled in test mode. + * + * @return A trigger that is true when the robot is enabled in test mode. + */ + public static Trigger test() { + return new Trigger(DriverStation::isTestEnabled); + } +} diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/RobotModeTriggers.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/RobotModeTriggers.cpp new file mode 100644 index 00000000000..ab593d8ff29 --- /dev/null +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/RobotModeTriggers.cpp @@ -0,0 +1,25 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "frc2/command/button/RobotModeTriggers.h" + +#include + +using namespace frc2; + +Trigger RobotModeTriggers::Autonomous() { + return Trigger{&frc::DriverStation::IsAutonomousEnabled}; +} + +Trigger RobotModeTriggers::Teleop() { + return Trigger{&frc::DriverStation::IsTeleopEnabled}; +} + +Trigger RobotModeTriggers::Disabled() { + return Trigger{&frc::DriverStation::IsDisabled}; +} + +Trigger RobotModeTriggers::Test() { + return Trigger{&frc::DriverStation::IsTestEnabled}; +} diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp index 38ec741e473..190a5448177 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/button/Trigger.cpp @@ -212,3 +212,7 @@ Trigger Trigger::Debounce(units::second_t debounceTime, return debouncer.Calculate(condition()); }); } + +bool Trigger::Get() const { + return m_condition(); +} diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/button/RobotModeTriggers.h b/wpilibNewCommands/src/main/native/include/frc2/command/button/RobotModeTriggers.h new file mode 100644 index 00000000000..2fbcc643851 --- /dev/null +++ b/wpilibNewCommands/src/main/native/include/frc2/command/button/RobotModeTriggers.h @@ -0,0 +1,50 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +#include "frc2/command/button/Trigger.h" + +namespace frc2 { + +/** + * A class containing static Trigger factories for running callbacks when robot + * mode changes. + */ +class RobotModeTriggers { + public: + RobotModeTriggers() = delete; + + /** + * Returns a trigger that is true when the robot is enabled in autonomous + * mode. + * + * @return A trigger that is true when the robot is enabled in autonomous + * mode. + */ + static Trigger Autonomous(); + + /** + * Returns a trigger that is true when the robot is enabled in teleop mode. + * + * @return A trigger that is true when the robot is enabled in teleop mode. + */ + static Trigger Teleop(); + + /** + * Returns a trigger that is true when the robot is disabled. + * + * @return A trigger that is true when the robot is disabled. + */ + static Trigger Disabled(); + + /** + * Returns a trigger that is true when the robot is enabled in test mode. + * + * @return A trigger that is true when the robot is enabled in test mode. + */ + static Trigger Test(); +}; + +} // namespace frc2 diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h b/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h index 533e0a6787b..4438aacdf90 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/button/Trigger.h @@ -268,6 +268,12 @@ class Trigger { frc::Debouncer::DebounceType type = frc::Debouncer::DebounceType::kRising); + /** + * Returns the current state of this trigger. + * @return A bool representing the current state of the trigger. + */ + bool Get() const; + private: frc::EventLoop* m_loop; std::function m_condition; diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggersTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggersTest.java new file mode 100644 index 00000000000..b2b41c4dfb6 --- /dev/null +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/button/RobotModeTriggersTest.java @@ -0,0 +1,58 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.wpilibj2.command.button; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import edu.wpi.first.wpilibj.DriverStation; +import edu.wpi.first.wpilibj.simulation.DriverStationSim; +import edu.wpi.first.wpilibj2.command.CommandTestBase; +import org.junit.jupiter.api.Test; + +class RobotModeTriggersTest extends CommandTestBase { + @Test + void autonomousTest() { + DriverStationSim.resetData(); + DriverStationSim.setAutonomous(true); + DriverStationSim.setTest(false); + DriverStationSim.setEnabled(true); + DriverStation.refreshData(); + Trigger auto = RobotModeTriggers.autonomous(); + assertTrue(auto.getAsBoolean()); + } + + @Test + void teleopTest() { + DriverStationSim.resetData(); + DriverStationSim.setAutonomous(false); + DriverStationSim.setTest(false); + DriverStationSim.setEnabled(true); + DriverStation.refreshData(); + Trigger teleop = RobotModeTriggers.teleop(); + assertTrue(teleop.getAsBoolean()); + } + + @Test + void testModeTest() { + DriverStationSim.resetData(); + DriverStationSim.setAutonomous(false); + DriverStationSim.setTest(true); + DriverStationSim.setEnabled(true); + DriverStation.refreshData(); + Trigger test = RobotModeTriggers.test(); + assertTrue(test.getAsBoolean()); + } + + @Test + void disabledTest() { + DriverStationSim.resetData(); + DriverStationSim.setAutonomous(false); + DriverStationSim.setTest(false); + DriverStationSim.setEnabled(false); + DriverStation.refreshData(); + Trigger disabled = RobotModeTriggers.disabled(); + assertTrue(disabled.getAsBoolean()); + } +} diff --git a/wpilibNewCommands/src/test/native/cpp/frc2/command/button/RobotModeTriggersTest.cpp b/wpilibNewCommands/src/test/native/cpp/frc2/command/button/RobotModeTriggersTest.cpp new file mode 100644 index 00000000000..cb9e9664ba0 --- /dev/null +++ b/wpilibNewCommands/src/test/native/cpp/frc2/command/button/RobotModeTriggersTest.cpp @@ -0,0 +1,54 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include +#include + +#include "../CommandTestBase.h" +#include "frc2/command/button/RobotModeTriggers.h" +#include "frc2/command/button/Trigger.h" + +using namespace frc2; +using namespace frc::sim; +class RobotModeTriggersTest : public CommandTestBase {}; + +TEST(RobotModeTriggersTest, Autonomous) { + DriverStationSim::ResetData(); + DriverStationSim::SetAutonomous(true); + DriverStationSim::SetTest(false); + DriverStationSim::SetEnabled(true); + frc::DriverStation::RefreshData(); + Trigger autonomous = RobotModeTriggers::Autonomous(); + EXPECT_TRUE(autonomous.Get()); +} + +TEST(RobotModeTriggersTest, Teleop) { + DriverStationSim::ResetData(); + DriverStationSim::SetAutonomous(false); + DriverStationSim::SetTest(false); + DriverStationSim::SetEnabled(true); + frc::DriverStation::RefreshData(); + Trigger teleop = RobotModeTriggers::Teleop(); + EXPECT_TRUE(teleop.Get()); +} + +TEST(RobotModeTriggersTest, Disabled) { + DriverStationSim::ResetData(); + DriverStationSim::SetAutonomous(false); + DriverStationSim::SetTest(false); + DriverStationSim::SetEnabled(false); + frc::DriverStation::RefreshData(); + Trigger disabled = RobotModeTriggers::Disabled(); + EXPECT_TRUE(disabled.Get()); +} + +TEST(RobotModeTriggersTest, TestMode) { + DriverStationSim::ResetData(); + DriverStationSim::SetAutonomous(false); + DriverStationSim::SetTest(true); + DriverStationSim::SetEnabled(true); + frc::DriverStation::RefreshData(); + Trigger test = RobotModeTriggers::Test(); + EXPECT_TRUE(test.Get()); +}